Java tutorial
/* * Copyright (c) 2010 mobiaware.com. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.mobiaware.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.Properties; import org.apache.commons.configuration.CombinedConfiguration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.tree.OverrideCombiner; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Throwables; public class PropertyManager { private static final String NAME = PropertyManager.class.getSimpleName(); private static final Logger LOG = LoggerFactory.getLogger(NAME); private final CombinedConfiguration _configuration; public PropertyManager(final String defaultFile, final String overrideFile) { _configuration = new CombinedConfiguration(new OverrideCombiner()); loadProperties(defaultFile, overrideFile); } public Properties getProperties() { Properties props = new Properties(); Iterator<String> it = _configuration.getKeys(); while (it.hasNext()) { String key = it.next(); if (key != null) { Object value = _configuration.getProperty(key); if (value != null) { props.put(key, value); } } } return props; } public String getString(final String key) { return _configuration.getString(key); } public int getInt(final String key) { return _configuration.getInt(key); } public boolean getBoolean(final String key) { return Boolean.parseBoolean(_configuration.getString(key)); } private void loadProperties(final String defaultFile, final String overrideFile) { loadProperties(defaultFile); loadProperties(overrideFile); } 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."); } } 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); } } } }