1 16 package org.apache.juddi.util; 17 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Enumeration ; 21 import java.util.Iterator ; 22 import java.util.Properties ; 23 import java.util.SortedSet ; 24 import java.util.TreeSet ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.juddi.registry.RegistryEngine; 29 30 36 public class Config extends Properties 37 { 38 private static Log log = LogFactory.getLog(Config.class); 40 41 static Config config; 43 44 48 private Config() 49 { 50 super(); 51 } 52 53 58 public static void addProperties(Properties props) 59 { 60 if (config == null) 61 config = createConfig(); 62 config.putAll(props); 63 } 64 65 70 public static Properties getProperties() 71 { 72 if (config == null) 73 config = createConfig(); 74 return config; 75 } 76 77 80 public static String getOperator() 81 { 82 return getStringProperty(RegistryEngine.PROPNAME_OPERATOR_NAME, 83 RegistryEngine.DEFAULT_OPERATOR_NAME); 84 } 85 86 89 public static String getDiscoveryURL() 90 { 91 return getStringProperty(RegistryEngine.PROPNAME_DISCOVERY_URL, 92 RegistryEngine.DEFAULT_DISCOVERY_URL); 93 } 94 95 98 public static int getMaxNameLengthAllowed() 99 { 100 return getIntProperty(RegistryEngine.PROPNAME_MAX_NAME_LENGTH, 101 RegistryEngine.DEFAULT_MAX_NAME_LENGTH); 102 } 103 104 107 public static int getMaxNameElementsAllowed() 108 { 109 return getIntProperty(RegistryEngine.PROPNAME_MAX_NAME_ELEMENTS, 110 RegistryEngine.DEFAULT_MAX_NAME_ELEMENTS); 111 } 112 113 120 public static String getStringProperty(String key, String defaultValue) 121 { 122 String stringVal = defaultValue; 123 124 String propValue = getStringProperty(key); 125 if (propValue != null) 126 stringVal = propValue; 127 128 return stringVal; 129 } 130 131 137 public static int getIntProperty(String key, int defaultValue) 138 { 139 int intVal = defaultValue; 140 141 String propValue = getStringProperty(key); 142 if (propValue != null) 143 intVal = Integer.parseInt(propValue); 144 145 return intVal; 146 } 147 148 154 public static long getLongProperty(String key, long defaultValue) 155 { 156 long longVal = defaultValue; 157 158 String propValue = getStringProperty(key); 159 if (propValue != null) 160 longVal = Long.parseLong(propValue); 161 162 return longVal; 163 } 164 165 174 public static boolean getBooleanProperty(String key, boolean defaultValue) 175 { 176 boolean boolVal = defaultValue; 177 178 String propValue = getStringProperty(key); 179 if ((propValue != null) && (propValue.equalsIgnoreCase("true"))) 180 boolVal = true; 181 182 return boolVal; 183 } 184 185 191 public static URL getURLProperty(String key, URL defaultValue) 192 { 193 URL urlVal = defaultValue; 194 195 String propValue = getStringProperty(key); 196 if (propValue != null) 197 { 198 try 199 { 200 urlVal = new URL (propValue); 201 } 202 catch (MalformedURLException muex) 203 { 204 log.error( 205 "The " + key + " property value is invalid: " + propValue,muex); 206 } 207 } 208 209 return urlVal; 210 } 211 212 219 public static String getStringProperty(String key) 220 { 221 if (config == null) 222 config = createConfig(); 223 224 if (config == null) 226 return null; 227 228 if (key == null) 230 return null; 231 232 return config.getProperty(key); 233 } 234 235 243 public static void setStringProperty(String name, String value) 244 { 245 if (config == null) 246 config = createConfig(); 247 248 if (config == null) 250 return; 251 252 if (name == null) 254 return; 255 256 if (value == null) 258 config.remove(name); 259 else 260 config.setProperty(name, value); } 262 263 268 private static synchronized Config createConfig() 269 { 270 273 if (config == null) 274 config = new Config(); 275 return config; 276 } 277 278 283 public String toString() 284 { 285 StringBuffer buff = new StringBuffer (100); 287 288 Enumeration propKeys = keys(); 290 while (propKeys.hasMoreElements()) 291 { 292 String propName = (String ) propKeys.nextElement(); 294 String propValue = getProperty(propName); 295 296 buff.append(propName.trim()); 298 buff.append("="); 299 buff.append(propValue.trim()); 300 buff.append("\n"); 301 } 302 303 return buff.toString(); 304 } 305 306 307 308 309 310 public static void main(String [] args) 311 { 312 Properties sysProps = null; 313 SortedSet sortedPropsSet = null; 314 315 sysProps = Config.getProperties(); 316 sortedPropsSet = new TreeSet (sysProps.keySet()); 317 for (Iterator keys = sortedPropsSet.iterator(); keys.hasNext();) 318 { 319 String key = (String ) keys.next(); 320 System.out.println(key + ": " + sysProps.getProperty(key)); 321 } 322 } 323 } | Popular Tags |