| 1 21 22 package com.izforge.izpack.util; 23 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.util.StringTokenizer ; 29 30 31 41 42 57 public class TargetFactory 58 { 59 60 64 66 67 public static final int WINDOWS = 0; 68 69 70 public static final int UNIX = 2; 71 72 73 public static final int GENERIC = 3; 74 75 77 78 public static final int STANDARD = 0; 79 80 84 public static final int NT = 1; 85 86 87 public static final int X = 2; 88 89 91 92 public static final int X86 = 0; 93 94 95 public static final int OTHER = 1; 96 97 109 static final String [] LIBRARY_EXTENSION = { "dll", "so", "", ""}; 110 111 123 static final String [] CLASS_PREFIX = { "Win_", "Mac_", "Unix_", ""}; 124 125 136 static final String [] CLASS_FLAVOR_PREFIX = { "", "NT_", "X_"}; 137 138 147 static final String [] CLASS_ARCHITECTURE_PREFIX = { "X86_", "U_" }; 151 152 166 static final String [] INSTALL_PATH_FRAGMENT = { "Program Files" + File.separator, 167 "/Applications" + File.separator, "/usr/local" + File.separator, 168 File.separator + "apps" + File.separator}; 169 170 180 static final String [][] INSTALL_PATH_RESOURCE_KEY = { 181 { "TargetPanel.dir.windows", "TargetPanel.dir.windows", ""}, { "TargetPanel.dir.mac", "", "TargetPanel.dir.macosx"}, { "TargetPanel.dir.unix", "", ""}, { "TargetPanel.dir", "", ""} }; 187 188 189 private static final String VERSION_DELIMITER = ".-"; 190 191 198 private static TargetFactory me = null; 199 200 201 private int os = -1; 202 203 204 private int osFlavor = -1; 205 206 207 private int architecture = -1; 208 209 210 private String version = ""; 211 212 213 216 217 225 private TargetFactory() 226 { 227 version = System.getProperty("os.version"); 228 229 if (OsVersion.IS_WINDOWS) 233 { 234 os = WINDOWS; 235 osFlavor = STANDARD; 236 architecture = X86; 237 String osName = OsVersion.OS_NAME.toLowerCase(); 238 239 if (osName.indexOf("nt") > -1) 240 { 241 osFlavor = NT; 242 } 243 else if (osName.indexOf("2000") > -1) 244 { 245 osFlavor = NT; 246 } 247 else if (osName.indexOf("xp") > -1) 248 { 249 osFlavor = NT; 250 } 251 } 252 else if (OsVersion.IS_OSX) 256 { 257 os = X; 258 osFlavor = STANDARD; 259 architecture = OTHER; 260 } 261 else 265 { 266 os = UNIX; 267 osFlavor = STANDARD; 268 architecture = OTHER; 269 String osName = OsVersion.OS_NAME.toLowerCase(); 270 271 if (osName.indexOf("x86") > -1) 272 { 273 architecture = X86; 274 } 275 } 276 } 277 278 279 284 285 public static TargetFactory getInstance() 286 { 287 if (me == null) 288 { 289 me = new TargetFactory(); 290 } 291 292 return me; 293 } 294 295 296 368 369 public Object makeObject(String name) throws Exception  370 { 371 int nameStart = name.lastIndexOf('.') + 1; 372 String packageName = name.substring(0, nameStart); 373 String className = name.substring(nameStart, name.length()); 374 String actualName; 375 376 try 377 { 378 actualName = packageName + CLASS_PREFIX[os] + CLASS_FLAVOR_PREFIX[osFlavor] + className; 379 Class temp = Class.forName(actualName); 380 return temp.newInstance(); 381 } 382 catch (Throwable exception1) 383 { 384 try 385 { 386 Class temp = Class.forName(packageName + CLASS_PREFIX[os] + className); 387 return temp.newInstance(); 388 } 389 catch (Throwable exception2) 390 { 391 try 392 { 393 actualName = name; 394 Class temp = Class.forName(actualName); 395 return temp.newInstance(); 396 } 397 catch (Throwable exception3) 398 { 399 throw new Exception ("can not instantiate class " + name); 400 } 401 } 402 } 403 } 404 405 406 415 416 429 public boolean versionIsHigher(String version) throws Exception  430 { 431 StringTokenizer targetVersion = new StringTokenizer (this.version, VERSION_DELIMITER); 432 StringTokenizer compareVersion = new StringTokenizer (version, VERSION_DELIMITER); 433 434 int target; 435 int compare; 436 437 while (targetVersion.hasMoreTokens() && compareVersion.hasMoreTokens()) 438 { 439 try 440 { 441 target = Integer.parseInt(targetVersion.nextToken()); 442 compare = Integer.parseInt(compareVersion.nextToken()); 443 } 444 catch (Throwable exception) 445 { 446 throw new Exception ("error in version string"); 447 } 448 449 if (compare > target) 450 { 451 return true; 452 } 453 else if (target > compare) { return false; } 454 } 455 456 return false; 457 } 458 459 460 469 470 public int getOS() 471 { 472 return os; 473 } 474 475 476 486 487 public int getOSFlavor() 488 { 489 return osFlavor; 490 } 491 492 493 501 502 public int getArchitecture() 503 { 504 return architecture; 505 } 506 507 508 515 516 public String getNativeLibraryExtension() 517 { 518 return LIBRARY_EXTENSION[os]; 519 } 520 521 522 545 546 557 public String getDefaultInstallPath(String appName) 558 { 559 String path = null; 560 InputStream input; 561 String keyFragment = "/res/" + INSTALL_PATH_RESOURCE_KEY[GENERIC][STANDARD]; 562 563 input = getClass().getResourceAsStream("/res/" + INSTALL_PATH_RESOURCE_KEY[os][osFlavor]); 568 569 if (input == null) 576 { 577 String key = OsVersion.OS_NAME.toLowerCase().replace(' ', '_'); key = keyFragment + key.toLowerCase(); input = TargetFactory.class.getResourceAsStream(key); 585 } 586 587 if (input == null) 592 { 593 input = TargetFactory.class.getResourceAsStream(keyFragment); 594 } 595 596 if (input != null) 601 { 602 InputStreamReader streamReader; 603 BufferedReader reader = null; 604 String line; 605 606 try 607 { 608 streamReader = new InputStreamReader (input); 609 reader = new BufferedReader (streamReader); 610 line = reader.readLine(); 611 612 while (line != null) 613 { 614 line = line.trim(); 615 if (!"".equals(line)) 616 { 617 break; 618 } 619 line = reader.readLine(); 620 } 621 path = line; 622 } 623 catch (Throwable exception) 624 {} 625 finally 626 { 627 try 628 { 629 if (reader != null) reader.close(); 630 } 631 catch (Throwable exception) 632 {} 633 } 634 } 635 636 if (path == null || "".equals(path)) 641 { 642 path = ""; 643 644 if (os == WINDOWS) 652 { 653 String home = System.getProperty("user.home"); 654 path = home.substring(0, home.indexOf(File.separatorChar) + 1); 656 } 657 658 path = path + INSTALL_PATH_FRAGMENT[os] + appName; 659 } 660 661 return path; 662 } 663 664 670 671 public static String getCurrentOSPrefix() 672 { 673 String OSName = System.getProperty("os.name").toLowerCase(); 674 String OSArch = System.getProperty("os.arch").toLowerCase(); 675 int OS = 0; 676 int OSFlavor = 0; 677 int OSarchitecture = 0; 678 if (OSName.indexOf("windows") > -1) 682 { 683 OS = WINDOWS; 684 OSFlavor = STANDARD; 685 OSarchitecture = X86; 686 687 if (OSName.indexOf("nt") > -1) 688 { 689 OSFlavor = NT; 690 } 691 else if (OSName.indexOf("2000") > -1) 692 { 693 OSFlavor = NT; 694 } 695 else if (OSName.indexOf("xp") > -1) 696 { 697 OSFlavor = NT; 698 } 699 } 700 else if (OSName.indexOf("mac") > -1) 704 { 705 OS = GENERIC; 706 OSFlavor = STANDARD; 707 OSarchitecture = OTHER; 708 709 if (OSName.indexOf("macosx") > -1) 710 { 711 OSFlavor = X; 712 } 713 } 714 else 718 { 719 OS = UNIX; 720 OSFlavor = STANDARD; 721 OSarchitecture = OTHER; 722 723 if (OSArch.indexOf("86") > -1) 724 { 725 OSarchitecture = X86; 726 } 727 } 728 729 return (CLASS_PREFIX[OS] + CLASS_FLAVOR_PREFIX[OSFlavor]); 730 } 731 732 } 733 734 | Popular Tags |