Example usage for org.apache.commons.configuration CompositeConfiguration getStringArray

List of usage examples for org.apache.commons.configuration CompositeConfiguration getStringArray

Introduction

In this page you can find the example usage for org.apache.commons.configuration CompositeConfiguration getStringArray.

Prototype

public String[] getStringArray(String key) 

Source Link

Usage

From source file:org.soaplab.services.Config.java

/**************************************************************************
 * The main method returning a configuration object. The returned
 * object is a singleton - but its contents (the properties it
 * carries) can be changed dynamically (the properties are
 * reloaded if their files are modified). <p>
 *
 * The configuration object contains all Java system properties
 * and properties read from the Soaplab configuration file. This
 * file name is given by the system property {@link
 * #PROP_SOAPLAB_CONFIGURATION}, or using a default name {@link
 * #SOAPLAB_CONFIG_FILENAME}. If the filename does not specify an
 * absolute path, the file will be searched automatically in the
 * following locations:/*from  ww w. j  a v a2  s  .co  m*/
 *
 * <ul>
 *  <li> in the current directory,
 *  <li> in the user home,
 *  <li> directory in the classpath
 * </ul><p>
 *
 * The System properties take precedence over properties read from
 * the Soaplab property file. <p>
 *
 * The configuration object can be anytime extended by properties
 * from other sources by using either methods defined in the
 * <tt>CompositeConfiguration</tt>'s API, or using a convenient
 * methods {@link #addConfigPropertyFile} and {@link
 * #addConfigXMLFile} defined here. Properties from these
 * additional files have higher priority than properties added
 * earlier - except System properties, they are always the most
 * prioritized. <p>
 *
 * @return a configuration object
 **************************************************************************/
public static synchronized CompositeConfiguration get() {
    if (config == null) {

        // set the Soaplab global configuration
        CompositeConfiguration cfg = new CompositeConfiguration();

        // first, add System properties
        cfg.addConfiguration(new SystemConfiguration());

        // second, add Soaplab properties file(s)
        String[] configFilenames = cfg.getStringArray(PROP_SOAPLAB_CONFIGURATION);
        if (configFilenames == null || configFilenames.length == 0)
            configFilenames = new String[] { SOAPLAB_CONFIG_FILENAME };
        for (int i = 0; i < configFilenames.length; i++) {
            log.info("Using configuration file: " + configFilenames[i]);
            addPropertiesConfiguration(cfg, configFilenames[i]);
        }

        // keep it for other calls
        config = cfg;
    }
    return config;
}

From source file:org.waveprotocol.wave.util.settings.SettingsBinder.java

/**
 * Bind configuration parameters into Guice Module.
 *
 * @return a Guice module configured with setting support.
 * @throws ConfigurationException on configuration error
 *//*from  w ww.jav a 2s.c o  m*/
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));
        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));
                }
            }
        }
    };
}