View Javadoc
1 /* 2 * @(#)RegistryProperties.java 1.17 01/02/27 3 * 4 * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved. 5 * 6 * This software is the proprietary information of Sun Microsystems, Inc. 7 * Use is subject to license terms. 8 * 9 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/util/RegistryProperties.java,v 1.4 2003/05/29 23:24:41 psterk Exp $ 10 * 11 */ 12 package com.sun.ebxml.registry.util; 13 import java.security.*; 14 import java.io.*; 15 import java.util.*; 16 17 import com.sun.ebxml.registry.*; 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 "ebxmlrr.properties", this value is used as the filename 29 * to load the ebxmlrr properties from. Skip to step 3 if this property is 30 * set and the properties successfully loaded. 31 * 2. If file Property{user.home}/.ebxmlrr.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/ebxmlrr/util/ebxmlrr.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_REGISTRY_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}/.ebxmlrr.properties 45 * 3. Lowest priority: any properties set in the Registry default property file 46 */ 47 public class RegistryProperties { 48 private static final String REGISTRY_PROPERTY_FILE_NAME = "ebxmlrr.properties"; 49 private static final String DEFAULT_REGISTRY_SYSTEM_PROPERTY_FILE = 50 "com/sun/ebxml/registry/util/ebxmlrr.properties"; 51 52 private static RegistryProperties instance = null; 53 private Properties props = null; 54 private String userHome=null; 55 private String ebxmlrrHome=null; 56 57 /*** 58 * Cache RegistryProperties. 59 */ 60 private RegistryProperties() { 61 loadPropertiesInternal(); 62 } 63 64 /* 65 * This method is used to load properties in memory. It is called by 66 * the RegistryProperties() constructor and by reloadProperties() 67 */ 68 private void loadPropertiesInternal() { 69 userHome = System.getProperty("user.home"); 70 String propertyFileName = userHome + "/" + REGISTRY_PROPERTY_FILE_NAME; 71 72 // start off by loading the lowest priority properties - ebxmlrr defaults 73 Properties tempProps = new Properties(defaultProperties()); 74 75 ebxmlrrHome = tempProps.getProperty("ebxmlrr.home"); 76 77 //Set ebxmlrrHOme to default of user.home if not set. 78 if (ebxmlrrHome == null) { 79 tempProps.setProperty("ebxmlrr.home", userHome+"/ebxmlrr"); 80 } 81 82 /* 83 * Check property ebxmlrr.property for a property file name. 84 * Default property file name is "user.home"/.ebxmlrr.properties. 85 */ 86 try { 87 88 // then load the medium priority properties - ebxmlrr user props 89 propertyFileName = System.getProperty(REGISTRY_PROPERTY_FILE_NAME, propertyFileName); 90 InputStream is = new FileInputStream(propertyFileName); 91 92 tempProps.load(is); 93 is.close(); 94 } catch (Exception e) { 95 // silently ignore missing client properties 96 } 97 98 // finally load the highest priority properties - command line props 99 // we don't really want all the system props, should we weed them out? 100 props = new Properties(tempProps); 101 AccessController. 102 doPrivileged(new PrivilegedAction() { 103 public Object run() { 104 Properties sysProps = systemProperties(); 105 106 for (Enumeration keys = sysProps.keys(); keys.hasMoreElements(); ) { 107 String key = (String) keys.nextElement(); 108 props.put(key, sysProps.getProperty(key)); 109 } 110 return null; 111 } 112 }); 113 114 substituteVariables(); 115 } 116 /*** 117 * Replace pre-defined variables in property values with the variable value from the 118 * corresponding property. 119 */ 120 private void substituteVariables() { 121 ebxmlrrHome = getProperty("ebxmlrr.home"); 122 123 //Now iterate and replace $ebxmlrr.home in all property values 124 125 126 for (Enumeration keys = props.propertyNames(); keys.hasMoreElements();) { 127 String key = (String)keys.nextElement(); 128 String value = getProperty(key); 129 130 //System.err.println("key = " + key + " value = " + value); 131 if (key.startsWith("ebxmlrr.")) { 132 int index = -1; 133 boolean modified = false; 134 while (true) { 135 index = value.indexOf("$ebxmlrr.home"); 136 if (index == -1) { 137 break; 138 } 139 value = value.substring(0, index) + ebxmlrrHome + value.substring(index+13); 140 modified = true; 141 } 142 143 if (modified) { 144 props.put(key, value); 145 } 146 } 147 } 148 } 149 150 /*** 151 * Implement Singleton class, this method is only way to get this object. 152 */ 153 public static RegistryProperties getInstance() { 154 if (instance == null) { 155 instance = new RegistryProperties(); 156 } 157 return instance; 158 } 159 160 /*** 161 * Provide a backdoor to get a property without adding a method to access 162 * the property. 163 */ 164 public String getProperty(String propertyName) { 165 return props.getProperty(propertyName); 166 } 167 168 /*** 169 * Method Declaration. 170 * 171 * 172 * @param propertyName 173 * @param def 174 * 175 * @return 176 * 177 * @see 178 */ 179 public String getProperty(String propertyName, String def) { 180 return props.getProperty(propertyName, def); 181 } 182 183 /*** 184 * Registry System default property settings. 185 */ 186 private Properties defaultProperties() { 187 Properties defaultProps = new Properties(); 188 189 try { 190 InputStream is = 191 getClass().getClassLoader().getResourceAsStream(DEFAULT_REGISTRY_SYSTEM_PROPERTY_FILE); 192 193 if (is == null) { 194 throw new RegistryException("Property file " + DEFAULT_REGISTRY_SYSTEM_PROPERTY_FILE + " not found in classpath"); 195 } 196 defaultProps.load(is); 197 is.close(); 198 } catch (Exception e) { 199 e.printStackTrace(); 200 System.err.println("Ignoring " + DEFAULT_REGISTRY_SYSTEM_PROPERTY_FILE 201 + " due to exception " + e.toString()); 202 } 203 return defaultProps; 204 } 205 206 /*** 207 * standard Java system.properties 208 * 209 * properties defined on the commandline get highest priority 210 * 211 */ 212 private Properties systemProperties() { 213 return (Properties)AccessController. 214 doPrivileged(new PrivilegedAction() { 215 public Object run() { 216 return System.getProperties(); 217 } 218 }); 219 } 220 221 222 public Iterator getPropertyNamesStartingWith(String prefix) { 223 List lst = new LinkedList(); 224 Enumeration e = props.propertyNames(); 225 while (e.hasMoreElements() ) { 226 String propertyName = (String)e.nextElement(); 227 if (propertyName.startsWith(prefix)) { 228 lst.add(propertyName); 229 } 230 } 231 return lst.iterator(); 232 } 233 234 /*** 235 * This method is used to reload properties into memory. 236 */ 237 public void reloadProperties() { 238 loadPropertiesInternal(); 239 } 240 241 } 242 243 244 245 246 247 248 249 250 251

This page was automatically generated by Maven