| 1 package com.sslexplorer.agent.client.util; 2 3 import java.io.BufferedReader ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileOutputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.io.OutputStream ; 12 import java.net.URL ; 13 import java.util.Locale ; 14 import java.util.MissingResourceException ; 15 import java.util.PropertyResourceBundle ; 16 import java.util.ResourceBundle ; 17 18 24 public class Utils { 25 26 29 public static int BUFFER_SIZE = 8192; 30 31 32 38 public static String trimBoth(String string) { 39 string = string.trim(); 40 for (int i = 0; i < string.length(); i++) { 41 if (string.charAt(i) != ' ') { 42 return string.substring(i); 43 } 44 } 45 return string; 46 } 47 48 56 public static String trimmedBothOrBlank(String string) { 57 return trimBoth(string == null ? "" : string.trim()); 58 } 59 60 67 public static String getHomeDirectory() { 68 69 String userHome = System.getProperty("user.home"); 71 if (System.getProperty("java.vendor") != null && System.getProperty("java.vendor").startsWith("Microsoft")) { 73 try { 74 Process process = Runtime.getRuntime().exec(new String [] { "cmd.exe", "/C", "echo", "%USERPROFILE%" }); BufferedReader reader = new BufferedReader (new InputStreamReader (process.getInputStream())); 76 77 String profileDir = reader.readLine(); 78 79 File f = new File (profileDir); 80 if (f.exists()) { 81 userHome = profileDir; 82 } 83 } catch (Throwable t) { 84 } 86 87 } 88 return userHome; 89 } 90 91 98 public static boolean isNullOrTrimmedBlank(String string) { 99 return string == null || string.trim().length() == 0; 100 } 101 102 110 public static Locale createLocale(String localeName) { 111 String lang = localeName; 112 String country = ""; 113 String variant = ""; 114 int idx = localeName.indexOf("_"); 115 if (idx != -1) { 116 country = lang.substring(idx + 1); 117 lang = lang.substring(0, idx); 118 } 119 idx = country.indexOf('_'); 120 if (idx != -1) { 121 variant = country.substring(idx + 1); 122 country = country.substring(0, idx); 123 } 124 return new Locale (lang, country, variant); 125 } 126 127 138 public static ResourceBundle getBundle(String basename, Locale locale, ClassLoader cl, URL url) throws MissingResourceException { 139 140 ResourceBundle resourceBundle = null; 141 142 146 String resource = basename.replace('.', '/') + ".properties"; 148 System.out.println("Looking for '" + resource + "' in locale " + locale + " (display name = " + locale.getDisplayName() + "'"); 149 150 if(locale != null && !"en".equalsIgnoreCase(locale.toString()) && 152 !"en_GB".equalsIgnoreCase(locale.toString())) { 153 try { 154 URL resourceUrl = new URL (url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + resource); 155 System.out.println("Non GB resource, so trying server '" + resourceUrl.toExternalForm() + "'"); 156 InputStream in = resourceUrl.openStream(); 157 try { 158 resourceBundle = new PropertyResourceBundle (in); 159 } 160 finally { 161 closeStream(in); 162 } 163 } catch (IOException ioe) { 164 ioe.printStackTrace(); 165 } 166 } 167 168 if(resourceBundle == null && cl != null) { 170 System.out.println("Must GB resource, so trying class load"); 171 InputStream in = cl.getResourceAsStream(resource); 172 try { 173 resourceBundle = new PropertyResourceBundle (in); 174 } catch (IOException ioe) { 175 ioe.printStackTrace(); 176 } 177 finally { 178 closeStream(in); 179 } 180 } 181 182 if(resourceBundle == null) { 184 throw new MissingResourceException ("No such bundle could be located for " + basename, basename, ""); 185 } 186 187 return resourceBundle; 188 } 189 190 197 public static boolean checkVersion(String applicationJRE) { 198 if (applicationJRE == null) { 199 return false; 200 } 201 202 int[] applicationVersion = Utils.getVersion(applicationJRE); 203 int[] installedJREVersion = Utils.getVersion(System.getProperty("java.version")); 205 for (int i = 0; i < applicationVersion.length && i < installedJREVersion.length; i++) { 206 if (applicationVersion[i] > installedJREVersion[i]) 207 return false; 208 } 209 210 return true; 211 } 212 213 219 public static int[] getVersion(String version) { 220 int idx = 0; 221 int pos = 0; 222 int[] result = new int[0]; 223 do { 224 225 idx = version.indexOf('.', pos); 226 int v; 227 if (idx > -1) { 228 v = Integer.parseInt(version.substring(pos, idx)); 229 pos = idx + 1; 230 } else { 231 try { 232 int sub = version.indexOf('_', pos); 233 if (sub == -1) { 234 sub = version.indexOf('-', pos); 235 } 236 if (sub > -1) { 237 v = Integer.parseInt(version.substring(pos, sub)); 238 } else { 239 v = Integer.parseInt(version.substring(pos)); 240 } 241 } catch (NumberFormatException ex) { 242 break; 244 } 245 } 246 int[] tmp = new int[result.length + 1]; 247 System.arraycopy(result, 0, tmp, 0, result.length); 248 tmp[tmp.length - 1] = v; 249 result = tmp; 250 251 } while (idx > -1); 252 253 return result; 254 } 255 256 public static boolean isSupportedJRE(String jre) { 257 return isVersion(jre, System.getProperty("java.version")); 258 } 259 260 public static boolean isVersion(String required, String have) { 261 262 int[] ourVersion = Utils.getVersion(have); 264 if (required.startsWith(">") || required.startsWith("+")) { 266 int[] requiredVersion = Utils.getVersion(required.substring(1)); 268 for (int i = 0; i < Math.max(ourVersion.length,requiredVersion.length); i++) { 269 if ( ( i < ourVersion.length ?ourVersion[i] : 0 ) < ( i < requiredVersion.length ? requiredVersion[i] : 0 ) ) 270 return false; 271 } 272 return true; 273 274 } else if (required.startsWith("<") || required.startsWith("-")) { int[] requiredVersion = Utils.getVersion(required.substring(1)); 277 for (int i = 0; i < Math.min(ourVersion.length,requiredVersion.length); i++) { 278 if ( ( i < ourVersion.length ?ourVersion[i] : 0 ) > ( i < requiredVersion.length ? requiredVersion[i] : 0 ) ) 279 return false; 280 } 281 return true; 282 283 } else { 284 int[] requiredVersion = Utils.getVersion(required); 286 for (int i = 0; i < Math.max(ourVersion.length,requiredVersion.length); i++) { 287 if ( ( i < ourVersion.length ?ourVersion[i] : 0 ) != ( i < requiredVersion.length ? requiredVersion[i] : 0 ) ) 288 return false; 289 } 290 return true; 291 292 } 293 294 } 295 296 public static boolean isSupportedOSVersion(String osVersion) { 297 return isVersion(osVersion, System.getProperty("os.version")); 298 } 299 300 public static boolean isSupportedPlatform(String os) { 301 if (os != null) { 302 String platform = System.getProperty("os.name").toUpperCase(); if(os.startsWith("!")) { return !platform.startsWith(os.substring(1).toUpperCase()); 307 } else 308 return platform.startsWith(os.toUpperCase()); 309 } else 310 return true; 311 } 312 313 public static boolean isSupportedArch(String arch) { 314 if (arch != null) { 315 String platformArch = System.getProperty("os.arch").toUpperCase(); if(arch.startsWith("!")) { if(isWindows64JREAvailable()) 318 return !arch.substring(1).toUpperCase().equals("AMD64"); 319 else 320 return !platformArch.startsWith(arch.substring(1).toUpperCase()); 321 } else { 322 if(isWindows64JREAvailable()) 323 return arch.toUpperCase().equals("AMD64"); 324 else 325 return platformArch.startsWith(arch.toUpperCase()); 326 } 327 } else 328 return true; 329 } 330 331 public static boolean isWindows64JREAvailable() { 332 333 try { 334 String javaHome = new File (System.getProperty("java.home")).getCanonicalPath(); 335 336 try { 337 if(System.getProperty("os.name").startsWith("Windows")) { 338 int dataModel = Integer.parseInt(System.getProperty("sun.arch.data.model")); 339 340 if(dataModel!=64) { 341 int idx = javaHome.indexOf(" (x86)"); 342 if(idx > -1) { 343 String programFiles = javaHome.substring(0, idx); 345 File j = new File (programFiles, "Java"); 346 if(j.exists()) { 347 String [] jres = j.list(); 349 for(int i=0;i<jres.length;i++) { 350 351 File h = new File (j, jres[i]); 352 File exe = new File (h, "bin\\java.exe"); 353 if(exe.exists()) { 354 return true; 356 } 357 } 358 } 359 } 360 } 361 } 362 } catch(NumberFormatException ex) { 363 } 364 365 return false; 366 } catch(IOException ex) { 367 return false; 368 } 369 } 370 371 public static String getJavaHome() { 372 373 381 try { 382 String javaHome = new File (System.getProperty("java.home")).getCanonicalPath(); 383 384 try { 385 if(System.getProperty("os.name").startsWith("Windows")) { 386 int dataModel = Integer.parseInt(System.getProperty("sun.arch.data.model")); 387 388 if(dataModel!=64) { 389 int idx = javaHome.indexOf(" (x86)"); 390 if(idx > -1) { 391 String programFiles = javaHome.substring(0, idx); 393 File j = new File (programFiles, "Java"); 394 if(j.exists()) { 395 String [] jres = j.list(); 397 for(int i=0;i<jres.length;i++) { 398 399 File h = new File (j, jres[i]); 400 File exe = new File (h, "bin\\java.exe"); 401 if(exe.exists()) { 402 javaHome = h.getAbsolutePath(); 404 break; 405 } 406 } 407 } 408 } 409 } 410 } 411 } catch(NumberFormatException ex) { 412 } 413 414 return javaHome; 415 } catch(IOException ex) { 416 return System.getProperty("java.home"); 417 } 418 } 419 420 427 public static void makeExecutable(File file) throws IOException { 428 Process p = Runtime.getRuntime().exec(new String [] { "chmod", "ug+rx", file.getAbsolutePath() }); 429 try { 430 copy(p.getErrorStream(), new ByteArrayOutputStream ()); 431 } finally { 432 try { 433 if (p.waitFor() != 0) { 434 throw new IOException ("Failed to set execute permission. Return code " + p.exitValue() + "."); 435 } 436 } catch (InterruptedException e) { 437 } 438 } 439 440 } 441 442 450 public static void makeReadOnly(File file) throws IOException { 451 try { 452 file.getClass().getMethod("setReadOnly", new Class [] {}).invoke(file, new Object [] {}); 453 } catch (Exception e) { 454 Process p = Runtime.getRuntime().exec(new String [] { "chmod", "a-w", file.getAbsolutePath() }); 455 try { 456 copy(p.getErrorStream(), new ByteArrayOutputStream ()); 457 } finally { 458 try { 459 if (p.waitFor() != 0) { 460 throw new IOException ("Failed to set execute permission. Return code " + p.exitValue() + "."); 461 } 462 } catch (InterruptedException ie) { 463 } 464 } 465 466 } 467 468 } 469 470 478 public static void copy(InputStream in, OutputStream out) throws IOException { 479 copy(in, out, -1); 480 } 481 482 491 public static void copy(InputStream in, OutputStream out, long count) throws IOException { 492 copy(in, out, count, BUFFER_SIZE); 493 } 494 495 505 public static void copy(InputStream in, OutputStream out, long count, int bufferSize) throws IOException { 506 byte buffer[] = new byte[bufferSize]; 507 int i = bufferSize; 508 if (count >= 0) { 509 while (count > 0) { 510 if (count < bufferSize) 511 i = in.read(buffer, 0, (int) count); 512 else 513 i = in.read(buffer, 0, bufferSize); 514 515 if (i == -1) 516 break; 517 518 count -= i; 519 out.write(buffer, 0, i); 520 } 521 } else { 522 while (true) { 523 i = in.read(buffer, 0, bufferSize); 524 if (i < 0) 525 break; 526 out.write(buffer, 0, i); 527 } 528 } 529 } 530 531 538 public static boolean closeStream(InputStream in) { 539 try { 540 if (in != null) { 541 in.close(); 542 } 543 544 return true; 545 } catch (IOException ioe) { 546 return false; 547 } 548 } 549 550 public static void copyFile(File from, File to) throws IOException { 551 552 if (from.isDirectory()) { 553 if (!to.exists()) { 554 to.mkdir(); 555 } 556 String [] children = from.list(); 557 for (int i = 0; i < children.length; i++) { 558 File f = new File (from, children[i]); 559 if (f.getName().equals(".") || f.getName().equals("..")) { 560 continue; 561 } 562 if (f.isDirectory()) { 563 File f2 = new File (to, f.getName()); 564 copyFile(f, f2); 565 } else { 566 copyFile(f, to); 567 } 568 } 569 } else if (from.isFile() && (to.isDirectory() || to.isFile())) { 570 if (to.isDirectory()) { 571 to = new File (to, from.getName()); 572 } 573 FileInputStream in = new FileInputStream (from); 574 FileOutputStream out = new FileOutputStream (to); 575 byte[] buf = new byte[32678]; 576 int read; 577 while ((read = in.read(buf)) > -1) { 578 out.write(buf, 0, read); 579 } 580 closeStream(in); 581 closeStream(out); 582 583 } 584 } 585 586 593 public static boolean closeStream(OutputStream out) { 594 try { 595 if (out != null) { 596 out.close(); 597 } 598 599 return true; 600 } catch (IOException ioe) { 601 return false; 602 } 603 } 604 605 } 606 | Popular Tags |