KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > ClientInterceptorHandler


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.orb;
22
23 import java.util.Enumeration JavaDoc;
24
25 import org.apache.avalon.framework.logger.*;
26
27 import org.jacorb.orb.giop.*;
28 import org.jacorb.orb.portableInterceptor.*;
29
30 import org.omg.CORBA.portable.ApplicationException JavaDoc;
31 import org.omg.CORBA.portable.RemarshalException JavaDoc;
32 import org.omg.IOP.ServiceContext JavaDoc;
33 import org.omg.GIOP.ReplyHeader_1_2;
34 import org.omg.GIOP.ReplyStatusType_1_2;
35 import org.omg.PortableInterceptor.*;
36
37 /**
38  * An instance of this class handles all interactions between one particular
39  * client request and any interceptors registered for it.
40  *
41  * @author Andre Spiegel
42  * @version $Id: ClientInterceptorHandler.java,v 1.8 2004/06/09 05:36:59 andre.spiegel Exp $
43  */

44 public class ClientInterceptorHandler
45 {
46     private ClientRequestInfoImpl info = null;
47     private Logger logger;
48     
49     /**
50      * Constructs an interceptor handler for the given parameters.
51      * If no interceptors are registered on the client side,
52      * the resulting object will be a dummy object that does nothing when
53      * invoked.
54      */

55     public ClientInterceptorHandler
56                       ( org.jacorb.orb.ORB orb,
57                         org.jacorb.orb.giop.RequestOutputStream ros,
58                         org.omg.CORBA.Object JavaDoc self,
59                         org.jacorb.orb.Delegate delegate,
60                         org.jacorb.orb.ParsedIOR piorOriginal,
61                         org.jacorb.orb.giop.ClientConnection connection )
62     {
63         if ( orb.hasClientRequestInterceptors() )
64         {
65             info = new ClientRequestInfoImpl ( orb, ros, self, delegate,
66                                                piorOriginal, connection );
67         }
68         logger =
69             orb.getConfiguration().getNamedLogger("jacorb.orb.client_interceptors");
70     }
71     
72     public void handle_send_request() throws RemarshalException JavaDoc
73     {
74         if ( info != null )
75         {
76             invokeInterceptors ( info, ClientInterceptorIterator.SEND_REQUEST );
77             
78             // Add any new service contexts to the message
79
Enumeration JavaDoc ctx = info.getRequestServiceContexts();
80             
81             while ( ctx.hasMoreElements() )
82             {
83                 info.request_os.addServiceContext
84                                        ( ( ServiceContext JavaDoc ) ctx.nextElement() );
85             }
86         }
87     }
88
89     public void handle_location_forward ( ReplyInputStream reply,
90                                           org.omg.CORBA.Object JavaDoc forward_reference )
91         throws RemarshalException JavaDoc
92     {
93         if ( info != null )
94         {
95             info.setReplyStatus (LOCATION_FORWARD.value);
96             info.setReplyServiceContexts( reply.rep_hdr.service_context );
97
98             info.setForwardReference (forward_reference);
99
100             //allow interceptors access to reply input stream
101
info.reply_is = reply;
102
103             invokeInterceptors( info,
104                                 ClientInterceptorIterator.RECEIVE_OTHER );
105         }
106     }
107
108     public void handle_receive_reply ( ReplyInputStream reply )
109         throws RemarshalException JavaDoc
110     {
111         if ( info != null )
112         {
113             ReplyHeader_1_2 header = reply.rep_hdr;
114
115             if ( header.reply_status.value() == ReplyStatusType_1_2._NO_EXCEPTION )
116             {
117                 info.setReplyStatus (SUCCESSFUL.value);
118
119                 info.setReplyServiceContexts( header.service_context );
120
121                 // the case that invoke was called from
122
// dii.Request._invoke() will be handled inside
123
// of dii.Request._invoke() itself, because the
124
// result will first be available there
125
if ( info.request_os.getRequest() == null )
126                 {
127                     InterceptorManager manager = info.orb.getInterceptorManager();
128                     info.setCurrent (manager.getCurrent());
129
130                     //allow interceptors access to reply input stream
131
info.reply_is = reply;
132
133                     invokeInterceptors( info,
134                                         ClientInterceptorIterator.RECEIVE_REPLY );
135                 }
136                 else
137                     info.request_os.getRequest().setInfo( info );
138             }
139         }
140     }
141             
142     public void handle_receive_other ( short reply_status )
143         throws RemarshalException JavaDoc
144     {
145         if ( info != null )
146         {
147             info.setReplyStatus (reply_status);
148             invokeInterceptors ( info, ClientInterceptorIterator.RECEIVE_OTHER );
149         }
150     }
151
152     public void handle_receive_exception ( org.omg.CORBA.SystemException JavaDoc ex )
153         throws RemarshalException JavaDoc
154     {
155         handle_receive_exception ( ex, null );
156     }
157
158     public void handle_receive_exception ( org.omg.CORBA.SystemException JavaDoc ex,
159                                            ReplyInputStream reply )
160         throws RemarshalException JavaDoc
161     {
162         if ( info != null )
163         {
164             SystemExceptionHelper.insert ( info.received_exception, ex );
165             try
166             {
167                 info.received_exception_id =
168                     SystemExceptionHelper.type ( ex ).id();
169             }
170             catch ( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
171             {
172                 if (logger.isDebugEnabled())
173                     logger.debug("BadKind: " + bk.getMessage());
174             }
175             info.setReplyStatus (SYSTEM_EXCEPTION.value);
176
177             if ( reply != null )
178             {
179                 info.setReplyServiceContexts ( reply.rep_hdr.service_context );
180                 info.reply_is = reply;
181             }
182             
183             invokeInterceptors ( info,
184                                  ClientInterceptorIterator.RECEIVE_EXCEPTION );
185         }
186     }
187
188     public void handle_receive_exception ( ApplicationException JavaDoc ex,
189                                            ReplyInputStream reply )
190         throws RemarshalException JavaDoc
191     {
192         if ( info != null )
193         {
194             info.received_exception_id = ex.getId();
195             try
196             {
197                 ApplicationExceptionHelper.insert( info.received_exception,
198                                                    ex );
199             }
200             catch ( Exception JavaDoc e )
201             {
202                 if (logger.isDebugEnabled())
203                     logger.debug(e.getMessage());
204                 SystemExceptionHelper.insert ( info.received_exception,
205                                                new org.omg.CORBA.UNKNOWN JavaDoc
206                                                   ( e.getMessage() ) );
207             }
208             info.setReplyStatus (USER_EXCEPTION.value);
209
210             try
211             {
212                 reply.reset();
213             }
214             catch ( Exception JavaDoc e )
215             {
216                 // shouldn't happen anyway
217
if (logger.isWarnEnabled())
218                     logger.warn(e.getMessage());
219             }
220
221             info.setReplyServiceContexts ( reply.rep_hdr.service_context );
222             info.reply_is = reply;
223             
224             invokeInterceptors ( info,
225                                  ClientInterceptorIterator.RECEIVE_EXCEPTION );
226         }
227     }
228         
229     private void invokeInterceptors( ClientRequestInfoImpl info, short op )
230       throws RemarshalException JavaDoc
231     {
232         ClientInterceptorIterator intercept_iter =
233             info.orb.getInterceptorManager().getClientIterator();
234
235         try
236         {
237             intercept_iter.iterate( info, op );
238         }
239         catch ( org.omg.PortableInterceptor.ForwardRequest JavaDoc fwd )
240         {
241             info.delegate.rebind( info.orb.object_to_string( fwd.forward ) );
242             throw new RemarshalException JavaDoc();
243         }
244         catch ( org.omg.CORBA.UserException JavaDoc ue )
245         {
246             if (logger.isWarnEnabled())
247                 logger.warn("UserException: " + ue.getMessage());
248         }
249     }
250
251     
252 }
253
Popular Tags