|
Page Edited :
DIRxSRVx11 :
4.1. Embedding ApacheDS into an application
4.1. Embedding ApacheDS into an application has been edited by Emmanuel Lécharny (Dec 05, 2008). Content:
Code for this sample : Embedding Apache Directory Server into an applicationThe 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 will go through a very simple example step by step to show you how this can be done. Setting up the environmentWe first need to define a base for our project. Here is the structure we will use : /
|
+--src
|
+--main
|
+--java : we will put all the sources into this directory
We will also build the project using Maven 2.0.9. So we need a pom.xml to build the project. It has been attached. Creating the applicationSo 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. package org.apache.directory; /** * A simple exemple exposing how to embed Apache Directory Server * inot an application. * * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> * @version $Rev$, $Date$ */ public class EmbeddedADS { /** The directory service */ private DirectoryService service; /** * Creates a new instance of EmbeddedADS. It initializes the directory service. * * @throws Exception If something went wrong */ public EmbeddedADS() throws Exception { init(); } /** * Main class. We just do a lookup on the server to check that it's available. * * @param args Not used. */ public static void main( String[] args ) //throws Exception { try { // Create the server EmbeddedADS ads = new EmbeddedADS(); // Read an entry Entry result = ads.service.getAdminSession().lookup( new LdapDN( "dc=apache,dc=org" ) ); // And print it if available System.out.println( "Found entry : " + result ); } catch ( Exception e ) { // Ok, we have something wrong going on ... e.printStackTrace(); } } } As you can see, we first initialize the server, and immediately do a lookup to check that we can read an entry from it. Let's focus on the initialization part now. Initializing the serverThis 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.
Here, we will create the apache partition, associated with the 'dc=apache,dc=org' DN. Here is the code for this method : /**
* Initialize the server. It creates the partition, add the index, and
* inject the context entries for the created partitions.
*
* @throws Exception if there were some problems why initializing the system
*/
private void init() throws Exception
{
// Initialize the LDAP service
service = new DefaultDirectoryService();
// Disable the ChangeLog system
service.getChangeLog().setEnabled( false );
// Create a new partition named 'apache'.
Partition apachePartition = addPartition( "apache", "dc=apache,dc=org" );
// Index some attributes on the apache partition
addIndex( apachePartition, "objectClass", "ou", "uid" );
// And start the service
service.startup();
// Inject the apache root entry if it does not already exist
try
{
service.getAdminSession().lookup( apachePartition.getSuffixDn() );
}
catch ( LdapNameNotFoundException lnnfe )
{
LdapDN dnApache = new LdapDN( "dc=Apache,dc=Org" );
ServerEntry entryApache = service.newEntry( dnApache );
entryApache.add( "objectClass", "top", "domain", "extensibleObject" );
entryApache.add( "dc", "Apache" );
service.getAdminSession().add( entryApache );
}
// We are all done !
}
We disabled the ChangeLog service because it's useless in our case. As you can see, the steps to initialize the server are :
One important point : as the data are remanent, we have to check that the added context entry does not exist already before adding it. Here, we simply assume it does exist, and if not, we catch the exception we got, and create the entry. Some helper methods have been used : addPartition and addIndex. Here they are : /**
* 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 );
service.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 );
}
That's it ! (the attached code will contain the needed imports) Running the sampleWhen the main method is run, you should obtain something like : log4j:WARN No appenders could be found for logger (org.apache.directory.server.schema.registries.DefaultNormalizerRegistry).
log4j:WARN Please initialize the log4j system properly.
Found entry : ServerEntry
dn[n]: 0.9.2342.19200300.100.1.25=apache,0.9.2342.19200300.100.1.25=org
objectClass: extensibleObject
objectClass: domain
objectClass: top
dc: Apache
That's it ! |