Nice!  Now I have it working.   Code that finally worked below.  Any
documentation with examples on setting up StartTLS/SSL and Replication?

/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package com.txdot.cris.directory;


import java.io.File;
import java.util.HashSet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.DirectoryService;
import org.apache.directory.server.core.entry.ServerEntry;
import org.apache.directory.server.core.partition.Partition;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex;
import
org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
import org.apache.directory.server.ldap.LdapServer;
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.apache.directory.server.xdbm.Index;
import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
import org.apache.directory.shared.ldap.name.LdapDN;





/**
 * A Servlet context listener to start and stop ApacheDS.
 *
 * @author <a href="mailto:d...@directory.apache.org";>Apache Directory
 *         Project</a>
 */
public class StartStopListener implements ServletContextListener
{

    private DirectoryService directoryService;

    private LdapServer ldapServer;


    /**
     * Startup ApacheDS embedded.
     */
    public void contextInitialized( ServletContextEvent evt )
    {

        try
        {
            directoryService = new DefaultDirectoryService();
            directoryService.setShutdownHookEnabled( true );

         // Disable the ChangeLog system
            directoryService.getChangeLog().setEnabled( false );
            directoryService.setDenormalizeOpAttrsEnabled( true );

            ldapServer = new LdapServer();
            ldapServer.setDirectoryService( directoryService );
            ldapServer.setAllowAnonymousAccess( true );

            // Set LDAP port to 10389
            TcpTransport ldapTransport = new TcpTransport( 10389 );
            ldapServer.setTransports( ldapTransport );

            // Determine an appropriate working directory
            ServletContext servletContext = evt.getServletContext();
            File workingDir = ( File ) servletContext.getAttribute(
"javax.servlet.context.tempdir" );
            directoryService.setWorkingDirectory( workingDir );

            // Create some new partitions named 'foo', 'bar' and 'CRIS'.
            // Partition fooPartition = addPartition( "foo", "dc=foo,dc=com"
);
            // Partition barPartition = addPartition( "bar", "dc=bar,dc=com"
);
            Partition crisPartition = addPartition( "CRIS", "ou=CRIS" );

            // Index some attributes on the CRIS partition
            addIndex( crisPartition, "objectClass", "ou", "uid" );

            // And start the service
            directoryService.startup();

            // Inject the CRIS root entry
            try
            {
             directoryService.getAdminSession().lookup(
crisPartition.getSuffixDn() );
            }
            catch ( LdapNameNotFoundException lnnfe )
            {
                LdapDN dnCRIS = new LdapDN( "ou=CRIS" );
                ServerEntry entryCRIS = directoryService.newEntry( dnCRIS );
                entryCRIS.add( "objectClass", "top", "organizationalUnit" );
                entryCRIS.add( "ou", "CRIS" );
                directoryService.getAdminSession().add( entryCRIS );
            }

            ldapServer.start();

            // Store directoryService in context to provide it to servlets
etc.
            servletContext.setAttribute( DirectoryService.JNDI_KEY,
directoryService );
        }
        catch ( Exception e )
        {
            throw new RuntimeException( e );
        }
    }

    /**
     * Add a new partition to the server
     *
     * @param partitionId The partition Id
     * @param partitionDn The partition DN
     * @return The newly added partition
     * @throws Exception If the partition can't be added
     */
    private Partition addPartition( String partitionId, String partitionDn )
throws Exception
    {
        // Create a new partition named 'foo'.
        Partition partition = new JdbmPartition();
        partition.setId( partitionId );
        partition.setSuffix( partitionDn );
        directoryService.addPartition( partition );

        return partition;
    }


    /**
     * Add a new set of index on the given attributes
     *
     * @param partition The partition on which we want to add index
     * @param attrs The list of attributes to index
     */
    private void addIndex( Partition partition, String... attrs )
    {
        // Index some attributes on the apache partition
        HashSet<Index<?, ServerEntry>> indexedAttributes = new
HashSet<Index<?, ServerEntry>>();

        for ( String attribute:attrs )
        {
            indexedAttributes.add( new JdbmIndex<String,ServerEntry>(
attribute ) );
        }

        ((JdbmPartition)partition).setIndexedAttributes( indexedAttributes
);
    }

    /**
     * Shutdown ApacheDS embedded.
     */
    public void contextDestroyed( ServletContextEvent evt )
    {
        try
        {
            ldapServer.stop();
            directoryService.shutdown();
        }
        catch ( Exception e )
        {
            throw new RuntimeException( e );
        }
    }
}

--

Thanks,

Dan McLaughlin


NOTICE: This e-mail message and all attachments transmitted with it are for
the sole use of the intended recipient(s) and may contain confidential and
privileged information. Any unauthorized review, use, disclosure or
distribution is strictly prohibited. The contents of this e-mail are
confidential and may be subject to work product privileges. If you are not
the intended recipient, please contact the sender by reply e-mail and
destroy all copies of the original message.

Need to schedule a meeting??? http://www.tungle.me/DanMcLaughlin


On Wed, Mar 17, 2010 at 9:55 AM, Stefan Zoerner <ste...@labeo.de> wrote:

> Dan McLaughlin wrote:
>
>> I followed this documentation to create the war
>>
>> http://cwiki.apache.org/DIRxSRVx11/43-embedding-apacheds-as-a-web-application.html
>>
>> Then I followed this documentation to create a partition ou=test
>>
>> http://cwiki.apache.org/DIRxSRVx11/61-how-to-write-a-simple-custom-partition-for-apacheds.html
>>
>> I can see ou=test, but it's read only. Looking at the code in the
>> documentation it's obvious why.
>>
>
> Ah, now I see. Yes it is obvious why, but it was not necessary to implement
> a custom partition. Simply use the default implementation, which is
> writable.
>
> A source code on how to accomplish this is here:
>
>
> http://cwiki.apache.org/DIRxSRVx11/41-embedding-apacheds-into-an-application.html
>
> the method addPartition uses a JdbmPartition as implementation.
>
> hope this helps.
>
> For your question regarding reading the server.xml in order to setup the
> server: I don't think there is a sample in the docs. But at least it is
> possible to check the source code of the server itself to get inspired.
>
>
> http://svn.apache.org/repos/asf/directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/Service.java
>
> Hope this helps as well.
>
> Greetings from Hamburg,
>    StefanZ
>
>
>

Reply via email to