KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tools > BootJarSignature


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
11
12 public class BootJarSignature {
13
14   public static final char SIGNATURE_SEPARATOR = '_';
15
16   private static final String JavaDoc OS_WINDOWS = "win32";
17   private static final String JavaDoc OS_LINUX = "linux";
18   private static final String JavaDoc OS_SOLARIS_SPARC = "solaris";
19   private static final String JavaDoc OS_MAC_OSX = "osx";
20   private static final String JavaDoc OS_SOLARIS_X86 = "solaris-x86";
21
22   private static final String JavaDoc VM_VENDOR_SUN = "hotspot";
23   private static final String JavaDoc VM_VENDOR_BEA = "jrockit";
24
25   private final String JavaDoc signature;
26
27   BootJarSignature(final Properties JavaDoc source) throws UnsupportedVMException {
28     signature = generateBootJarSignature(source);
29   }
30
31   BootJarSignature(final String JavaDoc signature) {
32     this.signature = signature;
33   }
34
35   private String JavaDoc generateBootJarSignature(final Properties JavaDoc source) throws UnsupportedVMException {
36     String JavaDoc os = getOS(source);
37     String JavaDoc version = getVMVersion(source);
38     String JavaDoc 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 JavaDoc getVendor(final Properties JavaDoc source) throws UnsupportedVMException {
48     String JavaDoc 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         // In at least one case, jrockit 1.4.2_05 on linux, you get "Sun Microsystems Inc." as the vendor...err
69
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 JavaDoc getVMVersion(final Properties JavaDoc 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 JavaDoc getOS(final Properties JavaDoc source) throws UnsupportedVMException {
88     final String JavaDoc 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 JavaDoc 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 JavaDoc arch = source.getProperty("os.arch");
101       if (arch != null) {
102         final String JavaDoc 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 JavaDoc component) {
120     if (component == null || component.indexOf('.') >= 0) {
121       final AssertionError JavaDoc ae = new AssertionError JavaDoc("Invalid component string: " + component);
122       throw ae;
123     }
124   }
125
126   String JavaDoc getSignature() {
127     return signature;
128   }
129
130   public String JavaDoc toString() {
131     return getSignature();
132   }
133
134   public boolean isCompatibleWith(final BootJarSignature compare) {
135     // This can be a place to hide ugly compatibility stuff as needed
136
// For now just do a regular string equality check on the signature
137
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 JavaDoc getBootJarNameForThisVM() throws UnsupportedVMException {
145     BootJarSignature signatureForThisVM = getSignatureForThisVM();
146     return BootJar.JAR_NAME_PREFIX + signatureForThisVM + ".jar";
147   }
148
149   public static void main(String JavaDoc args[]) {
150     // README: This main() method is called from the dso-java[.bat] script. It isn't for simple test purposes or
151
// anything. Specificallly, there is a contract here.....running main() should output the expected name of the dso
152
// boot jar for the VM running this method. If you think you want to change what this method does, you better have a
153
// look at the calling scripts ;-)
154

155     try {
156       System.out.println(getBootJarNameForThisVM());
157     } catch (Throwable JavaDoc t) {
158       System.err.println("ERROR: " + t.getMessage());
159       System.exit(1);
160     }
161     System.exit(0);
162   }
163
164 }
165
Popular Tags