1 16 17 package org.apache.commons.latka; 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.util.Properties ; 22 23 import org.apache.log4j.Category; 24 25 38 public class LatkaProperties { 39 40 41 protected static final Category _log = 42 Category.getInstance(LatkaProperties.class); 43 44 45 protected static Properties _initProps = loadDefaultProps(); 46 47 static { 48 _initProps.putAll(loadUserProps()); 49 } 50 51 55 protected static ThreadLocal _propsThreadLocal = 56 new LatkaThreadLocal(_initProps); 57 58 65 public static Properties getProperties() { 66 return (Properties ) _propsThreadLocal.get(); 67 } 68 69 75 public static void resetProperties() { 76 Properties props = (Properties ) _propsThreadLocal.get(); 77 props.clear(); 78 props.putAll(_initProps); 79 } 80 81 89 protected static Properties loadDefaultProps() { 90 91 Properties properties = new Properties (); 92 93 try { 94 properties.putAll( 95 loadPropsFromClasspath("latka.properties.internal")); 96 } catch (IOException e) { 97 _log.error( 98 "Couldn't find latka.properties.internal file in the classpath", 99 e); 100 } 101 102 return properties; 103 104 } 105 106 110 protected static Properties loadUserProps() { 111 112 Properties properties = new Properties (); 113 114 try { 115 properties.putAll(loadPropsFromClasspath("latka.properties")); 116 } catch (IOException e) { 117 _log.debug(e); 118 _log.warn( 119 "No user-defined latka.properties file in the classpath (optional)" 120 ); 121 } 122 123 return properties; 124 125 } 126 127 133 protected static Properties 134 loadPropsFromClasspath(String classpathLocation) 135 throws IOException { 136 Properties properties = new Properties (); 137 138 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 139 if (loader == null) { 140 loader = LatkaProperties.class.getClassLoader(); 142 } 143 144 InputStream stream = loader.getResourceAsStream(classpathLocation); 145 146 if (stream == null) { 147 throw new IOException ("Could not find this file in classpath: " 148 + classpathLocation); 149 } 150 151 properties.load(stream); 152 153 return properties; 154 } 155 156 162 private static class LatkaThreadLocal extends ThreadLocal { 163 164 protected Properties _initProps = null; 165 166 172 public LatkaThreadLocal(Properties initProps) { 173 _initProps = initProps; 174 } 175 176 182 protected Object initialValue() { 183 return _initProps.clone(); 184 } 185 186 } 187 188 } 189 | Popular Tags |