Name Service Design
Chris Liao
2006/12/31
1.1.J2EE Specification on Naming
Detailed Description, Please refer to
relative specification in
sun¡¯s website
Name Service is an important part of
J2EE architecture; J2EE server uses its interfaces to bind some objects into its
service container, for example: EJB components and Data Sources, and then these
objects can be lookup by client or server. Maybe you have experience on how to
find objects from J2EE server. Every obtained object has a unique name, which is
a mapping key and stored in service container with associated object.

We can regard the service container as a
tree, every obtained object is one node of its. There are some leaves (a kind of
nodes) and trunks (another type nodes) on the tree. The top nodes (leaves)
are bind into container from outside, and trunks work as sub containers to store
leaves or sub trunks.In fact, leaves are the bind objects,
and trunks are some middle containers, which mean that it is possible that
several middle nodes will be passed through from the root to the target node.
The depth is not divinable and depends on the expression of mapping name. The
name expression is similar with the folder path in Operation System., separators
represent the deep level. A sample expression and a structure diagram are below.
/school/classRoom1/xx_student

Common Object Request Broker
Architecture (CORBA) is provided by Object Management Group (OMG), which
has published realizable specification for it and but not give out one
implementation on it. The Architecture can be realized by many computer
languages, for example: c, c++, java and so on. Although these implementing
products are written in different languages, communication between them is well.
Why? Because a common protocol is used among them, which assure communication.
OMG has name the protocol as Internet Inner Object protocol (IIOP), and
we call these product ORB (object Request broker), which are more
platform than framework. Some useful services run on these platforms, include
CosNameService, CosTransaction, EventService and so on. CosName Service
offer similar interfaces as same as Naming service in J2EE server. In order to
support CORRBA, CosNameService should be extended in J2EE server. Some
relational diagrams between them are below.
¡¡


¡¡
Name Service consists of two parts:
Server tier and client tier. When J2EE server startup, it becomes accessible,
otherwise it is unavailable to client. As same as this, if you want to lookup an
object in remote server, some initial actions are expected. In order to support
CORBA, J2EE Naming service should realize some interfaces of CosName in
CORBA. Here, we give out some classes not only support J2EE specification but
also implement CORBA Interfaces. The following subjects will describe the
requirements.
¡¡
u
Preparation
u
Register on Server
u
Initialize on client
u
Invoke methods
¡¡
Ø
Define new classes and interfaces
Ø
Specify relationship between them.
Ø
Generate new classes to over IIOP
¡¡
Some new Classes and Interfaces are
defined here,
New Class or interface |
Implemented Interfaces |
Extended classes |
Responsibility |
JminNameContextFactory |
InitialContextFactory |
|
Instance
JndiClientContext |
JndiContext |
Remote, Context |
|
a remote
interface to over IIOP |
JndiClientContext |
Context |
|
Actual
object operating on
Name,
which run in client side |
JndiServerContext |
JndiContext |
|
Implementation on RMI Object Over CORBA, which run in sever side |
JndiContext_Stub |
JndiContext, |
javax.rmi.CORBA. Stub |
Work on
client ,generated by RMIC |
JndiServerContext_Tie |
javax.rmi.CORBA.Tie |
Org.omg.ObjectImp |
Work on
server, generated by RMIC |
NameInitializer
|
org.omg.PortableInterceptor.
ORBInitializer |
|
Initialize and register J2EE naming service |
Color indicator:
Interfaces classes
classes generated by tool
¡¡
We use UML (Unified Modeling Language)
to describe the relationship for the above classes and interfaces.
¡¡

Name service
need be over CORBA, so some new objects over IIOP are expected. JDK
supply a very useful tool to generate new files for this. That is ¡®Rmic.exe,¡¯
which is used to create CORBA files for java classes. Yes, these java classes
should realize some remote interfaces. Here, we will give out a sample and some
steps to show you that how to use this tool.
A:
Define a remote Interface
import
java.rmi.Remote;
import
java.rmi.RemoteException;
public interface
Hello extends Remote {
public void sayHello() throws
RemoteException;
}
B:
Implement the interface
import
java.rmi.Remote;
import
java.rmi.RemoteException;
public class
HelloImp implements Hello {
public void sayHello() throws
RemoteException{
System.out.println(¡°Hello world!¡±);
}
}
C:
Compile them with java tool
javac
Hello.java HelloImp.java
D: Use
RMIC tool to generate CORBA files
Rmic
¨CIIOP HelloImp
If get success on it, two new files will
be created on your current folder, they are below with relative names
Hello_Stub.class
HelloImp_Tie.class
Now, please
come to our subject, you can generate files like the fourth step, run the
similar command in console
Rmic ¨CIIOP
JndiServerContext
The target files are following:
JndiContext _Stub.class (run
on client side)
JndiServerContext
_Tie.class (run on server side)
Note: the implementation must has
been finished and compiled successfully.
JndiServerContext
object, which realize context interface in J2EE specification, and need
run in server¡¯s environment. When server startup, the object should be loaded
up, but the object doesn¡¯t extend a CORBA Object and can¡¯t run directly on CORBA
platform. What should we do? We have generated two new classes (JndiContext
_Stub.class, JndiServerContext
_Tie.class) in the previous
chapter, which are both CORBA objects. There are an important method ¡°setTarget
(Remote obj)¡± in class:
JndiServerContext_Tie and appears
in all Tie type classes, this method is used to set implementation on remote
interface into tie objects.
Services are
registered in initialization. User can define some initializer applied in orb
initialization. Here we register an instance of
JndiServerContext_Tie
class in
org.jmin.name.NameInitializer
object.
Below are source in
NameInitializer.java
//
NameInitializer.java
package
org.jmin.name;
import
org.omg.CosNaming.*;
import
org.omg.PortableInterceptor.*;
public class
NameInitializer implements ORBInitializer{
public void
pre_init (org.omg.PortableInterceptor.ORBInitInfo info) {
NameContext corbaContext =
( NameContext) info.
resolve_initial_references(¡°NameService ¡±);
JndiServerContext serverContext = new JndiServerContext(corbaContext);
JndiServerContext_Tie ctx_Tie = new JndiServerContext_Tie();
ctx_Tie.setTarget(serverContext);
info.
register_initial_reference(¡°JminNaming
¡±,ctx_Tie);
}
public void
pre_init (org.omg.PortableInterceptor.ORBInitInfo info) {
// do nothing
}
}
Notes that: How
to set the ORBInitializerClass to initialization environment
props.
put("org.omg.PortableInterceptor.ORBInitializerClass.JminNameService",
"
org.jmin.name.NameInitializer ");
ORB orb =
ORB.init(props);
¡¡¡¡¡¡.
The following activity diagram and
sequence diagram describe the course on registering name service.


¡¡
²
initialization
Client can use service handle to operate
name service, but before operation, its handle should be retrieved
in
client.
Here, interface JndiContext
represents this handle, which works as a member in class
JndiClientContext.In
fact, the handle is an instance of
JndiContext_Stub class can be over IIOP,
sample code as below:
¡¡¡¡¡¡¡¡¡¡¡¡¡
JndiContext
jndiContext = ( JndiContext) orb.
resolve_initial_references(¡°JndiNaming¡±);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡
²
Work Flow
Commonly, at first, client will
initialize name context, which is an entrance to J2EE name service. Some
necessary environment parameters are need be specified, which contained in a
properties object. Please refer to the similar source.
Properties prop = new
Properties ();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,¡±org.jmin.name.JminNameContextFactory
¡± );
prop.setProperty(Context.PROVIDER_URL,¡±iiop://localhost:9988¡±);
Context ctx = new
InitialContext(prop);
Object obj =
ctx..lookup(¡°xxxxx¡±);
Here, a sequence diagram is for it.

When remote call
coming to the
JndiServerContext_Tie object, it
forwards this remote request to
JndiServerContext object
obtained in tie.
Compile all java sources, and then zip
all classes file into a jar file with name: name.jar
and deploy it on server and client

Objective: startup
server and bind an object into Name service
//Server. Java
import javax.naming.*
import
org.jmin.name.*;
public class Server{
public static void main(String args[]) {
Properties prop = new Properties ();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,¡±org.jmin.name.JminNameContextFactory
¡±);
prop.setProperty(
Context.PROVIDER_URL,¡±iiop://localhost:9988¡±); //local port
Context ctx = new InitialContext(prop);
//initialize
String name = ¡°TestName¡± //target name
Object bindObj =
new String (¡°current object will be
bind into name service¡±); //target object
ctx.bind (name, bindObj); //bind object
Thread mainThread =
Thread.currentThread(); // main thread
mainThread.wait (); // keep block status
}
}
Objective:
lookup the bind object from Name service
//Client. Java
import javax.naming.*
import
org.jmin.name.*;
public class Client{
public static void main(String args[]) {
Properties prop = new Properties ();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,¡±org.jmin.name.JminNameContextFactory
¡±);
prop.setProperty(
Context.PROVIDER_URL,¡±iiop://localhost:9988¡±);
//server host and server port
Context ctx = new InitialContext(prop);
//initialize
String name = ¡°TestName¡±
Object targetObject =
new String (¡°current object will be
bind into name service¡±);
Object souceObj = ctx.lookup(name);
if (targetObject.equals(souceObj)) {
System.out.println(¡°Successful to test
!¡±);
} else {
System.out.println(¡°Failed to test !¡±);
}
}
}
Note: before development, please choose a ORB for it and be
familiar with the configuration of the ORB
|