KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > giop > ServerRequestListener


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

22
23 import java.io.IOException JavaDoc;
24 import java.util.*;
25
26 import org.apache.avalon.framework.logger.*;
27 import org.apache.avalon.framework.configuration.*;
28
29 import org.jacorb.orb.ORB;
30 import org.jacorb.orb.SystemExceptionHelper;
31 import org.jacorb.orb.dsi.ServerRequest;
32 import org.jacorb.poa.POA;
33 import org.jacorb.poa.POAConstants;
34 import org.jacorb.poa.util.POAUtil;
35
36 import org.omg.CONV_FRAME.CodeSetContext;
37 import org.omg.CORBA.CompletionStatus JavaDoc;
38 import org.omg.CORBA.NO_PERMISSION JavaDoc;
39 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc;
40 import org.omg.GIOP.LocateStatusType_1_2;
41 import org.omg.GIOP.ReplyStatusType_1_2;
42
43
44 /**
45  * ServerRequestListener.java
46  *
47  *
48  * Created: Sun Aug 12 22:26:25 2002
49  *
50  * @author Nicolas Noffke
51  * @version $Id: ServerRequestListener.java,v 1.20 2005/04/22 13:27:24 andre.spiegel Exp $
52  */

53
54 public class ServerRequestListener
55     implements RequestListener, Configurable
56 {
57     private ORB orb = null;
58     private POA rootPOA = null;
59
60     /** the configuration object */
61     private org.jacorb.config.Configuration configuration = null;
62     private Logger logger = null;
63     private boolean require_ssl = false;
64
65     public ServerRequestListener( org.omg.CORBA.ORB JavaDoc orb,
66                                   org.omg.PortableServer.POA JavaDoc rootPOA )
67     {
68         this.orb = (ORB)orb;
69         this.rootPOA = (POA)rootPOA;
70     }
71
72     public void configure(Configuration myConfiguration)
73         throws ConfigurationException
74     {
75         this.configuration = (org.jacorb.config.Configuration)myConfiguration;
76         logger =
77             configuration.getNamedLogger("jacorb.giop.server.listener");
78
79         boolean supportSSL =
80             configuration.getAttribute("jacorb.security.support_ssl","off").equals("on");
81
82         if( supportSSL )
83         {
84             int required =
85                 configuration.getAttributeAsInteger("jacorb.security.ssl.server.required_options",16);
86
87             //if we require EstablishTrustInTarget or
88
//EstablishTrustInClient, SSL must be used.
89
require_ssl = supportSSL && (required & 0x60) != 0;
90         }
91     }
92
93     public void requestReceived( byte[] request,
94                                  GIOPConnection connection )
95     {
96         RequestInputStream in =
97         new RequestInputStream( orb, request );
98
99         if( require_ssl && ! connection.isSSL() )
100         {
101             ReplyOutputStream out =
102                 new ReplyOutputStream( in.req_hdr.request_id,
103                                        ReplyStatusType_1_2.SYSTEM_EXCEPTION,
104                                        in.getGIOPMinor(),
105                                        false,
106                                        logger); //no locate reply
107

108             if (logger.isDebugEnabled())
109                 logger.debug("About to reject request because connection is not SSL.");
110
111             SystemExceptionHelper.write( out,
112                                          new NO_PERMISSION JavaDoc( 3, CompletionStatus.COMPLETED_NO ));
113
114             try
115             {
116                 connection.sendReply( out );
117             }
118             catch( IOException JavaDoc e )
119             {
120                 if (logger.isWarnEnabled())
121                     logger.warn("IOException",e);
122             }
123
124             return;
125         }
126
127         //only block timeouts, if a reply needs to be sent
128
if( Messages.responseExpected( in.req_hdr.response_flags ))
129         {
130             connection.incPendingMessages();
131         }
132
133         if( ! connection.isTCSNegotiated() )
134         {
135             //If GIOP 1.0 is used don't check for a codeset context
136
if( in.getGIOPMinor() == 0 )
137             {
138                 connection.markTCSNegotiated();
139             }
140             else
141             {
142                 CodeSetContext ctx =
143                 CodeSet.getCodeSetContext( in.req_hdr.service_context );
144
145                 if( ctx != null )
146                 {
147                     connection.setCodeSets( ctx.char_data, ctx.wchar_data );
148                     connection.markTCSNegotiated();
149                     if (logger.isDebugEnabled())
150                         logger.debug("Received CodeSetContext. Using " +
151                                      CodeSet.csName( ctx.char_data ) +
152                                      " as TCS and " +
153                                      CodeSet.csName( ctx.wchar_data ) +
154                                      " as TCSW" );
155                 }
156             }
157         }
158
159         in.setCodeSet( connection.getTCS(), connection.getTCSW() );
160
161         ServerRequest server_request = null;
162
163         try
164         {
165             server_request =
166             new ServerRequest( orb, in, connection );
167         }
168         catch( org.jacorb.poa.except.POAInternalError pie )
169         {
170             if (logger.isWarnEnabled())
171                 logger.warn("Received a request with a non-jacorb object key" );
172
173             if( in.isLocateRequest() )
174             {
175                 LocateReplyOutputStream lr_out =
176                 new LocateReplyOutputStream(in.req_hdr.request_id,
177                                             LocateStatusType_1_2._UNKNOWN_OBJECT,
178                                             in.getGIOPMinor() );
179
180                 try
181                 {
182                     connection.sendReply( lr_out );
183                 }
184                 catch( IOException JavaDoc e )
185                 {
186                     if (logger.isWarnEnabled())
187                         logger.warn("IOException",e);
188                 }
189             }
190             else
191             {
192                 ReplyOutputStream out =
193                     new ReplyOutputStream( in.req_hdr.request_id,
194                                            ReplyStatusType_1_2.SYSTEM_EXCEPTION,
195                                            in.getGIOPMinor(),
196                                            false,
197                                            logger );//no locate reply
198

199                 SystemExceptionHelper.write( out,
200                                              new OBJECT_NOT_EXIST JavaDoc( 0, CompletionStatus.COMPLETED_NO ));
201
202                 try
203                 {
204                     connection.sendReply( out );
205                 }
206                 catch( IOException JavaDoc e )
207                 {
208                     if (logger.isWarnEnabled())
209                         logger.warn("IOException",e);
210                 }
211             }
212
213             return;
214         }
215
216         deliverRequest( server_request );
217     }
218
219     public void locateRequestReceived ( byte[] request,
220                                         GIOPConnection connection )
221     {
222         //for the time being, map to normal request
223
requestReceived( request, connection );
224     }
225
226
227     public void cancelRequestReceived( byte[] request,
228                                        GIOPConnection connection )
229     {
230     }
231
232     private void deliverRequest( ServerRequest request )
233     {
234         POA tmp_poa = rootPOA;
235         String JavaDoc res;
236         List scopes;
237
238         try
239         {
240             // Get cached scopes from ServerRequest
241
scopes = request.getScopes();
242
243             for( int i = 0; i < scopes.size(); i++)
244             {
245                 res = ((String JavaDoc)scopes.get (i));
246
247                 if( res.equals(""))
248                     break;
249
250                 /* the following is a call to a method in the private
251                    interface between the ORB and the POA. It does the
252                    necessary synchronization between incoming,
253                    potentially concurrent requests to activate a POA
254                    using its adapter activator. This call will block
255                    until the correct POA is activated and ready to
256                    service requests. Thus, concurrent calls
257                    originating from a single, multi-threaded client
258                    will be serialized because the thread that accepts
259                    incoming requests from the client process is
260                    blocked. Concurrent calls from other destinations
261                    are not serialized unless they involve activating
262                    the same adapter.
263                 */

264
265                 try
266                 {
267                     tmp_poa = tmp_poa._getChildPOA( res );
268                 }
269                 catch ( org.jacorb.poa.except.ParentIsHolding p )
270                 {
271                     /* if one of the POAs is in holding state, we
272                        simply deliver deliver the request to this
273                        POA. It will forward the request to its
274                        child POAs if necessary when changing back
275                        to active For the POA to be able to forward
276                        this request to its child POAa, we need to
277                        supply the remaining part of the child's
278                        POA name */

279
280                     String JavaDoc [] rest_of_name = new String JavaDoc[scopes.size () - i];
281                     for( int j = 0; j < i; j++ )
282                     {
283                         rest_of_name[j] = (String JavaDoc)scopes.get( j+i );
284                     }
285
286                     request.setRemainingPOAName(rest_of_name);
287
288                     break;
289                 }
290             }
291
292             if( tmp_poa == null )
293             {
294                 throw new org.omg.CORBA.INTERNAL JavaDoc("Request POA null!");
295             }
296             else
297             {
298                 /* hand over to the POA */
299                 tmp_poa._invoke( request );
300             }
301         }
302         catch( org.omg.PortableServer.POAPackage.WrongAdapter JavaDoc wa )
303         {
304             // unknown oid (not previously generated)
305
request.setSystemException( new org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc("unknown oid") );
306             request.reply();
307         }
308         catch( org.omg.CORBA.SystemException JavaDoc one )
309         {
310             request.setSystemException( one );
311             request.reply();
312         }
313         catch( Throwable JavaDoc th )
314         {
315             request.setSystemException( new org.omg.CORBA.UNKNOWN JavaDoc( th.toString()) );
316             request.reply();
317             if (logger.isWarnEnabled())
318                 logger.warn("IOException",th);
319             // th.printStackTrace(); // TODO
320
}
321     }
322 }// ServerRequestListener
323
Popular Tags