Skip to content

Instantly share code, notes, and snippets.

@arnobroekhof
Created September 2, 2013 20:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnobroekhof/6416762 to your computer and use it in GitHub Desktop.
Save arnobroekhof/6416762 to your computer and use it in GitHub Desktop.
Embedded Apache Directory Server
package nl.techop.frontdoor.ldap;
import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.DirectoryService;
import org.apache.directory.server.core.entry.ServerEntry;
import org.apache.directory.server.core.partition.Partition;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
import org.apache.directory.server.ldap.LdapServer;
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.apache.directory.server.xdbm.Index;
import org.apache.directory.shared.ldap.entry.Entry;
import org.apache.directory.shared.ldap.name.LdapDN;
import java.util.HashSet;
/**
* A simple example exposing how to embed Apache Directory Server
* into an application.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev: 1062306 $, $Date: 2011-01-23 02:17:06 +0100 (Sun, 23 Jan 2011) $
*/
public class EmbeddedADS
{
/** The directory service */
private DirectoryService service;
private LdapServer ldapService;
private final String HOST = "0.0.0.0";
private final int PORT = 10389;
/**
* 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();
}
}
/**
* Initialize the server. It creates the partition, adds the index, and
* injects the context entries for the created partitions.
*
* @throws Exception if there were some problems while 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
if ( !service.getAdminSession().exists( apachePartition.getSuffixDn() ) ) {
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 );
}
ldapService = new LdapServer();
ldapService.setTransports(new TcpTransport(HOST,PORT));
ldapService.setDirectoryService(service);
ldapService.start();
}
/**
* 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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment