1 45 package org.exolab.jms.net.orb; 46 47 import java.lang.reflect.InvocationTargetException ; 48 import java.lang.reflect.Method ; 49 import java.rmi.RemoteException ; 50 import java.rmi.StubNotFoundException ; 51 import java.rmi.ConnectException ; 52 import java.rmi.server.ExportException ; 53 import java.security.Principal ; 54 import java.util.Map ; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 59 import org.exolab.jms.common.security.BasicPrincipal; 60 import org.exolab.jms.net.connector.Authenticator; 61 import org.exolab.jms.net.connector.Caller; 62 import org.exolab.jms.net.connector.CallerListener; 63 import org.exolab.jms.net.connector.Connection; 64 import org.exolab.jms.net.connector.Invocation; 65 import org.exolab.jms.net.connector.InvocationHandler; 66 import org.exolab.jms.net.connector.MulticastCallerListener; 67 import org.exolab.jms.net.connector.Request; 68 import org.exolab.jms.net.connector.ResourceException; 69 import org.exolab.jms.net.connector.Response; 70 import org.exolab.jms.net.connector.AbstractConnectionManager; 71 import org.exolab.jms.net.proxy.Proxy; 72 import org.exolab.jms.net.registry.LocalRegistry; 73 import org.exolab.jms.net.registry.Registry; 74 import org.exolab.jms.net.uri.InvalidURIException; 75 import org.exolab.jms.net.uri.URI; 76 import org.exolab.jms.net.util.MethodHelper; 77 78 79 85 class DefaultORB extends AbstractORB { 86 87 90 private LocalRegistry _registry; 91 92 95 private AbstractConnectionManager _manager; 96 97 100 private MulticastCallerListener _listeners; 101 102 105 private final ThreadLocal _caller = new ThreadLocal (); 106 107 110 private static final Log _log = LogFactory.getLog(DefaultORB.class); 111 112 113 121 public DefaultORB(Authenticator authenticator) 122 throws RemoteException { 123 this(authenticator, DefaultORB.class.getClassLoader(), null); 124 } 125 126 136 public DefaultORB(Authenticator authenticator, Map properties) 137 throws RemoteException { 138 this(authenticator, DefaultORB.class.getClassLoader(), properties); 139 } 140 141 148 public DefaultORB(Map properties) throws RemoteException { 149 this(new DummyAuthenticator(), DefaultORB.class.getClassLoader(), 150 properties); 151 } 152 153 160 public DefaultORB() throws RemoteException { 161 this(new DummyAuthenticator(), DefaultORB.class.getClassLoader(), 162 null); 163 } 164 165 176 public DefaultORB(Authenticator authenticator, ClassLoader loader, 177 Map properties) 178 throws RemoteException { 179 super(loader, properties); 180 if (authenticator == null) { 181 throw new IllegalArgumentException ( 182 "Argument 'authenticator' is null"); 183 } 184 185 try { 186 _manager = createConnectionManager(new Handler(), authenticator); 187 } catch (ResourceException exception) { 188 throw new RemoteException ("Failed to construct connection manager", 189 exception); 190 } 191 } 192 193 199 public synchronized LocalRegistry getRegistry() throws RemoteException { 200 if (_registry == null) { 201 _registry = new RegistryService(this); 202 } 203 return _registry; 204 } 205 206 213 public Registry getRegistry(Map properties) throws RemoteException { 214 if (properties == null || properties.get(PROVIDER_URI) == null) { 215 throw new ConnectException (PROVIDER_URI + " not specified"); 216 } 217 Registry registry; 218 String uri = (String ) properties.get(PROVIDER_URI); 219 String principal = (String ) properties.get(SECURITY_PRINCIPAL); 220 String credentials = (String ) properties.get(SECURITY_CREDENTIALS); 221 Principal subject = null; 222 223 if (principal != null) { 224 subject = new BasicPrincipal(principal, credentials); 225 } 226 227 try { 228 registry = Locator.getRegistry(subject, uri, _manager, 229 getProxyClassLoader(), 230 properties); 231 } catch (InvalidURIException exception) { 232 throw new RemoteException ("Invalid URI: " + uri, exception); 233 } 234 return registry; 235 } 236 237 246 public Proxy exportObjectTo(Object object) throws ExportException , 247 StubNotFoundException { 248 Caller caller = (Caller) _caller.get(); 249 if (caller == null) { 250 throw new ExportException ("Cannot export - no current caller"); 251 } 252 return doExportTo(object, caller.getLocalURI()); 253 } 254 255 262 public Caller getCaller() throws RemoteException { 263 return (Caller) _caller.get(); 264 } 265 266 273 public void addCallerListener(String uri, CallerListener listener) 274 throws InvalidURIException { 275 synchronized (this) { 276 if (_listeners == null) { 277 _listeners = new MulticastCallerListener(); 278 _manager.setCallerListener(_listeners); 279 } 280 } 281 _listeners.addCallerListener(uri, listener); 282 } 283 284 291 public void removeCallerListener(String uri, CallerListener listener) 292 throws InvalidURIException { 293 MulticastCallerListener listeners = null; 294 synchronized (this) { 295 listeners = _listeners; 296 } 297 if (listeners != null) { 298 listeners.removeCallerListener(uri, listener); 299 } 300 } 301 302 307 public void shutdown() throws RemoteException { 308 try { 309 _manager.close(); 310 } catch (ResourceException exception) { 311 throw new RemoteException ("Failed to close connection manager", 312 exception); 313 } 314 } 316 317 325 protected AbstractConnectionManager createConnectionManager( 326 InvocationHandler handler, Authenticator authenticator) 327 throws ResourceException { 328 return new DefaultConnectionManager(handler, authenticator, 329 getProperties()); 330 } 331 332 342 protected URI connect(URI uri, String principal, String credentials) 343 throws ExportException { 344 URI result; 345 try { 346 Principal subject = null; 347 if (principal != null) { 348 subject = new BasicPrincipal(principal, credentials); 349 } 350 Connection connection = _manager.getConnection(subject, uri); 351 result = connection.getLocalURI(); 352 } catch (ResourceException exception) { 353 throw new ExportException ("Failed to connect to URI: " + uri, 354 exception); 355 } 356 return result; 357 } 358 359 365 protected void accept(URI uri) throws ExportException { 366 try { 367 _manager.accept(uri, getProperties()); 368 } catch (ResourceException exception) { 369 throw new ExportException ("Failed to accept connections on URI: " 370 + uri, exception); 371 } 372 } 373 374 383 private Method getMethod(Object object, long methodID) 384 throws NoSuchMethodException { 385 386 Method result = null; 387 Method [] methods = MethodHelper.getAllInterfaceMethods( 388 object.getClass()); 389 for (int i = 0; i < methods.length; ++i) { 390 Method method = methods[i]; 391 if (MethodHelper.getMethodID(method) == methodID) { 392 result = method; 393 break; 394 } 395 } 396 if (result == null) { 397 throw new NoSuchMethodException ( 398 "Failed to resolve method for methodID=" + methodID); 399 } 400 return result; 401 } 402 403 407 private class Handler implements InvocationHandler { 408 409 412 public void prepare() { 413 } 415 416 423 public Response invoke(Request request, Caller caller) { 424 Response response; 425 try { 426 Object object = getObject(request.getObjID(), 427 request.getURI()); 428 Method method = request.getMethod(); 429 if (method == null) { 430 method = getMethod(object, request.getMethodID()); 432 } 433 Object [] args = request.getArgs(); 434 if (args == null) { 435 args = request.readArgs(method); 437 } 438 if (_log.isDebugEnabled()) { 439 _log.debug("Invoking " + method + " on " + object); 440 } 441 _caller.set(caller); 442 Object result = method.invoke(object, args); 443 response = new Response(result, method); 444 } catch (InvocationTargetException exception) { 445 Throwable target = exception.getTargetException(); 446 if (target == null) { 447 target = exception; 448 } 449 response = new Response(target); 450 } catch (Throwable exception) { 451 response = new Response(exception); 452 } finally { 453 _caller.set(null); 454 } 455 return response; 456 } 457 458 463 public void invoke(Invocation invocation) { 464 Response response; 465 try { 466 Request request = invocation.getRequest(); 467 Caller caller = invocation.getCaller(); 468 response = invoke(request, caller); 469 } catch (Throwable exception) { 470 response = new Response(exception); 471 } 472 473 invocation.setResponse(response); 474 } 475 476 } 477 478 482 private static class DummyAuthenticator implements Authenticator { 483 484 492 public boolean authenticate(Principal principal) 493 throws ResourceException { 494 return true; 495 } 496 } 497 498 } 499 | Popular Tags |