Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

After the server has been started from the Listener, it will be accessible from the outside via the network using LDAP. In order to demonstrate how to interact with the server from within the VM, a simple servlet is shown. It allows you to communicate with the embedded server via web browser. This is so simple, because the server already lives within a web application, only a servlet has to added to act as an entry point. Our sample servlet will diplay display the Root DSE of the server.

...

  • contextInitialized() is executed if the web application is started by the servlet container, it starts ApacheDS embedded
  • contextDestroyed() is executed if the web application is stopped by the servlet container, it stops the embedded server

First of all contextInitialized The contextInitialized method creates a DefaultDirectoryService object. It configures the LDAP protocol and determines an appropriate working directory for the server. This directory is need to persist the partition data (entries). Our example uses a simple yet portable way for this task: the context attribute javax.servlet.context.tempdir.

Afterwards the method creates a configuration object which is suitable to start the server (class MutableServerStartupConfiguration). LDAP networking is enabled on port 10389, and the working directory is set. The configuration is combined with the environment from the helper class above. Invoking the constructor of InitialDirContext with these settings causes the ApacheDS core to start.starts network protocol and directory service.

Finally the DirectoryService component is stored in the application context of the web application. This is done in order to provided it to embedded clients in the same web app (see the servlet below for an example).

The method contextDestroyed simply stops the protocol and shuts down the serviceThe method contextDestroyed is comparable. It uses a configuration suitable to shut down the server.

Code Block
java
java
titleStartStopListener.java
package org.apache.directory.samples.embed.webapp;

import java.io.File;

import javajavax.utilservlet.HashtableServletContext;

import javax.namingservlet.NamingExceptionServletContextEvent;
import javax.namingservlet.directory.InitialDirContextServletContextListener;

import javaxorg.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.configurationcore.MutableServerStartupConfigurationDirectoryService;
import org.apache.directory.server.coreldap.configuration.ShutdownConfigurationLdapService;
import org.apache.directory.server.ldapprotocol.shared.LdapConfigurationSocketAcceptor;

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

    /**
private     DirectoryService directoryService;

    private SocketAcceptor socketAcceptor;
    private LdapService ldapService;

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

 contextInitialized(ServletContextEvent evt) {

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

            socketAcceptor = new SocketAcceptor(null);
            ldapService = new LdapService();
            ldapService.setSocketAcceptor(socketAcceptor);
           try { ldapService.setDirectoryService(directoryService);

            // CreateSet LDAP aport defaultto configuration10389
            MutableServerStartupConfiguration cfg = new MutableServerStartupConfiguration(ldapService.setIpPort(10389);

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

            // Set LDAP port to 10389
            LdapConfiguration ldapCfg = cfg.getLdapConfigurationdirectoryService.startup();
            ldapCfgldapService.setIpPortstart(10389);

            // StartStore thedirectoryService Server
in context to provide it to servlets  etc.
    Hashtable env = EnvHelper.createEnv();
     servletContext.setAttribute(DirectoryService.JNDI_KEY,
       env.putAll(cfg.toJndiEnvironment());
            new InitialDirContext(envdirectoryService);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Shutdown ApacheDS embedded.
     */
    public void contextDestroyed(ServletContextEvent evt) {
        try {
            Hashtable env = EnvHelper.createEnvldapService.stop();
            ShutdownConfiguration cfg = new ShutdownConfiguration();
            env.putAll(cfg.toJndiEnvironment());
            new InitialDirContext(envdirectoryService.shutdown();
        } catch (NamingExceptionException e) {
            throw new RuntimeException(e);
        }
    }
}

...

EnvHelper.java
StartStopListener.java

RootDseServlet.java
web.xml
ApacheDSWebApp.zip all sources including a build script for Apache Ant (build.xml)