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    package org.apache.directory.server.integ;
020    
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.apache.directory.server.core.DefaultDirectoryService;
026    import org.apache.directory.server.core.DirectoryService;
027    import org.apache.directory.server.core.integ.IntegrationUtils;
028    import org.apache.directory.server.ldap.LdapServer;
029    import org.apache.directory.server.ldap.handlers.bind.MechanismHandler;
030    import org.apache.directory.server.ldap.handlers.bind.SimpleMechanismHandler;
031    import org.apache.directory.server.ldap.handlers.bind.cramMD5.CramMd5MechanismHandler;
032    import org.apache.directory.server.ldap.handlers.bind.digestMD5.DigestMd5MechanismHandler;
033    import org.apache.directory.server.ldap.handlers.bind.gssapi.GssapiMechanismHandler;
034    import org.apache.directory.server.ldap.handlers.bind.ntlm.NtlmMechanismHandler;
035    import org.apache.directory.server.ldap.handlers.extended.StartTlsHandler;
036    import org.apache.directory.server.ldap.handlers.extended.StoredProcedureExtendedOperationHandler;
037    import org.apache.directory.server.protocol.shared.transport.TcpTransport;
038    import org.apache.directory.shared.ldap.constants.SupportedSaslMechanisms;
039    import org.apache.mina.util.AvailablePortFinder;
040    
041    
042    /**
043     * A factory used to generate differently configured DirectoryService objects.
044     * Since the DirectoryService itself is what is configured then a factory for
045     * these objects acts as a configurator.  Tests can provide different factory
046     * methods to be used.
047     *
048     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
049     * @version $Rev$, $Date$
050     */
051    public interface LdapServerFactory
052    {
053        /**
054         * The default factory returns stock instances of a directory
055         * service with smart defaults
056         */
057        LdapServerFactory DEFAULT = new LdapServerFactory()
058        {
059            public LdapServer newInstance() throws Exception
060            {
061                DirectoryService service = new DefaultDirectoryService();
062                IntegrationUtils.doDelete( service.getWorkingDirectory() );
063                service.getChangeLog().setEnabled( true );
064                service.setShutdownHookEnabled( false );
065    
066                // change the working directory to something that is unique
067                // on the system and somewhere either under target directory
068                // or somewhere in a temp area of the machine.
069    
070                LdapServer ldapServer = new LdapServer();
071                ldapServer.setDirectoryService( service );
072                int port = AvailablePortFinder.getNextAvailable( 1024 );
073                ldapServer.setTransports( new TcpTransport( port, 3 ) );
074                ldapServer.addExtendedOperationHandler( new StartTlsHandler() );
075                ldapServer.addExtendedOperationHandler( new StoredProcedureExtendedOperationHandler() );
076    
077                // Setup SASL Mechanisms
078                
079                Map<String, MechanismHandler> mechanismHandlerMap = new HashMap<String,MechanismHandler>();
080                mechanismHandlerMap.put( SupportedSaslMechanisms.PLAIN, new SimpleMechanismHandler() );
081    
082                CramMd5MechanismHandler cramMd5MechanismHandler = new CramMd5MechanismHandler();
083                mechanismHandlerMap.put( SupportedSaslMechanisms.CRAM_MD5, cramMd5MechanismHandler );
084    
085                DigestMd5MechanismHandler digestMd5MechanismHandler = new DigestMd5MechanismHandler();
086                mechanismHandlerMap.put( SupportedSaslMechanisms.DIGEST_MD5, digestMd5MechanismHandler );
087    
088                GssapiMechanismHandler gssapiMechanismHandler = new GssapiMechanismHandler();
089                mechanismHandlerMap.put( SupportedSaslMechanisms.GSSAPI, gssapiMechanismHandler );
090    
091                NtlmMechanismHandler ntlmMechanismHandler = new NtlmMechanismHandler();
092                mechanismHandlerMap.put( SupportedSaslMechanisms.NTLM, ntlmMechanismHandler );
093                mechanismHandlerMap.put( SupportedSaslMechanisms.GSS_SPNEGO, ntlmMechanismHandler );
094    
095                ldapServer.setSaslMechanismHandlers( mechanismHandlerMap );
096    
097                return ldapServer;
098            }
099        };
100    
101        
102        LdapServer newInstance() throws Exception;
103    }