List of usage examples for org.apache.commons.configuration PropertiesConfiguration load
public synchronized void load(Reader in) throws ConfigurationException
From source file:org.powertac.server.ServerPropertiesService.java
/** * Loads the properties from classpath, default config file, * and user-specified config file, just in case it's not already been * loaded. This is done when properties are first requested, to ensure * that the logger has been initialized. Because the CompositeConfiguration * treats its config sources in FIFO order, this should be called <i>after</i> * any user-specified config is loaded.//from w w w . j a v a 2 s. c o m */ void lazyInit() { // only do this once if (initialized) return; initialized = true; // find and load the default properties file log.info("lazyInit"); try { File defaultProps = new File("config/server.properties"); if (defaultProps.canRead()) { log.debug("adding " + defaultProps.getName()); config.addConfiguration(new PropertiesConfiguration(defaultProps)); } } catch (Exception e1) { log.warn("config/server.properties not found: " + e1.toString()); } // set up the classpath props try { Resource[] xmlResources = context.getResources("classpath*:config/properties.xml"); for (Resource xml : xmlResources) { if (validXmlResource(xml)) { log.info("loading config from " + xml.getURI()); XMLConfiguration xconfig = new XMLConfiguration(); xconfig.load(xml.getInputStream()); config.addConfiguration(xconfig); } } Resource[] propResources = context.getResources("classpath*:config/*.properties"); for (Resource prop : propResources) { if (validPropResource(prop)) { log.info("loading config from " + prop.getURI()); PropertiesConfiguration pconfig = new PropertiesConfiguration(); pconfig.load(prop.getInputStream()); config.addConfiguration(pconfig); } } } catch (ConfigurationException e) { log.error("Error loading configuration: " + e.toString()); } catch (Exception e) { log.error("Error loading configuration: " + e.toString()); } // set up the configurator configurator.setConfiguration(config); }
From source file:org.powertac.server.ServerPropertiesService.java
public void setUserConfig(URL userConfigURL) throws ConfigurationException, IOException { // then load the user-specified config PropertiesConfiguration pconfig = new PropertiesConfiguration(); pconfig.load(userConfigURL.openStream()); config.addConfiguration(pconfig);/*from w ww.ja va 2 s .c om*/ log.debug("setUserConfig " + userConfigURL.toExternalForm()); lazyInit(); }
From source file:org.roda.core.RodaCoreFactory.java
public static Configuration getConfiguration(String configurationFile) throws ConfigurationException { Path config = RodaCoreFactory.getConfigPath().resolve(configurationFile); PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(); propertiesConfiguration.setDelimiterParsingDisabled(true); propertiesConfiguration.setEncoding(RodaConstants.DEFAULT_ENCODING); if (FSUtils.exists(config)) { LOGGER.trace("Loading configuration from file {}", config); propertiesConfiguration.load(config.toFile()); RodaPropertiesReloadStrategy rodaPropertiesReloadStrategy = new RodaPropertiesReloadStrategy(); rodaPropertiesReloadStrategy.setRefreshDelay(5000); propertiesConfiguration.setReloadingStrategy(rodaPropertiesReloadStrategy); } else {/* w ww . ja v a 2 s . c o m*/ InputStream inputStream = RodaCoreFactory.class .getResourceAsStream("/" + RodaConstants.CORE_CONFIG_FOLDER + "/" + configurationFile); if (inputStream != null) { LOGGER.trace("Loading configuration from classpath {}", configurationFile); propertiesConfiguration.load(inputStream); } else { LOGGER.trace("Configuration {} doesn't exist", configurationFile); } } return propertiesConfiguration; }
From source file:org.roqmessaging.log.FileConfigurationReader.java
/** * @param file//from w w w .j a v a 2s.c o m * the GCM property file * @return the GCM dao for properties * @throws ConfigurationException */ public LogConfigDAO loadLogConfiguration(String propertyFile) throws ConfigurationException { LogConfigDAO configDao = new LogConfigDAO(); try { PropertiesConfiguration config = new PropertiesConfiguration(); config.load(propertyFile); configDao.setDirectory(config.getString("directory")); configDao.setMaxFileSize(config.getLong("maxFileSize")); configDao.setDuration(config.getInt("duration")); configDao.setReplicationFactor(config.getInt("replicationFactor")); configDao.setUseLog(config.getBoolean("useLog")); } catch (Exception e) { logger.error("Error while reading configuration file - skipped but set the default configuration", e); } return configDao; }
From source file:org.roqmessaging.management.config.internal.FileConfigurationReader.java
/** * @param file/* w ww .ja v a 2s. c om*/ * the GCM property file * @return the GCM dao for properties * @throws ConfigurationException */ public GCMPropertyDAO loadGCMConfiguration(String file) throws ConfigurationException { // 1. Define the DAO GCMPropertyDAO configDao = new GCMPropertyDAO(); // 2. Load the file try { PropertiesConfiguration config = new PropertiesConfiguration(); config.load(file); // 3. Set the properties configDao.setPeriod(config.getInt("period")); configDao.setFormatDB(config.getBoolean("formatDB")); configDao.setUseCloud(config.getBoolean("cloud.use")); if (configDao.isUseCloud()) { configDao.setCloudEndPoint(config.getString("cloud.endpoint")); configDao.setCloudUser(config.getString("cloud.user")); configDao.setCloudPasswd(config.getString("cloud.password")); configDao.setCloudGateWay(config.getString("cloud.gateway")); } } catch (Exception configE) { logger.error("Error while reading configuration file - skipped but set the default configuration", configE); } return configDao; }
From source file:org.roqmessaging.management.config.internal.FileConfigurationReader.java
/** * @param file//from ww w . ja v a 2 s .co m * the HCM property file * @return the HCM dao for properties * @throws ConfigurationException */ public HostConfigDAO loadHCMConfiguration(String file) throws ConfigurationException { // 1. Define the DAO HostConfigDAO configDao = new HostConfigDAO(); // 2. Load the file try { PropertiesConfiguration config = new PropertiesConfiguration(); config.load(file); // 3. Set the properties configDao.setGcmAddress(config.getString("gcm.address") != null ? config.getString("gcm.address") : configDao.getGcmAddress()); configDao.setExchangeFrontEndPort(config.getInt("exchange.base.port")); configDao.setMonitorBasePort(config.getInt("monitor.base.port")); configDao.setStatMonitorBasePort(config.getInt("statmonitor.base.port")); configDao.setStatPeriod(config.getInt("monitor.stat.period")); configDao.setMaxNumberEchanges(config.getInt("exchange.max.perhost")); configDao.setQueueInHcmVm(config.getBoolean("queue.hcm.vm")); configDao.setExchangeInHcmVm(config.getBoolean("exchange.hcm.vm")); configDao.setExchangeHeap( config.getInt("exchange.vm.heap") != -1 ? config.getInt("exchange.vm.heap") : 256); if (config.containsKey("network.interface")) configDao.setNetworkInterface(config.getString("network.interface")); } catch (Exception configE) { logger.error("Error while reading configuration file - skipped but set the default configuration", configE); } return configDao; }
From source file:org.roqmessaging.management.properties.TestCommonsApacheConfiguration.java
@Test public void test() { try {//ww w . j ava 2s. com PropertiesConfiguration config = new PropertiesConfiguration(); config.load("GCM.properties"); int period = config.getInt("period"); logger.info("Period = " + period); Assert.assertEquals(60000, period); boolean formatDB = config.getBoolean("formatDB"); logger.info("format DB = " + formatDB); Assert.assertEquals(false, formatDB); } catch (ConfigurationException e) { logger.error(e); } }
From source file:org.talend.mdm.commmon.util.core.EncryptUtil.java
public static void encryptProperties(String location, String[] properties) { try {// ww w.j a v a 2 s .c o m File file = new File(location); if (file.exists()) { PropertiesConfiguration config = new PropertiesConfiguration(); config.setDelimiterParsingDisabled(true); config.load(file); if (file.getName().equals("mdm.conf")) { //$NON-NLS-1$ dataSourceName = config.getString(DB_DEFAULT_DATASOURCE) == null ? StringUtils.EMPTY : config.getString(DB_DEFAULT_DATASOURCE); } updated = false; for (String property : properties) { String password = config.getString(property); if (StringUtils.isNotEmpty(password) && !password.endsWith(Crypt.ENCRYPT)) { password = Crypt.encrypt(password); config.setProperty(property, password); updated = true; } } if (updated) { config.save(file); } } } catch (Exception e) { LOGGER.error("Encrypt password in '" + location + "' error.", e); //$NON-NLS-1$ //$NON-NLS-2$ } }
From source file:org.talend.mdm.commmon.util.core.EncryptUtilTest.java
@Test public void testEncypt() throws Exception { String path = getClass().getResource("mdm.conf").getFile(); path = StringUtils.substringBefore(path, "mdm.conf"); EncryptUtil.encrypt(path);//from w w w .j a v a2 s. c o m File confFile = new File(path + "mdm.conf"); PropertiesConfiguration confConfig = new PropertiesConfiguration(); confConfig.setDelimiterParsingDisabled(true); confConfig.load(confFile); assertEquals("aYfBEdcXYP3t9pofaispXA==,Encrypt", confConfig.getString(MDMConfiguration.ADMIN_PASSWORD)); assertEquals("tKyTop7U6czAJKGTd9yWRA==,Encrypt", confConfig.getString(MDMConfiguration.TECHNICAL_PASSWORD)); assertEquals("DlqU02M503JUOVBeup29+w==,Encrypt", confConfig.getString(EncryptUtil.ACTIVEMQ_PASSWORD)); File tdscFile = new File(path + "tdsc-database.properties"); PropertiesConfiguration tdscConfig = new PropertiesConfiguration(); tdscConfig.setDelimiterParsingDisabled(true); tdscConfig.load(tdscFile); assertEquals("yzuBTeQahXQS7ts8Dh6zeQ==,Encrypt", tdscConfig.getString(EncryptUtil.TDSC_DATABASE_PASSWORD)); File datasource = new File(path + "datasources.xml"); XMLConfiguration config = new XMLConfiguration(); config.setDelimiterParsingDisabled(true); config.load(datasource); HierarchicalConfiguration sub = config.configurationAt("datasource(0)"); String password = sub.getString("master.rdbms-configuration.connection-password"); assertEquals("sa", password); password = sub.getString("master.rdbms-configuration.init.connection-password"); assertNull(password); sub = config.configurationAt("datasource(1)"); password = sub.getString("master.rdbms-configuration.connection-password"); assertEquals("+WNho+eyvY2IdYENFaoKIA==,Encrypt", password); password = sub.getString("master.rdbms-configuration.init.connection-password"); assertEquals("+WNho+eyvY2IdYENFaoKIA==,Encrypt", password); }
From source file:org.talend.mdm.commmon.util.core.MDMConfiguration.java
private Properties getProperties(boolean reload, boolean ignoreIfNotFound) { if (reload) { properties = null;/*from w ww . ja v a 2s .c o m*/ } if (properties != null) { return properties; } properties = new Properties(); File file = new File(location); if (file.exists()) { LOGGER.info("MDM Configuration: found in '" + file.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$ try { PropertiesConfiguration config = new PropertiesConfiguration(); config.setDelimiterParsingDisabled(true); config.load(file); // Decrypt the passwords in mdm.conf config.setProperty(ADMIN_PASSWORD, Crypt.decrypt(config.getString(ADMIN_PASSWORD))); config.setProperty(TECHNICAL_PASSWORD, Crypt.decrypt(config.getString(TECHNICAL_PASSWORD))); config.setProperty(TDS_PASSWORD, Crypt.decrypt(config.getString(TDS_PASSWORD))); properties = ConfigurationConverter.getProperties(config); } catch (Exception e) { if (!ignoreIfNotFound) { throw new IllegalStateException("Unable to load MDM configuration from '" //$NON-NLS-1$ + file.getAbsolutePath() + "'", e); //$NON-NLS-1$ } LOGGER.warn("Unable to load MDM configuration from '" + file.getAbsolutePath() + "'", e); //$NON-NLS-1$ //$NON-NLS-2$ } } else { if (!ignoreIfNotFound) { throw new IllegalStateException("Unable to load MDM configuration from '" + file.getAbsolutePath() //$NON-NLS-1$ + "'"); //$NON-NLS-1$ } LOGGER.warn("Unable to load MDM configuration from '" + file.getAbsolutePath() + "'"); //$NON-NLS-1$ //$NON-NLS-2$ } return properties; }