1 5 package com.tc.object.tools; 6 7 import com.tc.util.runtime.Vm; 8 import com.tc.util.runtime.Vm.UnknownJvmVersionException; 9 10 import java.util.Properties ; 11 12 public class BootJarSignature { 13 14 public static final char SIGNATURE_SEPARATOR = '_'; 15 16 private static final String OS_WINDOWS = "win32"; 17 private static final String OS_LINUX = "linux"; 18 private static final String OS_SOLARIS_SPARC = "solaris"; 19 private static final String OS_MAC_OSX = "osx"; 20 private static final String OS_SOLARIS_X86 = "solaris-x86"; 21 22 private static final String VM_VENDOR_SUN = "hotspot"; 23 private static final String VM_VENDOR_BEA = "jrockit"; 24 25 private final String signature; 26 27 BootJarSignature(final Properties source) throws UnsupportedVMException { 28 signature = generateBootJarSignature(source); 29 } 30 31 BootJarSignature(final String signature) { 32 this.signature = signature; 33 } 34 35 private String generateBootJarSignature(final Properties source) throws UnsupportedVMException { 36 String os = getOS(source); 37 String version = getVMVersion(source); 38 String vendor = getVendor(source); 39 40 validateComponent(os); 41 validateComponent(version); 42 validateComponent(vendor); 43 44 return vendor + SIGNATURE_SEPARATOR + os + SIGNATURE_SEPARATOR + version; 45 } 46 47 private String getVendor(final Properties source) throws UnsupportedVMException { 48 String vendor = source.getProperty("java.vendor"); 49 50 if (vendor == null) { 51 UnsupportedVMException uvme = new UnsupportedVMException("Cannot determine VM vendor: " 52 + "(\"java.vendor\" system property is null)"); 53 throw uvme; 54 } 55 56 if (vendor.toLowerCase().startsWith("bea ")) { return VM_VENDOR_BEA; } 57 if (vendor.toLowerCase().startsWith("apple ")) { return VM_VENDOR_SUN; } 58 if (vendor.toLowerCase().startsWith("sun ")) { 59 final Vm.Version vmVersion; 60 try { 61 vmVersion = new Vm.Version(source); 62 } catch (UnknownJvmVersionException ujve) { 63 UnsupportedVMException uvme = new UnsupportedVMException("Unable to extract the JVM version with properties: " 64 + source, ujve); 65 throw uvme; 66 } 67 if (vmVersion.isJRockit()) { 68 return VM_VENDOR_BEA; 70 } 71 return VM_VENDOR_SUN; 72 } 73 74 throw new UnsupportedVMException("Unknown or unsupported vendor string: " + vendor); 75 } 76 77 private static String getVMVersion(final Properties source) throws UnsupportedVMException { 78 try { 79 final Vm.Version vmVersion = new Vm.Version(source); 80 return vmVersion.toString().replaceAll("\\.", ""); 81 } catch (final UnknownJvmVersionException ujve) { 82 final UnsupportedVMException uvme = new UnsupportedVMException("Cannot determine VM version", ujve); 83 throw uvme; 84 } 85 } 86 87 private static String getOS(final Properties source) throws UnsupportedVMException { 88 final String osProp = source.getProperty("os.name"); 89 if (osProp == null) { 90 UnsupportedVMException uvme = new UnsupportedVMException("Cannot determine operating system: " 91 + "(\"os.name\" system property is null)"); 92 throw uvme; 93 } 94 final String lowerCaseOS = osProp.toLowerCase(); 95 96 if (lowerCaseOS.startsWith("windows")) { return OS_WINDOWS; } 97 if (lowerCaseOS.startsWith("linux")) { return OS_LINUX; } 98 if (lowerCaseOS.startsWith("mac")) { return OS_MAC_OSX; } 99 if (lowerCaseOS.startsWith("sunos")) { 100 final String arch = source.getProperty("os.arch"); 101 if (arch != null) { 102 final String lowerCaseArch = arch.toLowerCase(); 103 if (lowerCaseArch.startsWith("sparc")) { 104 return OS_SOLARIS_SPARC; 105 } else if (lowerCaseArch.indexOf("86") > -1) { 106 return OS_SOLARIS_X86; 107 } else { 108 throw new UnsupportedVMException("Unknown Solaris architecture: " + "(\"os.arch\" = " + arch + ")"); 109 } 110 } else { 111 throw new UnsupportedVMException("Cannot determine Solaris architecture: " 112 + "(\"os.arch\" system property is null)"); 113 } 114 } 115 116 throw new UnsupportedVMException("Unknown or unsupported OS detected: " + osProp); 117 } 118 119 private static void validateComponent(final String component) { 120 if (component == null || component.indexOf('.') >= 0) { 121 final AssertionError ae = new AssertionError ("Invalid component string: " + component); 122 throw ae; 123 } 124 } 125 126 String getSignature() { 127 return signature; 128 } 129 130 public String toString() { 131 return getSignature(); 132 } 133 134 public boolean isCompatibleWith(final BootJarSignature compare) { 135 return signature.equals(compare.signature); 138 } 139 140 static BootJarSignature getSignatureForThisVM() throws UnsupportedVMException { 141 return new BootJarSignature(System.getProperties()); 142 } 143 144 public static String getBootJarNameForThisVM() throws UnsupportedVMException { 145 BootJarSignature signatureForThisVM = getSignatureForThisVM(); 146 return BootJar.JAR_NAME_PREFIX + signatureForThisVM + ".jar"; 147 } 148 149 public static void main(String args[]) { 150 155 try { 156 System.out.println(getBootJarNameForThisVM()); 157 } catch (Throwable t) { 158 System.err.println("ERROR: " + t.getMessage()); 159 System.exit(1); 160 } 161 System.exit(0); 162 } 163 164 } 165 | Popular Tags |