KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > info > JVMInfo


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.info.JVMInfo
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.info;
23
24
25 /**
26     What's the current JDK runtime environment.
27  */

28 public abstract class JVMInfo
29 {
30     /**
31         The JVM's runtime environment.
32         <UL>
33         <LI> 1 - not used was JDK 1.1
34         <LI> 2 - J2SE_13- JDK 1.2, 1.3
35         <LI> 4 - J2SE_14 - JDK 1.4.0 or 1.4.1
36         <LI> 5 - J2SE_142 - JDK 1.4.2
37         <LI> 6 - J2SE_15 - JDK 1.5
38         </UL>
39     */

40     public static final int JDK_ID;
41
42     public static final int J2SE_13 = 2;
43     public static final int J2SE_14 = 4;
44     public static final int J2SE_142 = 5;
45     public static final int J2SE_15 = 6; // aka J2SE 5.0
46
public static final int J2SE_16 = 7; // Java SE 6, not J2SE
47

48     public static final boolean J2ME;
49
50     /**
51     JDBC Boolean type - Types.BIT in JDK1.1 & 1.2 & 1.3, Types.BOOLEAN in JDK1.4
52     */

53     public static final int JAVA_SQL_TYPES_BOOLEAN;
54
55     static
56     {
57         int id;
58
59         //
60
// If the property java.specification.version is set, then try to parse
61
// that. Anything we don't recognize, default to Java 2 platform
62
// because java.specification.version is a property that is introduced
63
// in Java 2. We hope that JVM vendors don't implement Java 1 and
64
// set a Java 2 system property.
65
//
66
// Otherwise, see if we recognize what is set in java.version.
67
// If we don't recoginze that, or if the property is not set, assume
68
// version 1.3.
69
//
70
String JavaDoc javaVersion;
71         String JavaDoc javaSpec;
72         boolean isJ2ME;
73
74         try {
75             javaSpec = System.getProperty("java.specification.name");
76         } catch (SecurityException JavaDoc se) {
77             // some vms do not know about this property so they
78
// throw a security exception when access is restricted.
79
javaSpec = null;
80         }
81
82         try {
83             javaVersion = System.getProperty("java.specification.version", "1.3");
84
85         } catch (SecurityException JavaDoc se) {
86             // some vms do not know about this property so they
87
// throw a security exception when access is restricted.
88
javaVersion = "1.3";
89         }
90
91         if (javaSpec != null && javaSpec.startsWith("J2ME"))
92         {
93             // IBM's WCTME 5.7 returns these values for CDC 1.0 profiles.
94
// "J2ME Foundation Specification"
95
//
96

97             // Foundation 1.0 and Personal Profile 1.0 based
98
// upon CDC 1.0 which is JDK 1.3 based
99
id = J2SE_13;
100             isJ2ME = true;
101         }
102         else
103         {
104             // J2SE/J2EE
105
isJ2ME = false;
106
107             if (javaVersion.equals("1.2") || javaVersion.equals("1.3"))
108             {
109                 id = J2SE_13; //jdk1.3 is still Java2 platform with the same API
110
}
111             else if (javaVersion.equals("1.4"))
112             {
113                 String JavaDoc vmVersion = System.getProperty("java.version", "1.4.0");
114
115                 if (JVMInfo.vmCheck(vmVersion, "1.4.0") || JVMInfo.vmCheck(vmVersion, "1.4.1"))
116                     id = J2SE_14;
117                 else
118                     id = J2SE_142;
119             }
120             else if (javaVersion.equals("1.5"))
121             {
122                 id = J2SE_15;
123             }
124             else if (javaVersion.equals("1.6"))
125             {
126                 id = J2SE_16;
127             }
128             else
129             {
130                 // aussme our lowest support unless the java spec
131
// is greater than our highest level.
132
id = J2SE_13;
133
134                 try {
135
136                     if (Float.valueOf(javaVersion).floatValue() > 1.4f)
137                         id = 5;
138                 } catch (NumberFormatException JavaDoc nfe) {
139                 }
140             }
141         }
142
143         JDK_ID = id;
144         J2ME = isJ2ME;
145         JAVA_SQL_TYPES_BOOLEAN = (isJ2ME || id >= J2SE_14) ?
146             org.apache.derby.iapi.reference.JDBC30Translation.SQL_TYPES_BOOLEAN :java.sql.Types.BIT;
147     }
148
149     /**
150         Check the vmVersion against a speciifc value.
151         Sun jvms are of the form
152     */

153     private static boolean vmCheck(String JavaDoc vmVersion, String JavaDoc id)
154     {
155         return vmVersion.equals(id) || vmVersion.startsWith(id + "_");
156     }
157
158     /**
159         Return Derby's understanding of the virtual machine's environment.
160     */

161     public static String JavaDoc derbyVMLevel()
162     {
163         switch (JDK_ID)
164         {
165         case J2SE_13: return J2ME ? "J2ME - JDBC for CDC/FP 1.0" : "J2SE 1.3 - JDBC 2.1";
166         case J2SE_14: return "J2SE 1.4 - JDBC 3.0";
167         case J2SE_142: return "J2SE 1.4.2 - JDBC 3.0";
168         case J2SE_15: return "J2SE 5.0 - JDBC 3.0";
169         case J2SE_16: return "Java SE 6 - JDBC 4.0";
170         default: return "?-?";
171         }
172     }
173 }
174
Popular Tags