KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > jrmp > interceptor > JServerInterceptorHelper


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: JServerInterceptorHelper.java,v 1.6 2004/09/01 11:02:41 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.jrmp.interceptor;
29
30 // java import
31
import java.io.IOException JavaDoc;
32 import java.io.ObjectInput JavaDoc;
33 import java.io.ObjectOutput JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import org.objectweb.carol.util.configuration.TraceCarol;
38
39 /**
40  * Class <code>JServerInterceptorHelper</code> is the CAROL JRMP Server
41  * Interceptor Helper this class is used by the other pakage class to manage
42  * server interception
43  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
44  * @version 1.0, 15/07/200
45  */

46 public class JServerInterceptorHelper extends JInterceptorHelper {
47
48     /**
49      * Thread Local for protocol context propagation
50      */

51     private static InheritableThreadLocal JavaDoc threadCtx = new InheritableThreadLocal JavaDoc();
52
53     /**
54      * Receive request
55      * @param ObjectInput in
56      * @param JServerRequestInterceptor All interceptor for this context
57      * @exception IOException if an exception occur with the ObjectOutput
58      */

59     public static void receive_request(ObjectInput JavaDoc in, JServerRequestInterceptor[] sis) throws IOException JavaDoc {
60         try {
61             int ctxValue = in.readInt();
62             JServerRequestInfo jsr = new JRMPServerRequestInfoImpl();
63             if ((sis == null) || (sis.length == 0)) {
64                 // no interceptions
65
if (TraceCarol.isDebugRmiCarol()) {
66                     TraceCarol.debugRmiCarol("JServerInterceptorHelper receive request without interceptors");
67                 }
68                 getRequestServerContextFromInput(in, ctxValue, jsr);
69             } else {
70                 // context and interception
71
if (TraceCarol.isDebugRmiCarol()) {
72                     TraceCarol.debugRmiCarol("JServerInterceptorHelper receive request contexts");
73                 }
74                 JServerRequestInfo ri = getRequestServerContextFromInput(in, ctxValue, jsr);
75                 for (int i = 0; i < sis.length; i++) {
76                     sis[i].receive_request(ri);
77                 }
78             }
79         } catch (ClassNotFoundException JavaDoc cnfe) {
80             throw new IOException JavaDoc("" + cnfe);
81         }
82     }
83
84     /**
85      * send reply with context
86      * @param ObjectOutput out
87      * @param JServerRequestInterceptor All interceptor for this context
88      * @exception IOException if an exception occur with the ObjectOutput
89      */

90     public static void send_reply(ObjectOutput JavaDoc out, JServerRequestInterceptor[] sis) throws IOException JavaDoc {
91         if ((sis == null) || (sis.length == 0)) {
92             if (TraceCarol.isDebugRmiCarol()) {
93                 TraceCarol.debugRmiCarol("JServerInterceptorHelper send reply without context");
94             }
95             // send no service context
96
out.writeInt(NO_CTX);
97         } else {
98             JServerRequestInfo jsr = new JRMPServerRequestInfoImpl();
99             for (int i = 0; i < sis.length; i++) {
100                 sis[i].send_reply(jsr);
101             }
102             setServerContextInOutput(out, jsr, isLocal());
103             threadCtx.set(null);
104         }
105         // flush and reset output stream for garbage collection
106
out.flush();
107     }
108
109     /**
110      * send exception with context
111      * @param ObjectOutput out
112      * @param JServerRequestInterceptor All interceptor for this context
113      * @exception IOException if an exception occur with the ObjectOutput
114      */

115     public static void send_exception(ObjectOutput JavaDoc out, JServerRequestInterceptor[] sis) throws IOException JavaDoc {
116         if ((sis == null) || (sis.length == 0)) {
117             if (TraceCarol.isDebugRmiCarol()) {
118                 TraceCarol.debugRmiCarol("JServerInterceptorHelper send exception without context");
119             }
120             // send no service context
121
out.writeInt(NO_CTX);
122         } else {
123             JServerRequestInfo jsr = new JRMPServerRequestInfoImpl();
124             if (TraceCarol.isDebugRmiCarol()) {
125                 TraceCarol.debugRmiCarol("JServerInterceptorHelper send exception contexts");
126             }
127             for (int i = 0; i < sis.length; i++) {
128                 sis[i].send_exception(jsr);
129             }
130             setServerContextInOutput(out, jsr, isLocal());
131             threadCtx.set(null);
132         }
133         // flush and reset output stream for garbage collection
134
out.flush();
135     }
136
137     /*
138      * send other with context @param ObjectOutput out @param
139      * JServerRequestInterceptor All interceptor for this context @exception
140      * IOException if an exception occur with the ObjectOutput
141      */

142     public static void send_other(ObjectOutput JavaDoc out, JServerRequestInterceptor[] sis) throws IOException JavaDoc {
143         if ((sis == null) || (sis.length == 0)) {
144             if (TraceCarol.isDebugRmiCarol()) {
145                 TraceCarol.debugRmiCarol("JServerInterceptorHelper send other with no contexts");
146             }
147             // send no service context
148
out.writeInt(NO_CTX);
149         } else {
150             JServerRequestInfo jsr = new JRMPServerRequestInfoImpl();
151             for (int i = 0; i < sis.length; i++) {
152                 sis[i].send_other(jsr);
153             }
154             setServerContextInOutput(out, jsr, isLocal());
155             threadCtx.set(null);
156         }
157         // flush and reset output stream for garbage collection
158
out.flush();
159     }
160
161     /**
162      * Get Context from Object Input
163      * @param ObjectInput in the object input stream
164      * @param int the context value
165      * @param boolean do not build Request Info
166      */

167     public static JServerRequestInfo getRequestServerContextFromInput(ObjectInput JavaDoc in, int ctxValue,
168             JServerRequestInfo jsr) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
169         if (ctxValue == NO_CTX) {
170             if (TraceCarol.isDebugRmiCarol()) {
171                 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive no context");
172             }
173             return jsr;
174         } else if (ctxValue == REMOTE_CTX) {
175             int sz = in.readInt();
176             for (int i = 0; i < sz; i++) {
177                 jsr.add_reply_service_context((JServiceContext) in.readObject());
178             }
179             // remote context
180
if (TraceCarol.isDebugRmiCarol()) {
181                 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive remote contexts");
182                 // print the contexts sended
183
for (Iterator JavaDoc i = jsr.get_all_reply_service_context().iterator(); i.hasNext();) {
184                     TraceCarol.debugRmiCarol("ctx:" + i.next());
185                 }
186             }
187             return jsr;
188         } else if (ctxValue == LOCAL_CTX) {
189             setLocal();
190             // local context case
191
int id = in.readInt();
192             jsr.add_all_reply_service_context((Collection JavaDoc) JContextStore.getObject(id));
193             // local context
194
if (TraceCarol.isDebugRmiCarol()) {
195                 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive local contexts id(" + id + ")");
196                 // print the contexts sended
197
for (Iterator JavaDoc i = jsr.get_all_reply_service_context().iterator(); i.hasNext();) {
198                     TraceCarol.debugRmiCarol("ctx:" + i.next());
199                 }
200             }
201             return jsr;
202         } else {
203             throw new IOException JavaDoc("Unknow context type:" + ctxValue);
204         }
205     }
206
207     /**
208      * Set Context inObject Outut
209      * @param ObjectOutput in the object OutPutStream
210      * @param int the context value
211      * @param boolean do not build Request Info
212      */

213     public static void setServerContextInOutput(ObjectOutput JavaDoc out, JServerRequestInfo ri, boolean locRef)
214             throws IOException JavaDoc {
215         if (!ri.hasContexts()) {
216             if (TraceCarol.isDebugRmiCarol()) {
217                 TraceCarol.debugRmiCarol("JServerInterceptorHelper send without contexts");
218             }
219             // send no service context
220
out.writeInt(NO_CTX);
221         } else if (locRef) {
222             Object JavaDoc ctx = ri.get_all_reply_service_context();
223             int k = JContextStore.storeObject(ctx);
224             out.writeInt(LOCAL_CTX);
225             out.writeInt(k);
226             if (TraceCarol.isDebugRmiCarol()) {
227                 TraceCarol.debugRmiCarol("JServerInterceptorHelper send with local contexts id(" + k + ")");
228             }
229             // send local service context
230
} else {
231             if (TraceCarol.isDebugRmiCarol()) {
232                 TraceCarol.debugRmiCarol("JServerInterceptorHelper send with remote contexts");
233             }
234             // send remotes service context
235
out.writeInt(REMOTE_CTX);
236             Collection JavaDoc allCtx = ri.get_all_reply_service_context();
237             out.writeInt(allCtx.size());
238             for (Iterator JavaDoc i = allCtx.iterator(); i.hasNext();) {
239                 out.writeObject(i.next());
240             }
241         }
242     }
243
244     /**
245      * Set Local Reference
246      */

247     public static void setLocal() {
248         threadCtx.set("local");
249     }
250
251     /**
252      * is Local Reference
253      * @return true if local reference
254      */

255     public static boolean isLocal() {
256         return (threadCtx.get() != null);
257     }
258
259 }
Popular Tags