KickJava   Java API By Example, From Geeks To Geeks.

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


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: JClientInterceptorHelper.java,v 1.8 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>JClientInterceptorHelper</code> is the CAROL JRMP Client
41  * Interceptor Helper this class is used by the other pakage class to manage
42  * client interception
43  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
44  * @version 1.0, 15/07/2002
45  */

46 public class JClientInterceptorHelper extends JInterceptorHelper {
47
48     /**
49      * send client context with the request. The sendingRequest method of the
50      * PortableInterceptors is called prior to marshalling arguments and
51      * contexts
52      * @param ObjectOutput out
53      * @param ClientRequestInterceptor All interceptor for this context
54      * @exception IOException if an exception occur with the ObjectOutput
55      */

56     public static void send_request(ObjectOutput JavaDoc out, JClientRequestInterceptor[] cis, boolean localRef)
57             throws IOException JavaDoc {
58         if ((cis == null) || (cis.length == 0)) {
59             if (TraceCarol.isDebugRmiCarol()) {
60                 TraceCarol.debugRmiCarol("JClientInterceptorHelper send request without interceptors");
61             }
62             // send no service context
63
out.writeInt(NO_CTX);
64         } else {
65             JClientRequestInfo jrc = new JRMPClientRequestInfoImpl();
66             for (int i = 0; i < cis.length; i++) {
67                 cis[i].send_request(jrc);
68             }
69             setClientContextInOutput(out, jrc, localRef);
70         }
71         // flush and reset output stream for garbage collection
72
out.flush();
73     }
74
75     /**
76      * send client context in pool (see CORBA Specifications)
77      * @param ObjectOutput out
78      * @param JClientRequestInterceptor All interceptor for this context
79      * @exception IOException if an exception occur with the ObjectOutput
80      */

81     public static void send_poll(ObjectOutput JavaDoc out, JClientRequestInterceptor[] cis, boolean localRef)
82             throws IOException JavaDoc {
83     }
84
85     /**
86      * Receive reply interception
87      * @param ObjectInput in
88      * @param ClientRequestInterceptor All interceptor for this context
89      * @exception IOException if an exception occur with the ObjectOutput
90      */

91     public static void receive_reply(ObjectInput JavaDoc in, JClientRequestInterceptor[] cis) throws IOException JavaDoc {
92         try {
93             int ctxValue = in.readInt();
94             JClientRequestInfo jrc = new JRMPClientRequestInfoImpl();
95             if ((cis == null) || (cis.length == 0)) {
96                 // no interceptions
97
if (TraceCarol.isDebugRmiCarol()) {
98                     TraceCarol.debugRmiCarol("JClientInterceptorHelper receive reply without interceptors");
99                 }
100                 getClientRequestContextFromInput(in, ctxValue, jrc);
101             } else {
102                 // context and interception
103
if (TraceCarol.isDebugRmiCarol()) {
104                     TraceCarol.debugRmiCarol("JClientInterceptorHelper receive reply contexts");
105                 }
106                 JClientRequestInfo ri = getClientRequestContextFromInput(in, ctxValue, jrc);
107                 for (int i = 0; i < cis.length; i++) {
108                     cis[i].receive_reply(ri);
109                 }
110             }
111         } catch (ClassNotFoundException JavaDoc cnfe) {
112             throw new IOException JavaDoc("" + cnfe);
113         }
114     }
115
116     /**
117      * Receive exception interception
118      * @param ObjectInput in
119      * @param ClientRequestInterceptor All interceptor for this context
120      * @exception IOException if an exception occur with the ObjectOutput
121      */

122     public static void receive_exception(ObjectInput JavaDoc in, JClientRequestInterceptor[] cis) throws IOException JavaDoc {
123         try {
124             int ctxValue = in.readInt();
125             JClientRequestInfo jrc = new JRMPClientRequestInfoImpl();
126             if ((cis == null) || (cis.length == 0)) {
127                 // no interceptions
128
if (TraceCarol.isDebugRmiCarol()) {
129                     TraceCarol.debugRmiCarol("JClientInterceptorHelper receive exception without interceptors");
130                 }
131                 getClientRequestContextFromInput(in, ctxValue, jrc);
132             } else {
133                 // context and interception
134
if (TraceCarol.isDebugRmiCarol()) {
135                     TraceCarol.debugRmiCarol("JClientInterceptorHelper receive exception contexts");
136                 }
137                 JClientRequestInfo ri = getClientRequestContextFromInput(in, ctxValue, jrc);
138                 for (int i = 0; i < cis.length; i++) {
139                     cis[i].receive_exception(ri);
140                 }
141             }
142         } catch (ClassNotFoundException JavaDoc cnfe) {
143             throw new IOException JavaDoc("" + cnfe);
144         }
145     }
146
147     /*
148      * Receive other interception
149      * @deprecated @param ObjectInput in @param ClientRequestInterceptor All
150      * interceptor for this context @exception IOException if an
151      * exception occur with the ObjectOutput
152      */

153     public static void receive_other(ObjectInput JavaDoc in, JClientRequestInterceptor[] cis) throws IOException JavaDoc {
154         try {
155             int ctxValue = in.readInt();
156             JClientRequestInfo jrc = new JRMPClientRequestInfoImpl();
157             if ((cis == null) || (cis.length == 0)) {
158                 // no interceptions
159
if (TraceCarol.isDebugRmiCarol()) {
160                     TraceCarol.debugRmiCarol("JClientInterceptorHelper receive other without interceptors");
161                 }
162                 getClientRequestContextFromInput(in, ctxValue, jrc);
163             } else {
164                 // context and interception
165
if (TraceCarol.isDebugRmiCarol()) {
166                     TraceCarol.debugRmiCarol("JClientInterceptorHelper receive other contexts");
167                 }
168                 JClientRequestInfo ri = getClientRequestContextFromInput(in, ctxValue, jrc);
169                 for (int i = 0; i < cis.length; i++) {
170                     cis[i].receive_other(ri);
171                 }
172             }
173         } catch (ClassNotFoundException JavaDoc cnfe) {
174             throw new IOException JavaDoc("" + cnfe);
175         }
176     }
177
178     /**
179      * Get Context from Object Input
180      * @param ObjectInput in the object input stream
181      * @param int the context value
182      * @param boolean do not build Request Info
183      * @return jrc the client request info
184      */

185     public static JClientRequestInfo getClientRequestContextFromInput(ObjectInput JavaDoc in, int ctxValue,
186             JClientRequestInfo jrc) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
187         if (ctxValue == NO_CTX) {
188             if (TraceCarol.isDebugRmiCarol()) {
189                 TraceCarol.debugRmiCarol("JClientInterceptorHelper getObjectFromInput no context");
190             }
191             return jrc;
192         } else if (ctxValue == REMOTE_CTX) {
193             if (TraceCarol.isDebugRmiCarol()) {
194                 TraceCarol.debugRmiCarol("JClientInterceptorHelper getObjectFromInput remote");
195             }
196             int sz = in.readInt();
197             for (int i = 0; i < sz; i++) {
198                 jrc.add_request_service_context((JServiceContext) in.readObject());
199             }
200             return jrc;
201         } else if (ctxValue == LOCAL_CTX) {
202             // local context case
203
int id = in.readInt();
204             // local context
205
if (TraceCarol.isDebugRmiCarol()) {
206                 TraceCarol.debugRmiCarol("JClientInterceptorHelper getObjectFromInput local id(" + id + ")");
207             }
208             jrc.add_all_request_service_context((Collection JavaDoc) JContextStore.getObject(id));
209             return jrc;
210         } else {
211             throw new IOException JavaDoc("Unknow context type:" + ctxValue);
212         }
213     }
214
215     /**
216      * Set Context inObject Outut
217      * @param ObjectOutput in the object OutPutStream
218      * @param int the context value
219      * @param boolean do not build Request Info
220      */

221     public static void setClientContextInOutput(ObjectOutput JavaDoc out, JClientRequestInfo ri, boolean locRef)
222             throws IOException JavaDoc {
223         if (!ri.hasContexts()) {
224             if (TraceCarol.isDebugRmiCarol()) {
225                 TraceCarol.debugRmiCarol("JClientInterceptorHelper send request without contexts");
226             }
227             // send no service context
228
out.writeInt(NO_CTX);
229         } else if (locRef) {
230             Object JavaDoc ctx = ri.get_all_request_service_context();
231             int k = JContextStore.storeObject(ctx);
232             out.writeInt(LOCAL_CTX);
233             out.writeInt(k);
234             if (TraceCarol.isDebugRmiCarol()) {
235                 TraceCarol.debugRmiCarol("JClientInterceptorHelper send request with local contexts id(" + k + ")");
236                 // print the contexts sended
237
for (Iterator JavaDoc i = ((Collection JavaDoc) ctx).iterator(); i.hasNext();) {
238                     TraceCarol.debugRmiCarol("ctx:" + i.next());
239                 }
240             }
241         } else {
242             // send remotes service context
243
out.writeInt(REMOTE_CTX);
244             Collection JavaDoc allCtx = ri.get_all_request_service_context();
245             out.writeInt(allCtx.size());
246             for (Iterator JavaDoc i = allCtx.iterator(); i.hasNext();) {
247                 out.writeObject(i.next());
248             }
249             if (TraceCarol.isDebugRmiCarol()) {
250                 TraceCarol.debugRmiCarol("JClientInterceptorHelper send request with remote contexts");
251                 // print the contexts sended
252
for (Iterator JavaDoc i = allCtx.iterator(); i.hasNext();) {
253                     TraceCarol.debugRmiCarol("ctx:" + i.next());
254                 }
255             }
256         }
257     }
258 }
Popular Tags