Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Embedding Apache Directory Server into an application

...

...

This site was updated for ApacheDS 1.5.5.

...

For an example on how to embed the Apache Directory Server version 1.5.7 please look at the
code present at Example For Embedding version 1.5.7

...

The idea is to use the server directly without having to communicate with a remote server, eliminating all the network layer. This is one of the reason ADS has been developed in Java: to allow such usage.

...

We first need to define a base for our project. We use Maven (2.0.9 or above) to build our project, so the structure we will use looks like this:

...

...

You could download both files here:
EmbeddedADS.java
pom.xml

...

If you don't want to use Maven you need to add the following dependencies to your classpath in order to compile and run this sample.
* apacheds-all-1.5.5.jar
* slf4j-api-1.5.6.jar
* slf4j-log4j12-1.5.6.jar
* log4j-1.2.14.jar

...

So let's start with the code. It's quite simple: we define a class, add a main() method, and initialize the server, before using it. In the code snippet below, we have removed all the initialization part to keep only the main() method.

...

As you can see, we first initialize the server, and immediately do a lookup to check that we can read an entry from it.

...

This is done in the init() method. It will create the DirectoryService global object (service), and create a partition in which we will store the entries.

...

...

A partition is a storage point, associated with a DN, root point for this partition. It's a bit like a mounting point on Unix.
We also need a context entry associated to this DN.

...

Here, we will create the apache partition, associated with the 'dc=apache,dc=org' DN.

Here is the code for this method :

...

...

We disabled the ChangeLog service because it's useless in our case. As you can see, the steps to initialize the server are:

  • create a new DirectoryService instance
  • add a partition
  • add some index
  • start the service
  • add the context entry

One important point: as the data are remanent, we have to check that the added context entry does not exist already before adding it.

Some helper methods have been used : addPartition and addIndex. Here they are :

...

...

That's it! (the attached code will contain the needed imports)

...

Using Maven this is easy:

...

The resulting jar can be found in the target directory.

...

Using Maven this is easy:

...

...

When the main method is run, you should obtain something like :

...

...

That's it!