KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
24 import java.net.*;
25 import java.util.*;
26 import java.lang.reflect.Constructor JavaDoc;
27
28 import org.apache.avalon.framework.logger.Logger;
29 import org.apache.avalon.framework.configuration.Configurable;
30 import org.apache.avalon.framework.configuration.Configuration;
31 import org.apache.avalon.framework.configuration.DefaultConfiguration;
32 import org.apache.avalon.framework.configuration.ConfigurationException;
33
34 import org.omg.ETF.Factories;
35
36 import org.jacorb.orb.*;
37 import org.jacorb.orb.factory.*;
38 import org.jacorb.orb.iiop.*;
39 import org.jacorb.util.ObjectUtil;
40
41 /**
42  * This class manages connections.<br>
43  *
44  * @author Gerald Brose, FU Berlin
45  * @version $Id: ClientConnectionManager.java,v 1.21 2005/02/09 09:51:18 andre.spiegel Exp $
46  *
47  */

48
49 public class ClientConnectionManager
50     implements Configurable
51 {
52     private org.jacorb.orb.ORB orb = null;
53
54     /** connection mgmt. */
55     private Map connections = new HashMap();
56
57     private SocketFactory socket_factory = null;
58     private SocketFactory ssl_socket_factory = null;
59
60     private RequestListener request_listener = null;
61
62     private MessageReceptorPool receptor_pool = null;
63
64     private TransportManager transport_manager = null;
65     private GIOPConnectionManager giop_connection_manager = null;
66
67     /** the configuration object */
68     private org.jacorb.config.Configuration configuration = null;
69     private Logger logger = null;
70
71     public ClientConnectionManager( ORB orb,
72                                     TransportManager transport_manager,
73                                     GIOPConnectionManager giop_connection_manager )
74     {
75         this.orb = orb;
76         this.transport_manager = transport_manager;
77         this.giop_connection_manager = giop_connection_manager;
78     }
79
80     /**
81      * configure this connection manager
82      */

83
84     public void configure(Configuration myConfiguration)
85         throws ConfigurationException
86     {
87         // Moved from the constructor to facilitate logging.
88
receptor_pool = MessageReceptorPool.getInstance(myConfiguration);
89
90         this.configuration = (org.jacorb.config.Configuration)myConfiguration;
91         logger = configuration.getNamedLogger("jacorb.orb.giop");
92
93         request_listener = new NoBiDirClientRequestListener(logger);
94
95         socket_factory =
96             transport_manager.getSocketFactoryManager().getSocketFactory();
97
98         if (configuration.getAttribute("jacorb.security.support_ssl","off").equals("on") )
99         {
100             String JavaDoc s = configuration.getAttribute("jacorb.ssl.socket_factory","");
101             if ( s.length() == 0)
102             {
103                 throw new RuntimeException JavaDoc( "SSL support is on, but the property \"jacorb.ssl.socket_factory\" is not set!" );
104             }
105
106             try
107             {
108                 Class JavaDoc ssl = ObjectUtil.classForName(s);
109
110                 Constructor JavaDoc constr =
111                     ssl.getConstructor( new Class JavaDoc[]{ ORB.class });
112
113                 ssl_socket_factory =
114                     (SocketFactory)constr.newInstance( new Object JavaDoc[]{ orb });
115             }
116             catch (Exception JavaDoc e)
117             {
118                 throw new RuntimeException JavaDoc( "SSL support is on, but the ssl socket factory can't be instantiated ("+e.getMessage()+")!" );
119             }
120         }
121
122
123     }
124
125
126     public void setRequestListener( RequestListener listener )
127     {
128         request_listener = listener;
129     }
130
131     public synchronized ClientConnection getConnection
132                                               (org.omg.ETF.Profile profile)
133     {
134         /* look for an existing connection */
135
136         ClientConnection c =
137             (ClientConnection)connections.get( profile );
138
139         if (c == null)
140         {
141             int tag = profile.tag();
142             Factories factories = transport_manager.getFactories (tag);
143             if (factories == null)
144             {
145                 throw new RuntimeException JavaDoc
146                     ("No transport plugin for profile tag " + tag);
147             }
148             GIOPConnection connection =
149                 giop_connection_manager.createClientGIOPConnection(
150                     profile,
151                     factories.create_connection (null),
152                     request_listener,
153                     null );
154
155             c = new ClientConnection( connection, orb, this,
156                                       profile, true );
157
158             if( logger.isInfoEnabled())
159             {
160                 logger.info("ClientConnectionManager: created new conn to target " +
161                             c.getInfo() );
162             }
163
164             connections.put( profile, c );
165             receptor_pool.connectionCreated( connection );
166         }
167         else
168         {
169             if( logger.isInfoEnabled())
170             {
171                 logger.info("ClientConnectionManager: found conn to target " +
172                             c.getInfo());
173             }
174         }
175
176         c.incClients();
177
178         return c;
179     }
180
181     /**
182      * Only used by Delegate for client-initiated connections.
183      */

184     public synchronized void releaseConnection( ClientConnection c )
185     {
186         // hasNoMoreClients now merged into decClients.
187
if ( c.decClients() )
188         {
189             c.close();
190             connections.remove(c.getRegisteredProfile());
191         }
192     }
193
194     /**
195      * Only used by ClientConnection to unregister server-side of
196      * BiDir connection.
197      */

198     public synchronized void removeConnection(ClientConnection c)
199     {
200         connections.remove( c.getRegisteredProfile() );
201     }
202
203     public synchronized void addConnection( GIOPConnection connection )
204     {
205         addConnection(connection,
206                       connection.getTransport().get_server_profile() );
207     }
208
209     public synchronized void addConnection( GIOPConnection connection,
210                                             org.omg.ETF.Profile profile )
211     {
212         if( !connections.containsKey( profile ))
213         {
214             ClientConnection c = new ClientConnection
215             (
216                 connection, orb, this,
217                 profile,
218                 false
219             );
220
221             //this is a bit of a hack: the bidirectional client
222
//connections have to persist until their underlying GIOP
223
//connection is closed. Therefore, we set the initial
224
//client count to 1, so the connection will be kept even
225
//if there are currently no associated Delegates.
226

227             c.incClients();
228
229             connections.put( profile, c );
230         }
231     }
232
233     public void shutdown()
234     {
235         /* release all open connections */
236
237         for( Iterator i = connections.values().iterator(); i.hasNext(); )
238         {
239             ((ClientConnection) i.next()).close();
240         }
241
242         if( logger.isDebugEnabled())
243         {
244             logger.debug("ClientConnectionManager shut down (all connections released)");
245         }
246
247         connections.clear();
248     }
249 }
250
Popular Tags