Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * 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 jp.terasoluna.fw.util; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.TreeMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * ??? * <p> * ?? ApplicationResources ??? ApplicationResources ????????? ???????? * </p> * <strong>ApplicationResources.properties??</strong><br> * <code><pre> * add.property.file.1 = <i><??1></i> * add.property.file.2 = <i><??2></i> * ... * </pre></code> * <p> * ????????? * <ol> * <li>??</li> * <li>?</li> * </ol> * ?? getPropertyNames() ? getPropertiesValues() ? * </p> */ public class PropertyUtil { /** * */ private static Log log = LogFactory.getLog(PropertyUtil.class); /** * ?? */ public static final String DEFAULT_PROPERTY_FILE = "ApplicationResources.properties"; /** * ? */ private static final String ADD_PROPERTY_PREFIX = "add.property.file."; /** * ?? */ private static final String PROPERTY_EXTENSION = ".properties"; /** * ????? */ private static TreeMap<String, String> props = new TreeMap<String, String>(); /** * ???? */ private static Set<String> files = new HashSet<String>(); /** * ????? */ static { StringBuilder key = new StringBuilder(); load(DEFAULT_PROPERTY_FILE); if (props != null) { for (int i = 1;; i++) { key.setLength(0); key.append(ADD_PROPERTY_PREFIX); key.append(i); String path = getProperty(key.toString()); if (path == null) { break; } addPropertyFile(path); } } overrideProperties(); } /** * ??? * <p> * ????? ????? * </p> * @param name ?? */ private static void load(String name) { StringBuilder key = new StringBuilder(); Properties p = readPropertyFile(name); for (@SuppressWarnings("rawtypes") Map.Entry e : p.entrySet()) { // ??????props?? props.put((String) e.getKey(), (String) e.getValue()); } if (p != null) { for (int i = 1;; i++) { key.setLength(0); key.append(ADD_PROPERTY_PREFIX); key.append(i); String addfile = p.getProperty(key.toString()); if (addfile == null) { break; } String path = getPropertiesPath(name, addfile); addPropertyFile(path); } } } /** * ??? * <p> * ????? * </p> * @param name ?? * @return */ private static Properties readPropertyFile(String name) { // ??? // WEB-INF/classes???????????? // ??JNLP??????? // ?????????? InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); if (is == null) { is = PropertyUtil.class.getResourceAsStream("/" + name); } Properties p = new Properties(); try { try { p.load(is); files.add(name); } catch (NullPointerException e) { System.err.println("!!! PANIC: Cannot load " + name + " !!!"); System.err.println(ExceptionUtil.getStackTrace(e)); } catch (IOException e) { System.err.println("!!! PANIC: Cannot load " + name + " !!!"); System.err.println(ExceptionUtil.getStackTrace(e)); } } finally { try { if (is != null) { is.close(); } } catch (IOException e) { log.error("", e); } } return p; } /** * ????? ? "-D" ??? ???? */ private static void overrideProperties() { Enumeration<String> enumeration = Collections.enumeration(props.keySet()); while (enumeration.hasMoreElements()) { String name = enumeration.nextElement(); String value = System.getProperty(name); if (value != null) { props.put(name, value); } } } /** * ???? * <p> * ???1?????? ??? ".properties" ????? * </p> * @param name ?? */ public static synchronized void addPropertyFile(String name) { if (!name.endsWith(PROPERTY_EXTENSION)) { StringBuilder nameBuf = new StringBuilder(); nameBuf.append(name); nameBuf.append(PROPERTY_EXTENSION); name = nameBuf.toString(); } if (!files.contains(name)) { load(name); } } /** * ????? * <p> * ?? "@" ?????????? ? "@" ?????? <code>key=@key</code> * ?????????????? <code>@key</code>?? ? "@" ???????? ? "@@" * "@" ?? ????????? * </p> * @param key ? * @return ???? */ public static String getProperty(String key) { String result = props.get(key); // ()=@()???? if (result != null && result.equals("@" + key)) { return result; } // @@??????@??? if (result != null && result.startsWith("@@")) { return result.substring(1); } if (result != null && result.startsWith("@")) { result = getProperty(result.substring(1)); } return result; } /** * ????? * <p> * ??????????????? * </p> * @param key ? * @param defaultValue ? * @return ???? */ public static String getProperty(String key, String defaultValue) { String result = props.get(key); if (result == null) { return defaultValue; } return result; } /** * ???????? * @return ?????? */ public static Enumeration<String> getPropertyNames() { return Collections.enumeration(props.keySet()); } /** * ??????? * @param keyPrefix ? * @return ????? */ public static Enumeration<String> getPropertyNames(String keyPrefix) { Map<String, String> map = props.tailMap(keyPrefix); Iterator<String> iter = map.keySet().iterator(); while (iter.hasNext()) { String name = iter.next(); if (!name.startsWith(keyPrefix)) { return Collections.enumeration(props.subMap(keyPrefix, name).keySet()); } } return Collections.enumeration(map.keySet()); } /** * ??????? ?? * @param propertyName ?? * @param keyPrefix * @return */ public static Set<?> getPropertiesValues(String propertyName, String keyPrefix) { Properties localProps = loadProperties(propertyName); if (localProps == null) { return null; } Enumeration<String> keyEnum = getPropertyNames(localProps, keyPrefix); if (keyEnum == null) { return null; } return getPropertiesValues(localProps, keyEnum); } /** * ????? ?? * @param localProps * @param keyPrefix * @return ??? */ public static Enumeration<String> getPropertyNames(Properties localProps, String keyPrefix) { if (localProps == null || keyPrefix == null) { return null; } Collection<String> matchedNames = new ArrayList<String>(); Enumeration<?> propNames = localProps.propertyNames(); while (propNames.hasMoreElements()) { String name = (String) propNames.nextElement(); if (name.startsWith(keyPrefix)) { matchedNames.add(name); } } return Collections.enumeration(matchedNames); } /** * ???????? * @param localProps * @param propertyNames ? * @return */ public static Set<String> getPropertiesValues(Properties localProps, Enumeration<String> propertyNames) { if (localProps == null || propertyNames == null) { return null; } Set<String> retSet = new HashSet<String>(); while (propertyNames.hasMoreElements()) { retSet.add(localProps.getProperty(propertyNames.nextElement())); } return retSet; } /** * ???????? * @param propertyName * @return */ public static Properties loadProperties(String propertyName) { // propertyName?null?????null?? if (propertyName == null || "".equals(propertyName)) { return null; } Properties retProps = new Properties(); StringBuilder resourceName = new StringBuilder(); resourceName.append(propertyName); if (!propertyName.endsWith(PROPERTY_EXTENSION)) { resourceName.append(PROPERTY_EXTENSION); } // ??? // WEB-INF/classes???????????? // ??JNLP??????? // ?????????? InputStream is = Thread.currentThread().getContextClassLoader() .getResourceAsStream(resourceName.toString()); if (is == null) { is = PropertyUtil.class.getResourceAsStream("/" + propertyName + PROPERTY_EXTENSION); } try { retProps.load(is); } catch (NullPointerException npe) { log.warn("*** Can not find property-file [" + propertyName + ".properties] ***", npe); retProps = null; } catch (IOException ie) { log.error("", ie); retProps = null; } finally { try { if (is != null) { is.close(); } } catch (IOException ie) { log.error("", ie); retProps = null; } } return retProps; } /** * ????? ??? ??????? ????? * @param resource ??? * @param addFile ? * @return ???? */ private static String getPropertiesPath(String resource, String addFile) { File file = new File(resource); String dir = file.getParent(); if (dir != null) { StringBuilder dirBuf = new StringBuilder(); dirBuf.setLength(0); dirBuf.append(dir); dirBuf.append(File.separator); dir = dirBuf.toString(); } else { dir = ""; } StringBuilder retBuf = new StringBuilder(); retBuf.setLength(0); retBuf.append(dir); retBuf.append(addFile); return retBuf.toString(); } }