 |
Overview of the LDAP enabled JAIN SLEE Service example
The LDAP enabled JAIN SLEE service is an example that uses JNDI to retrieve some data from a LDAP Server.
Source code
How to install, setup & run
- Get updated version of mobicents and mobicents-examples from CVS.
- Setup the LDAP Server environment in mobicents-examples/ldap-example/src/org/mobicents/slee/examples/ldap/LdapExample-sbb-jar.xml The default values are set for a LDAP Server directory pointing URL of "ldap://localhost:10389/o=sevenSeas", that is, the server host is "localhost", the server port is "10389", and the DN is "o=sevenSeas". Regarding authentication, the user is "uid=admin,ou=system" and password is "secret", with "simple" authentication. To install Apache DS LDAP Server to match that environment simply use Apache DS's basic user guide @ http://directory.apache.org/apacheds/1.0/apacheds-v10-basic-users-guide.html.
- Run mobicents .
- Go to mobicents-examples/ldap-example and run ant. This will build and deploy the example service. If everything is fine you should see in the server log the top attributes retrieved from the LDAP server. If you are using Apache DS with the sample data from the it's basic user guide, you should see a log in your server console with a similar entry:
15:44:18,921 INFO [LdapExampleSbb] Attributes retreived:
[objectClass: organization, extensibleObject, top, o: sevenSeas]
Use Cases
This example can be used as a starting point for developing more sophisticated services that offer richer functionality, integrating data from LDAP server with call control applications, as an example
Code review
The example application is very simple:
1. Reads the LDAP server envirionment from the SBB deployment descriptor
/**
* Called when an sbb object is instantied and enters the pooled state.
*/
public void setSbbContext(SbbContext context) {
log4j.info("setSbbContext(context=" + context.toString() + ")");
this.sbbContext = context;
try {
myEnv = (Context) new InitialContext().lookup("java:comp/env");
PROVIDER_URL = (String) myEnv.lookup("PROVIDER_URL");
SECURITY_PRINCIPAL = (String) myEnv.lookup("SECURITY_PRINCIPAL");
SECURITY_CREDENTIALS = (String) myEnv
.lookup("SECURITY_CREDENTIALS");
SECURITY_AUTHENTICATION = (String) myEnv
.lookup("SECURITY_AUTHENTICATION");
if (log4j.isInfoEnabled()) {
StringBuilder sb = new StringBuilder(
"Connection properties: { PROVIDER_URL=").append(
PROVIDER_URL).append(", SECURITY_PRINCIPAL=").append(
SECURITY_PRINCIPAL).append(", SECURITY_CREDENTIALS=")
.append(SECURITY_CREDENTIALS).append(
", SECURITY_AUTHENTICATION=").append(
SECURITY_AUTHENTICATION).append(" }");
log4j.info(sb.toString());
}
} catch (NamingException e) {
log4j.error("Can't set sbb context.", e);
}
}
2. Handles the ServiceStartedEvent, fired by SLEE when the apllication is activated and
/*
* Init the connection and retreive data when the service is activated by SLEE
*/
public void onServiceStartedEvent(
javax.slee.serviceactivity.ServiceStartedEvent event,
ActivityContextInterface aci) {
log4j.info("onServiceStartedEvent(event=" + event.toString() + ",aci="
+ aci.toString() + ")");
try {
//check if it's my service that is starting
ServiceActivity sa = ((ServiceActivityFactory) myEnv
.lookup("slee/serviceactivity/factory")).getActivity();
if (sa.equals(aci.getActivity())) {
log4j.info("Service activated...");
3. Connects to LDAP Server to retrieve top attributes. The result is logged.
log4j.info("Opening connection to server...");
InitialDirContext ctx = null;
NamingEnumeration enm = null;
try {
// setup connection environment
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, PROVIDER_URL);
env.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL);
env.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS);
env.put(Context.SECURITY_AUTHENTICATION,
SECURITY_AUTHENTICATION);
// create directory context
ctx = new InitialDirContext(env);
// get and log all top attributes
Attributes attrs = ctx.getAttributes("");
enm = attrs.getAll();
ArrayList<Attribute> list = new ArrayList<Attribute>();
while (enm.hasMore()) {
list.add((Attribute) enm.next());
}
log4j.info("Attributes retreived:\n " + list);
4. But don't forget to clear JNDI resources!
} finally {
// close resources
if (enm != null) {
try {
enm.close();
} catch (NamingException e) {
log4j.error("Unable to close naming enumeration.",
e);
}
}
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {
log4j.error("Unable to close dir context.", e);
}
}
}
Future Work
JNDI usage for LDAP is boring, too much code on setting environment, cleaning resources, etc., and since SBBs are just logic executed by JAIN SLEE event router, it's hard to maintain and share resources for better performance. If there is much demand from users, Mobicents may go a little further and develop a LDAP Resource Adaptor offering a simple interface (hidding JNDI details) and connection pools. Please use the forum if you are interested on such a subproject.
Related resources
-- Main.eduardomartins - 30 Jul 2007
|