KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > client > ConnectionPool


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop.client;
20
21 import gcc.*;
22 import gcc.rmi.*;
23 import gcc.rmi.iiop.*;
24 import gcc.util.*;
25 import java.util.*;
26
27 public class ConnectionPool
28 {
29     public static ConnectionPool getInstance(ClientNamingContext namingContext)
30     {
31         ConnectionPool object = new ConnectionPool();
32         object.init(namingContext);
33         return object;
34     }
35
36     // private data
37

38     private ClientNamingContext _namingContext;
39
40     private HashMap _poolMap;
41
42     // public methods
43

44     public Connection get(int protocol, String endpoint, ObjectRef objectRef)
45     {
46         System.out.println( "ConnectionPool.get(): protocol: " + protocol + ", endpoint: " + endpoint + ", objectRef: " + objectRef );
47
48         HostList hostList = resolve(endpoint, objectRef);
49         System.out.println( "ConnectionPool.get(): hostlist: " + hostList );
50         if (hostList == null)
51         {
52             InstancePool pool = getInstancePool(protocol, endpoint);
53             System.out.println( "ConnectionPool.get(): pool: " + pool );
54             Connection conn = (Connection)pool.get();
55             if (conn == null)
56             {
57                 conn = newConnection(protocol, endpoint, objectRef, pool);
58             }
59             return conn;
60         }
61         else
62         {
63             for (int pass = 1; pass <= 2; pass++)
64             {
65                 int baseIndex;
66                 ArrayList servers;
67                 if (pass == 1)
68                 {
69                     baseIndex = hostList.getPreferredIndex();
70                     servers = hostList.getPreferredServers();
71                 }
72                 else
73                 {
74                     baseIndex = hostList.getAlternateIndex();
75                     servers = hostList.getAlternateServers();
76                 }
77                 int n = servers.size();
78                 for (int i = 0; i < n; i++)
79                 {
80                     int tryIndex = (baseIndex + i) % n;
81                     String host = (String)servers.get(tryIndex);
82                     String hostPort = getEndpoint(protocol, host, objectRef);
83                     if (hostPort == null)
84                     {
85                         continue;
86                     }
87                     InstancePool pool = getInstancePool(protocol, hostPort);
88                     Connection conn = (Connection)pool.get();
89                     if (conn == null)
90                     {
91                         conn = newConnection(protocol, hostPort, objectRef, pool);
92                         hostList.countConnect();
93                     }
94                     return conn;
95                 }
96             }
97             // TODO: I18N
98
throw new SystemException("CONNECT FAILED: host list = " + hostList);
99         }
100     }
101
102     public void put(Connection conn)
103     {
104         conn.getInstancePool().put(conn);
105     }
106
107     // protected methods
108

109     protected void init(ClientNamingContext namingContext)
110     {
111         _namingContext = namingContext;
112         _poolMap = new HashMap();
113     }
114
115     /**
116      ** Get the endpoint (host:port) for host, which is either a host name,
117      ** IP address, or URL. If it is a URL, then return null if its protocol
118      ** does not match expectations or if its port number suffix doesn't match
119      ** the object reference. This method is used frequently, so given the
120      ** above requirements should be as efficient as possible.
121      **/

122     protected String getEndpoint(int protocol, String host, ObjectRef objectRef)
123     {
124         System.out.println( "ConnectionPool.getEndpoint(): protocol: " + protocol + ", host: " + host + ", objectRef: " + objectRef );
125
126         int ssPos = host.indexOf("://");
127         if (ssPos != -1)
128         {
129             String scheme = Protocol.getScheme(protocol); // e.g. "iiop:"
130
if (! host.startsWith(scheme))
131             {
132                 return null;
133             }
134             int portPos = host.lastIndexOf(':');
135             if (portPos > ssPos)
136             {
137                 int port = 0;
138                 int n = host.length();
139                 for (int i = portPos + 1; i < n; i++)
140                 {
141                     char c = host.charAt(i);
142                     if (c == ']')
143                     {
144                         // Part of IP6 host name, no port number is present.
145
port = -1;
146                         break;
147                     }
148                     port = 10 * port + (c - '0');
149                 }
150                 if (port != -1)
151                 {
152                     if (port % 10 != objectRef.$getPort() % 10)
153                     {
154                         // TODO: make this configurable via HostInfo?
155
return null;
156                     }
157                     return host.substring(ssPos + 3);
158                 }
159             }
160             StringBuffer hp = new StringBuffer(host.length()); // shouldn't require expansion in append
161
hp.append(host.substring(ssPos + 3));
162             hp.append(':');
163             hp.append(objectRef.$getPort());
164             return hp.toString();
165         }
166         else
167         {
168             StringBuffer hp = new StringBuffer(host.length() + 6); // shouldn't require expansion in append
169
hp.append(host);
170             hp.append(':');
171             hp.append(objectRef.$getPort());
172             return hp.toString();
173         }
174     }
175
176     protected InstancePool getInstancePool(final int protocol, final String endpoint)
177     {
178         System.out.println( "ConnectionPool.getInstancePool(): protocol: " + protocol + ", endpoint: " + endpoint );
179
180         InstancePool pool = (InstancePool)_poolMap.get(endpoint);
181         if (pool == null)
182         {
183             synchronized (_poolMap)
184             {
185                 pool = (InstancePool)_poolMap.get(endpoint);
186                 if (pool == null)
187                 {
188                     String poolName = Protocol.getName(protocol) + "://" + endpoint;
189                     //long idleTimeout = 1000 * _namingContext.getIdleConnectionTimeout();
190
//if (idleTimeout > 0)
191
//{
192
/*
193                         pool = new InstancePool
194                         (
195                             poolName,
196                             idleTimeout,
197                             new TimeoutObject()
198                             {
199                                 public void onTimeout(Object object)
200                                 {
201                                     Connection conn = (Connection)object;
202                                     if (RmiTrace.CONNECT)
203                                     {
204                                         RmiTrace.getInstance().traceDisconnect(Protocol.getName(protocol) + "://" + endpoint);
205                                     }
206                                     conn.shutdown();
207                                 }
208                             }
209                         );
210                         */

211                     //}
212
//else
213
//{
214
pool = new InstancePool(poolName);
215                     //}
216
_poolMap.put(endpoint, pool);
217                 }
218             }
219         }
220         return pool;
221     }
222
223     protected Connection newConnection(int protocol, String endpoint, ObjectRef objectRef, InstancePool pool)
224     {
225         System.out.println( "ConnectionPool.newConnection(): protocol: " + protocol + ", endpoint: " + endpoint + ", pool: " + pool );
226
227         if (endpoint == null)
228         {
229             // See getEndpoint above, noting that it may return null.
230
return null;
231         }
232         Connection conn;
233         // Name Service connections use different pools from ordinary
234
// connections. This is achieved using the "ns~" endpoint
235
// prefix. Since this is not valid for host names, we must remove
236
// it now.
237
endpoint = StringUtil.removePrefix(endpoint, "ns~");
238         switch (protocol)
239         {
240           case Protocol.IIOP:
241             conn = iiopConnection(endpoint, objectRef);
242             break;
243           case Protocol.IIOPS:
244             conn = iiopsConnection(endpoint, objectRef);
245             break;
246           case Protocol.HTTP:
247             conn = httpConnection(endpoint, objectRef);
248             break;
249           case Protocol.HTTPS:
250             conn = httpsConnection(endpoint, objectRef);
251             break;
252           default:
253             throw new IllegalArgumentException("protocol = " + protocol);
254         }
255         conn.setInstancePool(pool);
256         if (RmiTrace.CONNECT)
257         {
258             RmiTrace.traceConnect(Protocol.getName(protocol) + "://" + endpoint);
259         }
260         return conn;
261     }
262
263     protected Connection iiopConnection(String endpoint, ObjectRef objectRef)
264     {
265         return Connection.getInstance(endpoint, objectRef, _namingContext.getConnectionProperties());
266     }
267
268     protected Connection iiopsConnection(String endpoint, ObjectRef objectRef)
269     {
270         throw new SystemException("TODO");
271     }
272
273     protected Connection httpConnection(String endpoint, ObjectRef objectRef)
274     {
275         throw new SystemException("TODO");
276     }
277
278     protected Connection httpsConnection(String endpoint, ObjectRef objectRef)
279     {
280         throw new SystemException("TODO");
281     }
282
283     protected HostList resolve(String endpoint, ObjectRef objectRef)
284     {
285         /*
286         if (objectRef instanceof gcc.rmi.iiop.NameService
287             && ! endpoint.startsWith("ns~mh~"))
288         {
289             return null; // Avoid unbounded recursion
290         }
291         */

292         HostList hostList = _namingContext.lookupHost(objectRef.$getHost()); // this uses a cache for good performance
293
if (hostList != null
294             && hostList.getPreferredServers().size() == 0
295             && hostList.getAlternateServers().size() == 0)
296         {
297             // If the host list doesn't have any elements, we are better
298
// off using the original endpoint string.
299
hostList = null;
300         }
301         return hostList;
302     }
303 }
304
Popular Tags