1 package org.igfay.util; 2 import java.net.InetAddress ; 3 import java.net.UnknownHostException ; 4 import java.util.Enumeration ; 5 import java.util.Properties ; 6 7 import org.apache.log4j.Logger; 8 9 15 public class PropertyUtility { 16 17 private static Logger log = Logger.getLogger(PropertyUtility.class); 18 19 22 public PropertyUtility() { 23 super(); 24 } 25 26 27 34 35 public static void addCommandLineProperties(String [] args) { 36 41 log.debug(""); 42 if (args == null) { 43 log.debug("No properties specified. Return."); 44 return; 45 } 46 String name = null; 47 String value = null; 48 for (int ix = 0; ix < args.length; ix++) { 49 if (args[ix].length() > 2) { 50 String swChars = args[ix].substring(0, 2); 51 if (swChars.equalsIgnoreCase("-D")) { 52 String sw = args[ix].substring(2); 53 int indexOfEqualSign = sw.indexOf('='); 54 if (indexOfEqualSign > 0) { 55 name = sw.substring(0, indexOfEqualSign); 56 value = sw.substring(indexOfEqualSign + 1); 57 } else { 58 name = sw; 59 value = ""; 60 } 61 setProperty(name, value); 62 } 63 } 65 } 67 69 processSelectedProperties(); 70 } 72 73 74 80 81 public static void addPropertiesFromParameters(java.applet.Applet applet) { 82 log.debug("Adding applet parameters"); 83 String parameters[] = {"DEBUG", "VDTROOT", "PARM", "PARMS", "REDIRECT", "SERVLETHOST", "SERVLETPORT", "DBHOST"}; 84 String value = null; 85 for (int i = 0; i < parameters.length; i++) { 86 log.debug("i " + i + " Key: " + parameters[i]); 87 value = applet.getParameter(parameters[i]); 88 log.debug(" value: " + value); 89 if (value != null) { 90 setProperty(parameters[i], value); 91 log.debug("addPropertiesFromParameters(" + parameters[i] + ", " + value + ")"); 92 } 93 } 94 processSelectedProperties(); 95 } 96 97 98 100 103 public static void listProperties() { 104 try { 105 Properties pr = System.getProperties(); 106 Enumeration enumeration = pr.propertyNames(); 107 String enum_key; 108 log.info("listProperties(): ----------------------------"); 109 while (enumeration.hasMoreElements()) { 110 enum_key = (String ) enumeration.nextElement(); 111 log.info( 112 "Key: {" + enum_key + "} Value: {" + pr.getProperty(enum_key) + "}"); 113 } 114 } catch (Exception e) { 115 log.warn("*** Exception " + e); 116 } 117 } 118 119 120 123 public static void processSelectedProperties() { 124 125 } 126 127 128 135 136 public static boolean propertyEquals(String propertyName, String value) { 137 try { 138 String xx = System.getProperty(propertyName.toUpperCase(), value); 139 if (xx.equalsIgnoreCase(value)) { 140 return true; 141 } 142 } catch (Exception e) { 143 } 144 return false; 145 } 146 147 148 155 156 public static void replaceProperty(String keyWord, String valueString) { 157 try { 158 Properties pr = System.getProperties(); 159 log.debug("Adding keyword: " 160 + keyWord 161 + " Value: " 162 + valueString); 163 pr.remove(keyWord); 164 pr.put(keyWord, valueString); 166 } catch (Exception e) { 167 log.info("*** Exception adding property. keyword: " 168 + keyWord 169 + " Value: " 170 + valueString 171 + "\n e " 172 + e); 173 } 174 } 175 176 177 184 185 public static String getProperty(String propertyName) { 186 try { 187 String defaultValue = "X$X$X"; 188 String xx = getProperty(propertyName, defaultValue); 189 if (xx.equals(defaultValue)) { 190 return null; 191 } 192 return xx; 193 } catch (Exception e) { 194 log.info("getProperty(S) *** Exception getting property: " 195 + propertyName 196 + "\n e: " 197 + e); 198 } 199 return null; 200 } 201 202 203 205 213 214 public static String getProperty(String propertyName, String defaultValue) { 215 try { 216 String xx = System.getProperty(propertyName); 218 if (xx != null) { 219 return xx; 220 } 221 222 Properties pr = System.getProperties(); 224 Enumeration enumeration = pr.propertyNames(); 225 String enumKey; 226 while (enumeration.hasMoreElements()) { 227 enumKey = (String ) enumeration.nextElement(); 228 if (enumKey.equalsIgnoreCase(propertyName)) { 229 return pr.getProperty(enumKey); 230 } 231 } 232 234 } catch (Exception e) { 235 log.info("*** Exception getting property. propertyName: " 236 + propertyName + " defaultValue: " + defaultValue + "\n e: " + e); 237 } 238 return defaultValue; 239 } 240 241 242 247 public static String getHostName() { 248 String HostName; 249 InetAddress localHost = null; 250 try { 251 localHost = InetAddress.getLocalHost(); 252 } catch (UnknownHostException e) { 253 } 255 HostName = new String (localHost.getHostName()); 256 return HostName; 257 } 258 259 260 public static String getOSString() { 261 if (getProperty("os.name").toUpperCase().indexOf("WIN") >= 0) { 262 return "win"; 263 } else if (getProperty("os.name").toUpperCase().indexOf("SUN") >= 0 || 264 getProperty("os.name").toUpperCase().indexOf("LIN") >= 0) { 265 return "unix"; 266 } else { 267 return "mac"; 268 } 269 } 270 271 272 public static String getShortHostName() { 273 String shortHostName = getHostName(); 274 if (shortHostName.indexOf(".") > 0) { 275 shortHostName = shortHostName.substring(0, shortHostName.indexOf(".")); 276 } 277 return shortHostName; 278 } 279 280 281 282 287 public static String getFileSeparator() { 288 return getProperty("file.separator"); 289 } 290 291 292 public static boolean isNeedingWindowsFileSeparator(String aDirectory) { 293 return(isWindows() && !aDirectory.endsWith("/") && !aDirectory.endsWith("\\")); 294 } 295 296 public static boolean isWindows() { 297 return getProperty("os.name").toUpperCase().indexOf("WIN") >= 0; 298 } 299 300 301 302 308 public static void setProperty(String keyWord, String value) { 309 Properties pr = System.getProperties(); 310 log.debug("Setting keyword: " + keyWord + " Value: " + value); 311 if (value == null) { 312 pr.remove(keyWord); 313 } else { 314 pr.put(keyWord, value); 315 } 316 317 } 318 public static void main(String [] args){ 319 listProperties(); 320 } 321 322 } 323 | Popular Tags |