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