View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.xml.registry.ebxml.util; 11 12 import java.io.*; 13 import java.security.AccessController; 14 import java.security.PrivilegedAction; 15 import java.util.*; 16 17 import javax.xml.registry.JAXRException; 18 19 /*** 20 * Abstraction for Registry Configuration properties. 21 * 22 * Initial implementation just uses Java Property file, 23 * future implementation might acquire configuration info 24 * from XML. Thus, all java property methods are called 25 * via this abstraction. 26 * 27 * Registry Property File Search Order 28 * 1. If Property "jaxr-ebxml.properties", this value is used as the filename 29 * to load the jaxr properties from. Skip to step 3 if this property is 30 * set and the properties successfully loaded. 31 * 2. If file Property{user.home}/.jaxr-ebxml.properties exists, the properties 32 * are loaded from here. 33 * 3. System default properties are read from the CLASSPATH. The first 34 * file named com/sun/jaxr/util/jaxr-ebxml.properties will be used to set 35 * default Registry properties. These properties are overriden by the 36 * same named property set by either steps 1 or 2. 37 * 38 * When adding a new property, be sure to set a default value for the 39 * property in DEFAULT_SYSTEM_PROPERTY_FILE. 40 * 41 * Property Priority 42 * 1. Highest priority: any java system properties including command line 43 * properties set with -D 44 * 2. Medium priority: any properties set in {user.home}/.jaxr-ebxml.properties 45 * 3. Lowest priority: any properties set in the Registry default property file 46 */ 47 public class ProviderProperties { 48 private static final String PROPERTY_FILE_NAME = "jaxr-ebxml.properties"; 49 private static final String DEFAULT_SYSTEM_PROPERTY_FILE = 50 "com/sun/xml/registry/ebxml/util/jaxr-ebxml-defaults.properties"; 51 52 private static ProviderProperties instance = null; 53 private Properties props = null; 54 private String userHome=null; 55 private String jaxrHome=null; 56 57 /*** 58 * Cache ProviderProperties. 59 */ 60 private ProviderProperties() { 61 userHome = System.getProperty("user.home"); 62 String propertyFileName = userHome + "/" + PROPERTY_FILE_NAME; 63 64 // start off by loading the lowest priority properties - jaxr defaults 65 Properties tempProps = new Properties(defaultProperties()); 66 67 jaxrHome = tempProps.getProperty("jaxr-ebxml.home"); 68 69 //Set jaxrHOme to default of user.home if not set. 70 if (jaxrHome == null) { 71 tempProps.setProperty("jaxr-ebxml.home", userHome+"/jaxr-ebxml"); 72 } 73 74 /* 75 * Check property jaxr-ebxml.property for a property file name. 76 * Default property file name is "user.home"/.jaxr-ebxml.properties. 77 */ 78 try { 79 80 // then load the medium priority properties - jaxr user props 81 propertyFileName = System.getProperty(PROPERTY_FILE_NAME, propertyFileName); 82 InputStream is = new FileInputStream(propertyFileName); 83 84 tempProps.load(is); 85 is.close(); 86 } catch (Exception e) { 87 // silently ignore missing client properties 88 } 89 90 // finally load the highest priority properties - command line props 91 // we don't really want all the system props, should we weed them out? 92 props = new Properties(tempProps); 93 AccessController. 94 doPrivileged(new PrivilegedAction() { 95 public Object run() { 96 Properties sysProps = systemProperties(); 97 98 for (Enumeration keys = sysProps.keys(); keys.hasMoreElements(); ) { 99 String key = (String) keys.nextElement(); 100 String value = sysProps.getProperty(key); 101 if (key != null && value != null) { 102 props.put(key, value); 103 } 104 } 105 return null; 106 } 107 }); 108 109 substituteVariables(); 110 } 111 112 113 /*** 114 * Implement Singleton class, this method is only way to get this object. 115 */ 116 public static ProviderProperties getInstance() { 117 if (instance == null) { 118 instance = new ProviderProperties(); 119 } 120 return instance; 121 } 122 123 124 /*** 125 * Merge properties with existing properties. 126 * 127 * @param p New properties 128 */ 129 public void mergeProperties(Properties p) { 130 Set keys = p.keySet(); 131 Iterator i = keys.iterator(); 132 Object key; 133 while (i.hasNext()) { 134 key = i.next(); 135 this.props.put(key, p.get(key)); 136 } 137 } 138 139 140 /*** 141 * Get properties 142 * 143 * @return Properties May be null 144 */ 145 public Properties getProperties() { 146 return this.props; 147 } 148 149 150 /*** 151 * Provide a backdoor to get a property without adding a method to access 152 * the property. 153 */ 154 public String getProperty(String propertyName) { 155 return props.getProperty(propertyName); 156 } 157 158 159 /*** 160 * Method Declaration. 161 * 162 * 163 * @param propertyName 164 * @param def 165 * 166 * @return 167 * 168 * @see 169 */ 170 public String getProperty(String propertyName, String def) { 171 return props.getProperty(propertyName, def); 172 } 173 174 175 public Iterator getPropertyNamesStartingWith(String prefix) { 176 List lst = new LinkedList(); 177 Enumeration e = props.propertyNames(); 178 while (e.hasMoreElements() ) { 179 String propertyName = (String)e.nextElement(); 180 if (propertyName.startsWith(prefix)) { 181 lst.add(propertyName); 182 } 183 } 184 return lst.iterator(); 185 } 186 187 188 /*** 189 * @see java.util.Properties#put(Object,Object) 190 */ 191 public void put(Object key, Object value) { 192 this.props.put(key, value); 193 } 194 195 196 /*** 197 * Replace pre-defined variables in property values with the variable value from the 198 * corresponding property. 199 */ 200 private void substituteVariables() { 201 jaxrHome = getProperty("jaxr-ebxml.home"); 202 203 if ((jaxrHome == null) || (jaxrHome.length() == 0)) { 204 String userHome = getProperty("user.home"); 205 // Set the default value here 206 File jaxrHomeFile = new File(userHome, "jaxr-ebxml"); 207 try { 208 jaxrHome = jaxrHomeFile.getCanonicalPath(); 209 } catch (IOException e) { 210 System.err.println("Could not determine path for jaxr-ebxml.home: " + e.toString()); 211 } 212 if (! jaxrHomeFile.exists()) { 213 try { 214 boolean createdJaxrHome = jaxrHomeFile.mkdir(); 215 if (! createdJaxrHome) 216 System.err.println("Could not create default home directory " + jaxrHome); 217 } catch (SecurityException e) { 218 System.err.println("Could not create default home directory " 219 + jaxrHome + ": " + e.toString()); 220 } 221 } 222 if (! jaxrHomeFile.isDirectory()) { 223 System.err.println("jaxr-ebxml.home file \"" + jaxrHome 224 + "\" is not a directory"); 225 } 226 227 props.setProperty("jaxr-ebxml.home", jaxrHome); 228 jaxrHome = getProperty("jaxr-ebxml.home"); 229 } 230 231 //Now iterate and replace $jaxr-ebxml.home in all property values 232 233 for (Enumeration keys = props.propertyNames(); keys.hasMoreElements();) { 234 String key = (String)keys.nextElement(); 235 String value = getProperty(key); 236 237 System.err.println("key = " + key + " value = " + value); 238 if (key.startsWith("jaxr-ebxml.")) { 239 int index = -1; 240 boolean modified = false; 241 while (true) { 242 index = value.indexOf("$jaxr-ebxml.home"); 243 if (index == -1) { 244 break; 245 } 246 value = value.substring(0, index) + jaxrHome + value.substring(index+18); 247 modified = true; 248 } 249 250 if (modified) { 251 props.put(key, value); 252 } 253 } 254 } 255 } 256 257 258 /*** 259 * Registry System default property settings. 260 */ 261 private Properties defaultProperties() { 262 Properties defaultProps = new Properties(); 263 264 try { 265 InputStream is = 266 getClass().getClassLoader().getResourceAsStream(DEFAULT_SYSTEM_PROPERTY_FILE); 267 268 if (is == null) { 269 is = getClass().getClassLoader().getResourceAsStream("/" + DEFAULT_SYSTEM_PROPERTY_FILE); 270 if (is == null) { 271 throw new JAXRException("Property file " + DEFAULT_SYSTEM_PROPERTY_FILE + " not found in classpath"); 272 } 273 } 274 defaultProps.load(is); 275 is.close(); 276 } catch (Exception e) { 277 e.printStackTrace(); 278 System.err.println("Ignoring " + DEFAULT_SYSTEM_PROPERTY_FILE 279 + " due to exception " + e.toString()); 280 } 281 return defaultProps; 282 } 283 284 285 /*** 286 * standard Java system.properties 287 * 288 * properties defined on the commandline get highest priority 289 * 290 */ 291 private Properties systemProperties() { 292 return (Properties)AccessController. 293 doPrivileged(new PrivilegedAction() { 294 public Object run() { 295 return System.getProperties(); 296 } 297 }); 298 } 299 300 } 301 302 303 304 305 306 307 308 309 310

This page was automatically generated by Maven