org.objectweb.fractal.rmi.stub
Class RmiStubFactory

java.lang.Object
  extended byorg.objectweb.fractal.rmi.stub.RmiStubFactory
All Implemented Interfaces:
BindingController, Constants, SkeletonFactory, StubFactory

public class RmiStubFactory
extends Object
implements Constants, StubFactory, SkeletonFactory, BindingController

A stub and skeleton factory based on ASM. This factory generates the stub and skeleton classes dynamically, when they are needed. It is therefore not necessary for the user to statically generate these classes with a tool such as rmic or idlc.
The stubs and skeletons created by this factory marshall and unmarshall the method invocations by using the following format: a method invocation message contains a four bytes method index, which uniquely and unambiguously identifies a method among the methods provided by a Java interface, followed by the method's arguments, marshalled in the order of their declaration in the method's signature.


Field Summary
protected  Logger logger
          The logger used to log messages.
protected  LoggerFactory loggerFactory
          The optional logger factory used to get a logger for this component.
protected  MarshallerFactory marshallerFactory
          The marshaller factory to be used by the stubs created by this factory.
 
Fields inherited from interface org.objectweb.asm.Constants
AALOAD, AASTORE, ACC_ABSTRACT, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH
 
Constructor Summary
RmiStubFactory()
          Constructs a new RmiStubFactory.
 
Method Summary
 void bindFc(String clientItfName, Object serverItf)
          Binds the client interface whose name is given to a server interface.
protected  void generateConstructor(ClassVisitor cv, String superClass)
          Generates an empty constructor for the given class.
protected  void generateSkeletonClass(ClassVisitor cv, Class itf)
          Generates a skeleton class for the given Java interface.
protected  void generateSkeletonMethod(ClassVisitor cv, String name, Class itf)
          Generates a skeleton's send method.
protected  void generateStubClass(ClassVisitor cv, Class itf)
          Generates a stub class for the given Java interface.
protected  void generateStubMethod(ClassVisitor cv, String name, Method m, int index)
          Generates a stub method.
protected  boolean isClassParameter(Method m, int p)
          Returns true if the specified parameter contains the name of a class.
 String[] listFc()
          Returns the names of the client interfaces of the component to which this interface belongs.
 Object lookupFc(String clientItfName)
          Returns the interface to which the given client interface is bound.
 RequestSession newSkeleton(Object target)
          Creates a new skeleton implementing the RequestSession interface.
 Object newStub(SessionIdentifier sessionId, Identifier[] ids, Context hints)
          Creates a new stub.
protected static void sort(Method[] methods)
          Sorts the given methods.
 void unbindFc(String clientItfName)
          Unbinds the given client interface.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

marshallerFactory

protected MarshallerFactory marshallerFactory
The marshaller factory to be used by the stubs created by this factory.


loggerFactory

protected LoggerFactory loggerFactory
The optional logger factory used to get a logger for this component.


logger

protected Logger logger
The logger used to log messages. May be null.

Constructor Detail

RmiStubFactory

public RmiStubFactory()
Constructs a new RmiStubFactory.

Method Detail

listFc

public String[] listFc()
Description copied from interface: BindingController
Returns the names of the client interfaces of the component to which this interface belongs.

Specified by:
listFc in interface BindingController
Returns:
the names of the client interfaces of the component to which this interface belongs.

lookupFc

public Object lookupFc(String clientItfName)
Description copied from interface: BindingController
Returns the interface to which the given client interface is bound. More precisely, returns the server interface to which the client interface whose name is given is bound. This server interface is necessarily in the same address space as the client interface (see bindFc).

Specified by:
lookupFc in interface BindingController
Parameters:
clientItfName - the name of a client interface of the component to which this interface belongs.
Returns:
the server interface to which the given interface is bound, or null if it is not bound.

bindFc

public void bindFc(String clientItfName,
                   Object serverItf)
Description copied from interface: BindingController
Binds the client interface whose name is given to a server interface. More precisely, binds the client interface of the component to which this interface belongs, and whose name is equal to the given name, to the given server interface. The given server interface must be in the same address space as the client interface.

Specified by:
bindFc in interface BindingController
Parameters:
clientItfName - the name of a client interface of the component to which this interface belongs.
serverItf - a server interface.

unbindFc

public void unbindFc(String clientItfName)
Description copied from interface: BindingController
Unbinds the given client interface. More precisely, unbinds the client interface of the component to which this interface belongs, and whose name is equal to the given name.

Specified by:
unbindFc in interface BindingController
Parameters:
clientItfName - the name of a client interface of the component to which this interface belongs.

newStub

public Object newStub(SessionIdentifier sessionId,
                      Identifier[] ids,
                      Context hints)
               throws JonathanException
Creates a new stub. A stub plays two roles:

Specified by:
newStub in interface StubFactory
Parameters:
sessionId - a session identifier, to be used to send marshalled data to the object represented by the stub;
ids - the set of identifiers of the stub;
hints - other data possibly used to create the stub. This method requires the fully qualified name of the Java interface to which the stub will gives access. This name must be associated to the "interface_type" key.
Returns:
an instance of a sub class of the Stub class.
Throws:
JonathanException - if something goes wrong.

newSkeleton

public RequestSession newSkeleton(Object target)
                           throws JonathanException
Description copied from interface: SkeletonFactory
Creates a new skeleton implementing the RequestSession interface.

Specified by:
newSkeleton in interface SkeletonFactory
Parameters:
target - the remote object.
Returns:
the created skeleton.
Throws:
JonathanException - if something goes wrong.

generateStubClass

protected void generateStubClass(ClassVisitor cv,
                                 Class itf)
Generates a stub class for the given Java interface. This method generates a sub class of the Stub class that implements the given interface, by using the given class visitor.

Parameters:
cv - the class visitor to be used to generate the stub class.
itf - the Java interface that the stub class must implement.

generateSkeletonClass

protected void generateSkeletonClass(ClassVisitor cv,
                                     Class itf)
Generates a skeleton class for the given Java interface. This method generates a sub class of the Skeleton class whose target is an object implementing the given interface, by using the given class visitor.

Parameters:
cv - the class visitor to be used to generate the stub class.
itf - the Java interface implemented by the skeleton's target.

generateConstructor

protected void generateConstructor(ClassVisitor cv,
                                   String superClass)
Generates an empty constructor for the given class.

Parameters:
cv - the class visitor to be used to generate the constructor.
superClass - the internal name of the super class of the generated class. This name is used to generate a call to the super constructor.

generateStubMethod

protected void generateStubMethod(ClassVisitor cv,
                                  String name,
                                  Method m,
                                  int index)
Generates a stub method. A stub method is of the following form:

 public T m (T0 arg0, ...) throws E0, ... {
   try {
     Marshaller marshaller = request();
     ReplyInterface reply = prepareInvocation(marshaller);
     marshaller.writeInt(methodIndex);
     marshaller.writeXXX(arg0);
     ...
     invoke(marshaller);
     UnMarshaller unmarshaller = reply.listen();
     T result = (T)unmarshaller.readXXX();
     unmarshaller.close();
     return result;
   } catch (Exception e) {
     e = handleException(e);
     if (e instanceof E0) throw (E0)e;
     ...
     if (e instanceof RuntimeException) throw (RuntimeException)e;
     throw new RemoteException("server side exception", e);
   }
 }
 

Parameters:
cv - the class visitor to be used to generate the stub method.
name - the internal name of the generated stub class.
m - the signature of the method that must be generated.
index - the index of this method. This index will be marshalled by the generated stub method in each invocation message, to specify the method that must be called on the server side. This index is the index of the method in the sorted array of all the methods implemented by the stub.

generateSkeletonMethod

protected void generateSkeletonMethod(ClassVisitor cv,
                                      String name,
                                      Class itf)
Generates a skeleton's send method. The generated method is of the following form:

 public void send (UnMarshaller unmarshaller, ReplySession session)
   throws JonathanException
 {
   int methodIndex = unmarshaller.readInt();
   try {
     switch (methodIndex) {
       case 0:
         T0 arg0 = unmarshaller.readXXX();
         ...
         unmarshaller.close();
         T result = ((I)target).m0(arg0, ... );
         Marshaller marshaller = session.prepareReply();
         marshaller.writeXXX(result);
         session.send(marshaller);
         session.close();
         return;
       ...
       default:
         handleInterfaceMethods(unmarshaller, session, methodIndex);
     }
   } catch (Exception e) {
     handleException(e, session);
   }
 }
 

Parameters:
cv - the class visitor to be used to generate the stub method.
name - the internal name of the generated stub class.
itf - the target object's interface that must be exported by the skeleton.

isClassParameter

protected boolean isClassParameter(Method m,
                                   int p)
Returns true if the specified parameter contains the name of a class. This method is used to marshall a Class object, instead of a String, for parameters that contains class names, in order to enable these classes to be downloaded from the network if needed. The default implementation of this method returns true for:

Parameters:
m - a method.
p - index of a parameter of this method, or -1 to designate the return value.
Returns:
true if the specified parameter contains the name of a class.

sort

protected static void sort(Method[] methods)
Sorts the given methods. This method is used to assign an unambiguous index to each method of an interface (the index of a method in the array returned by getMethods cannot directly be used for this purpose, since this method returns the methods in any order).

Parameters:
methods - the method array to be sorted.