Previous Next Contents Index


6


C-Beans

A client bean, or c-bean, is a stub object that represents a remote m-bean to a manager developed with the adaptor client API. The manager accesses an m-bean by performing operations on the c-bean. The operations are then propagated to the m-bean. Events emitted by the m-bean are propagated to the c-bean. A c-bean is generated from its m-bean by using the mogen compiler supplied with the Java Dynamic Management Kit.


Overview of the mogen Compiler

The mogen compiler takes the compiled Java class of an m-bean and generates the Java interface and Java stubs of a c-bean. The Java stubs consist of Java source code that implements the interface. To develop a Java manager with code generated by mogen, you use the adaptorMO interface. This makes sure that the behavior of the stubs is the same on any adaptor client, provided that it implements the adaptorMO interface. The behavior of the stubs is not affected by how the adaptor client is implemented. For more information on the adaptorMO interface, refer to Chapter 7.

Options of the mogen compiler enable you to modify the characteristics of the stubs you generate from an m-bean. For example, options are available that enable you to generate read-only or read-write stubs. By generating from the same m-bean a set of stubs with different characteristics, you can develop a Java manager the behavior of which is modified at runtime, depending on which stubs are loaded. For example, when the read-only stubs are loaded, the Java manager will not be able to modify properties in the m-bean.


Command for Starting mogen

To start mogen, type the command for your operating environment:

The mogen compiler displays progress messages during compilation.

Options for mogen

The mogen tool can be invoked with the following options:

mogen [-d dir] [-ro] [-f] [-l className] [-li className] [-nl]
[-nlmo] [-nlas][-tp pkgName][-np] [-ne] [-nc]
[-classpath path] [-help] class1...classN


-d dir

Specifies the destination directory for the generated code.

-ro

Generates a read-only c-bean stub. The interface generated is complete but an exception is returned by the c-bean when an attempt is made to set a property.

-f

Generates a flattened c-bean, in which all inherited methods and attributes are included. For more information, see "Generating a Flattened C-Bean" on page 72.

-l className

Limits the flattening of a c-bean. Methods and attributes in classes in the inheritance tree up to and including the Java class specified by className are included in the c-bean. The c-bean does not extend the c-bean class corresponding to the superclass of className. For more information, see "Limiting the Flattening of a C-Bean" on page 73.

-li className

Limits the flattening of a c-bean and preserves inheritance. Methods and attributes in classes in the inheritance tree up to and including the Java class specified by className are included in the c-bean. The c-bean extends the c-bean class corresponding to the superclass of className. For more information, see "Limiting the Flattening of a C-Bean" on page 73.

-nl

Specifies that no code is generated in the c-bean for listeners, even if the m-bean contains listeners. For more information, see "Output From mogen for an M-Bean With Listeners and Events" on page 188.

-nlmo

Specifies that no code is generated in the c-bean for the Java interface for the listener. For more information, see "Output From mogen for an M-Bean With Listeners and Events" on page 188.

-nlas

Specifies that no code is generated in the c-bean for the listener stub. For more information, see "Output From mogen for an M-Bean With Listeners and Events" on page 188.

-tp pkgName

Generates code in the Java package specified by pkgName.

-np

Specifies that no code for actions is generated in the c-bean. For more information, see "Methods" on page 74.

-ne

Specifies that no code is generated in the c-bean for events, even if the m-bean contains code for handling events. For more information, see "Output From mogen for an M-Bean With Listeners and Events" on page 188.

-nc

Specifies that no code is generated in the c-bean for cascading.

-classpath path

Specifies the class path to be used for locating the classes to be compiled. By default mogen searches in the class path specified in the environment.

-help

Prints a usage message describing the options for mogen.

class1...classN

Specifies the list of m-bean classes to be compiled. For each class you must specify the full class name, including the package prefix. Do not specify the.class extension. Each class in the list must be separated from the one that precedes it by a space. The m-bean classes supplied to mogen must be in the form of compiled Java classes, not source files.

Example

CODE EXAMPLE 6-1 shows the command for using mogen to compile the class example.mo.Simple.Simple. The class is contained in a directory that is specified in the class path.

CODE  EXAMPLE  6-1     Starting mogen

prompt% mogen example.mo.Simple.Simple



Output of the mogen Compiler

For an m-bean defined in the Java class MyBean, the mogen compiler generates:

For example, when an m-bean representing a Java class named Simple is compiled, mogen generates the source code of:

This is illustrated in FIGURE 6-1.

FIGURE  6-1 Output of the mogen Compiler for a Simple M-Bean
An example showing a simple m-bean and the Java interface that mogen generates when compiling this simple m-bean is given in Chapter 2.


Note - For an m-bean that uses listeners, mogen generates additional files, as described in "Output From mogen for an M-Bean With Listeners and Events" on page 188.

Mapping Rules

The mogen compiler uses the Java Reflection API for analyzing an m-bean and generating its associated c-bean. It parses an m-bean using the design patterns defined in Chapter 3. The mapping rules that mogen uses for generating the c-bean are described in the following subsections.

Inheritance

By default, the mogen compiler translates inheritance expressed in m-beans into inheritance from c-beans. When you compile an m-bean that inherits methods and attributes from another m-bean, the inheritance of methods and attributes in the m-bean is duplicated in the c-bean generated. For example if m-bean B inherits from m-bean A, compiling B generates B', which corresponds to m-bean B and inherits from c-bean A'. Similarly, if m-bean C inherits from m-bean B, compiling C generates C', which corresponds to m-bean C and inherits from c-bean B'. This is illustrated in FIGURE 6-2.


Note - When you compile an m-bean, no c-beans are generated for its superclasses. You have to compile these superclasses explicitly.

FIGURE  6-2 Default Translation of Inheritance in M-Beans
Options of the mogen compiler enable you to generate flattened c-beans and to limit the scope of inheritance in c-beans.

Generating a Flattened C-Bean
To generate a flattened c-bean, specify the -f option in the command to start mogen. In a flattened c-bean, all methods and attributes that the m-bean inherits are included in the generated c-bean. This is illustrated in FIGURE 6-3.

FIGURE  6-3 Flattened C-Bean
Limiting the Flattening of a C-Bean
To limit the flattening of a c-bean, specify one of these options in the command to start mogen:

In the flattened c-bean, the methods and attributes inherited by className are not included. If you specify the -l className option, there is no inheritance in the c-bean. This is illustrated in FIGURE 6-4. If you specify the -li className option, inheritance is preserved in the c-bean. This is illustrated in FIGURE 6-5.

FIGURE  6-4 Limiting the Flattening of a C-Bean - no Inheritance

FIGURE  6-5 Limiting the Flattening of a C-Bean - Inheritance preserved
Notice that these two commands produce the same output:

prompt % mogen -f C



prompt % mogen -li A C

Properties

Each property of the m-bean is present in the c-bean with the same accessor methods. Therefore, if a property is read-only in the m-bean, the property is read-only in the generated c-bean.

Methods

In addition to the property accessors, mogen generates code only for public methods complying with the action design pattern defined in "Actions" on page 39. Other public methods do not appear in the c-bean generated by mogen.

An example of the definition of an action method in an m-bean is shown in CODE EXAMPLE 6-2.  


CODE  EXAMPLE  6-2     Action Method in an M-Bean 

public class simpleMBean implements java.io.Serializable {
...
public void performReverse() {
StringBuffer buf = new StringBuffer(name);

name = buf.reverse().toString();
}

public Boolean performReverse(Integer nFirst) {
StringBuffer buf = new StringBuffer();
int n = nFirst.intValue();

if (name.length() < n + 1) {
return (new Boolean(false));
}

buf.append(name.substring(0, n));

name = new String(buf.reverse().toString() +
name.substring(n));

return (new Boolean(true));
   }
...
}


The code generated in the MO interface from this definition is shown in CODE EXAMPLE 6-3. The code shown in CODE EXAMPLE 6-3 is a subset of the code mogen generates in the MO interface when compiling the m-bean.  


CODE  EXAMPLE  6-3     Action Method in a C-Bean 

public interface simpleMBeanMO extends ManagedObject {
...
public Boolean performReverse(Integer p0)
throws InstanceNotFoundException, CommunicationException,
IllegalAccessException, ServiceNotFoundException,
NoSuchMethodException;

public void performReverse()
throws InstanceNotFoundException, CommunicationException,
IllegalAccessException, ServiceNotFoundException,
NoSuchMethodException;
...
}


Options of the mogen compiler enable you to generate a c-bean without any code for actions. This is useful if you want to reduce the size of a c-bean, or if you want to prevent a user of the Java manager from performing actions on the m-bean. To generate a c-bean without any code for actions, specify the -np option in the command to start mogen.

Methods Present Only in C-Beans

The c-beans that mogen generates also contain methods that are not present in the m-bean. They implement the methods defined in the Java interface com.sun.jaw.reference.client.mo.ManagedObject. These methods are public methods that do not follow the design patterns defined by the JavaBeans component model. Their purpose is to improve the performance of applications by:


Using the Generated Code

The mogen compiler generates Java source code that you use for developing Java managers. To develop a Java manager by using code generated by mogen, you use the adaptorMO interface. For more information on the adaptorMO interface, refer to Chapter 7. Using the adaptorMO interface enables you to develop Java managers without having to modify the code that mogen generates. However, you can modify the code that mogen generates if you want to define a specific view of an m-bean.

The mogen compiler generates Java source code, not compiled Java classes. For your c-beans to be accessible to a Java manager, you have to compile the files that mogen generates, and make sure that the compiled Java classes are stored at a location specified in the CLASSPATH environment variable of the manager.

Developing Applications With Code From mogen

The applications you develop with the generated code are based on the Java interface that mogen generates. The stubs that mogen generates implement the methods defined in this interface. Options of the mogen compiler enable you to modify the characteristics of the stubs you generate from an m-bean for the same interface. For example, options are available that enable you to generate read-only or read-write stubs.

By generating from the same m-bean a set of stubs with different characteristics, you can develop a Java manager the behavior of which is modified at runtime, depending on which stubs are loaded by the adaptorMO. For example, you could develop an m-bean browser that behaves differently depending on whether the read-only or read-write stubs are loaded. When the read-only stubs are loaded, the m-bean browser will not be able to modify properties in the m-bean.

Modifying the Code Generated by mogen

If you want to define a specific view of an m-bean, you can modify the generated code. To ensure that the modified code remains consistent with the m-bean it represents, do not modify the interface; modify only the stub.



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

Previous Next Contents Index