Versions Compared

Key

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

Embedding ApacheDS as a Web Application

My initial aim was to demonstrate embedding ApacheDS in a very simple, but nevertheless impressive way. I thought about embedding the server in Apache Tomcat first. But then I got a better plan: Creating a standard web application which wraps ApacheDS and can be deployed on any compliant application server. ApacheDS in a war-archive!

...

Info
titleVersion check

Although the concepts depicted below apply to all version of ApacheDS (even before 1.0), the configuration for starting and stopping the embedded server uses the style introduced with ApacheDS 1.5.4. Be sure that you use this version of the server, or a later one.

Solution Outline

Note
titleProof of concept character

Although it works well, please note that this is just an example on how to embed ApacheDS in an application! If you plan to run the server as LDAP production system, this is not the first option to consider. Some more steps have to be done, especially in the area of configuration.

...

In the following we have choosen the second option.

A Servlet Context Listener to start and stop ApacheDS

A servlet context listener receives notifications about changes to the servlet context of the web application it is part of. Documentation of the ServletContextListener interface can be found here. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application. The two life cycle methods contextInitialized and contextDestroyed are suitable to start and stop ApacheDS.

A client within

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 display the Root DSE of the server.

The following class diagram visualizes the complete example. The gray elements will be developed in two steps and use Servlet and ApacheDS API.

Step 1: The web component which starts and stops the server

The ApacheDS core is comprised of JavaBeans components, and can easily be instantiated started and stopped with simple Java code. This is done by the following listener.

...

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

import java.io.File;

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.ldap.LdapService;
import org.apache.directory.server.protocol.shared.SocketAcceptor;

/**
 * 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) {

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

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

            // Set LDAP port to 10389
            ldapService.setIpPort(10389);

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

            directoryService.startup();
            ldapService.start();

            // Store directoryService in context to provide it to servlets etc.
            servletContext.setAttribute(DirectoryService.JNDI_KEY,
                    directoryService);

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

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

Deployment descriptor

In order to execute the listener code, the class has to be defined in the deployment descriptor of a web application, as depicted below:

Code Block
xml
xml
titleweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <display-name>ApacheDS embedded in a WebApp</display-name>
  <description>
    A simple yet portable way to run ApacheDS within a servlet
    container
  </description>

  <listener>
    <listener-class>
      org.apache.directory.samples.embed.webapp.StartStopListener
    </listener-class>
  </listener>
</web-app>

Packaging and Deploying the WebApp

A standard web archive (war-File) is needed in order to deploy the application to a servlet container. The Resources area at the end of this page provides a zip-File which contains the file structure. A build script for Apache Ant is included as well.

Directory layout for the sources, war file layout

The build script assumes that you have ApacheDS 1.5.4 and Tomcat 6.0.18 installed locally; it uses and (in the case of ApacheDS) copies the necessary files from their lib directories to the lib directory of the web application. You will likely want to adjust the installation directories defined in the build.xml file.

...

The webapp target in the build.xml file (which is the default target) packs the files for the web application together in a web archive called ApacheDS.war.

Deploying on Apache Tomcat

In order to run the application within Tomcat, simply put the ApacheDS.war file in the webapps directory of your Tomcat installation and start the server. If you have the manager application enabled (as described here), you can see and "manage" (start/stop) ApacheDS within its list view:

Connecting to ApacheDS from the outside

ApacheDS is up and running within the servlet container. Besides the administration tool listing, it seems to be invisible. But because we have configured network access via port 10389, you can easily access the server with an arbitrary LDAP client from outside.

...

After successfully connecting to the embedded ApacheDS, you can browse the tree, add and manipulate entries and so on. If you check the connection properties, you can study the Root DSE as well.

Other Web Application Servers

The web application described here has been successfully deployed on

...

Here is a screen shot of the web based administration console of WebSphere Application Server 6.1 with the ApacheDS.war deployed and running, no changes in the deployment archive were needed.

Step 2: Adding functionality: A servlet which displays the Root DSE

To finish with, here is a simple example on how to access the server internally.

...

Redeploy the web application. If you point to your tomcat server with the appropriate URL (http://localhost:8080/ApacheDS/RootDse), you'll see the content of the Root DSE as depicted below:

Generate Maven Project

To get the sample code you can either generate a maven project using the apacheds-webapp archetype or

Download the source code

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