Example usage for org.apache.commons.configuration PropertiesConfiguration load

List of usage examples for org.apache.commons.configuration PropertiesConfiguration load

Introduction

In this page you can find the example usage for org.apache.commons.configuration PropertiesConfiguration load.

Prototype

public synchronized void load(Reader in) throws ConfigurationException 

Source Link

Document

Load the properties from the given reader.

Usage

From source file:com.easyvalidation.xml.processor.XMLProcessor.java

/**
 * Parse the XML & return list of rules.
 * /*from  w w w  . j a v  a  2 s  . com*/
 * @param fileNames
 * @return
 * @throws ValidationException
 */
public static Map<String, List<Rule>> parseXML(String[] fileNames) throws ValidationException {
    Map<String, List<Rule>> validationMap = new HashMap<String, List<Rule>>();

    com.easyvalidation.xml.config.EasyValidationXmlConfiguration configuration = new com.easyvalidation.xml.config.EasyValidationXmlConfiguration();
    //System.out.println("Have we came here?");
    configuration.setAutoSave(false);
    //System.out.println("Have we came here?");
    configuration.clear();
    //System.out.println("Have we came here?");
    configuration.setValidating(true);

    try {
        for (String fileName : fileNames) {
            //System.out.println("Have we came here4?");
            configuration.load(fileName);
            //System.out.println("Have we came here5?");
        }
        //System.out.println("Have we came here?");
        boolean isMessageFromKeyAllowed = false;

        Map<String, PropertiesConfiguration> propertiesMap = new HashMap<String, PropertiesConfiguration>();
        int propertiesSize = configuration.getMaxIndex(Nodes.PROPERTIES);
        for (int propertiesIndex = 0; propertiesIndex <= propertiesSize; propertiesIndex++) {

            String locale = configuration.getString(RuleElementPathUtil.getPropsFileLocale(propertiesIndex));

            PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
            propertiesConfiguration.setAutoSave(false);
            propertiesConfiguration.clear();

            HierarchicalConfiguration subConfig = configuration
                    .configurationAt(RuleElementPathUtil.getPropertiesPath(propertiesIndex));
            //System.out.println("Have we came here6?");
            int propFileSize = subConfig.getMaxIndex(Nodes.FILE);
            for (int propFileIndex = 0; propFileIndex <= propFileSize; propFileIndex++) {
                String propFileName = subConfig.getString(RuleElementPathUtil.getPropsFilePath(propFileIndex));
                if (!Utils.isEmpty(propFileName)) {
                    propertiesConfiguration.load(propFileName);
                }
            }
            propertiesMap.put(locale, propertiesConfiguration);
        }
        //System.out.println("Have we came here7?");
        if (!propertiesMap.isEmpty()) {
            isMessageFromKeyAllowed = true;
        }

        int validationSize = configuration.getMaxIndex(Nodes.VALIDATION);

        for (int validationIndex = 0; validationIndex <= validationSize; validationIndex++) {
            String name = configuration.getString(RuleElementPathUtil.getValidationName(validationIndex));

            HierarchicalConfiguration subConfig = configuration
                    .configurationAt(RuleElementPathUtil.getValidation(validationIndex));

            int ruleSize = subConfig.getMaxIndex(Nodes.RULE);
            List<Rule> ruleList = new ArrayList<Rule>();
            for (int ruleIndex = 0; ruleIndex <= ruleSize; ruleIndex++) {
                String ruleType = subConfig.getString(RuleElementPathUtil.getRuleType(ruleIndex));

                String property = subConfig.getString(RuleElementPathUtil.getRuleFieldName(ruleIndex));

                String message = subConfig.getString(RuleElementPathUtil.getRuleMessage(ruleIndex));

                String min = subConfig.getString(RuleElementPathUtil.getRuleMin(ruleIndex));

                String max = subConfig.getString(RuleElementPathUtil.getRuleMax(ruleIndex));

                String dateFormat = subConfig.getString(RuleElementPathUtil.getRuleDateFormat(ruleIndex));

                String key = subConfig.getString(RuleElementPathUtil.getMessageKey(ruleIndex));

                String useAttributePlaceHolder = subConfig
                        .getString(RuleElementPathUtil.getMessageUseAttributePlaceHolder(ruleIndex));

                String regEx = subConfig.getString(RuleElementPathUtil.getRuleRegEx(ruleIndex));

                String expression = subConfig.getString(RuleElementPathUtil.getRuleExpression(ruleIndex));

                Rule rule = new Rule(ruleType);

                rule.setProperty(property);
                rule.setMessage(message);
                rule.setMin(min);
                rule.setMax(max);
                rule.setDateFormat(dateFormat);
                rule.setKey(key);
                rule.setUseAttributePlaceHolder(Boolean.valueOf(useAttributePlaceHolder));
                rule.setRegEx(Boolean.valueOf(regEx));
                rule.setExpression(expression);
                if (isMessageFromKeyAllowed) {
                    rule.setPropertiesMap(propertiesMap);
                }

                rule.populateRule();
                ruleList.add(rule);
            }
            validationMap.put(name, ruleList);
        }
    } catch (Exception ex) {
        throw new ValidationException(ex);
    }

    return validationMap;
}

From source file:com.germinus.easyconf.ConfigurationLearningTest.java

public void testGetFromClasspath() throws ConfigurationException {
    URL result = ConfigurationUtils.locate(null, "test_module.properties");
    assertNotNull(result);/*  ww w.j  ava 2s.c o m*/
    PropertiesConfiguration conf = new PropertiesConfiguration();
    conf.load("test_module.properties");
    assertEquals("Error reading properties file from classpath", "test_module",
            conf.getString("property-only-in-test-module"));
}

From source file:com.astamuse.asta4d.web.initialization.SimplePropertyFileInitializer.java

@Override
public void initliaze(InputStream input, WebApplicationConfiguration configuration) throws Exception {
    PropertiesConfiguration pc = new PropertiesConfiguration();
    pc.load(input);

    BeanUtilsBean bu = retrieveBeanUtilsBean();
    Iterator<String> keys = pc.getKeys();
    while (keys.hasNext()) {
        String key = keys.next();
        String value = pc.getString(key);
        fillConfiguration(configuration, bu, key, value);
    }//from  w ww . ja v a2s.  c  om

}

From source file:com.mobiaware.util.PropertyManager.java

private void loadProperties(final String fileName) {
    if (StringUtils.isNotBlank(fileName)) {
        File file = new File(fileName);
        try (InputStream is = file.isFile() ? new FileInputStream(file)
                : getClass().getClassLoader().getResourceAsStream((fileName))) {
            if (is != null) {
                PropertiesConfiguration config = new PropertiesConfiguration();

                config.load(is);
                _configuration.addConfiguration(config);
            } else {
                throw new IllegalArgumentException("Property file " + fileName + " was not found.");
            }//w ww . ja  va  2  s.  co m
        } catch (IOException e) {
            LOG.error(Throwables.getStackTraceAsString(e));
            throw new IllegalArgumentException("Error loading properties file", e);
        } catch (ConfigurationException e) {
            LOG.error(Throwables.getStackTraceAsString(e));
            throw new IllegalArgumentException("Error loading properties file", e);
        }
    }
}

From source file:com.yahoo.bard.webservice.config.ConfigResourceLoader.java

/**
 * Build a configuration object from a resource, processing it as a properties file.
 *
 * @param resource  The resource referring to a properties file
 *
 * @return a Configuration object containing a properties configuration
 *///from www .  j  a  v a 2  s .c  o  m
public Configuration loadConfigFromResource(Resource resource) {
    PropertiesConfiguration result = new PropertiesConfiguration();
    try {
        result.load(resource.getInputStream());
        return result;
    } catch (ConfigurationException | IOException e) {
        LOG.error(CONFIGURATION_LOAD_ERROR.format(resource.getFilename()), e);
        throw new SystemConfigException(CONFIGURATION_LOAD_ERROR.format(resource.getFilename()), e);
    }
}

From source file:me.preilly.util.ConfigFactory.java

public synchronized PropertiesConfiguration getConfigProperties(String env, InputStream stream) {
    if (StringUtils.isEmpty(env)) {
        env = getDefaultEnvProperty();//  ww w  .j a v  a  2  s  .  c om
    }

    PropertiesConfiguration config = new LocalizedPropertiesConfiguration(env);
    try {
        config.load(stream);
    } catch (ConfigurationException ce) {
        LOGGER.info("caught configuration exception {}", ce);
        System.exit(0);
    }

    LOGGER.info("simplepush is running with the following env [{}]", env);

    return config;
}

From source file:edu.cmu.lti.oaqa.bioasq.document.retrieval.GoPubMedDocumentRetrievalExecutor.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);
    String conf = UimaContextHelper.getConfigParameterStringValue(context, "conf");
    PropertiesConfiguration gopubmedProperties = new PropertiesConfiguration();
    try {/*from   w  w w.j ava2s.  c o  m*/
        gopubmedProperties.load(getClass().getResourceAsStream(conf));
    } catch (ConfigurationException e) {
        throw new ResourceInitializationException(e);
    }
    service = new GoPubMedService(gopubmedProperties);
    pages = UimaContextHelper.getConfigParameterIntValue(context, "pages", 1);
    hits = UimaContextHelper.getConfigParameterIntValue(context, "hits", 100);
    queryStringConstructor = new PubMedQueryStringConstructor();
}

From source file:edu.cmu.lti.oaqa.bioasq.concept.retrieval.GoPubMedConceptRetrievalExecutor.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);
    String conf = UimaContextHelper.getConfigParameterStringValue(context, "conf");
    PropertiesConfiguration gopubmedProperties = new PropertiesConfiguration();
    try {//from w  w  w.  ja v  a 2  s  .  co  m
        gopubmedProperties.load(getClass().getResourceAsStream(conf));
    } catch (ConfigurationException e) {
        throw new ResourceInitializationException(e);
    }
    service = new GoPubMedService(gopubmedProperties);
    pages = UimaContextHelper.getConfigParameterIntValue(context, "pages", 1);
    hits = UimaContextHelper.getConfigParameterIntValue(context, "hits", 100);
    bopQueryStringConstructor = new BagOfPhraseQueryStringConstructor();
    timeout = UimaContextHelper.getConfigParameterIntValue(context, "timeout", 4);
    limit = UimaContextHelper.getConfigParameterIntValue(context, "limit", Integer.MAX_VALUE);
}

From source file:edu.cmu.lti.oaqa.bioasq.concept.retrieval.GoPubMedSeparateConceptRetrievalExecutor.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);
    String conf = UimaContextHelper.getConfigParameterStringValue(context, "conf");
    PropertiesConfiguration gopubmedProperties = new PropertiesConfiguration();
    try {//from  www. j a  va  2s . co m
        gopubmedProperties.load(getClass().getResourceAsStream(conf));
    } catch (ConfigurationException e) {
        throw new ResourceInitializationException(e);
    }
    service = new GoPubMedService(gopubmedProperties);
    pages = UimaContextHelper.getConfigParameterIntValue(context, "pages", 1);
    hits = UimaContextHelper.getConfigParameterIntValue(context, "hits", 1);
    bopQueryStringConstructor = new BagOfPhraseQueryStringConstructor();
    timeout = UimaContextHelper.getConfigParameterIntValue(context, "timeout", 4);
    limit = UimaContextHelper.getConfigParameterIntValue(context, "limit", Integer.MAX_VALUE);
}

From source file:com.google.api.ads.adwords.keywordoptimizer.KeywordOptimizer.java

/**
 * Loads the properties from a file specified by the commandLine.
 *
 * @param cmdLine the parsed command line parameters
 * @throws KeywordOptimizerException in case there is a problem reading the configuration file
 *//*from w ww  .j  a va 2s.  c om*/
private static Configuration loadConfiguration(CommandLine cmdLine) throws KeywordOptimizerException {
    PropertiesConfiguration properties = new PropertiesConfiguration();

    try {
        if (cmdLine.hasOption("kp")) {
            // If properties file specified by parameter, then load from this file.
            String propertiesPath = cmdLine.getOptionValue("kp");
            File propertiesFile = new File(propertiesPath);
            log("Using keyword optimizer properties file: " + propertiesFile.getAbsolutePath());
            FileInputStream is = new FileInputStream(propertiesFile);
            properties.load(is);
            is.close();
        } else {
            // Otherwise load it from classpath.
            log("Loading keyword optimizer properties from classpath");
            InputStream is = KeywordOptimizer.class.getResourceAsStream(DEFAULT_PROPERTIES_PATH);
            properties.load(is);
            is.close();
        }
    } catch (IOException e) {
        throw new KeywordOptimizerException("Error loading the configuration file", e);
    } catch (ConfigurationException e) {
        throw new KeywordOptimizerException("Error parsing the configuration file", e);
    }

    return properties;
}