List of usage examples for org.apache.commons.configuration CompositeConfiguration addConfiguration
public void addConfiguration(Configuration config)
From source file:com.algoTrader.util.ConfigurationUtil.java
public static Configuration getStrategyConfig(String strategyName) { if (StrategyImpl.BASE.equals(strategyName.toUpperCase())) { return getBaseConfig(); }/*w w w . j a v a 2s .c o m*/ CompositeConfiguration strategyConfig = strategyConfigMap.get(strategyName.toUpperCase()); if (strategyConfig == null) { strategyConfig = new CompositeConfiguration(); strategyConfig.addConfiguration(new SystemConfiguration()); try { strategyConfig.addConfiguration( new PropertiesConfiguration("conf-" + strategyName.toLowerCase() + ".properties")); strategyConfig.addConfiguration(new PropertiesConfiguration(baseFileName)); } catch (ConfigurationException e) { logger.error("error loading " + strategyName.toLowerCase() + ".properties", e); } strategyConfigMap.put(strategyName.toUpperCase(), strategyConfig); } return strategyConfig; }
From source file:dk.itst.oiosaml.configuration.SAMLConfiguration.java
/** * Get the current system configuration. * The configuration is stored in {@link SAMLUtil#OIOSAML_HOME}. The property is normally set in {@link SPFilter}. * @throws IllegalStateException When the system has not been configured properly yet. */// w w w .j a va2 s .c om public static Configuration getSystemConfiguration() throws IllegalStateException { if (systemConfiguration != null) return systemConfiguration; if (home == null || !isConfigured()) { throw new IllegalStateException("System not configured"); } CompositeConfiguration conf = new CompositeConfiguration(); conf.setProperty("oiosaml.home", home); try { conf.addConfiguration(new PropertiesConfiguration(new File(home, name + ".properties"))); conf.addConfiguration(getCommonConfiguration()); systemConfiguration = conf; return systemConfiguration; } catch (ConfigurationException e) { log.error("Cannot load the configuration file", e); throw new WrappedException(Layer.DATAACCESS, e); } catch (IOException e) { log.error("Unable to load oiosaml-common.propeties from classpath", e); throw new WrappedException(Layer.DATAACCESS, e); } }
From source file:net.elsched.utils.SettingsBinder.java
/** * Bind configuration parameters into Guice Module. * /*from w ww.j av a 2s.com*/ * @return a Guice module configured with setting support. * @throws ConfigurationException * on configuration error */ public static Module bindSettings(String propertiesFileKey, Class<?>... settingsArg) throws ConfigurationException { final CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); String propertyFile = config.getString(propertiesFileKey); if (propertyFile != null) { config.addConfiguration(new PropertiesConfiguration(propertyFile)); } List<Field> fields = new ArrayList<Field>(); for (Class<?> settings : settingsArg) { fields.addAll(Arrays.asList(settings.getDeclaredFields())); } // Reflect on settings class and absorb settings final Map<Setting, Field> settings = new LinkedHashMap<Setting, Field>(); for (Field field : fields) { if (!field.isAnnotationPresent(Setting.class)) { continue; } // Validate target type SettingTypeValidator typeHelper = supportedSettingTypes.get(field.getType()); if (typeHelper == null || !typeHelper.check(field.getGenericType())) { throw new IllegalArgumentException(field.getType() + " is not one of the supported setting types"); } Setting setting = field.getAnnotation(Setting.class); settings.put(setting, field); } // Now validate them List<String> missingProperties = new ArrayList<String>(); for (Setting setting : settings.keySet()) { if (setting.defaultValue().isEmpty()) { if (!config.containsKey(setting.name())) { missingProperties.add(setting.name()); } } } if (missingProperties.size() > 0) { StringBuilder error = new StringBuilder(); error.append("The following required properties are missing from the server configuration: "); error.append(Joiner.on(", ").join(missingProperties)); } // bundle everything up in an injectable guice module return new AbstractModule() { @Override protected void configure() { // We must iterate the settings a third time when binding. // Note: do not collapse these loops as that will damage // early error detection. The runtime is still O(n) in setting // count. for (Map.Entry<Setting, Field> entry : settings.entrySet()) { Class<?> type = entry.getValue().getType(); Setting setting = entry.getKey(); if (int.class.equals(type)) { Integer defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Integer.parseInt(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getInteger(setting.name(), defaultValue)); } else if (boolean.class.equals(type)) { Boolean defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Boolean.parseBoolean(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getBoolean(setting.name(), defaultValue)); } else if (String.class.equals(type)) { bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getString(setting.name(), setting.defaultValue())); } else { String[] value = config.getStringArray(setting.name()); if (value.length == 0 && !setting.defaultValue().isEmpty()) { value = setting.defaultValue().split(","); } bind(new TypeLiteral<List<String>>() { }).annotatedWith(Names.named(setting.name())).toInstance(ImmutableList.copyOf(value)); } } } }; }
From source file:cc.kune.wave.server.CustomSettingsBinder.java
/** * Bind configuration parameters into Guice Module. * * @param propertyFile the property file * @param settingsArg the settings arg//from www .ja va 2s.c om * @return a Guice module configured with setting support. * @throws ConfigurationException on configuration error */ public static Module bindSettings(String propertyFile, Class<?>... settingsArg) throws ConfigurationException { final CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); config.addConfiguration(new PropertiesConfiguration(propertyFile)); List<Field> fields = new ArrayList<Field>(); for (Class<?> settings : settingsArg) { fields.addAll(Arrays.asList(settings.getDeclaredFields())); } // Reflect on settings class and absorb settings final Map<Setting, Field> settings = new LinkedHashMap<Setting, Field>(); for (Field field : fields) { if (!field.isAnnotationPresent(Setting.class)) { continue; } // Validate target type SettingTypeValidator typeHelper = supportedSettingTypes.get(field.getType()); if (typeHelper == null || !typeHelper.check(field.getGenericType())) { throw new IllegalArgumentException(field.getType() + " is not one of the supported setting types"); } Setting setting = field.getAnnotation(Setting.class); settings.put(setting, field); } // Now validate them List<String> missingProperties = new ArrayList<String>(); for (Setting setting : settings.keySet()) { if (setting.defaultValue().isEmpty()) { if (!config.containsKey(setting.name())) { missingProperties.add(setting.name()); } } } if (missingProperties.size() > 0) { StringBuilder error = new StringBuilder(); error.append("The following required properties are missing from the server configuration: "); error.append(Joiner.on(", ").join(missingProperties)); throw new ConfigurationException(error.toString()); } // bundle everything up in an injectable guice module return new AbstractModule() { @Override protected void configure() { // We must iterate the settings a third time when binding. // Note: do not collapse these loops as that will damage // early error detection. The runtime is still O(n) in setting count. for (Map.Entry<Setting, Field> entry : settings.entrySet()) { Class<?> type = entry.getValue().getType(); Setting setting = entry.getKey(); if (int.class.equals(type)) { Integer defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Integer.parseInt(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getInteger(setting.name(), defaultValue)); } else if (boolean.class.equals(type)) { Boolean defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Boolean.parseBoolean(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getBoolean(setting.name(), defaultValue)); } else if (String.class.equals(type)) { bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getString(setting.name(), setting.defaultValue())); } else { String[] value = config.getStringArray(setting.name()); if (value.length == 0 && !setting.defaultValue().isEmpty()) { value = setting.defaultValue().split(","); } bind(new TypeLiteral<List<String>>() { }).annotatedWith(Names.named(setting.name())).toInstance(ImmutableList.copyOf(value)); } } } }; }
From source file:com.shmsoft.dmass.main.ParameterProcessing.java
/** * Default configuration / processing parameters * * @return//from w ww .j av a 2 s.com */ public static Configuration setDefaultParameters() { CompositeConfiguration cc = new CompositeConfiguration(); try { Configuration defaults = new SHMcloudConfiguration(DEFAULT_PARAMETER_FILE); cc.addConfiguration(defaults); } catch (Exception e) { e.printStackTrace(System.out); // follow the "fail-fast" design pattern System.exit(0); } return cc; }
From source file:ffx.autoparm.Keyword_poltype.java
/** * This method sets up configuration properties in the following precedence * order: 1.) Java system properties a.) -Dkey=value from the Java command * line b.) System.setProperty("key","value") within Java code. * * 2.) Structure specific properties (for example pdbname.properties) * * 3.) User specific properties (~/.ffx/ffx.properties) * * 4.) System wide properties (file defined by environment variable * FFX_PROPERTIES)//from w ww.jav a2s . c o m * * 5.) Internal force field definition. * * @since 1.0 * @param file a {@link java.io.File} object. * @return a {@link org.apache.commons.configuration.CompositeConfiguration} * object. */ public static CompositeConfiguration loadProperties(File file) { /** * Command line options take precedences. */ CompositeConfiguration properties = new CompositeConfiguration(); properties.addConfiguration(new SystemConfiguration()); /** * Structure specific options are 2nd. */ if (file != null && file.exists() && file.canRead()) { try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String prmfilename = br.readLine().split(" +")[1]; File prmfile = new File(prmfilename); if (prmfile.exists() && prmfile.canRead()) { properties.addConfiguration(new PropertiesConfiguration(prmfile)); properties.addProperty("propertyFile", prmfile.getCanonicalPath()); } } catch (Exception e1) { e1.printStackTrace(); } } // /** // * User specific options are 3rd. // */ // String filename = System.getProperty("user.home") + File.separator // + ".ffx/ffx.properties"; // File userPropFile = new File(filename); // if (userPropFile.exists() && userPropFile.canRead()) { // try { // properties.addConfiguration(new PropertiesConfiguration( // userPropFile)); // } catch (Exception e) { // logger.info("Error loading " + filename + "."); // } // } // // /** // * System wide options are 2nd to last. // */ // filename = System.getenv("FFX_PROPERTIES"); // if (filename != null) { // File systemPropFile = new File(filename); // if (systemPropFile.exists() && systemPropFile.canRead()) { // try { // properties.addConfiguration(new PropertiesConfiguration( // systemPropFile)); // } catch (Exception e) { // logger.info("Error loading " + filename + "."); // } // } // } /** * Echo the interpolated configuration. */ if (logger.isLoggable(Level.FINE)) { Configuration config = properties.interpolatedConfiguration(); Iterator<String> i = config.getKeys(); while (i.hasNext()) { String s = i.next(); logger.fine("Key: " + s + ", Value: " + Arrays.toString(config.getList(s).toArray())); } } return properties; }
From source file:com.cloudera.whirr.cm.integration.BaseITServer.java
private static Configuration clusterConfig() { CompositeConfiguration configuration = new CompositeConfiguration(); try {//from w w w . ja v a 2s .co m if (System.getProperty("config") != null) { configuration.addConfiguration(new PropertiesConfiguration(System.getProperty("config"))); } configuration.addConfiguration(new PropertiesConfiguration(TEST_CM_TEST_PREFIX_PROPERTIES + (System.getProperty(TEST_PLATFORM) == null || System.getProperty(TEST_PLATFORM).equals("") ? BaseTest.TEST_PLATFORM_DEFAULT : System.getProperty(TEST_PLATFORM)) + ".properties")); configuration.addConfiguration(new PropertiesConfiguration(TEST_CM_TEST_PROPERTIES)); configuration.addConfiguration(new PropertiesConfiguration(TEST_CM_TEST_GLOBAL_PROPERTIES)); configuration.addConfiguration(new PropertiesConfiguration(TEST_CM_EXAMPLE_PROPERTIES)); configuration.addConfiguration(new PropertiesConfiguration( CmServerClusterInstance.class.getClassLoader().getResource(CONFIG_WHIRR_DEFAULT_FILE))); } catch (ConfigurationException e) { throw new RuntimeException("Could not load integration test properties", e); } return configuration; }
From source file:com.civprod.writerstoolbox.apps.Context.java
private static Configuration createConfigurationFromParent(Context inParent) { CompositeConfiguration tempCompositeConfiguration = new CompositeConfiguration(); if (inParent != null) { tempCompositeConfiguration.addConfiguration(inParent.getConfiguration()); }//from www .jav a 2 s.c o m return tempCompositeConfiguration; }
From source file:dk.itst.oiosaml.configuration.SAMLConfiguration.java
public static Configuration getCommonConfiguration() throws IOException { CompositeConfiguration conf = new CompositeConfiguration(); Enumeration<URL> resources = SAMLConfiguration.class.getClassLoader() .getResources("oiosaml-common.properties"); while (resources.hasMoreElements()) { URL u = resources.nextElement(); log.debug("Loading config from " + u); try {/* ww w . j a v a 2 s .c o m*/ conf.addConfiguration(new PropertiesConfiguration(u)); } catch (ConfigurationException e) { log.error("Cannot load the configuration file", e); throw new WrappedException(Layer.DATAACCESS, e); } } return conf; }
From source file:com.shmsoft.dmass.main.ParameterProcessing.java
/** * Custom configuration / processing parameters * * @param customParametersFile file path of properties file * @return//from w ww .ja v a 2 s . c o m */ public static Configuration collectProcessingParameters(String customParametersFile) { // apache.commons configuration class CompositeConfiguration cc = new CompositeConfiguration(); try { // custom parameter file is first priority if (customParametersFile != null) { // read file Configuration customProperties = new SHMcloudConfiguration(customParametersFile); // add to configuration cc.addConfiguration(customProperties); } // default parameter file is last priority // read file Configuration defaults = new SHMcloudConfiguration(DEFAULT_PARAMETER_FILE); // add to configuration cc.addConfiguration(defaults); // set project file name cc.setProperty(PROJECT_FILE_NAME, customParametersFile); } catch (Exception e) { e.printStackTrace(System.out); // follow the "fail-fast" design pattern System.exit(0); } return cc; }