Previous Next Contents Index


12


Class and Library Loading Service

The class and library server serves classes and libraries to the class loader and the library loader. An agent uses the class loader to obtain new classes from an arbitrary location. The library loader loads native libraries into the framework.


Class and Library Server

The class and library server serves classes and libraries to the class loader and the library loader. It is provided as the Java class com.sun.jaw.impl.server.rmi.NetClassServerImpl. It works in conjunction with the RMI network class loader provided with the Java Dynamic Management Kit.

The supplied server runs as a stand-alone Java program. However, it is implemented as an m-bean. This enables it to be integrated with a Java Dynamic Management agent and managed through the adaptorMO interface.


Note - The class and library server does not support JAR files or ZIP files.

Starting the Class and Library Server

To start the server, type this command:

java [options] com.sun.jaw.impl.server.rmi.NetClassServerImpl

If there is no RMI registry running on the machine where the server is started, the server starts the registry and registers itself.

Options

You can specify the following system properties:

-DDEBUG

Specifies that debug messages are printed.

-Dclasspath=classpath

Specifies the search path for Java classes. The directories in this search path must not contain .jar or .zip files. By default it is the current directory.

-Dlibpath=libpath

Specifies the search path for libraries. By default it is the current directory.

-Dport=portno

Specifies the port number for RMI access. By default it is 1099.

-Dservice=servicename

Specifies the service name for registering the server object with the RMI registry. By default it is NetClassServer.

-Ddirfile=directivefile

Forces the class server to run in secure mode, using the code signing directive file provided. The format of the code signing directive file is specified by the javakey utility provided by the Java Development Kit (JDK). For further information, see the JDK product documentation.

Example

In the example shown in CODE EXAMPLE 12-1, trace messages are printed and Java classes are searched for in the /project/netman/mbeans and /project/servman/mbeans directories.

CODE  EXAMPLE  12-1     Starting the Class and Library Server

java
-DDEBUG
-Dclasspath=/project/netman/mbeans:/project/servman/mbeans
com.sun.jaw.impl.server.rmi.NetClassServerImpl


Retrieving and Updating Search Paths

When you start the server, you have to specify the search paths for locating:

These paths can be retrieved and updated dynamically, either by using the RMI interface defined by the server, or by using the adaptorMO interface if the server is integrated with a Java Dynamic Management agent.

Implementing a Class and Library Server

Any class and library server you implement must implement the com.sun.jaw.impl.server.rmi.NetClassServer interface.


Class and Library Loader

The class and library loader loads classes and libraries into the framework. The com.sun.jaw.impl.agent.services.loader.rmi.NetClassLoader Java class provides an implementation of the class and library loader. It works in conjunction with the RMI network class server provided with the Java Dynamic Management Kit.

FIGURE  12-1 Operation of the Network Class Loader

Object Name of an RMI Network Class Loader

The object name you assign to a class loader must contain the configuration information that the class loader requires. To ensure this, specify the following attributes in the search key of the object name:

host = hostname

The host name where the class server is running. By default this is the local host.

port = portno

The port number to be used. By default this is 1099.

service = servicename

The service name of the RMI object to be used as the class and library server. By default this is NetClassServer.

secure = [true|false]

This flag defines the security mode for the RMI network class loader. By default this is false.

The URL rmi://host:port/service should correspond to the URL of the class server.

An example of the object name of a class loader is given in CODE EXAMPLE 12-2.

Adding an RMI Network Class Loader Through a Manager

The supplied class loader is implemented as an m-bean. This enables it to be managed through the adaptorMO interface. To add a class loader through a manager, you include in the manager code that requests the class loader to be added to the agent. The c-bean that the adaptorMO interface requires for managing a class loader is supplied as the Java class com.sun.jaw.impl.agent.services.loader.rmi.NetClassLoaderMO. Therefore, the manager you develop must contain the code required for instantiating this class.

CODE EXAMPLE 12-2 shows code for instantiating a class loader through the adaptorMO interface. The newly instantiated class loader obtains classes and libraries from an RMI server that has the URL rmi://sky:1099/NetClassServer.


CODE  EXAMPLE  12-2     Instantiating a Network Class Loader Through a Manager 

// Define the name of the class to be instantiated
//
String nc = "com.sun.jaw.impl.agent.services.loader.rmi.NetClassLoader";

// Build object name for registering the instance
//
String prop = ".host=sky,port=1099,service=NetClassServer,secure=true";
ObjectName name = new ObjectName(MOFactory.getDomain()+
":com.sun.jaw.impl.agent.services.loader.rmi.NetClassLoaderMO"+prop);

// Create the m-bean and its corresponding c-bean, and register the m-bean
//
NetClassLoaderMO ncl = (NetClassLoaderMO) MOFactory.cb_newMO(nc,name,null);


If your manager needs to obtain c-beans from a remote class server, you have to instantiate a class loader in the manager. For more information, refer to "Class Loader" on page 87.

Directly Adding an RMI Network Class Loader

To add a class loader directly to an agent, include in the agent the code required for instantiating the com.sun.jaw.impl.agent.services.loader.rmi.NetClassLoader class.

CODE EXAMPLE 12-3 shows code in an agent for instantiating a class loader directly. The newly instantiated class loader obtains classes and libraries from an RMI server that has the URL rmi://sky:1099/NetClassServer.


CODE  EXAMPLE  12-3     Instantiating a Network Class Loader in Agent Code 

// Define the name of the class to be instantiated
//
String nc = "com.sun.jaw.impl.agent.services.loader.rmi.NetClassLoader";

// Build object name for registering the instance with the repository
//
String prop = ".host=sky,port=1099,service=NetClassServer,secure=true";
ObjectName name = new ObjectName(cmf.getDomain()+
":com.sun.jaw.impl.agent.services.loader.rmi.NetClassLoaderMO"+prop);

// Create the m-bean and register it with the repository
//
NetClassLoader ncl = (NetClassLoader) cmf.newObject(nc, name, null);


Instantiating a Remote M-Bean Using the RMI Network Class Loader

The methods for instantiating a remote m-bean are the same as those for instantiating any other m-bean, although it is necessary to specify the object name of the class loader to be used.

CODE EXAMPLE 12-4 shows code for instantiating a remote m-bean using the RMI network class loader through a manager. In this example, the class RemoteClass is stored on a remote class server. The RemoteClassON is the object name given to the m-bean to register it with the repository. The NetClassLoaderON is the object name of the class loader to be used.


CODE  EXAMPLE  12-4     Instantiating a Remote M-Bean Through a Manager

RemoteClassMO rcmo = (RemoteClassMO) MOFactory.cb_newMO(
   "RemoteClass", RemoteClassON, null, NetClassLoaderON);


CODE EXAMPLE 12-5 shows code for instantiating a remote m-bean using the RMI network class loader through an agent. In this example, the class RemoteClass is stored on a remote class server. The RemoteClassON is the object name given to the m-bean to register it with the repository. The NetClassLoaderON is the object name of the class loader to be used.


CODE  EXAMPLE  12-5     Instantiating a Remote M-Bean Through an Agent

RemoteClass rc = (RemoteClass) cmf.newObject(
   "RemoteClass", RemoteClassON, NetClassLoaderON, null);



Using the Library Loading Service

The RMI network class loader implements the LibraryLoaderIf interface to allow the loading of dynamic native libraries. The basic operation of the service is described in "Library Loaders" on page 142. The library loading service provides a versioning capability based on a version number stored in the libversion file and the libpath parameter in these classes:

Library Versioning

When the net class loader attempts to load a native library, it checks for native libraries in the local file system (specified by the directory property). When the required library is found, the version number in the libversion file is checked and compared to the version number in the libversion file held on the class server. If the version on the server is newer than the one stored on the agent, the updated library is downloaded to the agent's local file system. The server side libversion file is supplied and maintained by the user. The libversion file is an ascii file with an integer value on the first line that specifies the version number .

Libpath in RMI Network Class Server

The NetClassServerImpl class libpath property specifies the directory in which the search for native libraries will begin. CODE EXAMPLE 12-6 shows the command to start the RMI network class loader, specifying /class_server/lib/libs1 and /class_server/lib/libs2 as example search directories.   

CODE  EXAMPLE  12-6     Starting the Class and Library Server

java
-Dlibpath=/class_server/lib/libs1:/class_server/lib/libs2
com.sun.jaw.impl.server.rmi.NetClassServerImpl


The library path contains two library directories, libs1 and libs2. These are located in the /class_server/lib/ directories. The directories, libs1 and libs2 must contain a user created libversion file to enable library versioning. CODE EXAMPLE 12-7 shows an example hierarchy for the libraries on the RMI network class server.


CODE  EXAMPLE  12-7     Library Hierarchy in the RMI Network Class Server

Library hierarchy on remote server:

/class_server/lib/libs1/libversion
                        libA.so

/class_server/lib/libs2/libversion
                        libA.so
                        Solaris/sparc/2.x/lib/libB.so
                        WindowsNT/x86/4.0/lib/B.dll


Libpath in RMI Network Class Loader

The library path property specifies the directory into which the required library will be copied before the RMI network class loader loads it into memory. CODE EXAMPLE 12-8 shows the code to set the directory property of the network class loader instantiated in CODE EXAMPLE 12-3.

CODE  EXAMPLE  12-8     Setting the directory property

Vector libpath = new Vector();
libpath.addElement(new String("/tmp/libs1"));
libpath.addElement(new String("/tmp/libs2"));
ncl.setLibPaths(libpath);


Basename

The basename is the first unique directory name where the libraries are stored on the server. Each directory referred to by a basename contains a libversion file. Basenames enable you to maintain versioning control on any number of libraries. The basenames on the class server and the class loader must be identical for the library loading service to operate correctly.

CODE EXAMPLE 12-6 and CODE EXAMPLE 12-8 show example library paths. The basenames are libs1 and libs2. Although the library base paths are different, the basenames are identical.

Loading a Library From the Local File System

CODE EXAMPLE 12-9 shows an example hierarchy for the libraries on the agent host.   

CODE  EXAMPLE  12-9     Library Hierarchy on the Agent Host

Library hierarchy on local file system of the agent host:

/usr/lib/libsocket.so


Local libraries are loaded by invoking the LibLoader.loadLibrary method. The LibLoader.loadLibrary method is invoked by a class that was loaded through the network class loader. The LibLoader.loadLibrary method is described in "Including Native Code in an M-Bean" on page 143. CODE EXAMPLE 12-10 shows two methods of loading the same library.


CODE  EXAMPLE  12-10     Loading a Library From the Local File System 

LibLoader.loadLibrary(this.getClass(),"","socket");
// loads /usr/lib/libsocket.so

LibLoader.loadLibrary(this.getClass(),"","/usr/lib/libsocket.so");
// loads /usr/lib/libsocket.so


Loading a Library From the Class Server

Libraries are loaded from the class server using the LibLoader.loadLibrary method. CODE EXAMPLE 12-11 shows code for loading two instances of the same library using the basename to differentiate between the two. CODE EXAMPLE 12-11 also shows code for loading libraries into two different operating environments.

CODE  EXAMPLE  12-11     Loading a Library From the Class Server 

LibLoader.loadLibrary(this.getClass(),"libs1","A");
// loads /class_server/lib/libs1/libA.so

LibLoader.loadLibrary(this.getClass(),"libs2","A");
// loads /class_server/lib/libs2/libA.so

LibLoader.loadLibrary(this.getClass(),"libs2","B");

// loads /class_server/lib/libs2/Solaris/sparc/2.x/lib/libB.so
// if the agent runs on Solaris

// loads /class_server/lib/libs2/WindowsNT/x86/4.0/lib/B.dll
// if the agent runs on Windows NT

LibLoader.loadLibrary(this.getClass(),"libs2",
                                      "Solaris/sparc/2.x/lib/libB.so");
// loads /class_server/lib/libs2/Solaris/sparc/2.x/lib/libB.so

LibLoader.loadLibrary(this.getClass(),"libs2","WindowsNT/x86/4.0/lib/B.dll");
// loads /class_server/lib/libs2/WindowsNT/x86/4.0/lib/B.dll



Security Manager

The security manager supplied with the Java Dynamic Management Kit is programmed to accept incoming libraries. For further information, refer to "Security Manager" on page 145.

When running the RMI network class loader in non-secure mode, you must use the security manager provided with the Java Dynamic Management Kit to be able to load native libraries.

When running the RMI network class loader in secure mode, you must write a specific security manager that supports code signing and native library loading, as specified in "Code Signing" on page 145.


Code Signing

The RMI class and library loading service supports class signing, enabling it to ensure that only classes from a trusted source are downloaded over a network. This involves:



Copyright 1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303 U.S.A.
Copyright in French

Previous Next Contents Index