1 package com.ca.commons.cbutil; 2 3 import java.io.*; 4 import java.util.Enumeration ; 5 import java.util.logging.*; 6 7 23 24 public class CBSystemProperties 25 { 26 private static Logger log = Logger.getLogger(CBSystemProperties.class.getName()); 27 28 33 34 public static boolean isWindows() 35 { 36 String os = System.getProperty("os.name"); 37 if (os != null && os.toLowerCase().indexOf("windows") > -1) 38 { 39 log.fine("this is a windows machine."); 40 return true; 41 } 42 43 log.fine("this is a unix machine."); 44 return false; 45 } 46 47 48 54 55 private static void writeBatchFile(String batchFileName) 56 throws IOException 57 { 58 File setBatch = new File(batchFileName); 59 60 if (setBatch.exists() == false) 62 { 63 FileWriter out = new FileWriter(setBatch); 64 out.write("set", 0, 3); 65 out.flush(); 66 out.close(); 67 } 68 } 69 70 75 76 private static void deleteBatchFile(String batchFileName) 77 { 78 File setBatch = new File(batchFileName); 79 80 try 84 { 85 setBatch.delete(); 86 } 87 catch (Exception e) 88 { 89 log.log(Level.WARNING, "unable to delete batch file " + batchFileName, e); 90 } 91 } 92 93 100 101 private static void readSystemProperties(String batchFileName) 102 throws IOException 103 { 104 Process bloop = Runtime.getRuntime().exec(batchFileName); 106 107 BufferedReader input = new BufferedReader(new InputStreamReader(bloop.getInputStream())); 108 BufferedReader errors = new BufferedReader(new InputStreamReader(bloop.getErrorStream())); 109 110 113 String line = ""; 114 115 log.fine("reading output from batch file"); 116 117 setSystemPropertiesFromBufferedReader(input); 118 119 while ((line = errors.readLine()) != null) 120 { 121 System.out.println("ERRORS: " + line); 122 } 123 124 125 } 126 127 private static void setSystemPropertiesFromBufferedReader(BufferedReader input) throws IOException 128 { 129 String line; 130 int pos; 131 String name; 132 String value; 133 while ((line = input.readLine()) != null) 134 { 135 log.finest("read raw line: " + line); 136 137 if ((pos = line.indexOf('=')) > 0) 138 { 139 name = line.substring(0, pos); 140 if (line.length() > pos) 141 value = line.substring(pos + 1).trim(); 142 else 143 value = ""; 144 145 146 if (System.getProperty(name) == null) 147 { 148 System.setProperty(name, value); 149 log.fine("SET setting property '" + name + "' equal '" + value + "'"); 150 } 151 else 152 log.fine("skipping existing value for: " + name); 153 154 } 155 } 156 } 157 158 161 162 private static void dumpProperties() 165 { 166 167 Enumeration keys = System.getProperties().propertyNames(); 168 while (keys.hasMoreElements()) 169 { 170 String key = (String ) keys.nextElement(); 171 log.fine("SYSPROPS: " + key + " : " + System.getProperty(key)); 172 } 173 } 174 175 183 public static boolean loadSystemProperties(File propertiesListFile) 184 { 185 if (propertiesListFile.exists() == false) 186 { 187 log.warning("Unable to find system properties file: '" + propertiesListFile.toString() + "' - attempting batch load instead"); 188 return loadSystemProperties(); 189 } 190 191 try 192 { 193 setSystemPropertiesFromBufferedReader(new BufferedReader(new FileReader(propertiesListFile))); 194 dumpProperties(); 195 196 return true; 197 } 198 catch (IOException e) 199 { 200 log.log(Level.WARNING, "Error trying to load system properties file '" + propertiesListFile.toString() + "' - - attempting batch load instead.", e); 201 return loadSystemProperties(); 202 } 203 } 204 205 213 214 public static boolean loadSystemProperties() 215 { 216 try 217 { 218 String batchFileName = isWindows() ? "runset.bat" : "runset.sh"; 221 222 223 writeBatchFile(batchFileName); 225 226 readSystemProperties(batchFileName); 229 230 dumpProperties(); 232 233 deleteBatchFile(batchFileName); 235 236 return true; 237 } 238 catch (Exception e) 239 { 240 log.log(Level.SEVERE, "Error reading system properties...\n", e); 241 244 return false; 245 } 246 } 247 248 253 254 public static void main(String argsv[]) 255 { 256 log.addHandler(new ConsoleHandler()); 257 log.setLevel(Level.FINEST); 258 log.fine("CBSystemProperties log active"); 259 260 loadSystemProperties(); 261 } 262 263 } | Popular Tags |