1 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 38 private ClientNamingContext _namingContext; 39 40 private HashMap _poolMap; 41 42 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 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 109 protected void init(ClientNamingContext namingContext) 110 { 111 _namingContext = namingContext; 112 _poolMap = new HashMap(); 113 } 114 115 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); 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 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 return null; 156 } 157 return host.substring(ssPos + 3); 158 } 159 } 160 StringBuffer hp = new StringBuffer(host.length()); 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); 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 211 pool = new InstancePool(poolName); 215 _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 return null; 231 } 232 Connection conn; 233 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 292 HostList hostList = _namingContext.lookupHost(objectRef.$getHost()); if (hostList != null 294 && hostList.getPreferredServers().size() == 0 295 && hostList.getAlternateServers().size() == 0) 296 { 297 hostList = null; 300 } 301 return hostList; 302 } 303 } 304 | Popular Tags |