KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > jrmp > server > JUnicastRef


1 /**
2  * Copyright (C) 2002,2004 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: JUnicastRef.java,v 1.11 2004/09/01 11:02:41 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.jrmp.server;
29
30 //sun import
31
import java.io.IOException JavaDoc;
32 import java.io.ObjectInput JavaDoc;
33 import java.io.ObjectOutput JavaDoc;
34 import java.rmi.MarshalException JavaDoc;
35 import java.rmi.Remote JavaDoc;
36 import java.rmi.RemoteException JavaDoc;
37 import java.rmi.UnmarshalException JavaDoc;
38 import java.rmi.server.Operation JavaDoc;
39 import java.rmi.server.RemoteCall JavaDoc;
40 import java.rmi.server.RemoteObject JavaDoc;
41 import java.rmi.server.UID JavaDoc;
42 import java.util.Arrays JavaDoc;
43
44 import org.objectweb.carol.rmi.jrmp.interceptor.JClientInterceptorHelper;
45 import org.objectweb.carol.rmi.jrmp.interceptor.JClientRequestInterceptor;
46 import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorHelper;
47 import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorStore;
48
49 import sun.rmi.server.UnicastRef;
50 import sun.rmi.transport.Connection;
51 import sun.rmi.transport.LiveRef;
52 import sun.rmi.transport.StreamRemoteCall;
53
54 /**
55  * Class <code>JUnicastRef</code> is the CAROL JRMP UnicastRef with context
56  * propagation
57  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
58  * @version 1.0, 15/07/2002 Unicast Reference ensuring context propagation
59  */

60 public class JUnicastRef extends UnicastRef {
61
62     /**
63      * flag for local server
64      */

65     private transient boolean localRef = false;
66
67     /**
68      * flag for InetAddress
69      */

70     private transient byte[] raddr = null;
71
72     /**
73      * flag for UID
74      */

75     private transient UID JavaDoc ruid = null;
76
77     /**
78      * Client Interceptor for context propagation
79      */

80     protected transient JClientRequestInterceptor[] cis = null;
81
82     /**
83      * Interceptors initialisers for this References
84      */

85     protected transient String JavaDoc[] initializers = null;
86
87     private transient int localId = -2;
88
89     /**
90      * empty constructor
91      */

92     public JUnicastRef() {
93     }
94
95     /**
96      * Constructor without Interceptor
97      * @param liveRef the live reference
98      */

99     public JUnicastRef(LiveRef liveRef) {
100         super(liveRef);
101     }
102
103     /**
104      * Constructor with interceptor
105      * @param liveRef the live reference
106      * @param cis the client interceptor array
107      * @param int localId
108      */

109     public JUnicastRef(LiveRef liveRef, JClientRequestInterceptor[] cis, String JavaDoc[] initial, int local) {
110         super(liveRef);
111         this.initializers = initial;
112         this.cis = cis;
113         this.raddr = JInterceptorHelper.getInetAddress();
114         this.ruid = JInterceptorHelper.getSpaceID();
115         this.localId = local;
116     }
117
118     /**
119      * get the ref class always return null
120      * @param out the output stream
121      */

122     public String JavaDoc getRefClass(ObjectOutput JavaDoc out) {
123         super.getRefClass(out);
124         return null;
125     }
126
127     /**
128      * wrap the call to send the contexts
129      * @param obj the remote object to invoke
130      * @param method the method to invoque
131      * @param params the methid parametters
132      * @param opnum the operation number
133      */

134     public Object JavaDoc invoke(Remote JavaDoc obj, java.lang.reflect.Method JavaDoc method, Object JavaDoc[] params, long opnum) throws Exception JavaDoc {
135         if ((localRef) && (localId != -2)) {
136             // local call on the object
137
//System.out.println("local call on object id:"+localId);
138
return method.invoke(JLocalObjectStore.getObject(localId), params);
139         } else {
140             //System.out.println("remote (local ref="+localRef+") call on
141
// object id:"+localId);
142
Connection conn = ref.getChannel().newConnection();
143             java.rmi.server.RemoteCall JavaDoc call = null;
144             boolean reuse = true;
145             boolean alreadyFreed = false;
146
147             try {
148                 call = new JRemoteCall(conn, ref.getObjID(), -1, opnum, cis);
149                 try {
150                     ObjectOutput JavaDoc out = call.getOutputStream();
151                     marshalCustomCallData(out);
152                     Class JavaDoc[] types = method.getParameterTypes();
153                     for (int i = 0; i < types.length; i++) {
154                         marshalValue(types[i], params[i], out);
155                     }
156                 } catch (IOException JavaDoc e) {
157                     throw new MarshalException JavaDoc("error marshalling arguments" + e);
158                 }
159
160                 // unmarshal return
161
call.executeCall();
162
163                 try {
164                     Class JavaDoc rtype = method.getReturnType();
165                     if (rtype == void.class) {
166                         return null;
167                     }
168                     ObjectInput JavaDoc in = call.getInputStream();
169                     Object JavaDoc returnValue = unmarshalValue(rtype, in);
170                     alreadyFreed = true;
171                     ref.getChannel().free(conn, true);
172
173                     return returnValue;
174
175                 } catch (IOException JavaDoc e) {
176                     throw new UnmarshalException JavaDoc("IOException unmarshalling return" + e);
177                 } catch (ClassNotFoundException JavaDoc e) {
178                     throw new UnmarshalException JavaDoc("ClassNotFoundException unmarshalling return" + e);
179                 } finally {
180                     try {
181                         call.done();
182                     } catch (IOException JavaDoc e) {
183                         reuse = false;
184                     }
185                 }
186
187             } catch (RuntimeException JavaDoc e) {
188                 if ((call == null) || (((StreamRemoteCall) call).getServerException() != e)) {
189                     reuse = false;
190                 }
191                 throw e;
192
193             } catch (RemoteException JavaDoc e) {
194                 reuse = false;
195                 throw e;
196
197             } catch (Error JavaDoc e) {
198                 reuse = false;
199                 throw e;
200
201             } finally {
202                 if (!alreadyFreed) {
203                     ref.getChannel().free(conn, reuse);
204                 }
205             }
206         }
207     }
208
209     /**
210      * v1.1 style of Stubs call this invoke
211      * @param call the remote call
212      * @deprecated
213      */

214     public void invoke(java.rmi.server.RemoteCall JavaDoc call) throws Exception JavaDoc {
215         super.invoke(call);
216     }
217
218     /**
219      * override v1.1 RemoteCall instanciation to wrap the call and send the
220      * context
221      * @param obj the remote object
222      * @param ops the operation
223      * @param opnum the operation number
224      * @param hash the hash code
225      * @deprecated
226      */

227     public RemoteCall JavaDoc newCall(RemoteObject JavaDoc obj, Operation JavaDoc[] ops, int opnum, long hash) throws RemoteException JavaDoc {
228
229         Connection conn = ref.getChannel().newConnection();
230         try {
231             RemoteCall JavaDoc call = new JRemoteCall(conn, ref.getObjID(), opnum, hash, cis);
232             try {
233                 marshalCustomCallData(call.getOutputStream());
234             } catch (IOException JavaDoc e) {
235                 throw new MarshalException JavaDoc("error marshaling " + "custom call data");
236             }
237             return call;
238         } catch (RemoteException JavaDoc e) {
239             ref.getChannel().free(conn, false);
240             throw e;
241         }
242     }
243
244     /**
245      * override marshalCustomCallData to pass and disociate contexts on the
246      * client
247      * @param out the ObjectOutput for the call marchalling
248      */

249     protected void marshalCustomCallData(ObjectOutput JavaDoc out) throws IOException JavaDoc {
250         JClientInterceptorHelper.send_request(out, cis, localRef);
251         super.marshalCustomCallData(out);
252     }
253
254     /**
255      * override readExternal to initialise localRef We could actually receive
256      * anything from the server on lookup
257      * @param in the ObjectInput
258      */

259     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
260         readExternal(in, false);
261     }
262
263     /**
264      * override writeExternal to send spaceID We could actually send anything to
265      * the client on lookup
266      * @param out the object output stream
267      */

268     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
269         writeExternal(out, false);
270     }
271
272     /**
273      * override readExternal to initialise localRef We could actually receive
274      * anything from the server on lookup
275      * @param in the object input
276      * @param newFormat the new format boolean
277      */

278     public void readExternal(ObjectInput JavaDoc in, boolean newFormat) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
279         raddr = new byte[in.readInt()];
280         in.read(raddr);
281         ruid = UID.read(in);
282         localRef = ((Arrays.equals(raddr, JInterceptorHelper.getInetAddress())) && (ruid.equals(JInterceptorHelper
283                 .getSpaceID())));
284         // write initializers array in UTF
285
String JavaDoc[] ia = new String JavaDoc[in.readInt()];
286         for (int i = 0; i < ia.length; i++) {
287             ia[i] = in.readUTF();
288         }
289         this.initializers = ia;
290         cis = JInterceptorStore.setRemoteInterceptors(raddr, ruid, ia);
291         localId = in.readInt();
292         ref = LiveRef.read(in, newFormat);
293     }
294
295     /**
296      * override writeExternal to send spaceID And the interceptor We could
297      * actually send anything to the client on lookup
298      * @param out the object output
299      * @param newFormat the boolean new format
300      */

301     public void writeExternal(ObjectOutput JavaDoc out, boolean newFormat) throws IOException JavaDoc {
302         out.writeInt(raddr.length);
303         out.write(raddr);
304         ruid.write(out);
305         // write initializers array in UTF
306
String JavaDoc[] ia = this.initializers;
307         out.writeInt(ia.length);
308         for (int i = 0; i < ia.length; i++) {
309             out.writeUTF(ia[i]);
310         }
311         // write the local identificator
312
out.writeInt(getLocalId());
313         ref.write(out, newFormat);
314         out.flush();
315     }
316
317     /**
318      * @return
319      */

320     public int getLocalId() {
321         // TODO Auto-generated method stub
322
return localId;
323     }
324 }
Popular Tags