List of usage examples for org.apache.commons.configuration Configuration getProperty
Object getProperty(String key);
From source file:org.apache.whirr.service.chef.Recipe.java
/** * Package protected to be used only by {@link ChefClusterActionHandler}. * //from www . ja v a2 s .c o m * @param config */ Recipe(String cookbook, String recipe, Configuration config) { this(cookbook, recipe); for (Iterator<?> iter = config.getKeys(); iter.hasNext();) { String key = (String) iter.next(); String attribute = key.substring(cookbook.length() + 1, key.length()); if (attribute.equals("url")) { this.url = config.getString(key); } else { attribs.put(attribute, config.getProperty(key)); } } }
From source file:org.apache.whirr.service.elasticsearch.ElasticSearchConfigurationBuilder.java
private static List<String> asYamlLines(Configuration config, int depth) { List<String> lines = Lists.newArrayList(); Set<String> prefixes = Sets.newHashSet(); Iterator<String> keys = config.getKeys(); while (keys.hasNext()) { String key = keys.next(); String[] parts = key.split("\\."); String prefix = parts[0]; if (prefixes.contains(prefix)) { continue; // skip parsed set of keys }// ww w . jav a2 s . c o m if (parts.length == 1) { lines.add(spaces(depth * 2) + key + ": " + config.getProperty(key)); } else if (parts.length > 1) { lines.add(spaces(depth * 2) + prefix + ":"); lines.addAll(asYamlLines(config.subset(prefix), depth + 1)); } prefixes.add(prefix); } return lines; }
From source file:org.apache.whirr.service.puppet.statements.CreateSitePpAndApplyRoles.java
@SuppressWarnings("unchecked") @VisibleForTesting//w w w . j av a 2 s. co m static Manifest getManifestForClusterSpecAndRole(String subroleModuleManifest, Configuration manifestProps) { int firstColon = subroleModuleManifest.indexOf(':'); String moduleName, manifestClassName; if (firstColon == -1) { moduleName = subroleModuleManifest; manifestClassName = null; } else { moduleName = subroleModuleManifest.substring(0, firstColon); int firstDoubleColon = subroleModuleManifest.indexOf("::"); if (firstDoubleColon != firstColon) throw new IllegalArgumentException("Malformed subrole spec for role puppet: " + "format should be puppet:module or puppet:module::manifest"); manifestClassName = subroleModuleManifest.substring(firstDoubleColon + 2); } // now create and populate the manifest Manifest manifest = new Manifest(moduleName, manifestClassName); for (Iterator<?> longkeyI = manifestProps.getKeys(); longkeyI.hasNext();) { String longkey = (String) longkeyI.next(); String key = longkey.substring(subroleModuleManifest.replaceAll(":+", ".").length() + 1); if (key.indexOf('.') >= 0) { // it's for a sub-manifest; skip it } else { Object value = manifestProps.getProperty(longkey); // an array, e.g. ['1', '2'] gets parsed as a list of two strings, which we need to join // with ", " String vs = ""; if (value == null) LOG.warn("Invalid value for key " + longkey + ": null"); // shouldn't happen else if (value instanceof Collection) { Iterator<?> vi = ((Collection<?>) value).iterator(); if (!vi.hasNext()) LOG.warn("Invalid value for key " + longkey + ": empty list"); // shouldn't happen else { vs += vi.next(); while (vi.hasNext()) vs += ", " + vi.next(); } } else { vs = value.toString(); } manifest.attribs.put(key, vs); } } LOG.debug("Bootstrapping " + subroleModuleManifest + ", produced manifest:\n" + manifest); return manifest; }
From source file:org.bhave.experiment.AbstractConfigurablePrototype.java
/** * Creates a deep copy for the supplied configuration object * * @param config a configuration object/*from w w w . j av a 2 s. c om*/ */ @Override public void loadConfiguration(Configuration config) { Configuration newConfig = new PropertiesConfiguration(); Iterator<String> keyIt = config.getKeys(); while (keyIt.hasNext()) { String key = keyIt.next(); newConfig.setProperty(key, config.getProperty(key)); } this.config = newConfig; }
From source file:org.bhave.sweeper.impl.DefaultCombinedParameterSweep.java
/** * This creates a file to store the configuration ids and the respective * parameter values for each configuration * * the file also has a header with the parameter names * * @param filename/*from ww w. jav a 2 s .c o m*/ * @return * @throws java.io.IOException */ @Override public File writeSweepFile(String filename) throws IOException { File file = new File(filename); BufferedWriter writer; writer = new BufferedWriter(new FileWriter(file)); Iterator<Configuration> paramIT = this.iterator(); StringBuilder sb = new StringBuilder(CombinedParameterSweep.CFG_ID_PARAM); sb.append(';'); List<ParameterSweep> parameters = this.getParameterSweeps(); for (ParameterSweep sweep : parameters) { sb.append(sweep.getParameterName()).append(';'); } writer.write(sb.toString()); writer.newLine(); int i = 0; while (paramIT.hasNext()) { Configuration cfg = paramIT.next(); if (i == 0) { sb = new StringBuilder(); sb.append(cfg.getInt(CombinedParameterSweep.CFG_ID_PARAM)); sb.append(';'); for (ParameterSweep sweep : parameters) { String param = sweep.getParameterName(); sb.append(cfg.getProperty(param).toString()).append(';'); } writer.write(sb.toString()); writer.newLine(); } i = (i + 1) % runs; } writer.close(); return file; }
From source file:org.bhave.sweeper.SingleValueSweepTest.java
@Test public void test() { SingleValueSweep<Integer> sweep = new SingleValueSweep<Integer>("p1", 1); for (Configuration config : sweep) { System.out.println(config.getProperty("p1")); }/*from w ww .j a v a 2 s . co m*/ assertEquals(1, sweep.size()); }
From source file:org.cesecore.dbprotection.ProtectedDataConfiguration.java
/** * Fill the maps with crypto tokens and key labels from the configuration file *//*from w ww .j ava 2 s .com*/ private void fillKeyIdsAndCryptoTokens() { final Configuration conf = ConfigurationHolder.instance(); final String keyidstr = "databaseprotection.keyid."; final String labelstr = "databaseprotection.keylabel."; final String classtr = "databaseprotection.classname."; final String propstr = "databaseprotection.properties."; final String datastr = "databaseprotection.data."; final String pinstr = "databaseprotection.tokenpin."; final String versionstr = "databaseprotection.version."; for (int i = 0; i < 255; i++) { final String keyid = conf.getString(keyidstr + i); if (keyid != null) { // Get label, must exist String label = conf.getString(labelstr + i); if (label != null) { // A null value in the properties file means that we should not use this value, so set it to null for real if (label.equalsIgnoreCase("null")) { label = null; log.info("Found keyid " + keyid + ", but label defined as 'null'. Not adding to list of crypto tokens."); } else { // Get version string, there is a default final String version = conf.getString(versionstr + i); if (StringUtils.isNotEmpty(version)) { protectVersions.put(Integer.parseInt(keyid), Integer.parseInt(version)); } // Get classname, must exist final String classname = conf.getString(classtr + i); if (StringUtils.isNotEmpty(classname)) { // Get properties and data not required // Properties (string with comma in it) are returned as an ArrayList so we can not use: final String str = conf.getString(propstr+i); Object o = conf.getProperty(propstr + i); String str = ""; if (o instanceof String) { str = (String) o; } else if (o != null) { @SuppressWarnings("unchecked") final ArrayList<String> list = (ArrayList<String>) o; // We have to do a bit of magic in order to make the properties into something that // Properties.load will swallow, it is a bit stupid. for (String s : list) { if (str.length() > 0) { str += "\n"; } str += s; } } final Properties properties = new Properties(); try { if (StringUtils.isNotEmpty(str)) { // Remove any curly braces from the input, otherwise Properties.load does not do what we want str = StringUtils.remove(str, '{'); str = StringUtils.remove(str, '}'); // If the input string contains \ (backslash on windows) we must convert it to \\ // Otherwise properties.load will parse it as an escaped character, and that is not good str = StringUtils.replace(str, "\\", "\\\\"); properties.load(new ByteArrayInputStream(str.getBytes())); } final String data = conf.getString(datastr + i); byte[] keydata = null; if (StringUtils.isNotEmpty(data)) { // Data is base64 encoded byte[] so decode it keydata = Base64.decode(data.getBytes()); } final CryptoToken token = CryptoTokenFactory.createCryptoToken(classname, properties, keydata, Integer.valueOf(keyid).intValue()); // We must activate the token as well (if not using a default pwd of course, in which case we assume the tokenpin property is not set) final String pin = conf.getString(pinstr + i); try { if (StringUtils.isNotEmpty(pin)) { token.activate(pin.toCharArray()); } cryptoTokens.put(Integer.parseInt(keyid), new CachedCryptoToken(token)); keyLabels.put(Integer.parseInt(keyid), label); } catch (CryptoTokenAuthenticationFailedException e) { log.error("Found keyid " + keyid + ", but activation of crypto token fails. Not adding to list of crypto tokens.", e); } catch (CryptoTokenOfflineException e) { log.error("Found keyid " + keyid + ", but activation of crypto token fails. Not adding to list of crypto tokens.", e); } } catch (IOException e) { log.error("Found keyid " + keyid + ", but properties fails to load. Not adding to list of crypto tokens.", e); } } else { log.error("Found keyid " + keyid + ", but no classname defined. Not adding to list of crypto tokens."); } } } else { log.error("Found keyid " + keyid + ", but no label defined. Not adding to list of crypto tokens."); } } else { // No keyid with that number = no more keyids so break, log.debug("Read " + i + " crypto tokens for protected data."); break; } // After reading this, get the default key label final String defaultstr = "databaseprotection.keyid"; final String defkeyid = conf.getString(defaultstr); if (StringUtils.isEmpty(defkeyid)) { log.error("No default key id, will not be able to use database protection."); } defaultkeyId = Integer.valueOf(defkeyid); } }
From source file:org.hawkular.inventory.impl.tinkerpop.sql.impl.SqlGraph.java
private void setupDataSource(DataSource dataSource, Configuration configuration) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(dataSource.getClass()); PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(); Map<String, PropertyDescriptor> propsByName = new HashMap<>(); for (PropertyDescriptor p : properties) { propsByName.put(p.getName().toLowerCase(), p); }/*from w w w . j a va 2 s .c o m*/ Iterator it = configuration.getKeys("sql.datasource"); while (it.hasNext()) { String key = (String) it.next(); String property = key.substring("sql.datasource.".length()).toLowerCase(); PropertyDescriptor d = propsByName.get(property); if (d == null) { continue; } Method write = d.getWriteMethod(); if (write != null) { write.invoke(dataSource, configuration.getProperty(key)); } } }
From source file:org.janusgraph.hadoop.MapReduceIndexManagement.java
private static void copyInputKeys(org.apache.hadoop.conf.Configuration hadoopConf, org.apache.commons.configuration.Configuration source) { // Copy IndexUpdateJob settings into the hadoop-backed cfg Iterator<String> keyIter = source.getKeys(); while (keyIter.hasNext()) { String key = keyIter.next(); ConfigElement.PathIdentifier pid; try {/* w w w. j a v a 2 s . c o m*/ pid = ConfigElement.parse(ROOT_NS, key); } catch (RuntimeException e) { log.debug("[inputkeys] Skipping {}", key, e); continue; } if (!pid.element.isOption()) continue; String k = ConfigElement.getPath(JanusGraphHadoopConfiguration.GRAPH_CONFIG_KEYS, true) + "." + key; String v = source.getProperty(key).toString(); hadoopConf.set(k, v); log.debug("[inputkeys] Set {}={}", k, v); } }
From source file:org.jkcsoft.java.util.ConfigHelper.java
public static boolean validateRequiredProperty(Configuration pConfig, String propName, List errors) { boolean valid = false; try {/*from w w w . j a v a2 s . c om*/ Object value = pConfig.getProperty(propName); valid = true; } catch (NoSuchElementException e) { String msg = "Missing configuration property [" + propName + "]"; errors.add(msg); valid = false; } return valid; }