KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > invocation > InvokeCommand


1 package org.sapia.ubik.rmi.server.invocation;
2
3 import org.sapia.ubik.rmi.Consts;
4 import org.sapia.ubik.rmi.server.Hub;
5 import org.sapia.ubik.rmi.server.Log;
6 import org.sapia.ubik.rmi.server.OID;
7 import org.sapia.ubik.rmi.server.RMICommand;
8 import org.sapia.ubik.rmi.server.RmiUtils;
9 import org.sapia.ubik.rmi.server.ShutdownException;
10 import org.sapia.ubik.rmi.server.VmId;
11 import org.sapia.ubik.rmi.server.transport.ClassDescriptor;
12 import org.sapia.ubik.rmi.server.transport.MarshalledObject;
13
14 import java.io.Externalizable JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInput JavaDoc;
17 import java.io.ObjectOutput JavaDoc;
18
19 import java.lang.reflect.*;
20
21
22 /**
23  * This commands performs a remote method invocation.
24  *
25  * @author Yanick Duchesne
26  * <dl>
27  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
28  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
29  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
30  * </dl>
31  */

32 public class InvokeCommand extends RMICommand implements Externalizable JavaDoc {
33   static final long serialVersionUID = 1L;
34   private static final boolean _vmUsesMarshalledObjects = (System.getProperty(Consts.MARSHALLING) != null) &&
35     System.getProperty(Consts.MARSHALLING).equals("true");
36   private transient Class JavaDoc[] _paramTypes;
37   private transient String JavaDoc _transportType;
38   private OID _oid;
39   private String JavaDoc _methodName;
40   private Object JavaDoc[] _params;
41   private ClassDescriptor[] _paramClasses;
42   private boolean _usesMarshalledObjects = _vmUsesMarshalledObjects;
43
44   /**
45    * Do not call; used for externalization only.
46    */

47   public InvokeCommand() {
48   }
49
50   /**
51    * @param oid the <code>OID<code> (unique object identifier) of the
52    * object on which the method call should be performed.
53    * @param methodName the name of the method to call.
54    * @param params the method's parameters.
55    * @param paramClasses the method's signature, as a class array.
56    */

57   public InvokeCommand(OID oid, String JavaDoc methodName, Object JavaDoc[] params,
58     Class JavaDoc[] paramClasses, String JavaDoc transportType) {
59     _oid = oid;
60     _methodName = methodName;
61     _params = params;
62     _paramClasses = new ClassDescriptor[paramClasses.length];
63
64     for (int i = 0; i < paramClasses.length; i++) {
65       _paramClasses[i] = new ClassDescriptor(paramClasses[i]);
66     }
67
68     _paramTypes = paramClasses;
69     _transportType = transportType;
70   }
71
72   /**
73    * Returns the object identifier of the object on which
74    * the invocation will be performed.
75    *
76    * @return an <code>OID</code>.
77    */

78   public OID getOID() {
79     return _oid;
80   }
81
82   /**
83    * Returns the name of the method to invoke.
84    *
85    * @return a method name.
86    */

87   public String JavaDoc getMethodName() {
88     return _methodName;
89   }
90
91   /**
92    * Returns the signature (the types of the method's parameters) of the method
93    * to call.
94    *
95    * @return an array of <code>Class</code> instances.
96    */

97   public Class JavaDoc[] getParameterTypes() {
98     return _paramTypes;
99   }
100
101   public void setParams(Object JavaDoc[] params) {
102     _params = params;
103   }
104
105   /**
106    * Returns true if this instance encapsulates method call parameters and
107    * return value in <code>MarshalledObject</code> instances.
108    *
109    * @return <code>true</code> if this instance uses <code>MarshalledObject</code>s.
110    *
111    * @see MarshalledObject
112    */

113   public boolean usesMarshalledObjects() {
114     return _usesMarshalledObjects;
115   }
116
117   /**
118    * Returns the parameters of the method to call.
119    */

120   public Object JavaDoc[] getParams() {
121     return _params;
122   }
123
124   /**
125    * @see org.sapia.ubik.rmi.server.RMICommand#execute()
126    */

127   public Object JavaDoc execute() throws Throwable JavaDoc {
128     if (Hub.isShutdown()) {
129       throw new ShutdownException();
130     }
131
132     Object JavaDoc obj = Hub.serverRuntime.objectTable.getObjectFor(_oid);
133
134     if (_paramTypes == null) {
135       if(obj.getClass().getClassLoader() == null){
136         convertParams(Thread.currentThread().getContextClassLoader());
137       }
138       else{
139         convertParams(obj.getClass().getClassLoader());
140       }
141     }
142
143     Method mt = obj.getClass().getMethod(_methodName, _paramTypes);
144
145     try {
146       if (Log.isDebug()) {
147         Log.debug(getClass(),
148           "invoking " + mt.getName() + " on " + _oid + "(" + obj + ")");
149       }
150
151       long start = System.currentTimeMillis();
152
153       ServerPreInvokeEvent preEvt = new ServerPreInvokeEvent(this, obj);
154
155       Hub.serverRuntime.dispatchEvent(preEvt);
156
157       Object JavaDoc toReturn = mt.invoke(preEvt.getTarget(),
158           preEvt.getInvokeCommand().getParams());
159
160       ServerPostInvokeEvent postEvt = new ServerPostInvokeEvent(preEvt.getTarget(),
161           preEvt.getInvokeCommand(),
162           System.currentTimeMillis() - preEvt.getInvokeTime());
163
164       Hub.serverRuntime.dispatchEvent(postEvt);
165
166       if (_usesMarshalledObjects) {
167         toReturn = new MarshalledObject(toReturn, VmId.getInstance(),
168             _config.getServerAddress().getTransportType(), _oid.getCodebase());
169       }
170
171       return toReturn;
172     } catch (InvocationTargetException e) {
173       if (_usesMarshalledObjects) {
174         return new MarshalledObject(e, VmId.getInstance(),
175           _config.getServerAddress().getTransportType(), _oid.getCodebase());
176       }
177
178       throw e.getTargetException();
179     }
180   }
181
182   /**
183    * @see java.io.Externalizable#readExternal(ObjectInput)
184    */

185   public void readExternal(ObjectInput JavaDoc in)
186     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
187     super.readExternal(in);
188     _oid = (OID) in.readObject();
189     _methodName = (String JavaDoc) in.readObject();
190     _paramClasses = (ClassDescriptor[]) in.readObject();
191     _params = (Object JavaDoc[]) in.readObject();
192     _usesMarshalledObjects = in.readBoolean();
193   }
194
195   /**
196    * @see java.io.Externalizable#writeExternal(ObjectOutput)
197    */

198   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
199     super.writeExternal(out);
200     out.writeObject(_oid);
201     out.writeObject(_methodName);
202
203     if (_usesMarshalledObjects) {
204       if ((_params != null) && (_params.length > 0) &&
205             !(_params[0] instanceof MarshalledObject)) {
206         for (int i = 0; i < _params.length; i++) {
207           _params[i] = new MarshalledObject(_params[i], _vmId, _transportType, RmiUtils.CODE_BASE);
208         }
209       }
210     }
211
212     out.writeObject(_paramClasses);
213     out.writeObject(_params);
214     out.writeBoolean(_usesMarshalledObjects);
215   }
216
217   /**
218    * Internally converts the parameters of the method to call. Internally unmarshals
219    * the parameters if they are instances of <code>MarshalledObject</code>.
220    *
221    * @see MarshalledObject
222    */

223   protected void convertParams(ClassLoader JavaDoc loader)
224     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
225     _paramTypes = new Class JavaDoc[_paramClasses.length];
226
227     for (int i = 0; i < _paramClasses.length; i++) {
228       _paramTypes[i] = _paramClasses[i].resolve(loader);
229     }
230
231     if (_usesMarshalledObjects && (_params != null)) {
232       for (int i = 0; i < _params.length; i++) {
233         _params[i] = ((MarshalledObject) _params[i]).get(loader);
234       }
235     }
236   }
237 }
238
Popular Tags