001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.system.configuration.condition;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    /**
026     * Provides access to Java version details for use in condition expressions.
027     *
028     * @version $Rev: 454037 $ $Date: 2006-10-07 15:26:46 -0700 (Sat, 07 Oct 2006) $
029     */
030    public class JavaVariable
031    {
032        private static final Log log = LogFactory.getLog(JavaVariable.class);
033        
034        public String getVendor() {
035            return SystemUtils.JAVA_VENDOR;
036        }
037    
038        public String getVresion() {
039            return SystemUtils.JAVA_VERSION;
040        }
041    
042        public String getVmVendor() {
043            return SystemUtils.JAVA_VM_VENDOR;
044        }
045        
046        public String getVmVersion() {
047            return SystemUtils.JAVA_VM_VERSION;
048        }
049    
050        public boolean getIs1_1() {
051            return SystemUtils.IS_JAVA_1_1;
052        }
053    
054        public boolean getIs1_2() {
055            return SystemUtils.IS_JAVA_1_2;
056        }
057    
058        public boolean getIs1_3() {
059            return SystemUtils.IS_JAVA_1_3;
060        }
061    
062        public boolean getIs1_4() {
063            return SystemUtils.IS_JAVA_1_4;
064        }
065    
066        public boolean getIs1_5() {
067            return SystemUtils.IS_JAVA_1_5;
068        }
069    
070        public boolean getIs1_6() {
071            return SystemUtils.IS_JAVA_1_6;
072        }
073    
074        public boolean getIsVersionAtLeast(final float requiredVersion) {
075            return SystemUtils.isJavaVersionAtLeast(requiredVersion);
076        }
077    
078        public boolean getIsVersionAtLeast(final int requiredVersion) {
079            return SystemUtils.isJavaVersionAtLeast(requiredVersion);
080        }
081        
082        public boolean getVersionMatches(String version) {
083            version = version.trim();
084            
085            boolean debug = log.isDebugEnabled();
086            boolean result = false;
087            
088            if (version.endsWith("*")) {
089                version = version.substring(0, version.length() - 1).trim();
090                
091                if (debug) {
092                    log.debug("Checking Java version is in the same group as: " + version);
093                }
094                
095                String tmp = SystemUtils.JAVA_VERSION_TRIMMED;
096                
097                if (debug) {
098                    log.debug("Requested version: " + tmp);
099                    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    }