001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.configuration.condition;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    /**
023     * Provides access to Java version details for use in condition expressions.
024     *
025     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
026     */
027    public class JavaVariable
028    {
029        private static final Log log = LogFactory.getLog(JavaVariable.class);
030        
031        public String getVendor() {
032            return SystemUtils.JAVA_VENDOR;
033        }
034    
035        public String getVresion() {
036            return SystemUtils.JAVA_VERSION;
037        }
038    
039        public String getVmVendor() {
040            return SystemUtils.JAVA_VM_VENDOR;
041        }
042        
043        public String getVmVersion() {
044            return SystemUtils.JAVA_VM_VERSION;
045        }
046    
047        public boolean getIs1_1() {
048            return SystemUtils.IS_JAVA_1_1;
049        }
050    
051        public boolean getIs1_2() {
052            return SystemUtils.IS_JAVA_1_2;
053        }
054    
055        public boolean getIs1_3() {
056            return SystemUtils.IS_JAVA_1_3;
057        }
058    
059        public boolean getIs1_4() {
060            return SystemUtils.IS_JAVA_1_4;
061        }
062    
063        public boolean getIs1_5() {
064            return SystemUtils.IS_JAVA_1_5;
065        }
066    
067        public boolean getIs1_6() {
068            return SystemUtils.IS_JAVA_1_6;
069        }
070    
071        public boolean getIsVersionAtLeast(final float requiredVersion) {
072            return SystemUtils.isJavaVersionAtLeast(requiredVersion);
073        }
074    
075        public boolean getIsVersionAtLeast(final int requiredVersion) {
076            return SystemUtils.isJavaVersionAtLeast(requiredVersion);
077        }
078        
079        public boolean getVersionMatches(String version) {
080            version = version.trim();
081            
082            boolean debug = log.isDebugEnabled();
083            boolean result = false;
084            
085            if (version.endsWith("*")) {
086                version = version.substring(0, version.length() - 1).trim();
087                
088                if (debug) {
089                    log.debug("Checking Java version is in the same group as: " + version);
090                }
091                
092                String tmp = SystemUtils.JAVA_VERSION_TRIMMED;
093                
094                if (debug) {
095                    log.debug("Requested version: " + tmp);
096                    log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
097                }
098                
099                result = tmp.startsWith(version);
100            }
101            else if (version.endsWith("+")) {
102                version = version.substring(0, version.length() - 1).trim();
103                
104                if (debug) {
105                    log.debug("Checking Java version is greater than: " + version);
106                }
107                
108                float tmp = Float.parseFloat(version);
109                
110                if (debug) {
111                    log.debug("Requested version: " + tmp);
112                    log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
113                }
114                
115                result = tmp <= SystemUtils.JAVA_VERSION_FLOAT;
116            }
117            else {
118                if (debug) {
119                    log.debug("Checking Java version is equal to: " + version);
120                }
121                
122                float tmp = Float.parseFloat(version);
123                
124                if (debug) {
125                    log.debug("Requested version: " + tmp);
126                    log.debug("JVM version: " + SystemUtils.JAVA_VERSION_FLOAT);
127                }
128                
129                result = tmp == SystemUtils.JAVA_VERSION_FLOAT;
130            }
131            
132            return result;
133        }
134    }