Java tutorial
/** * This file is part of tera-api. * * tera-api is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * tera-api is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with tera-api. If not, see <http://www.gnu.org/licenses/>. */ package com.tera.common.configuration.properties; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.util.Collection; import java.util.Enumeration; import java.util.Properties; import javolution.util.FastMap; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author ATracer */ public class CPropertyLoader implements PropertyLoader { private static final Logger log = LoggerFactory.getLogger(CPropertyLoader.class); private FastMap<String, String> loadedProperties = new FastMap<String, String>(); @Override public Properties[] loadProperties(String file) throws IOException { File f = new File(file); if (f.isDirectory()) { return loadPropertiesFromDirectory(f); } return loadPropertiesFromFiles(f); } @Override public Properties[] loadProperties(URL url) throws IOException { return new Properties[] { loadPropertiesFromURL(url) }; } @Override public String getProperty(String key) { return loadedProperties.get(key); } @Override public boolean isPropertyLoaded(String key) { return loadedProperties.containsKey(key); } @Override public void clearProperties() { loadedProperties.clear(); } /** * @param directory * @return * @throws IOException */ private Properties[] loadPropertiesFromDirectory(File directory) throws IOException { Collection<File> dirFiles = FileUtils.listFiles(directory, new String[] { "properties" }, true); return loadPropertiesFromFiles(dirFiles.toArray(new File[dirFiles.size()])); } /** * @param files * @return * @throws IOException */ private Properties[] loadPropertiesFromFiles(File... files) throws IOException { Properties[] properties = new Properties[files.length]; for (int i = 0; i < files.length; i++) { properties[i] = loadPropertiesFromFile(files[i]); } return properties; } /** * @param file * @return * @throws IOException */ private Properties loadPropertiesFromFile(File file) throws IOException { log.info("Loading properties from file: {}", file.getName()); Properties properties = new Properties(); FileInputStream fis = new FileInputStream(file); properties.load(fis); fis.close(); fillPropertiesMap(properties); return properties; } /** * @param url * @return * @throws IOException */ private Properties loadPropertiesFromURL(URL url) throws IOException { log.info("Loading properties from URL: {}", url.getFile()); Properties properties = new Properties(); properties.load(url.openStream()); fillPropertiesMap(properties); return properties; } /** * @param properties */ private void fillPropertiesMap(Properties properties) { Enumeration<Object> keys = properties.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); loadedProperties.put(key, properties.getProperty(key)); } } }