1 32 33 package com.knowgate.misc; 34 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.StringBufferInputStream ; 38 import java.io.FileNotFoundException ; 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.FileOutputStream ; 42 import java.io.FileReader ; 43 import java.io.FileWriter ; 44 45 import java.lang.System ; 46 import java.util.Properties ; 47 import java.util.HashMap ; 48 import java.util.Date ; 49 import java.util.Set ; 50 import java.text.SimpleDateFormat ; 51 52 import com.knowgate.debug.*; 53 54 59 60 public class Environment { 61 public static String DEFAULT_PROFILES_DIR = (System.getProperty("os.name").equals("Windows XP") ? "C:\\Windows\\" : (System.getProperty("os.name").startsWith("Windows") ? "C:\\WINNT\\" : "/etc/")); 62 63 private Environment() { } 64 65 67 private static String getEnvironmentDirectory() { 68 if (DEFAULT_PROFILES_DIR.equalsIgnoreCase("C:\\WINNT\\")) { 69 File oWinDir = new File ("C:\\WINNT"); 70 if (!oWinDir.exists()) { 71 oWinDir = new File ("C:\\WINDOWS"); 72 if (oWinDir.exists()) { 73 DEFAULT_PROFILES_DIR = "C:\\WINDOWS\\"; 74 } 75 else { 76 DEFAULT_PROFILES_DIR = getEnvVar("windir", getEnvVar("SystemRoot")); 77 } 78 } 79 } return DEFAULT_PROFILES_DIR; 81 } 82 83 85 private static void readEnvVars() throws java.lang.IllegalArgumentException { 86 envVars = new Properties (); 87 Runtime oRT; 88 Process oPT; 89 InputStream oST; 90 91 final int ENV_BUFFER_SIZE = 131072; 92 93 try { 94 if (System.getProperty("os.name").startsWith("Windows")) { 95 96 if (DebugFile.trace) DebugFile.writeln ("Runtime.getRuntime()"); 97 98 oRT = Runtime.getRuntime(); 99 100 if (DebugFile.trace) DebugFile.writeln ("Runtime.exec(\"cmd.exe /cset\")"); 101 102 oPT = oRT.exec("cmd.exe /cset"); 103 104 oST = oPT.getInputStream(); 105 106 byte[] byBuffer = new byte[ENV_BUFFER_SIZE]; 107 108 int iReaded = oST.read (byBuffer, 0, ENV_BUFFER_SIZE); 109 110 oST.close(); 111 112 oPT.destroy(); 113 114 oRT = null; 115 116 byte[] byEnvVars = new byte[ENV_BUFFER_SIZE+4096]; 118 int iEnvLength = 0; 119 120 for (int i=0; i<iReaded; i++) { 121 byEnvVars[iEnvLength++] = byBuffer[i]; 122 if (92==byBuffer[i]) 123 byEnvVars[iEnvLength++] = byBuffer[i]; 124 } 126 byBuffer = null; 127 128 if (DebugFile.trace) DebugFile.writeln (new String (byEnvVars, 0, iEnvLength)); 129 130 envVars.load (new StringBufferInputStream (new String (byEnvVars, 0, iEnvLength))); 131 132 } 133 else { 134 135 if (DebugFile.trace) DebugFile.writeln ("Runtime.getRuntime()"); 136 137 oRT = Runtime.getRuntime(); 138 139 if (DebugFile.trace) DebugFile.writeln ("Runtime.exec(\"/usr/bin/env\")"); 140 141 oPT = oRT.exec("/usr/bin/env"); 142 143 oST = oPT.getInputStream(); 144 145 if (DebugFile.trace) DebugFile.writeln ("Properties.load(Process.getInputStream())"); 146 147 envVars.load(oST); 148 149 oST.close(); 150 151 oPT.destroy(); 152 153 oRT = null; 154 } 155 } 156 catch (IOException ioe) { 157 if (DebugFile.trace) DebugFile.writeln ("Runtime.getRuntime().exec(...) IOException " + ioe.getMessage()); 158 } 159 catch (NullPointerException npe) { 160 if (DebugFile.trace) DebugFile.writeln ("Runtime.getRuntime().exec(...) NullPointerException " + npe.getMessage()); 161 } 162 finally { 163 if (null==envVars.getProperty("KNOWGATE_PROFILES")) { 164 if (DebugFile.trace) DebugFile.writeln ("KNOWGATE_PROFILES environment variable not found setting default to "+getEnvironmentDirectory()); 165 166 envVars.setProperty("KNOWGATE_PROFILES", getEnvironmentDirectory()); 167 } 168 } 169 } 171 173 181 182 public static String getEnvVar(String sVarName) 183 throws IllegalArgumentException { 184 185 if (envVars==null) readEnvVars(); 186 187 return envVars.getProperty(sVarName); 188 } 190 192 199 200 public static String getEnvVar(String sVarName, String sDefault) 201 throws IllegalArgumentException { 202 if (envVars==null) readEnvVars(); 203 204 String sRetVal = envVars.getProperty(sVarName); 205 206 if (sRetVal==null) 207 return sDefault; 208 else 209 return sRetVal; 210 } 212 214 221 public static String getTempDir() throws IllegalArgumentException { 222 if (System.getProperty("os.name").startsWith("Windows")) { 223 String sTempDir = getEnvVar("TEMP"); 224 if (null==sTempDir) { 225 File oWinDir; 226 oWinDir = new File ("C:\\WINNT\\TEMP\\"); 227 if (oWinDir.exists()) { 228 return "C:\\WINNT\\TEMP\\"; 229 } else { 230 oWinDir = new File ("C:\\WINDOWS\\TEMP\\"); 231 if (oWinDir.exists()) { 232 return "C:\\WINDOWS\\TEMP\\"; 233 } else { 234 return "C:\\TEMP\\"; 235 } 236 } 237 } else { 238 return sTempDir; 239 } 240 } else { return "/tmp/"; 242 } 243 } 245 247 258 public static Properties getProfile(String sProfile) { 259 String sProfilesHome; 260 Properties oProfile; 261 262 if (DebugFile.trace) DebugFile.writeln("Begin Environment.getProfile()"); 263 264 oProfile = (Properties ) profiles.get(sProfile); 265 266 if (oProfile==null) { 267 268 try { 269 sProfilesHome = System.getProperty("KNOWGATE_PROFILES", getEnvVar("KNOWGATE_PROFILES")); 270 } 271 catch (java.lang.IllegalArgumentException iae) { 272 sProfilesHome = getEnvironmentDirectory(); 273 274 if (DebugFile.trace) DebugFile.writeln("Environment.getEnvVar(KNOWGATE_PROFILES) IllegalArgumentException " + iae.getMessage()); 275 } 276 277 if (DebugFile.trace) DebugFile.writeln(" KNOWGATE_PROFILES=" + sProfilesHome); 278 279 if (!sProfilesHome.endsWith(System.getProperty("file.separator"))) 280 sProfilesHome += System.getProperty("file.separator"); 281 282 oProfile = loadProfile(sProfile, sProfilesHome + (sProfile.endsWith(".cnf") ? sProfile : sProfile + ".cnf")); 283 284 } 286 if (DebugFile.trace) DebugFile.writeln("End Environment.getProfile()"); 287 288 return oProfile; 289 } 291 293 301 public static Properties loadProfile(String sProfile, String sPath) { 302 FileInputStream oFileStream; 303 Properties oProfile; 304 305 if (DebugFile.trace) DebugFile.writeln("Begin Environment.loadProfile(" + sProfile + "," + sPath + ")"); 306 307 if (profiles.containsKey(sProfile)) 308 profiles.remove(sProfile); 309 310 oProfile = new Properties (); 311 try { 312 oFileStream = new FileInputStream (sPath); 313 314 oProfile.load(oFileStream); 315 oFileStream.close(); 316 317 profiles.put(sProfile, oProfile); 318 } 319 catch (FileNotFoundException nfe) { 320 if (DebugFile.trace) DebugFile.writeln("FileNotFoundException " + sPath + " " + nfe.getMessage()); 321 } 322 catch (IOException ioe) { 323 if (DebugFile.trace) DebugFile.writeln("IOException " + sPath + " " + ioe.getMessage()); 324 } 325 326 if (DebugFile.trace) DebugFile.writeln("End Environment.loadProfile()"); 327 328 return oProfile; 329 } 331 333 337 public static Set getProfilesSet() { 338 return profiles.keySet(); 339 } 340 341 343 352 public static String getProfileVar(String sProfile, String sVarName) { 353 String sRetVal; 354 Properties oProfile; 355 356 358 oProfile = getProfile(sProfile); 359 360 if (oProfile==null) 361 sRetVal = null; 362 else 363 sRetVal = oProfile.getProperty(sVarName); 364 366 368 return sRetVal; 369 } 371 373 378 public static Set getProfileVarSet(String sProfile) { 379 380 Set oRetVal; 381 Properties oProfile; 382 383 385 oProfile = getProfile(sProfile); 386 387 if (oProfile==null) 388 oRetVal = null; 389 else 390 oRetVal = oProfile.keySet(); 391 393 400 401 return oRetVal; 402 403 } 405 407 415 416 public static String getProfilePath(String sProfile, String sVarName) { 417 String sPath = getProfileVar(sProfile, sVarName); 418 return Gadgets.chomp(sPath, System.getProperty("file.separator")); 419 } 420 421 423 430 431 public static String getProfileVar(String sProfile, String sVarName, String sDefault) { 432 String sRetVal; 433 Properties oProfile; 434 435 437 oProfile = getProfile(sProfile); 438 439 if (oProfile==null) 440 sRetVal = null; 441 else 442 sRetVal = oProfile.getProperty(sVarName); 443 445 447 return (null!=sRetVal ? sRetVal : sDefault); 448 } 450 452 459 public static void setProfileVar(String sProfile, String sVarName, String sVarValue) { 460 Properties oProfile = Environment.getProfile(sProfile); 461 462 oProfile.setProperty(sVarName, sVarValue); 463 } 465 467 private static boolean execCommand(String sCmd) { 468 boolean bRetVal=true; 469 470 try { 471 Runtime.getRuntime().exec(sCmd); 472 } 473 catch (IOException ioe) { 474 bRetVal=false; 475 } 476 477 return bRetVal; 478 } 480 482 485 486 public static void refresh() { 487 envVars = null; 488 profiles = new HashMap (); 489 } 490 491 493 498 499 public static void updateSystemTime(long lTime) { 500 try 501 { 502 String system = System.getProperty("os.name"); 503 String cmdDate = ""; 504 String cmdTime = ""; 505 Date curDate = new Date (); 506 curDate.setTime(lTime); 507 SimpleDateFormat fmt = new SimpleDateFormat (getProfileVar("hipergate", "dateformat", "dd-MM-yyyy")); 508 String actDate = fmt.format(curDate); 511 fmt.applyPattern("HH:mm:ss"); 512 String actTime = fmt.format(curDate); 513 if (system.startsWith("Windows NT") || system.startsWith("Windows 2000")) { 514 execCommand("cmd /c date " + actDate); 515 cmdTime = "cmd /c time " + actTime; 516 execCommand(cmdTime); 517 } else 518 if (system.indexOf("Windows") == 0) { 520 fmt.applyPattern("MM.dd.yyyy"); 521 actDate = fmt.format(curDate); 522 execCommand("c:\\command.com /c date " + actDate); 523 fmt.applyPattern("dd-MM-yyyy"); 524 actDate = fmt.format(curDate); 525 execCommand("c:\\command /c date " + actDate); 526 fmt.applyPattern("yyyy-MM-dd"); 527 actDate = fmt.format(curDate); 528 execCommand("c:\\command /c date " + actDate); 529 cmdTime = "c:\\command.com /c time " + actTime; 530 execCommand(cmdTime); 531 } else 532 { 534 fmt.applyPattern("MM/dd/yyyy HH:mm:ss"); 535 actDate = fmt.format(curDate); 536 cmdDate = "date -u -s'" + actDate + "' +'%D %T'"; 537 execCommand(cmdDate); 538 } 539 } 540 catch (Exception e) { 541 } 542 } 544 546 private static Properties envVars = null; 547 private static HashMap profiles = new HashMap (); 548 549 }
| Popular Tags
|