KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-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.lang.reflect.Constructor JavaDoc;
24 import java.net.*;
25 import java.util.*;
26
27 import org.jacorb.orb.giop.*;
28 import org.jacorb.orb.iiop.*;
29 import org.jacorb.orb.factory.SSLServerSocketFactory;
30 import org.jacorb.orb.factory.ServerSocketFactory;
31 import org.jacorb.orb.factory.SocketFactory;
32 import org.jacorb.orb.factory.SocketFactoryManager;
33 import org.jacorb.util.ObjectUtil;
34
35 import org.apache.avalon.framework.logger.*;
36 import org.apache.avalon.framework.configuration.*;
37
38 import org.omg.CORBA.INTERNAL JavaDoc;
39 import org.omg.ETF.*;
40 import org.omg.PortableServer.POA JavaDoc;
41
42 /**
43  *
44  * Class BasicAdapter, used by the POA.
45  *
46  * @author Gerald Brose, FU Berlin
47  * @version $Id: BasicAdapter.java,v 1.45 2005/02/09 09:52:28 andre.spiegel Exp $
48  */

49
50 public class BasicAdapter
51     extends org.omg.ETF._HandleLocalBase
52     implements Configurable
53 {
54     public SSLServerSocketFactory ssl_socket_factory = null;
55     private ServerSocketFactory socket_factory = null;
56
57     private org.jacorb.orb.ORB orb;
58     private POA JavaDoc rootPOA;
59
60     private List listeners = new ArrayList();
61
62     private MessageReceptorPool receptor_pool = null;
63     private ServerRequestListener request_listener = null;
64     private ReplyListener reply_listener = null;
65
66     private TransportManager transport_manager = null;
67     private GIOPConnectionManager giop_connection_manager = null;
68
69     /** the configuration object */
70     private org.jacorb.config.Configuration configuration = null;
71     private Logger logger = null;
72
73     /**
74      * called from ORB.java
75      */

76
77     BasicAdapter( org.jacorb.orb.ORB orb,
78                   POA JavaDoc rootPOA,
79                   TransportManager transport_manager,
80                   GIOPConnectionManager giop_connection_manager )
81     {
82         this.orb = orb;
83         this.rootPOA = rootPOA;
84         this.transport_manager = transport_manager;
85         this.giop_connection_manager = giop_connection_manager;
86     }
87
88     /**
89      * configure the BasicAdapter
90      */

91
92     public void configure(Configuration myConfiguration)
93         throws ConfigurationException
94     {
95         this.configuration =
96             (org.jacorb.config.Configuration)myConfiguration;
97         logger =
98             configuration.getNamedLogger("jacorb.orb.basic");
99
100         socket_factory =
101             transport_manager.getSocketFactoryManager().getServerSocketFactory();
102
103         if( configuration.getAttribute("jacorb.security.support_ssl","off").equals("on"))
104         {
105             if( ssl_socket_factory == null )
106             {
107                 String JavaDoc s =
108                     configuration.getAttribute( "jacorb.ssl.server_socket_factory","" );
109                 if( s.length() == 0 )
110                 {
111                     throw new org.omg.CORBA.INITIALIZE JavaDoc( "SSL support is on, but the property \"jacorb.ssl.server_socket_factory\" is not set!" );
112                 }
113
114                 try
115                 {
116                     Class JavaDoc ssl = ObjectUtil.classForName( s );
117
118                     Constructor JavaDoc constr =
119                         ssl.getConstructor( new Class JavaDoc[]{org.jacorb.orb.ORB.class });
120
121                     ssl_socket_factory =
122                         (SSLServerSocketFactory)constr.newInstance( new Object JavaDoc[]{ orb });
123
124                     ((Configurable)ssl_socket_factory).configure(configuration);
125                 }
126                 catch (Exception JavaDoc e)
127                 {
128                     logger.warn("Exception",e);
129
130                     throw new org.omg.CORBA.INITIALIZE JavaDoc( "SSL support is on, but the ssl server socket factory can't be instanciated (see trace)!" );
131                 }
132             }
133
134         }
135
136         receptor_pool = MessageReceptorPool.getInstance(myConfiguration);
137
138         request_listener = new ServerRequestListener( orb, rootPOA );
139         request_listener.configure( configuration );
140         reply_listener = new NoBiDirServerReplyListener();
141
142         // create all Listeners
143
for (Iterator i = getListenerFactories().iterator();
144              i.hasNext();)
145         {
146              Factories f = (Factories)i.next();
147              Listener l = f.create_listener (null, (short)0, (short)0);
148              l.set_handle(this);
149              listeners.add (l);
150         }
151
152         // activate them
153
for (Iterator i = listeners.iterator(); i.hasNext();)
154         {
155             ((Listener)i.next()).listen();
156         }
157     }
158
159     public SSLServerSocketFactory getSSLSocketFactory()
160     {
161         return ssl_socket_factory;
162     }
163
164
165     /**
166      * Returns a List of Factories for all transport plugins that
167      * should listen for incoming connections.
168      */

169     private List getListenerFactories()
170         throws ConfigurationException
171     {
172         List result = new ArrayList();
173         List tags =
174             configuration.getAttributeList("jacorb.transport.server.listeners");
175
176         if (tags.isEmpty())
177         {
178             result.addAll(transport_manager.getFactoriesList());
179         }
180         else
181         {
182             for (Iterator i = tags.iterator(); i.hasNext();)
183             {
184                 String JavaDoc s = ((String JavaDoc)i.next());
185                 int tag = -1;
186                 try
187                 {
188                     tag = Integer.parseInt(s);
189                 }
190                 catch (NumberFormatException JavaDoc ex)
191                 {
192                     throw new RuntimeException JavaDoc
193                         ("could not parse profile tag for listener: " + s
194                          + " (should have been a number)");
195                 }
196                 Factories f = transport_manager.getFactories (tag);
197                 if (f == null)
198                     throw new RuntimeException JavaDoc
199                         ("could not find Factories for profile tag: " + tag);
200                 else
201                     result.add(f);
202             }
203         }
204         return result;
205     }
206
207     public RequestListener getRequestListener()
208     {
209         return request_listener;
210     }
211
212     /**
213      * Returns a List of endpoint profiles for all transports that listen
214      * for incoming connections. Each individual profile is a copy and can
215      * safely be modified by the caller (e.g. add an object key, patch the
216      * address, stuff it into an IOR, etc.).
217      */

218     public List getEndpointProfiles()
219     {
220         List result = new ArrayList();
221         for (Iterator i = listeners.iterator(); i.hasNext();)
222         {
223             Listener l = (Listener)i.next();
224             result.add (l.endpoint());
225         }
226         return result;
227     }
228
229     /**
230      * If only a single IIOPListener (and no other Listener) is
231      * active for this BasicAdapter, then this method returns it.
232      * Otherwise it returns null.
233      */

234     private IIOPListener getIIOPListener()
235     {
236         if (listeners.size() == 1)
237         {
238             Listener l = (Listener)listeners.get(0);
239             if (l instanceof IIOPListener)
240                 return (IIOPListener)l;
241             else
242                 return null;
243         }
244         else
245             return null;
246     }
247
248     /**
249      * @deprecated This method cannot return a sensible result in the presence
250      * of alternate transports, use {@link #getEndpointProfiles()} instead.
251      */

252     public int getPort()
253     {
254         IIOPListener l = getIIOPListener();
255         if (l != null)
256         {
257             IIOPProfile profile = (IIOPProfile)l.endpoint();
258             return profile.getAddress().getPort();
259         }
260         else
261         {
262             throw new RuntimeException JavaDoc
263                 ("Cannot find server port for non-IIOP transport");
264         }
265     }
266
267     /**
268      * @deprecated This method cannot return a sensible result in the presence
269      * of alternate transports, use {@link #getEndpointProfiles()} instead.
270      */

271     public int getSSLPort()
272     {
273         IIOPListener l = getIIOPListener();
274         if (l != null)
275         {
276             IIOPProfile profile = (IIOPProfile)l.endpoint();
277             return profile.getSSLPort();
278         }
279         else
280         {
281             throw new RuntimeException JavaDoc
282                 ("Non-IIOP transport does not have an SSL port");
283         }
284     }
285
286     /**
287      * @deprecated This method cannot return a sensible result in the presence
288      * of alternate transports, use {@link #getEndpointProfiles()} instead.
289      */

290     public boolean hasSSLListener()
291     {
292         return getSSLPort() != -1;
293     }
294
295     /**
296      * @deprecated This method cannot return a sensible result in the presence
297      * of alternate transports, use {@link #getEndpointProfiles()} instead.
298      */

299     public String JavaDoc getAddress()
300     {
301         IIOPListener l = getIIOPListener();
302         if (l != null)
303         {
304             IIOPProfile profile = (IIOPProfile)l.endpoint();
305             String JavaDoc dnsEnable =
306                 configuration.getAttribute("jacorb.dns.enable","off");
307
308             if (dnsEnable.equals("on"))
309                 return profile.getAddress().getHostname();
310             else
311                 return profile.getAddress().getIP();
312         }
313         else
314         {
315             throw new RuntimeException JavaDoc
316                 ("Cannot find server address for non-IIOP transport");
317         }
318     }
319
320     /**
321      * to be called from the POA, code duplicated for performance
322      * reasons to avoid synchronization in the private version of this
323      * method.
324      */

325     public synchronized void deliverRequest( org.jacorb.orb.dsi.ServerRequest request,
326                                              org.omg.PortableServer.POA JavaDoc poa )
327     {
328         org.jacorb.poa.POA tmp_poa = (org.jacorb.poa.POA)poa;
329         String JavaDoc scopes[] = request.remainingPOAName();
330
331         try
332         {
333             for( int i=0; i < scopes.length-1; i++)
334             {
335                 if( scopes[i].equals(""))
336                 {
337                     request.setRemainingPOAName(null);
338                     break;
339                 }
340                 try
341                 {
342                     tmp_poa = tmp_poa._getChildPOA( scopes[i] );
343                 }
344                 catch ( org.jacorb.poa.except.ParentIsHolding p )
345                 {
346                     /*
347                      * if one of the POAs is in holding state, we
348                      * simply deliver deliver the request to this
349                      * POA. It will forward the request to its child
350                      * POAs if necessary when changing back to active
351                      * For the POA to be able to forward this request
352                      * to its child POAa, we need to supply the
353                      * remaining part of the child's POA name
354                      */

355                     String JavaDoc[] rest_of_name = new String JavaDoc[scopes.length - i];
356                     for( int j = 0; j < i; j++ )
357                         rest_of_name[j] = scopes[j+i];
358                     request.setRemainingPOAName(rest_of_name);
359                     break;
360                 }
361             }
362
363             if( tmp_poa == null )
364             {
365                 throw new INTERNAL JavaDoc("Request POA null!");
366             }
367             else
368             {
369                 /* hand over to the POA */
370                 ((org.jacorb.poa.POA)tmp_poa)._invoke( request );
371             }
372
373         }
374         catch( org.omg.PortableServer.POAPackage.WrongAdapter JavaDoc wa )
375         {
376             // unknown oid (not previously generated)
377
request.setSystemException( new org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc("unknown oid") );
378             request.reply();
379         }
380         catch( org.omg.CORBA.SystemException JavaDoc one )
381         {
382             request.setSystemException( one );
383             request.reply();
384         }
385         catch( Throwable JavaDoc th )
386         {
387             request.setSystemException( new org.omg.CORBA.UNKNOWN JavaDoc( th.toString()) );
388             request.reply();
389             th.printStackTrace(); // TODO
390
}
391     }
392
393     /**
394      * to be called from the POA
395      */

396
397     public void return_result(org.jacorb.orb.dsi.ServerRequest request)
398     {
399         request.reply();
400     }
401
402     public void stopListeners()
403     {
404         for (Iterator i = listeners.iterator(); i.hasNext();)
405         {
406             ((Listener)i.next()).destroy();
407         }
408     }
409
410     // Handle methods below this line
411

412     /**
413      * Announces a new connection instance to the ORB.
414      * The caller shall examine the boolean return value and
415      * destroy the connection, if the call returns false.
416      * A new connection initially belongs to the plug-in,
417      * and it shall signal the connection to the ORB when
418      * the first incoming request data was received,
419      * using this Handle upcall.
420      * <p>
421      * The Handle shall accept the connection (and cache
422      * information about it if needed), as long as it is
423      * allowed to do so by the ORB. In this case it shall
424      * return true. If a new connection is currently not
425      * allowed, it shall ignore the passed instance and
426      * return false.
427      */

428     public boolean add_input (org.omg.ETF.Connection conn)
429     {
430         GIOPConnection giopConnection =
431             giop_connection_manager.createServerGIOPConnection
432             (
433                 conn.get_server_profile(),
434                 conn,
435                 request_listener,
436                 reply_listener
437             );
438         receptor_pool.connectionCreated( giopConnection );
439         return true;
440     }
441
442     /**
443      * In some cases, the client side can initiate the closing of a
444      * connection. The plugin shall signal this event to the server side
445      * ORB via its Handle by calling this function.
446      */

447     public void closed_by_peer (org.omg.ETF.Connection conn)
448     {
449         // We don't do this in JacORB; Connections are never
450
// given back to the Listener after they have been
451
// passed up initially.
452
throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
453     }
454
455     /**
456      * The plugged-in transport (e.g. the Listener instance) shall call
457      * this function when it owns a server-side Connection and data arrives
458      * on the local endpoint. This will start a new request dispatching
459      * cycle in the ORB. Subsequently, it shall ignore any other incoming
460      * data from this Connection until the Listener's completed_data function
461      * is called by the ORB.
462      */

463     public void signal_data_available (Connection conn)
464     {
465         // We don't do this in JacORB; Connections are never
466
// given back to the Listener after they have been
467
// passed up initially.
468
throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
469     }
470
471 }
472
Popular Tags