1 5 package com.tc.util.runtime; 6 7 import java.util.Properties ; 8 import java.util.regex.Matcher ; 9 import java.util.regex.Pattern ; 10 11 public class Vm { 12 13 public static final Pattern 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 (mve); 22 } 23 } 24 25 private Vm() { 26 } 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 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 { 66 private UnknownJvmVersionException(final String badVersion) { 67 super("Unable to parse JVM version '" + badVersion + "'"); 68 } 69 } 70 71 public final static class Version { 72 73 private final String vendorVersion; 74 private final int mega; 75 private final int major; 76 private final int minor; 77 private final String patch; 78 private final boolean isJRockit; 79 80 public Version(final Properties 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 vendorVersion, final boolean isJRockit) throws UnknownJvmVersionException { 87 this.vendorVersion = vendorVersion; 88 this.isJRockit = isJRockit; 89 final Matcher 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 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 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 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 toString() { 147 return vendorVersion; 148 } 149 } 150 151 } 152 | Popular Tags |