001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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.properties;
018    
019    import java.security.Provider;
020    import java.security.Security;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    /**
026     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
027     */
028    public class JvmVendor {
029    
030        private static final Log log = LogFactory.getLog(JvmVendor.class);
031    
032        private static final String JVM_VENDOR_PROPERTY_NAME = "java.vm.vendor";
033    
034        private static final boolean sun;
035    
036        private static final boolean ibm;
037    
038        private static final boolean apache;
039        
040        private static final boolean ibmHybrid;
041    
042        private JvmVendor () {
043        }
044    
045        static {
046            String fullVendorName = getFullName();
047            boolean bApache = fullVendorName.substring(0, 6).equalsIgnoreCase("Apache");    // aka. Apache Harmony
048            boolean bIBM = fullVendorName.substring(0, 3).equalsIgnoreCase("IBM");           // aka. IBM, but not IBM Hybrid
049            boolean bSun = !bIBM && !bApache;                                               // default all others to Sun
050            boolean bHP = fullVendorName.substring(0, 6).equalsIgnoreCase("Hewlet");       // aka. Hewlett-Packard Company
051            boolean bIBMHybrid = false;
052            
053            // Special code for IBM Hybrid SDK (Sun JVM with IBM extensions on Solaris and HP-UX)
054            if ( ((bSun == true) && (System.getProperty("os.name").equalsIgnoreCase("SunOS") == true)) ||
055                 ((bHP == true) && (System.getProperty("os.name").equalsIgnoreCase("HP-UX") == true)) )
056            {
057                log.debug("Looking for the IBM Hybrid SDK Extensions");
058                // Check if provider IBMJSSE Provider is installed.
059                try {
060                    if (Security.getProvider("com.ibm.jsse2.IBMJSSEProvider2") == null) {
061                        // IBMJSSE Provider is not installed, install it
062                        log.debug("Trying to load IBM JSSE2 Provider.");
063                        Class c = Class.forName("com.ibm.jsse2.IBMJSSEProvider2");
064                        Provider p = (Provider) c.newInstance();
065                        Security.addProvider(p);
066                        // Security.addProvider(new com.ibm.jsse2.IBMJSSEProvider2());
067                        log.debug("Loaded the IBM JSSE2 Provider");
068                    } else {
069                        log.debug("Found the IBM JSSE2 Provider = " + Security.getProvider("com.ibm.jsse2.IBMJSSEProvider2"));
070                    }
071                    if (Security.getProvider("IBMCertPath") == null) {
072                        // If we found IBMJSSE but not this one, then the JAVA_OPTS are probably messed up
073                        log.debug("No IBMCertPath provider found.");
074                        throw new RuntimeException("Could not find the IBMCertPath provider.");
075                    } else {
076                        log.debug("Found the IBMCertPath Provider = " + Security.getProvider("IBMCertPath"));
077                    }
078                    if (Security.getProvider("IBMJCE") == null) {
079                        // If we found IBMJSSE but not this one, then the JAVA_OPTS are probably messed up
080                        log.debug("No IBMJCE provider found.");
081                        throw new RuntimeException("Could not find the IBMJCE provider.");
082                    } else {
083                        log.debug("Found the IBMJCE Provider" + Security.getProvider("IBMJCE"));
084                    }
085                    System.setProperty("java.protocol.handler.pkgs", "com.ibm.net.ssl.www2.protocol");
086                    // All of the expected IBM Extensions were found, so we must be using the IBM Hybrid JDK
087                    bSun = false;
088                    bApache = false;
089                    bIBM = true;
090                    bIBMHybrid = true;
091                } catch (ClassNotFoundException e) {
092                    // Couldn't load the IBMJSSE Provider, so we must not be using the IBM Hybrid SDK
093                    log.debug("Caught Exception="+e.toString());
094                    log.debug("Could not load the IBM JSSE Provider.  Must be using the OS provider's Java.");
095                } catch (Throwable t) {
096                    // Couldn't load the IBMJSSE Provider, so we must not be using the IBM Hybrid SDK
097                    log.debug("Caught Throwable="+t.toString());
098                    log.debug("Assume we could not load the IBM JSSE Provider and that we are using the OS provider's Java.");
099                }
100            }
101            // now, set our statics
102            apache = bApache;
103            ibm = bIBM;
104            ibmHybrid = bIBMHybrid;
105            sun = bSun;
106            // log what we found
107            log.info(getJvmInfo());
108        }
109    
110        public static String getFullName() {
111            return System.getProperty(JVM_VENDOR_PROPERTY_NAME);
112        }
113    
114        public static boolean isSun() {
115            return sun;
116        }
117    
118        public static boolean isIBM() {
119            return ibm;
120        }
121    
122        public static boolean isIBMHybrid() {
123            return ibmHybrid;
124        }
125    
126        public static boolean isApache() {
127            return apache;
128        }
129    
130        public static String getJvmInfo() {
131            if (sun == true) {
132                return new String("Sun JVM " + System.getProperty("java.version"));
133            } else if (apache == true) {
134                return new String("Apache Harmony JVM " + System.getProperty("java.version"));
135            } else if (ibm == true) {
136                return new String("IBM JVM " + System.getProperty("java.version"));
137            } else if (ibmHybrid == true) {
138                return new String("IBM Hybrid JVM " + System.getProperty("java.version") + " on " + System.getProperty("os.name"));
139            } else {
140                // should never happen
141                return new String("Unknown JVM detected - " + getFullName());
142            }
143        }
144    
145    }