Store, search, and retrieve Java objects in ApacheDS
Introduction: In part 2nd, describes how to store Java™ objects in the Apache directory server (ApacheDS), Bilal Siddiqui will provide 9 sample applications that demonstrate the concepts learned in part 1th. In addition to introducing all the steps of using ApacheDS to store, search, retrieve, and modify Java objects, Bilal will also provide a reusable Java class when summarizing the full text, which can be grouped together using LDAP schema components in ApacheDS.
In the 1th part, I introduced the conceptual basis for storing Java objects in ApacheDS, explained the core architecture of ApacheDS, and discussed how it implements directory services and pluggable protocol support. I also introduced some LDAP concepts and terminology, explained how ApacheDS implemented the LDAP protocol, and described the various components used to store and manipulate objects in ApacheDS. Finally, we discuss the basics of Java objects and RMI, and you must understand them in order to store and retrieve Java objects in ApacheDS. I also introduced a sample application--a data management system for a manufacturing enterprise--and used it to illustrate some of the concepts discussed in this article.
In the 2nd part of this series, I rely almost entirely on the example (9 examples in total). These examples are based on the data management system described in part 1th, which is about how you can store, search, review, and update Java objects in ApacheDS.
If you have not downloaded and installed ApacheDS, be sure to download and install ApacheDS and jxplorer before you begin. You can download the full source code for the article.
Attention!
Note that to keep up with the examples in this article, you must understand basic LDAP terminology and concepts such as proprietary names (DN), relative proprietary names (RDN), naming contexts, object classes, and property types. If you are not familiar with these terms, read part 1th before continuing.
Application 1. Storing Java objects
Let me start with a few applications that demonstrate how to store Java objects in ApacheDS. For this purpose, a Java naming and directory Interface (JNDI) is required, which provides an interface and method for manipulating objects and properties in the directory. See storing Java objects in the Apache directory server, part 1th, for a discussion of how to expose directory services using the JNDI interface (ApacheDS).
Jndi is not an LDAP-specific interface, so you can have a JNDI implementation for any directory service type. If you want to implement your own directory service and expose its functionality with JNDI, you need to implement a JNDI interface for the directory service. Note that the Java 2 Standard Edition (J2SE) provides an LDAP client-side JNDI implementation that you can use to talk to ApacheDS. In my discussion, I will use this client implementation.
Listing 1 is a simple application called storealicepreferences. I'll use this application to describe how to store the user Alice option as a Java object in the ApacheDS.
Listing 1. Storealicepreferences
public class Storealicepreferences {
Public storealicepreferences ()
{
try {
------------------------------------------
//step1:setting up JNDI properties for ApacheDS
//- -----------------------------------------
InputStream inputstream = new FileInputStream ("Apacheds.properties" );
Properties Properties = new properties ();
Properties.load (InputStream);
Properties.setproperty ("Java.naming.security.credentials", "secret");
//------------------------------------------
//step2:fetching a DirContext object
------------------------------------------
DirContext ctx = new InitialDirContext (properties);
//------------------------------------------
//step3:instantiate a Java object
//- -----------------------------------------
Messagingpreferences preferences = new MessagingprefErences ();
//------------------------------------------
//step4:store The Java object in ApacheDS
------------------------------------------
String bindcontext = "Cn=preferences,uid=alice,ou=users";
Ctx.bind (Bindcontext, preferences);
} catch (Exception e) {
System.out.println ("Operation failed:" + e);
}
}
public static void Main (string[] args) {
Storealicepreferences storealicepref = new Storeal Icepreferences ();
}
}