Embedding ApacheDS as a Web Application
This site was updated for ApacheDS 1.5.5.
Maven archetype depends on the current trunk.
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!
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.5. Be sure that you use this version of the server, or a later one.
Solution Outline
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.
The solution is quite simple. A web application carries all the necessary jar files for ApacheDS within the lib-directory of the WEB-INF folder. When the web application is started by the servlet container, appropriate code has to be executed to start ApacheDS. And the server has to be stopped, if the web application goes down (for instance if the server shuts down). There are (at least) two standard compliant ways to acomplish this:
- A Servlet (automatically started with the web application, using the lifecycle methods init and destroy)
- A ServletContextListener
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.
The class StartStopListener implements ServletContextListener and therefore contains the following two life cycle methods:
- 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
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 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 service.
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:
Packaging and Deploying the WebApp
A standard web archive (war-File) is needed in order to deploy the application to a servlet container. The easiest way to create such a web archive including all dependencies is to use an Maven archetype we provide.
Creating the WebApp using the ApacheDS Maven Archetype
We assume you have Java Subversion and Maven 2.0.9 installed on your system.
To use the archetype you'll need to check it out and install it to your local repository:
Then change to your preferred location to create the new project and execute following command:
Then change to the created directory and run the following command:
This creates an ApacheDS.war file below the target folder.
Run on embedded Jetty
The fastest way to run the web application is to use the Maven Jetty plugin:
The sample servlet 'RootDseServlet' is available at the following URL:
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. The build script assumes that you have ApacheDS 1.5.5 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. Note: Within the build script, Tomcat is only used for compilation. To be more concrete, only the servlet-api.jar is needed. Other options to provide this library at build time are imaginable, especially if you plan to deploy ApacheDS on a Web Application Server other than Tomcat.
After building the project, the classes folder will contain the compiled class files of the two Java classes above, and a properties file to configure the logging framework log4j. The lib folder will contain all jar-Files necessary, these are 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.Directory layout for the sources, war file layout
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.
One option is a command line tool like ldapsearch (see ApacheDS Basic User's Guide for details on how to connect to ApacheDS with such tools in general). Here is an example how to connect as administrator (simple bind) and fetch the Root DSE of our embedded ApacheDS instance:
Another choice are graphical LDAP clients (see ApacheDS Basic User's Guide for details on how to connect to ApacheDS with such tools in general).
With our popular Eclipse RCP application Directory studio for instance, connecting goes like this:
In the Connections view, select "New connection ...". Within a wizard dialog, you provide the connection data (host name, port, bind DN and password).
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
- Apache Tomcat 5.5.20 and 6.0.18 (Homepage)
- IBM WebSphere Application Server 6.1 (Homepage)
- Jetty 6.1.0 (Homepage)
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 (Note: the servlet was already created by the maven archetype).
The following servlet, which will be deployed together with the other class in the web archive, connects to ApacheDS directly, i.e. via the internal JNDI provider. No network access is needed. In the doGet method it performs a search operation against the Root DSE of the server, as the examples above do.
In order to make the servlet available to clients, it has to be declared in the deployment descriptor web.xml, here are the additions (a servlet named RootDseServlet for the class above, and a URL mapping)
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:
StartStopListener.java (Step 1)Download the source code
RootDseServlet.java (Step 2)
web.xml
ApacheDSWebApp.zip all sources including a build script for Apache Ant (build.xml)