KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > runtime > Vm


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.util.runtime;
6
7 import java.util.Properties JavaDoc;
8 import java.util.regex.Matcher JavaDoc;
9 import java.util.regex.Pattern JavaDoc;
10
11 public class Vm {
12
13   public static final Pattern JavaDoc JVM_VERSION_PATTERN = Pattern
14                                                       .compile("^(\\p{Digit})\\.(\\p{Digit})\\.(\\p{Digit})(?:_(.+))?$");
15
16   public static final Version VERSION;
17   static {
18     try {
19       VERSION = new Version(System.getProperties());
20     } catch (UnknownJvmVersionException mve) {
21       throw new RuntimeException JavaDoc(mve);
22     }
23   }
24
25   private Vm() {
26   // Utility class
27
}
28
29   public static int getMegaVersion() {
30     return VERSION.getMegaVersion();
31   }
32
33   public static int getMajorVersion() {
34     return VERSION.getMajorVersion();
35   }
36
37   public static int getMinorVersion() {
38     return VERSION.getMinorVersion();
39   }
40
41   public static String JavaDoc getPatchLevel() {
42     return VERSION.getPatchLevel();
43   }
44
45   public static boolean isJDK14() {
46     return VERSION.isJDK14();
47   }
48
49   public static boolean isJDK15() {
50     return VERSION.isJDK15();
51   }
52
53   public static boolean isJDK16() {
54     return VERSION.isJDK16();
55   }
56   
57   public static boolean isJDK15Compliant() {
58     return VERSION.isJDK15() || VERSION.isJDK16();
59   }
60
61   public static boolean isJRockit() {
62     return VERSION.isJRockit();
63   }
64
65   public final static class UnknownJvmVersionException extends Exception JavaDoc {
66     private UnknownJvmVersionException(final String JavaDoc badVersion) {
67       super("Unable to parse JVM version '" + badVersion + "'");
68     }
69   }
70
71   public final static class Version {
72
73     private final String JavaDoc vendorVersion;
74     private final int mega;
75     private final int major;
76     private final int minor;
77     private final String JavaDoc patch;
78     private final boolean isJRockit;
79
80     public Version(final Properties JavaDoc properties) throws UnknownJvmVersionException {
81       this(properties.getProperty("java.version", "<error: java.version not specified in properties>"), properties
82           .getProperty("jrockit.version") != null
83           || properties.getProperty("java.vm.name", "").toLowerCase().indexOf("jrockit") >= 0);
84     }
85
86     public Version(final String JavaDoc vendorVersion, final boolean isJRockit) throws UnknownJvmVersionException {
87       this.vendorVersion = vendorVersion;
88       this.isJRockit = isJRockit;
89       final Matcher JavaDoc versionMatcher = JVM_VERSION_PATTERN.matcher(vendorVersion);
90       if (versionMatcher.matches()) {
91         mega = Integer.parseInt(versionMatcher.group(1));
92         major = Integer.parseInt(versionMatcher.group(2));
93         minor = Integer.parseInt(versionMatcher.group(3));
94         patch = versionMatcher.groupCount() == 4 ? versionMatcher.group(4) : null;
95       } else {
96         throw new UnknownJvmVersionException(vendorVersion);
97       }
98     }
99
100     /**
101      * Given the history of SunOS and Java version numbering by Sun, this will return '1' for a long time to come.
102      */

103     public int getMegaVersion() {
104       return mega;
105     }
106
107     public int getMajorVersion() {
108       return major;
109     }
110
111     public int getMinorVersion() {
112       return minor;
113     }
114
115     public String JavaDoc getPatchLevel() {
116       return patch;
117     }
118
119     public boolean isJDK14() {
120       return mega == 1 && major == 4;
121     }
122
123     public boolean isJDK15() {
124       return mega == 1 && major == 5;
125     }
126
127     public boolean isJDK16() {
128       return mega == 1 && major == 6;
129     }
130
131     public boolean isJRockit() {
132       return isJRockit;
133     }
134
135     public boolean equals(final Object JavaDoc o) {
136       if (!(o instanceof Version)) { return false; }
137
138       final Version other = (Version) o;
139       return vendorVersion.equals(other.vendorVersion);
140     }
141
142     public int hashCode() {
143       return vendorVersion.hashCode();
144     }
145
146     public String JavaDoc toString() {
147       return vendorVersion;
148     }
149   }
150
151 }
152
Popular Tags