Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: A simple sample, but interesting enough

A simple sample, but interesting enough

Tutorial Details:

JNDI overview, Part 4: the Doc-u-Matic, a JNDI application
JNDI overview, Part 4: the Doc-u-Matic, a JNDI application
By: By Todd Sundsted
Pull together your JNDI knowledge with a JNDI-enabled application
et's dive in. I've spent the last three months describing the Java Naming and Directory Interface (JNDI). By now, you should feel comfortable with naming and directory services and the operations they support. It's now time to build on that foundation. I've developed a JNDI-enabled
document publication and distribution application -- called Doc-u-Matic --
that illustrates and utilizes the material I've presented in the past three
How-To Java columns.
JNDI overview: Read the whole series!
Part 1. An introduction to naming services
Part 2. Use JNDI directory services to better manage your distributed applications
Part 3. Use JNDI to store your distributed application's objects
Part 4. Pull together what you've learned with a JNDI-enabled application
The Doc-u-Matic application illustrates JNDI's support for:
Centralized information administration
Network-wide information distribution
Object persistence
Note: To download the complete source code for this article, see Resources .
Keep the objects around
Doc-u-Matic is first-and-foremost a demonstration of JNDI-supported object persistence. Therefore, I think it makes sense to begin with a review of JNDI's support for stored objects.
You may recall from March's column that there are three techniques for storing Java objects in a JNDI service: as serialized data, as a reference to an object, and as the attributes on a directory context. Storing an object as serialized data is the simplest of the three techniques. Storing an object as a reference is useful in situations in which it doesn't make sense (or isn't possible) to store actual objects. Finally, storing objects as attributes on a directory context is useful when other, non-Java applications need access to the object's information. The application I've developed uses the first two methods of object storage.
The functional units
Figure 1, below, illustrates Doc-u-Matic from a functional perspective. Doc-u-Matic consists of three major functional units: the JNDI service (of course), a library service, and one or more clients.
Figure 1. Doc-u-Matic: A functional view
A library is a place to which you publish objects and from which you retrieve them. An object can be any Java instance, as long as it supports one
of the storage methods mentioned above -- it can be a String, a JavaBean, and so on.
JNDI plays two roles in Doc-u-Matic. It provides a single, well-known location where clients can locate the library service (standard address-book functionality), and it supports the implementation of the library service itself. On the latter point, it's worth noting that nothing in the design of a library implies that it has to be implemented on
top of JNDI -- that's just the direction I took (since this is an article on JNDI). You could implement a library on top of any technology that provides support for publishing and retrieving information, including HTTP, JDBC, NNTP, or IMAP. Best of all, the clients would never know the difference.
Before you proceed...
Doc-u-Matic assumes you have obtained, or have access to, and have configured an LDAP service. The application should support any naming and directory service that provides a JNDI service provider. However, I will assume you're using LDAP.
Before you proceed, make sure you've obtained, installed, and configured the following (if necessary, refer back to my earlier columns):
An LDAP implementation
Sun's JNDI reference implementation and the LDAP service provider
You'll also need to create the initial context that the application will connect to. I assume the following:
ou=HowTo,o=JavaWorld
If you're not sure how to do this, you'll need to refer to your LDAP service's documentation.
Usage
Before we dive into the code, let's stop for a moment and look at how to use Doc-u-Matic.
There are two usage roles: the administrator and the user. The administrator (think of a librarian) creates or deploys the library and publishes objects of various types in the library. The administrator also creates and distributes a properties file that contains all of the information necessary to connect to and use the library. Users, on the other hand, retrieve objects from the library.
The administrator's role
Let's begin by assuming the role of the administrator. The deployment tool is named JNDIDeploy . Before you deploy a library, you must create a properties file that contains the information necessary to connect to an initial context. The properties file must also contain the name that the JNDILibrary object will be bound to. If you configured your LDAP service with the LDIF I've supplied (see Resources ), the following properties file will provide a good place to start:
# DEPLOYMENT PROPERTIES
# This properties file contains all of the information necessary to
# find and connect to the JNDI service that holds the library and all
# published objects.
java.naming.factory.initial = com.sun.jndi.ldap.LdapCtxFactory
java.naming.provider.url = ldap://localhost:389/ou=HowTo,o=JavaWorld
library.name = cn=library
The library deploys as follows (I assume you've already set up the classpath to point to the jndi.jar and ldap.jar JAR files):
java JNDIDeploy
Once you, as administrator, have deployed a library, you must distribute a properties file to all users. The client applications I've developed all read a properties file pointed to by a URL. Therefore, the easiest way to distribute this file is to put it on a Web server and distribute the URL of the properties file.
Once you have deployed a library, you can publish objects to the library. By objects, I mean instances of Java classes. Objects play the role of abstract containers of information. As such, the simplest object is probably an instance of the String class. I've purposefully placed few requirements on publishable objects; they must be instantiateable via the Beans.instantiate() method, and they must be storable via JNDI. Objects are published as follows:
java Publish [name class]...
The Publish command requires the URL of the properties file mentioned above.
It also accepts any number of additional pairs of arguments. The first value
in each pair is the name of the object. The second is the name of the class
(including package information) that will be instantiated and stored. The
name must conform to whatever naming policy the JNDI service provider requires. In the case of LDAP, names will be of the form:
=
The user's role
Now, remove your administrator's hat and don your user's hat. It's time to retrieve some of the objects we've published. As a user, the only facts we have to know to retrieve an object are its name and the URL that describes the location of the library. We don't have to know how the library is implemented. This is especially useful if we, as users, must write programs to use the library. As you'll see a bit later, the code required to use a library is small indeed.
I've supplied a small client program that retrieves objects from the library. Objects are retrieved as follows:
java Client [name]...
The Client command requires the URL of the properties file. It also accepts
any number of additional arguments specifying the names of objects to retrieve from the library.
The code
Now, let's get to the core of this article -- the code. Figure 2, below, depicts the six classes that form the core of Doc-u-Matic. I'll visit
each, briefly describe its role, and then present the important bits of the
code (see Resources to download each class's complete source code).
Figure 2. Doc-u-Matic's six core classes
Library.java
The Library interface defines the methods that all libraries must provide. For the sake of simplicity, it requires only two methods: one to publish objects and one to retrieve objects. Full-featured libraries would provide search functionality as well.
/**
* Publishes an object in the library.
*
* @param object the object
* @param stringName the name of the object
* @param map a map containing the object's attributes
*
*/
public
void
publish(Object object, String stringName, Map map);
/**
* Retrieves a copy of a published object from the library
* and restores it if necessary.
*
* @param stringName the name of the object
*
* @returns the object, or null
*
*/
public
Object
retrieve(String stringName);
JNDILibrary.java
The JNDILibrary class provides a JNDI-based implementation of
the Library interface. The publish() and
retrieve() methods demonstrate how to use JNDI to bind objects to and look up objects from a directory service.
/**
* Publishes an object in the library.
*
* @param object the object
* @param stringName the name of the object
* @param map a map containing the object's attributes
*
*/
public
void
publish(Object object, String stringName, Map map) {
try {
// Use the properties to establish an initial directory context.
DirContext dircontext = new InitialDirContext(m_properties);
// Create an iterator that contains all the entries in the map.
Iterator iterator = map.entrySet().iterator();
// For each entry, create an attribute.
BasicAttributes basicattributes = new BasicAttributes();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();
basicattributes.put(entry.getKey().toString(),
entry.getValue().toString());
}
// Bind the object to the specified name.
dircontext.rebind(stringName, object, basicattributes);
// Close the context.
dircontext.close();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Retrieves a copy of a published object from the library
* and restores it if necessary.
*
* @param stringName the name of the object
*
* @returns the object, or


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
A simple sample, but interesting enough

View Tutorial:
A simple sample, but interesting enough

Related Tutorials:

Displaying 1 - 50 of about 1051 Related Tutorials.

Collection of Large Number of Java Sample Programs and Tutorials
Collection of Large Number of Java Sample Programs and Tutorials Collection of Large Number of Java Sample Programs and Tutorials... Simple Java Program for beginners that prints HelloWorld
 
The Sample Banner Example in Java
Applet Banners,Sample Banner,Sample Applet Banner,Sample Banner Tutorials Java Java - The Sample Banner Example in Java... the simple text rotation in a rectangular area of the appropriate applet
 
Using JMeter for a Simple Test
Using JMeter for a Simple Test Using JMeter for a Simple Test         ... a simple test to set up a test plan and stress test a Web application. Before
 
My Favorite Java Books
aren't going to give the absolute beginner a simple enough start, but if you
 
Simple Linked Lists
Java: Simple Linked Lists Java Notes: Simple Linked Lists This shows three programs. A simple... as simple as the singly-linked list, but makes some operations easier
 
Java Reference Books
-based, object-oriented language. It is designed to be simple enough that many..., and more complexity was introduced. Ant evolved from a simple-to-learn build... vast that there simply isn't enough room to provide equally
 
Perl Programming Books
few chapters, but I am open to suggestions. I am looking for interesting.... I work full time at the FSF now, and if the FSF receives enough donations via...;      The Perl Simple
 
Tips: WiFi Security for Home Networks
concern for anyone setting up a WiFi network, as anyone who is close enough... is simple. No one can access your laptop or computer when it is switched off
 
Difference between JSP 2.0 & JSP 2.1
1.2 with several new and interesting features. This version is fully compatible... understand the features added in JSP 2.0. The Features in JSP 2.0 are: Simple... invocation: Simple Tag Handlers:  Easy tags creation
 
Simple Editor
Java: Example - Simple Editor Java: Example - Simple Editor This simple text editor uses... 103 // editor/NutPad.java -- A very simple text editor -- Fred Swartz
 
Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple...) - A simple main program. User interface (CalcGUI.java... place. Altho this simple example doesn't show the full power of separating
 
Simple Form in Java
Simple Form in Java,Building Simple Form in Java,Code to Buid Form in Java Simple Form in Java    ...;          This is a simple
 
Simple Linked List Exercise 1
Java: Simple Linked List Exercise 1 Java Notes: Simple Linked List Exercise 1 Name: ___________________________________________ Fill in the blanks to for this program that reads
 
jQuery To Simple tabs
jQuery To Simple tabs jQuery To Simple tabs...; In this JQuery tutorial we will develop a program  to make simple tabs  Steps to develop the simple tabs . Step 1:        
 
jQuery to Simple Image Scroller
jQuery Example, jQuery to Retrieve Server's Current Time, jQuery Server time jQuery to Simple Image Scroller  ...;   Create a new file (simple_carousel.html) and add the following code
 
Simple Date example
Simple Date example, Date example Simple Date example         ...;    In this section we have presented a simple Date
 
XSD Simple Elements
XSD Simple Elements XSD Simple Elements...; simple element contains only text not even any other elements or attributes.But...; to a data type in order to limit its content. Defining a Simple Element The syntax
 
Simple Date Format Exception
Simple Date Format Exception Simple Date Format...;     Simple Date Format Exception inherits from..., parse and normalization of date. Simple Date-format provides you to work
 
Simple Bank Application in JSP
Simple Bank Application in JSP Simple Bank Application in JSP         ...;     In this section, we have developed a simple bank
 
More About Simple Trigger
More About Simple Trigger More About Simple...;   Simple trigger can be used to one shot execution...;        name: This is the name of Simple
 
Simple date formatter example
Simple date formatter example, date formatter Simple date formatter example      ...;        In this section of simple
 
VoIP Firewall
; VoIP's firewall challenges One of the interesting nuggets from SuperComm... Traffic with the PIX Firewall In this sample configuration, a PIX Firewall
 
Developing Simple Struts Tiles Application
Developing Simple Struts Tiles Application Developing Simple Struts Tiles Application     ... In this section  I will show you how to develop simple Struts Tiles Application
 
Building a Simple EJB Application ?A Tutorial
Building a Simple EJB Application ?A Tutorial Building a Simple EJB Application ?A Tutorial     ... a simple session EJB and a client web application using eclipse IDE along
 
Create a greeting in jsp
;   This is sample jsp code that creates a sample Birthday
 
Developing Simple Struts Tiles Application
Developing Simple Struts Tiles Application Developing Simple Struts Tiles Application     ... In this section  I will show you how to develop simple Struts Tiles Application
 
Simple Query on RDF file in Java
simple Query on RDF file in Java Simple Query... examples, so in this section we are going to describe how to run simple query on the RDF graph model. Now lets see the example that can fires a simple query
 
Simple Counter In Servlet
Servlets Counter Example,Simple Counter in Java Servlet,Free Counter Example Using Java Servlet Simple Counter in Servlet... of the program. Our program logic is simple. We have to just increment the value
 
JOptionPane - Simple Dialogs
Java: JOptionPane - Simple Dialogs Prev: none | Next: JOptionPane - More Dialogs Java: JOptionPane - Simple Dialogs Here are two useful static methods from
 
Java Building a Simple Web Service ? A Tutorial Tutorial
Building a Simple Web Service ? A Tutorial Building a Simple Web Service ? A Tutorial...;    Introduction In this tutorial we will create a simple web
 
Simple Basic Stroke Example
Simple Basic Stroke Example Simple Basic Stroke Example                      
 
Simple Font Paint Example
Simple Font Paint Example Simple Font Paint Example                       
 
Simple Gradient Paint Example
Simple Gradient Paint Example Simple Gradient Paint Example                      
 
JSF Simple Login Application
JSF Simple Login Application JSF Simple Login... provides you a simple but the important thing for protecting your any type of web...: http://localhost:8080/SimpleLogin/login.jsf in your browser. Output of Simple
 
A simple example of log4j
A simple example of log4j A simple example of log4j                       
 
A simple example of log4j for Servlet
A simple example of log4j for Servlet A simple example of log4j for Servlet                   
 
Very simple `Hello world' java program that prints HelloWorld
Very simple `Hello world' java program that prints HelloWorld Hello World Java Simple Java... language and  it is used to develop robust applications. Writing a simple Hello
 
Simple JSF Hello Application
Simple JSF Hello Application Simple JSF Hello Application                     
 
Business Intelligence in Retail
IN EVERY BUSINESS the most challenging job is to make yourself efficient enough
 
For Simple Calculation and Output
<c: out> For Simple Calculation and Output <c: out> For Simple Calculation and Output                
 
The Simple API for XML (SAX) APIs
The Simple API for XML (SAX) APIs The Simple API for XML (SAX) APIs                      
 
Simple Hash Table implementation in Java
Simple Hash Table implementation in Java Simple Hash Table implementation in Java                   
 
Clock Applet in Java
; Introduction Here is a sample of running clock provided by the java applet
 
Insert Image into Mysql Database through Simple Java Code
Insert image into mysql database using java code Insert Image into Mysql Database through Simple Java Code...; This is detailed simple java code that how save image into mysql database
 
Apache Ant - Building Simple Java Projects
Apache Ant - Building Simple Java Projects Apache Ant - Building Simple Java Projects                 
 
JavaScript array queue
the push() and shift() methods. It is a very simple way to implement queues. Queue...: Download Sample Source Code       
 
Javadoc
to generate API documentation into HTML format from Java source code. It is interesting... interesting thing to know about Javadoc comments is that we can embed HTML tags
 
Java error code
-time and Run-time. From java error we have given  you a sample of code
 
Foresight Linux 0.9.4 MR2 is released now
conary updateall', it's that simple. There are new ISOs (both DVD and CD... GNOME. Some of the things that may not be mature enough for some of the other
 
XML Interviews Question page3
of interesting but incompatible inventions from different manufacturers, because
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.