KickJava   Java API By Example, From Geeks To Geeks.

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


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: JUnicastServerRef.java,v 1.7 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.Remote JavaDoc;
35 import java.rmi.RemoteException JavaDoc;
36 import java.rmi.server.RemoteCall JavaDoc;
37 import java.rmi.server.RemoteRef JavaDoc;
38
39 import org.objectweb.carol.rmi.jrmp.interceptor.JClientRequestInterceptor;
40 import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorStore;
41 import org.objectweb.carol.rmi.jrmp.interceptor.JServerInterceptorHelper;
42 import org.objectweb.carol.rmi.jrmp.interceptor.JServerRequestInterceptor;
43
44 import sun.rmi.server.UnicastServerRef;
45 import sun.rmi.transport.LiveRef;
46
47 /**
48  * Class <code>JUnicastServerRef</code> implements the remote reference layer
49  * server-side behavior for remote objects exported with the JUnicastRef
50  * reference type.
51  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
52  * @version 1.0, 15/07/2002
53  */

54 public class JUnicastServerRef extends UnicastServerRef {
55
56     /**
57      * ServerRequestInterceptor array
58      */

59     protected JServerRequestInterceptor[] sis = null;
60
61     /**
62      * ClientRequestInterceptor array
63      */

64     protected JClientRequestInterceptor[] cis = null;
65
66     private int localId = -2;
67
68     /**
69      * constructor
70      */

71     public JUnicastServerRef() {
72     }
73
74     /**
75      * Constructor with interceptor
76      * @param ref the live reference
77      * @param sis the server interceptor array
78      * @param cis the client interceptor array
79      */

80     public JUnicastServerRef(LiveRef ref, JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis) {
81         super(ref);
82         this.sis = sis;
83         this.cis = cis;
84     }
85
86     /**
87      * Constructor with interceptor
88      * @param port the port reference
89      * @param sis the server interceptor array
90      * @param cis the client interceptor array
91      */

92     public JUnicastServerRef(int port, JServerRequestInterceptor[] sis, JClientRequestInterceptor[] cis) {
93         super(new LiveRef(port));
94         this.sis = sis;
95         this.cis = cis;
96     }
97
98     /**
99      * get the ref class name
100      * @return String the class name
101      */

102     public String JavaDoc getRefClass(ObjectOutput JavaDoc out) {
103         super.getRefClass(out);
104         return "org.objectweb.carol.rmi.jrmp.server.JUnicastServerRef";
105     }
106
107     /**
108      * use a different kind of RemoteRef instance. This method is used by the
109      * remote client to get the Client reference
110      * @return remote Ref the remote reference
111      */

112     protected RemoteRef JavaDoc getClientRef() {
113         return new JUnicastRef(ref, cis, JInterceptorStore.getJRMPInitializers(), localId);
114     }
115
116     /**
117      * @param obj
118      * @param localId
119      * @param object
120      * @return
121      */

122     public Remote JavaDoc exportObject(Remote JavaDoc obj, Object JavaDoc object, int localId) throws RemoteException JavaDoc {
123         this.localId = localId;
124         return super.exportObject(obj, object);
125     }
126
127     /**
128      * override unmarshalCustomCallData to receive and establish contexts sent
129      * by the client
130      * @param in the object input
131      */

132     protected void unmarshalCustomCallData(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
133         JServerInterceptorHelper.receive_request(in, sis);
134         super.unmarshalCustomCallData(in);
135     }
136
137     /**
138      * override dispatch to use a specific thread factory
139      * @param obj the remote object
140      * @param call the remote call on this object
141      */

142     public void dispatch(Remote JavaDoc obj, RemoteCall JavaDoc call) throws IOException JavaDoc {
143         JUnicastThreadFactory factory = JUnicastRemoteObject.getDefaultThreadFactory();
144         if (factory == null) {
145             runDispatch(obj, call);
146         } else {
147             DispatchRunnable dr = new DispatchRunnable(obj, call);
148             factory.getThread(dr).run(); // run the target
149
if (dr.getIOException() != null) throw dr.getIOException();
150         }
151     }
152
153     /**
154      * method used to invoke <code>super.dispatch</code> and wrap the call to
155      * ensure invocation of context propagators.
156      * @param obj the remote object
157      * @param call the remote call on this object
158      */

159     private void runDispatch(Remote JavaDoc obj, RemoteCall JavaDoc call) throws IOException JavaDoc {
160         super.dispatch(obj, new JRemoteServerCall(call, sis));
161     }
162
163     /**
164      * Class used to run dispatch in a separated thread
165      */

166     private class DispatchRunnable implements Runnable JavaDoc {
167
168         /**
169          * the remote object
170          */

171         Remote JavaDoc obj;
172
173         /**
174          * the remote call
175          */

176         RemoteCall JavaDoc call;
177
178         /**
179          * the exception (IOException)
180          */

181         IOException JavaDoc e = null;
182
183         /**
184          * method used to invoke <code>super.dispatch</code> and wrap the call
185          * to ensure invocation of context propagators.
186          * @param obj the remote object
187          * @param call the remote call on this object
188          */

189         public DispatchRunnable(Remote JavaDoc obj, RemoteCall JavaDoc call) {
190             this.obj = obj;
191             this.call = call;
192         }
193
194         /**
195          * thread run method
196          */

197         public void run() {
198             try {
199                 runDispatch(obj, call);
200             } catch (IOException JavaDoc e) {
201                 this.e = e;
202             }
203         }
204
205         /**
206          * Exception builder
207          */

208         public IOException JavaDoc getIOException() {
209             return e;
210         }
211     }
212 }
Popular Tags