Tuesday, July 29, 2008

Embedded OpenDS 1.0.0

I was recently playing with OpenDS LDAP server 1.0.0. As its pure Java implementation it is used in embedded mode in JBoss AS and JBoss Portal testsuites. What is so cool about embedded OpenDS and why you should look at it?
  • It requires only two jar files for basic features support
  • Just few lines of code for management operations are needed.
  • Community on the mailing lists is very responsive
There are few good resources describing how to embed OpenDS available on the web:
However as the project evolves rapidly they don't cover the most recent 1.0.0 version. Here are few simple steps needed to bootstrap OpenDS from Java code:

  1. Download OpenDS 1.0.0 and unzip it.
  2. Fire the 'setup' config script from the main directory and alter the configuration as you need. Remember to shutdown the server if you let to start it.
  3. Embedded OpenDS will require a special directory structure to be able to start. All needed files can be copied from the server directory and are shown on the picture below:

    The 'db' directory can be left empty if you want to add the root entry manually in the code

  4. Copy OpenDS.jar and je.jar files from OpenDS-1.0.0/lib/ directory and add them to the project classpath
  5. Edit opends/config/config.ldif and remove following entry:

    dn: cn=SNMP Connection Handler,cn=Connection Handlers,cn=config
    objectClass: top
    objectClass: ds-cfg-snmp-connection-handler
    objectClass: ds-cfg-connection-handler
    ds-cfg-listen-port: 161
    ds-cfg-enabled: false
    ds-cfg-trap-port: 162
    ds-cfg-java-class: org.opends.server.snmp.SNMPConnectionHandler
    cn: SNMP Connection Handler


    (This entry requires to have OpenDS-1.0.0/lib/extensions/snmp-mib2605.jar file on the classpath)
Now with such prepared directory structure OpenDS can be started directly from the Java code. Belpw there is a very simple class that manages server lifecycle - it just needs two jar files in the classpath (OpenDS.jar and je.jar)




public class OpenDSService
{
private String serverRoot = "";

public DirectoryEnvironmentConfig getConfig()
{
DirectoryEnvironmentConfig config = new DirectoryEnvironmentConfig();

try
{

// Server root points to the directory with opends configuration
config.setServerRoot(new File(getServerRoot()));
config.setForceDaemonThreads(true);

}
catch (InitializationException e)
{
e.printStackTrace();
}

return config;
}


public void start()
{
if (!EmbeddedUtils.isRunning())
{
try
{
EmbeddedUtils.startServer(getConfig());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

public void stop()
{
if (EmbeddedUtils.isRunning())
{
EmbeddedUtils.stopServer(this.getClass().getName(), null);
}
}

public String getServerRoot()
{
return serverRoot;
}

public void setServerRoot(String serverRoot)
{
this.serverRoot = serverRoot;
}
}





Here there is a trivial example maven project that starts opends and performs simple JNDI search.

2 comments:

Aleksandar Kostadinov said...
This comment has been removed by the author.
Aleksandar Kostadinov said...

Hello,

Thanks much for the howto. Much better than the official wiki I've found.

I just did a regular OpenDS 2.3.0-build002 install through webstart. Configured a server, added data through the manager application which can be started from the installer.

Then copied the files you have listed in your howto to another location to be used as the embedded server root directory.
For SSL to work (as configured with the GUI installer) I needed also to copy config/*store* files to the embedded server root directory.

So that's all. Put OpenDS.jar and je.jar to classpath and I now have an embedded OpenDS server running like you show in your sample code.

Thank you!

P.S. the only bad thing is that opends is not available through a public maven repo, at least nothing but 1.0 version.