1 9 package org.jboss.remoting; 10 11 12 import java.lang.reflect.Constructor ; 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import org.jboss.logging.Logger; 20 import org.jboss.remoting.transport.ClientInvoker; 21 import org.jboss.remoting.transport.http.HTTPClientInvoker; 22 import org.jboss.remoting.transport.http.HTTPServerInvoker; 23 import org.jboss.remoting.transport.http.ssl.HTTPSClientInvoker; 24 import org.jboss.remoting.transport.http.ssl.HTTPSServerInvoker; 25 import org.jboss.remoting.transport.local.LocalClientInvoker; 26 import org.jboss.remoting.transport.rmi.RMIClientInvoker; 27 import org.jboss.remoting.transport.rmi.RMIServerInvoker; 28 import org.jboss.remoting.transport.servlet.ServletServerInvoker; 29 import org.jboss.remoting.transport.socket.SocketClientInvoker; 30 import org.jboss.remoting.transport.socket.SocketServerInvoker; 31 import org.jboss.remoting.transport.socket.ssl.SSLSocketClientInvoker; 32 import org.jboss.remoting.transport.socket.ssl.SSLSocketServerInvoker; 33 34 43 public class InvokerRegistry 44 { 45 46 private static final Logger log = Logger.getLogger(InvokerRegistry.class); 47 48 private static final Map clientInvokers = new HashMap (); 49 private static final Map serverInvokers = new HashMap (); 50 private static final Map clientLocators = new HashMap (); 51 private static final Map serverLocators = new HashMap (); 52 private static final Set registeredLocators = new HashSet (); 53 private static final Object serverLock = new Object (); 54 private static final Object clientLock = new Object (); 55 56 static 57 { 58 registerInvoker("socket", SocketClientInvoker.class, SocketServerInvoker.class); 60 registerInvoker("sslsocket", SSLSocketClientInvoker.class, SSLSocketServerInvoker.class); 61 registerInvoker("rmi", RMIClientInvoker.class, RMIServerInvoker.class); 62 registerInvoker("http", HTTPClientInvoker.class, HTTPServerInvoker.class); 63 registerInvoker("https", HTTPSClientInvoker.class, HTTPSServerInvoker.class); 64 registerInvoker("servlet", HTTPSClientInvoker.class, ServletServerInvoker.class); 65 66 } 67 68 73 public static synchronized final InvokerLocator[] getRegisteredServerLocators() 74 { 75 return (InvokerLocator[]) registeredLocators.toArray(new InvokerLocator[registeredLocators.size()]); 76 } 77 78 85 public static synchronized InvokerLocator getSuitableServerLocatorForRemote(InvokerLocator remote) 86 { 87 Iterator iter = registeredLocators.iterator(); 88 while(iter.hasNext()) 89 { 90 InvokerLocator l = (InvokerLocator) iter.next(); 91 if(l.getProtocol().equals(remote.getProtocol())) 92 { 93 return l; 95 } 96 } 97 return null; 98 } 99 100 105 public static final String [] getRegisteredInvokerTransports() 106 { 107 synchronized(clientLock) 108 { 109 Set set = clientInvokers.keySet(); 110 String transports[] = new String [set.size()]; 111 return (String []) set.toArray(transports); 112 } 113 } 114 115 120 public static final ClientInvoker[] getClientInvokers() 121 { 122 synchronized(clientLock) 123 { 124 if(clientLocators.isEmpty()) 125 { 126 return new ClientInvoker[0]; 127 } 128 Collection collection = clientLocators.values(); 129 return (ClientInvoker[]) collection.toArray(new ClientInvoker[collection.size()]); 130 } 131 } 132 133 138 public static final ServerInvoker[] getServerInvokers() 139 { 140 synchronized(serverLock) 141 { 142 if(serverLocators.isEmpty()) 143 { 144 return new ServerInvoker[0]; 145 } 146 Collection collection = serverLocators.values(); 147 return (ServerInvoker[]) collection.toArray(new ServerInvoker[collection.size()]); 148 } 149 } 150 151 158 public static synchronized void registerInvoker(String transport, Class client, Class server) 159 { 160 clientInvokers.put(transport, client); 161 serverInvokers.put(transport, server); 162 } 163 164 169 public static synchronized void unregisterInvoker(String transport) 170 { 171 clientInvokers.remove(transport); 172 serverInvokers.remove(transport); 173 } 174 175 public static synchronized void unregisterLocator(InvokerLocator locator) 176 { 177 serverLocators.remove(locator); 178 registeredLocators.remove(locator); 179 } 180 181 187 public static boolean isClientInvokerRegistered(InvokerLocator locator) 188 { 189 synchronized(clientLock) 190 { 191 return clientLocators.containsKey(locator); 192 } 193 } 194 195 202 public static void destroyClientInvoker(InvokerLocator locator) 203 { 204 ClientInvoker invoker = null; 205 206 synchronized(clientLock) 207 { 208 invoker = (ClientInvoker) clientLocators.remove(locator); 209 } 210 211 if(invoker != null) 212 { 213 log.debug("destroying client for locator: " + locator + ", invoker:" + invoker + ", remaining list:" + clientLocators); 214 215 invoker.disconnect(); 216 invoker = null; 217 } 218 219 } 220 221 229 public static ClientInvoker createClientInvoker(InvokerLocator locator) 230 throws Exception 231 { 232 if(locator == null) 233 { 234 throw new NullPointerException ("locator cannot be null"); 235 } 236 synchronized(clientLock) 237 { 238 ClientInvoker invoker = (ClientInvoker) clientLocators.get(locator); 239 if(invoker != null) 240 { 241 return invoker; 242 } 243 244 boolean isPassByValue = false; 245 Map parameters = locator.getParameters(); 246 if(parameters != null) 247 { 248 String value = (String ) parameters.get(InvokerLocator.BYVALUE); 249 if(value != null && Boolean.valueOf(value).booleanValue()) 250 { 251 isPassByValue = true; 252 } 253 } 254 255 ServerInvoker svrInvoker = (ServerInvoker) serverLocators.get(locator); 258 if(svrInvoker != null && !isPassByValue) 259 { 260 LocalClientInvoker localInvoker = new LocalClientInvoker(locator); 261 localInvoker.setServerInvoker(svrInvoker); 263 invoker = localInvoker; 264 InvokerLocator l = invoker.getLocator(); 265 clientLocators.put(l, invoker); 266 } 267 else { 269 String protocol = locator.getProtocol(); 270 if(protocol == null) 271 { 272 throw new NullPointerException ("protocol cannot be null for the locator"); 273 } 274 Class cl = (Class ) clientInvokers.get(protocol); 275 if(cl == null) 276 { 277 throw new RuntimeException ("Couldn't find valid client invoker class for transport '" + protocol + "'"); 278 } 279 Constructor ctor = cl.getConstructor(new Class []{InvokerLocator.class}); 280 invoker = (ClientInvoker) ctor.newInstance(new Object []{locator}); 281 InvokerLocator l = invoker.getLocator(); 282 clientLocators.put(l, invoker); 283 } 284 return invoker; 285 } 286 } 287 288 294 public static boolean isServerInvokerRegistered(InvokerLocator locator) 295 { 296 synchronized(serverLock) 297 { 298 return serverLocators.containsKey(locator); 299 } 300 } 301 302 310 public static ServerInvoker createServerInvoker(InvokerLocator locator) 311 throws Exception 312 { 313 return createServerInvoker(locator, null); 314 } 315 316 324 public static ServerInvoker createServerInvoker(InvokerLocator locator, Map configuration) 325 throws Exception 326 { 327 328 ServerInvoker invoker = null; 329 synchronized(serverLock) 330 { 331 invoker = (ServerInvoker) serverLocators.get(locator); 332 if(invoker != null) 333 { 334 throw new InvalidConfigurationException("The invoker for locator (" + locator + ") is already " + 335 "in use by another Connector. Either change the locator or " + 336 "add new handlers to existing Connector."); 337 } 338 Class cl = (Class ) serverInvokers.get(locator.getProtocol()); 339 if(cl == null) 340 { 341 throw new RuntimeException ("Couldn't find valid server invoker class for transport '" + locator.getProtocol() + "'"); 342 } 343 if(configuration != null) 344 { 345 Constructor ctor = cl.getConstructor(new Class []{InvokerLocator.class, Map .class}); 346 invoker = (ServerInvoker) ctor.newInstance(new Object []{locator, configuration}); 347 } 348 else 349 { 350 Constructor ctor = cl.getConstructor(new Class []{InvokerLocator.class}); 351 invoker = (ServerInvoker) ctor.newInstance(new Object []{locator}); 352 } 353 serverLocators.put(locator, invoker); 354 registeredLocators.add(invoker.getLocator()); 355 } 356 return invoker; 357 } 358 359 public static void destroyServerInvoker(ServerInvoker invoker) 360 { 361 if(invoker != null) 362 { 363 serverLocators.remove(invoker.getLocator()); 364 } 365 } 366 367 374 public static synchronized void updateServerInvokerLocator(InvokerLocator locator, InvokerLocator newLocator) 375 { 376 Object si = serverLocators.get(locator); 377 serverLocators.remove(locator); 378 registeredLocators.remove(locator); 379 serverLocators.put(newLocator, si); 380 registeredLocators.add(newLocator); 381 } 382 } 383 | Popular Tags |