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.directory.shared.ldap.jndi;
021    
022    
023    import java.util.Hashtable;
024    
025    import javax.naming.Context;
026    import javax.naming.NamingException;
027    import javax.naming.spi.InitialContextFactory;
028    
029    
030    /**
031     * A context factory that delegates calls to the underlying JNDI 
032     * implementation of the JVM.
033     *
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$, $Date$
036     */
037    public class UniversalContextFactory implements InitialContextFactory
038    {
039        private static final String SUN_ICF_FQCN = "com.sun.jndi.ldap.LdapCtxFactory";
040        private static final String IBM_ICF_FQCN = "com.ibm.jndi.LDAPCtxFactory";
041        private static final String BEA_ICF_FQCN = SUN_ICF_FQCN;  // JRocket might use SUN classes
042        private static final String ICF_FQCN;
043        
044        
045        static
046        {
047            // -------------------------------------------------------------------
048            // for lack of a better approach we're just checking the JVM here and 
049            // setting the ICF_FQCN based on that using a bunch of conditional tests
050            // -------------------------------------------------------------------
051            
052            String jvmVendor = System.getProperty( "java.vm.vendor" );
053            
054            if ( jvmVendor.equalsIgnoreCase( "SUN Microsystems, Inc." ) )
055            {
056                ICF_FQCN = SUN_ICF_FQCN;
057            }
058            else if ( jvmVendor.equalsIgnoreCase( "BEA Systems, Inc." ) )
059            {
060                ICF_FQCN = BEA_ICF_FQCN;
061            }
062            else if ( jvmVendor.equalsIgnoreCase( "IBM, Inc." ) )
063            {
064                ICF_FQCN = IBM_ICF_FQCN;
065            }
066            else
067            {
068                ICF_FQCN = "Unknown";
069            }
070        }
071        
072        
073        private final InitialContextFactory factory;
074        
075        
076        public UniversalContextFactory() throws InstantiationException, IllegalAccessException, ClassNotFoundException
077        {
078            factory = ( InitialContextFactory ) Class.forName( ICF_FQCN ).newInstance();
079        }
080        
081        
082        public Context getInitialContext( Hashtable<?, ?> env ) throws NamingException
083        {
084            return factory.getInitialContext( env );
085        }
086    }