1 55 56 package org.apache.commons.discovery.tools; 57 58 import java.security.AccessController ; 59 import java.security.PrivilegedAction ; 60 import java.util.Enumeration ; 61 import java.util.HashMap ; 62 import java.util.Hashtable ; 63 import java.util.Map ; 64 import java.util.Properties ; 65 66 import org.apache.commons.discovery.jdk.JDKHooks; 67 68 69 70 118 public class ManagedProperties { 119 124 private static final HashMap propertiesCache = new HashMap (); 125 126 127 133 public static String getProperty(String propertyName) { 134 return getProperty(getThreadContextClassLoader(), propertyName); 135 } 136 137 145 public static String getProperty(String propertyName, String dephault) { 146 return getProperty(getThreadContextClassLoader(), propertyName, dephault); 147 } 148 149 156 public static String getProperty(ClassLoader classLoader, String propertyName) { 157 String value = System.getProperty(propertyName); 158 if (value == null) { 159 Value val = getValueProperty(classLoader, propertyName); 160 if (val != null) { 161 value = val.value; 162 } 163 } 164 return value; 165 } 166 167 176 public static String getProperty(ClassLoader classLoader, String propertyName, String dephault) { 177 String value = getProperty(classLoader, propertyName); 178 return (value == null) ? dephault : value; 179 } 180 181 186 public static void setProperty(String propertyName, String value) { 187 setProperty(propertyName, value, false); 188 } 189 190 200 public static void setProperty(String propertyName, String value, boolean isDefault) { 201 if (propertyName != null) { 202 synchronized (propertiesCache) { 203 ClassLoader classLoader = getThreadContextClassLoader(); 204 HashMap properties = (HashMap )propertiesCache.get(classLoader); 205 206 if (value == null) { 207 properties.remove(propertyName); 208 } else { 209 if (properties == null) { 210 properties = new HashMap (); 211 propertiesCache.put(classLoader, properties); 212 } 213 214 properties.put(propertyName, new Value(value, isDefault)); 215 } 216 } 217 } 218 } 219 220 226 public static void setProperties(Map newProperties) { 227 setProperties(newProperties, false); 228 } 229 230 231 242 public static void setProperties(Map newProperties, boolean isDefault) { 243 java.util.Iterator it = newProperties.entrySet().iterator(); 244 245 249 while (it.hasNext()) { 250 Map.Entry entry = (Map.Entry )it.next(); 251 setProperty( String.valueOf(entry.getKey()), 252 String.valueOf(entry.getValue()), 253 isDefault); 254 } 255 } 256 257 258 264 public static Enumeration propertyNames() { 265 Hashtable allProps = new Hashtable (); 266 267 ClassLoader classLoader = getThreadContextClassLoader(); 268 269 273 while (true) { 274 HashMap properties = null; 275 276 synchronized (propertiesCache) { 277 properties = (HashMap )propertiesCache.get(classLoader); 278 } 279 280 if (properties != null) { 281 allProps.putAll(properties); 282 } 283 284 if (classLoader == null) break; 285 286 classLoader = getParent(classLoader); 287 } 288 289 return allProps.keys(); 290 } 291 292 304 public static Properties getProperties() { 305 Properties p = new Properties (); 306 307 Enumeration names = propertyNames(); 308 while (names.hasMoreElements()) { 309 String name = (String )names.nextElement(); 310 p.put(name, getProperty(name)); 311 } 312 313 return p; 314 } 315 316 317 318 319 private static class Value { 320 final String value; 321 final boolean isDefault; 322 323 Value(String value, boolean isDefault) { 324 this.value = value; 325 this.isDefault = isDefault; 326 } 327 } 328 329 334 private static final Value getValueProperty(ClassLoader classLoader, String propertyName) { 335 Value value = null; 336 337 if (propertyName != null) { 338 342 if (classLoader != null) { 343 value = getValueProperty(getParent(classLoader), propertyName); 344 } 345 346 if (value == null || value.isDefault) { 347 synchronized (propertiesCache) { 348 HashMap properties = (HashMap )propertiesCache.get(classLoader); 349 350 if (properties != null) { 351 Value altValue = (Value)properties.get(propertyName); 352 353 if (altValue != null) 356 value = altValue; 357 } 358 } 359 } 360 } 361 362 return value; 363 } 364 365 private static final ClassLoader getThreadContextClassLoader() { 366 return JDKHooks.getJDKHooks().getThreadContextClassLoader(); 367 } 368 369 private static final ClassLoader getParent(final ClassLoader classLoader) { 370 return (ClassLoader )AccessController.doPrivileged(new PrivilegedAction () { 371 public Object run() { 372 return classLoader.getParent(); 373 } 374 }); 375 } 376 } 377 | Popular Tags |