1 22 package org.jboss.util.property; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.util.Map ; 27 import java.util.Iterator ; 28 29 import java.io.IOException ; 30 31 import org.jboss.util.ThrowableHandler; 32 33 41 public final class PropertyManager 42 { 43 44 public static final String READER_PROPERTY_NAME = "org.jboss.util.property.reader"; 45 46 47 public static final String DEFAULT_PROPERTY_READER_TOKEN = "DEFAULT"; 48 49 50 private static final String [] DEFAULT_PROPERTY_READERS = { DEFAULT_PROPERTY_READER_TOKEN }; 51 52 53 private static PropertyMap props; 54 55 58 private PropertyManager() 59 { 60 } 61 62 65 static 66 { 67 props = new PropertyMap(); 69 PrivilegedAction action = new PrivilegedAction () 70 { 71 public Object run() 72 { 73 props.putAll(System.getProperties()); 74 75 System.setProperties(props); 77 78 String [] readerNames = getArrayProperty(READER_PROPERTY_NAME, DEFAULT_PROPERTY_READERS); 80 81 for (int i = 0; i < readerNames.length; i++) 83 { 84 try 85 { 86 if (readerNames[i].equals(DEFAULT_PROPERTY_READER_TOKEN)) 87 { 88 load(new DefaultPropertyReader()); 89 } 90 else 91 { 92 load(readerNames[i]); 93 } 94 } 95 catch (IOException e) 96 { 97 ThrowableHandler.add(e); 98 } 99 } 100 return null; 101 } 102 }; 103 AccessController.doPrivileged(action); 104 } 105 106 111 public static PropertyMap getDefaultPropertyMap() 112 { 113 SecurityManager sm = System.getSecurityManager(); 114 if (sm != null) 115 sm.checkPropertiesAccess(); 116 return props; 117 } 118 119 124 public static void addPropertyListener(final PropertyListener listener) 125 { 126 SecurityManager sm = System.getSecurityManager(); 127 if (sm != null) 128 sm.checkPropertiesAccess(); 129 props.addPropertyListener(listener); 130 } 131 132 137 public static void addPropertyListeners(final PropertyListener[] listeners) 138 { 139 SecurityManager sm = System.getSecurityManager(); 140 if (sm != null) 141 sm.checkPropertiesAccess(); 142 props.addPropertyListeners(listeners); 143 } 144 145 151 public static boolean removePropertyListener(final PropertyListener listener) 152 { 153 return props.removePropertyListener(listener); 154 } 155 156 162 public static void load(final String prefix, final Map map) throws PropertyException 163 { 164 SecurityManager sm = System.getSecurityManager(); 165 if (sm != null) 166 sm.checkPropertiesAccess(); 167 props.load(prefix, map); 168 } 169 170 175 public static void load(final Map map) throws PropertyException, IOException 176 { 177 SecurityManager sm = System.getSecurityManager(); 178 if (sm != null) 179 sm.checkPropertiesAccess(); 180 props.load(map); 181 } 182 183 188 public static void load(final PropertyReader reader) throws PropertyException, IOException 189 { 190 SecurityManager sm = System.getSecurityManager(); 191 if (sm != null) 192 sm.checkPropertiesAccess(); 193 props.load(reader); 194 } 195 196 203 public static void load(final String classname) throws PropertyException, IOException 204 { 205 SecurityManager sm = System.getSecurityManager(); 206 if (sm != null) 207 sm.checkPropertiesAccess(); 208 props.load(classname); 209 } 210 211 218 public static String setProperty(final String name, final String value) 219 { 220 SecurityManager sm = System.getSecurityManager(); 221 if (sm != null) 222 sm.checkPropertyAccess(name); 223 return (String ) props.setProperty(name, value); 224 } 225 226 232 public static String removeProperty(final String name) 233 { 234 SecurityManager sm = System.getSecurityManager(); 235 if (sm != null) 236 sm.checkPropertyAccess(name); 237 return props.removeProperty(name); 238 } 239 240 247 public static String getProperty(final String name, final String defaultValue) 248 { 249 SecurityManager sm = System.getSecurityManager(); 250 if (sm != null) 251 sm.checkPropertyAccess(name); 252 return props.getProperty(name, defaultValue); 253 } 254 255 261 public static String getProperty(final String name) 262 { 263 SecurityManager sm = System.getSecurityManager(); 264 if (sm != null) 265 sm.checkPropertyAccess(name); 266 return props.getProperty(name); 267 } 268 269 276 public static String [] getArrayProperty(final String base, final String [] defaultValues) 277 { 278 SecurityManager sm = System.getSecurityManager(); 279 if (sm != null) 280 sm.checkPropertiesAccess(); 281 return props.getArrayProperty(base, defaultValues); 282 } 283 284 290 public static String [] getArrayProperty(final String name) 291 { 292 SecurityManager sm = System.getSecurityManager(); 293 if (sm != null) 294 sm.checkPropertyAccess(name); 295 return props.getArrayProperty(name); 296 } 297 298 303 public static Iterator names() 304 { 305 SecurityManager sm = System.getSecurityManager(); 306 if (sm != null) 307 sm.checkPropertiesAccess(); 308 return props.names(); 309 } 310 311 317 public static boolean containsProperty(final String name) 318 { 319 SecurityManager sm = System.getSecurityManager(); 320 if (sm != null) 321 sm.checkPropertyAccess(name); 322 return props.containsProperty(name); 323 } 324 325 331 public static PropertyGroup getPropertyGroup(final String basename) 332 { 333 SecurityManager sm = System.getSecurityManager(); 334 if (sm != null) 335 sm.checkPropertiesAccess(); 336 return props.getPropertyGroup(basename); 337 } 338 339 346 public static PropertyGroup getPropertyGroup(final String basename, final int index) 347 { 348 SecurityManager sm = System.getSecurityManager(); 349 if (sm != null) 350 sm.checkPropertiesAccess(); 351 return props.getPropertyGroup(basename, index); 352 } 353 } 354 | Popular Tags |