KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > client > ConnectionPool


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.client;
19
20 import java.util.HashMap JavaDoc;
21
22 import org.apache.geronimo.interop.SystemException;
23 import org.apache.geronimo.interop.rmi.RmiTrace;
24 import org.apache.geronimo.interop.rmi.iiop.ObjectRef;
25 import org.apache.geronimo.interop.rmi.iiop.Protocol;
26 import org.apache.geronimo.interop.util.InstancePool;
27 import org.apache.geronimo.interop.util.StringUtil;
28
29 public class ConnectionPool {
30     public static ConnectionPool getInstance(ClientNamingContext namingContext) {
31         ConnectionPool object = new ConnectionPool();
32         object.init(namingContext);
33         return object;
34     }
35
36     private ClientNamingContext namingContext;
37     private HashMap JavaDoc poolMap;
38
39     public Connection get(int protocol, String JavaDoc endpoint, ObjectRef objectRef) {
40         //System.out.println("ConnectionPool.get(): protocol: " + protocol + ", endpoint: " + endpoint + ", objectRef: " + objectRef);
41

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

93     }
94
95     public void put(Connection conn) {
96         conn.getInstancePool().put(conn);
97     }
98
99     protected void init(ClientNamingContext namingContext) {
100         this.namingContext = namingContext;
101         poolMap = new HashMap JavaDoc();
102     }
103
104     /**
105      * * Get the endpoint (host:port) for host, which is either a host name,
106      * * IP address, or URL. If it is a URL, then return null if its protocol
107      * * does not match expectations or if its port number suffix doesn't match
108      * * the object reference. This method is used frequently, so given the
109      * * above requirements should be as efficient as possible.
110      */

111     protected String JavaDoc getEndpoint(int protocol, String JavaDoc host, ObjectRef objectRef) {
112         System.out.println("ConnectionPool.getEndpoint(): protocol: " + protocol + ", host: " + host + ", objectRef: " + objectRef);
113
114         int ssPos = host.indexOf("://");
115         if (ssPos != -1) {
116             String JavaDoc scheme = Protocol.getScheme(protocol); // e.g. "iiop:"
117
if (!host.startsWith(scheme)) {
118                 return null;
119             }
120             int portPos = host.lastIndexOf(':');
121             if (portPos > ssPos) {
122                 int port = 0;
123                 int n = host.length();
124                 for (int i = portPos + 1; i < n; i++) {
125                     char c = host.charAt(i);
126                     if (c == ']') {
127                         // Part of IP6 host name, no port number is present.
128
port = -1;
129                         break;
130                     }
131                     port = 10 * port + (c - '0');
132                 }
133                 if (port != -1) {
134                     if (port % 10 != objectRef.$getPort() % 10) {
135                         // TODO: make this configurable via HostInfo?
136
return null;
137                     }
138                     return host.substring(ssPos + 3);
139                 }
140             }
141             StringBuffer JavaDoc hp = new StringBuffer JavaDoc(host.length()); // shouldn't require expansion in append
142
hp.append(host.substring(ssPos + 3));
143             hp.append(':');
144             hp.append(objectRef.$getPort());
145             return hp.toString();
146         } else {
147             StringBuffer JavaDoc hp = new StringBuffer JavaDoc(host.length() + 6); // shouldn't require expansion in append
148
hp.append(host);
149             hp.append(':');
150             hp.append(objectRef.$getPort());
151             return hp.toString();
152         }
153     }
154
155     protected InstancePool getInstancePool(final int protocol, final String JavaDoc endpoint) {
156         System.out.println("ConnectionPool.getInstancePool(): protocol: " + protocol + ", endpoint: " + endpoint);
157
158         InstancePool pool = (InstancePool) poolMap.get(endpoint);
159         if (pool == null) {
160             synchronized (poolMap) {
161                 pool = (InstancePool) poolMap.get(endpoint);
162                 if (pool == null) {
163                     String JavaDoc poolName = Protocol.getName(protocol) + "://" + endpoint;
164                     pool = new InstancePool(poolName);
165                     poolMap.put(endpoint, pool);
166                 }
167             }
168         }
169         return pool;
170     }
171
172     protected Connection newConnection(int protocol, String JavaDoc endpoint, ObjectRef objectRef, InstancePool pool) {
173         System.out.println("ConnectionPool.newConnection(): protocol: " + protocol + ", endpoint: " + endpoint + ", pool: " + pool);
174
175         if (endpoint == null) {
176             // See getEndpoint above, noting that it may return null.
177
return null;
178         }
179         Connection conn;
180         // Name Service connections use different pools from ordinary
181
// connections. This is achieved using the "ns~" endpoint
182
// prefix. Since this is not valid for host names, we must remove
183
// it now.
184
endpoint = StringUtil.removePrefix(endpoint, "ns~");
185         switch (protocol) {
186             case Protocol.IIOP:
187                 conn = iiopConnection(endpoint, objectRef);
188                 break;
189             case Protocol.IIOPS:
190                 conn = iiopsConnection(endpoint, objectRef);
191                 break;
192             case Protocol.HTTP:
193                 conn = httpConnection(endpoint, objectRef);
194                 break;
195             case Protocol.HTTPS:
196                 conn = httpsConnection(endpoint, objectRef);
197                 break;
198             default:
199                 throw new IllegalArgumentException JavaDoc("protocol = " + protocol);
200         }
201         conn.setInstancePool(pool);
202         if (RmiTrace.CONNECT) {
203             RmiTrace.traceConnect(Protocol.getName(protocol) + "://" + endpoint);
204         }
205         return conn;
206     }
207
208     protected Connection iiopConnection(String JavaDoc endpoint, ObjectRef objectRef) {
209         System.out.println( "endpoint : " + endpoint );
210         System.out.println( "objectRef : " + objectRef );
211         System.out.println( "namingContext : " + namingContext );
212         return Connection.getInstance(endpoint, objectRef, namingContext.getConnectionProperties());
213     }
214
215     protected Connection iiopsConnection(String JavaDoc endpoint, ObjectRef objectRef) {
216         throw new SystemException("TODO");
217     }
218
219     protected Connection httpConnection(String JavaDoc endpoint, ObjectRef objectRef) {
220         throw new SystemException("TODO");
221     }
222
223     protected Connection httpsConnection(String JavaDoc endpoint, ObjectRef objectRef) {
224         throw new SystemException("TODO");
225     }
226
227     /*
228     protected HostList resolve(String endpoint, ObjectRef objectRef) {
229
230         //if (objectRef instanceof org.apache.geronimo.interop.rmi.iiop.NameService
231         // && ! endpoint.startsWith("ns~mh~"))
232         //{
233         // return null; // Avoid unbounded recursion
234         //}
235
236         HostList hostList = _namingContext.lookupHost(objectRef.$getHost()); // this uses a cache for good performance
237         if (hostList != null
238             && hostList.getPreferredServers().size() == 0
239             && hostList.getAlternateServers().size() == 0) {
240             // If the host list doesn't have any elements, we are better
241             // off using the original endpoint string.
242             hostList = null;
243         }
244         return hostList;
245     }
246     */

247 }
248
Popular Tags