1 18 package org.apache.geronimo.interop.rmi.iiop.client; 19 20 import java.util.HashMap ; 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 poolMap; 38 39 public Connection get(int protocol, String endpoint, ObjectRef objectRef) { 40 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 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 (); 102 } 103 104 111 protected String getEndpoint(int protocol, String 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 scheme = Protocol.getScheme(protocol); 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 port = -1; 129 break; 130 } 131 port = 10 * port + (c - '0'); 132 } 133 if (port != -1) { 134 if (port % 10 != objectRef.$getPort() % 10) { 135 return null; 137 } 138 return host.substring(ssPos + 3); 139 } 140 } 141 StringBuffer hp = new StringBuffer (host.length()); hp.append(host.substring(ssPos + 3)); 143 hp.append(':'); 144 hp.append(objectRef.$getPort()); 145 return hp.toString(); 146 } else { 147 StringBuffer hp = new StringBuffer (host.length() + 6); 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 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 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 endpoint, ObjectRef objectRef, InstancePool pool) { 173 System.out.println("ConnectionPool.newConnection(): protocol: " + protocol + ", endpoint: " + endpoint + ", pool: " + pool); 174 175 if (endpoint == null) { 176 return null; 178 } 179 Connection conn; 180 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 ("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 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 endpoint, ObjectRef objectRef) { 216 throw new SystemException("TODO"); 217 } 218 219 protected Connection httpConnection(String endpoint, ObjectRef objectRef) { 220 throw new SystemException("TODO"); 221 } 222 223 protected Connection httpsConnection(String endpoint, ObjectRef objectRef) { 224 throw new SystemException("TODO"); 225 } 226 227 247 } 248 | Popular Tags |