View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.system.configuration.condition;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * Provides access to Java version details for use in condition expressions.
27   *
28   * @version $Rev: 454037 $ $Date: 2006-10-07 15:26:46 -0700 (Sat, 07 Oct 2006) $
29   */
30  public class JavaVariable
31  {
32      private static final Log log = LogFactory.getLog(JavaVariable.class);
33      
34      public String getVendor() {
35          return SystemUtils.JAVA_VENDOR;
36      }
37  
38      public String getVresion() {
39          return SystemUtils.JAVA_VERSION;
40      }
41  
42      public String getVmVendor() {
43          return SystemUtils.JAVA_VM_VENDOR;
44      }
45      
46      public String getVmVersion() {
47          return SystemUtils.JAVA_VM_VERSION;
48      }
49  
50      public boolean getIs1_1() {
51          return SystemUtils.IS_JAVA_1_1;
52      }
53  
54      public boolean getIs1_2() {
55          return SystemUtils.IS_JAVA_1_2;
56      }
57  
58      public boolean getIs1_3() {
59          return SystemUtils.IS_JAVA_1_3;
60      }
61  
62      public boolean getIs1_4() {
63          return SystemUtils.IS_JAVA_1_4;
64      }
65  
66      public boolean getIs1_5() {
67          return SystemUtils.IS_JAVA_1_5;
68      }
69  
70      public boolean getIs1_6() {
71          return SystemUtils.IS_JAVA_1_6;
72      }
73  
74      public boolean getIsVersionAtLeast(final float requiredVersion) {
75          return SystemUtils.isJavaVersionAtLeast(requiredVersion);
76      }
77  
78      public boolean getIsVersionAtLeast(final int requiredVersion) {
79          return SystemUtils.isJavaVersionAtLeast(requiredVersion);
80      }
81      
82      public boolean getVersionMatches(String version) {
83          version = version.trim();
84          
85          boolean debug = log.isDebugEnabled();
86          boolean result = false;
87          
88          if (version.endsWith("*")) {
89              version = version.substring(0, version.length() - 1).trim();
90              
91              if (debug) {
92                  log.debug("Checking Java version is in the same group as: " + version);
93              }
94              
95              String tmp = SystemUtils.JAVA_VERSION_TRIMMED;
96              
97              if (debug) {
98                  log.debug("Requested version: " + tmp);
99                  log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
100             }
101             
102             result = tmp.startsWith(version);
103         }
104         else if (version.endsWith("+")) {
105             version = version.substring(0, version.length() - 1).trim();
106             
107             if (debug) {
108                 log.debug("Checking Java version is greater than: " + version);
109             }
110             
111             float tmp = Float.parseFloat(version);
112             
113             if (debug) {
114                 log.debug("Requested version: " + tmp);
115                 log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
116             }
117             
118             result = tmp <= SystemUtils.JAVA_VERSION_FLOAT;
119         }
120         else {
121             if (debug) {
122                 log.debug("Checking Java version is equal to: " + version);
123             }
124             
125             float tmp = Float.parseFloat(version);
126             
127             if (debug) {
128                 log.debug("Requested version: " + tmp);
129                 log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
130             }
131             
132             result = tmp == SystemUtils.JAVA_VERSION_FLOAT;
133         }
134         
135         return result;
136     }
137 }