1 45 package org.exolab.jms.net.orb; 46 47 import java.rmi.NoSuchObjectException ; 48 import java.rmi.RemoteException ; 49 import java.rmi.StubNotFoundException ; 50 import java.rmi.server.ExportException ; 51 import java.rmi.server.ObjID ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 55 import org.exolab.jms.net.proxy.Proxy; 56 import org.exolab.jms.net.uri.InvalidURIException; 57 import org.exolab.jms.net.uri.URI; 58 import org.exolab.jms.net.uri.URIHelper; 59 60 61 67 public abstract class AbstractORB implements ORB { 68 69 72 private HashMap _objIDMap = new HashMap (); 73 74 77 private HashMap _objectMap = new HashMap (); 78 79 82 private final Map _properties; 83 84 87 private final String _defaultURI; 88 89 93 private HashMap _routes = new HashMap (); 94 95 98 private ClassLoader _loader; 99 100 101 108 public AbstractORB(ClassLoader loader, Map properties) { 109 if (loader == null) { 110 throw new IllegalArgumentException ("Argument 'loader' is null"); 111 } 112 _loader = loader; 113 if (properties != null) { 114 _properties = properties; 115 _defaultURI = (String ) properties.get(PROVIDER_URI); 116 } else { 117 _properties = new HashMap (); 118 _defaultURI = null; 119 } 120 } 121 122 129 public synchronized void addRoute(String uri, String toURI) 130 throws RemoteException { 131 if (uri == null) { 132 throw new IllegalArgumentException ("Argument 'uri' is null"); 133 } 134 if (toURI == null) { 135 throw new IllegalArgumentException ("Argument 'toURI' is null"); 136 } 137 _routes.put(URIHelper.parse(uri), URIHelper.parse(toURI)); 138 } 139 140 150 public synchronized Proxy getProxy(Object object, String uri) 151 throws NoSuchObjectException { 152 153 ObjectRef ref = (ObjectRef) _objectMap.get(object); 157 if (ref == null) { 158 throw new NoSuchObjectException ("Object not exported"); 159 } 160 URI parsed = null; 161 if (uri != null) { 162 try { 163 parsed = URIHelper.parse(uri); 164 } catch (InvalidURIException exception) { 165 throw new NoSuchObjectException (exception.getMessage()); 166 } 167 } 168 return ref.getProxy(parsed); 169 } 170 171 181 public synchronized Object getObject(ObjID objID, String uri) 182 throws NoSuchObjectException { 183 184 ObjectRef ref = (ObjectRef) _objIDMap.get(objID); 188 if (ref == null) { 189 throw new NoSuchObjectException ("Object not exported"); 190 } 191 return ref.getObject(); 194 } 195 196 204 public Proxy exportObject(Object object) 205 throws ExportException , StubNotFoundException { 206 return exportObject(object, _defaultURI); 207 } 208 209 218 public synchronized Proxy exportObject(Object object, String uri) 219 throws ExportException , StubNotFoundException { 220 221 if (object == null) { 222 throw new IllegalArgumentException ("Argument 'object' is null"); 223 } 224 if (uri == null) { 225 throw new IllegalArgumentException ("Argument 'uri' is null"); 226 } 227 228 URI parsed = null; 229 try { 230 parsed = URIHelper.parse(uri); 231 } catch (InvalidURIException exception) { 232 throw new ExportException (exception.getMessage(), exception); 233 } 234 235 Proxy proxy = null; 236 ObjectRef ref = (ObjectRef) _objectMap.get(object); 237 if (ref != null) { 238 proxy = addProxy(ref, parsed, object, ref.getProxyClass()); 239 } else { 240 ObjID objID = new ObjID (); 241 proxy = doExport(object, objID, parsed, getProxyClass(object)); 242 } 243 244 return proxy; 245 } 246 247 256 public Proxy exportObject(Object object, ObjID objID) 257 throws ExportException , StubNotFoundException { 258 return exportObject(object, objID, _defaultURI); 259 } 260 261 271 public synchronized Proxy exportObject(Object object, ObjID objID, 272 String uri) 273 throws ExportException , StubNotFoundException { 274 275 if (object == null) { 276 throw new IllegalArgumentException ("Argument 'object' is null"); 277 } 278 if (objID == null) { 279 throw new IllegalArgumentException ("Argument 'objID' is null"); 280 } 281 if (uri == null) { 282 throw new IllegalArgumentException ("Argument 'uri' is null"); 283 } 284 URI parsed = null; 285 try { 286 parsed = URIHelper.parse(uri); 287 } catch (InvalidURIException exception) { 288 throw new ExportException (exception.getMessage(), exception); 289 290 } 291 Proxy proxy = null; 292 ObjectRef ref = (ObjectRef) _objectMap.get(object); 293 if (ref != null) { 294 proxy = addProxy(ref, parsed, object, ref.getProxyClass()); 295 } else { 296 proxy = doExport(object, objID, parsed, getProxyClass(object)); 297 } 298 return proxy; 299 } 300 301 311 public Proxy exportObjectTo(Object object, String uri) 312 throws ExportException , StubNotFoundException { 313 return exportObjectTo(object, uri, null, null); 314 } 315 316 329 public Proxy exportObjectTo(Object object, String uri, String principal, 330 String credentials) 331 throws ExportException , StubNotFoundException { 332 if (object == null) { 333 throw new IllegalArgumentException ("Argument 'object' is null"); 334 } 335 if (uri == null) { 336 throw new IllegalArgumentException ("Argument 'uri' is null"); 337 } 338 URI remoteURI = null; 339 URI localURI = null; 340 try { 341 remoteURI = URIHelper.parse(uri); 342 } catch (InvalidURIException exception) { 343 throw new ExportException (exception.getMessage(), exception); 344 } 345 346 localURI = connect(remoteURI, principal, credentials); 347 348 return doExportTo(object, localURI); 349 } 350 351 357 public synchronized void unexportObject(Object object) 358 throws NoSuchObjectException { 359 360 ObjectRef ref = (ObjectRef) _objectMap.remove(object); 361 if (ref != null) { 362 _objIDMap.remove(ref.getObjID()); 363 } else { 364 throw new NoSuchObjectException ("Object not exported"); 365 } 366 } 367 368 378 protected abstract URI connect(URI uri, String principal, 379 String credentials) throws ExportException ; 380 381 387 protected abstract void accept(URI uri) throws ExportException ; 388 389 394 protected ClassLoader getProxyClassLoader() { 395 return _loader; 396 } 397 398 403 protected Map getProperties() { 404 return _properties; 405 } 406 407 415 protected Proxy doExportTo(Object object, URI uri) 416 throws ExportException , StubNotFoundException { 417 Proxy proxy = null; 418 ObjectRef ref = (ObjectRef) _objectMap.get(object); 419 if (ref != null) { 420 proxy = addProxyTo(ref, uri, object, ref.getProxyClass()); 421 } else { 422 ObjID objID = new ObjID (); 423 proxy = doExportTo(object, objID, uri, getProxyClass(object)); 424 } 425 426 return proxy; 427 } 428 429 439 private Proxy doExport(Object object, ObjID objID, URI uri, 440 Class proxyClass) throws ExportException { 441 accept(uri); 442 ObjectRef ref = new ObjectRef(objID, object, proxyClass); 443 Proxy proxy = ref.addProxy(getRoute(uri)); 444 _objIDMap.put(objID, ref); 445 _objectMap.put(object, ref); 446 return proxy; 447 } 448 449 459 private Proxy doExportTo(Object object, ObjID objID, URI uri, 460 Class proxyClass) throws ExportException { 461 ObjectRef ref = new ObjectRef(objID, object, proxyClass); 462 Proxy proxy = ref.addProxy(getRoute(uri)); 463 _objIDMap.put(objID, ref); 464 _objectMap.put(object, ref); 465 return proxy; 466 } 467 468 478 private Proxy addProxy(ObjectRef ref, URI uri, Object object, 479 Class proxyClass) throws ExportException { 480 481 if (object != ref.getObject()) { 482 throw new ExportException ("Cannot export object on URI=" + uri 483 + ": object mismatch"); 484 } 485 if (proxyClass != ref.getProxyClass()) { 486 throw new ExportException ("Cannot export object on URI=" + uri 487 + ": proxy class mismatch"); 488 } 489 490 accept(uri); 491 return ref.addProxy(getRoute(uri)); 492 } 493 494 504 private Proxy addProxyTo(ObjectRef ref, URI uri, Object object, 505 Class proxyClass) throws ExportException { 506 507 if (object != ref.getObject()) { 508 throw new ExportException ("Cannot export object on URI=" + uri 509 + ": object mismatch"); 510 } 511 if (proxyClass != ref.getProxyClass()) { 512 throw new ExportException ("Cannot export object on URI=" + uri 513 + ": proxy class mismatch"); 514 } 515 516 return ref.addProxy(uri); 517 } 518 519 526 private Class getProxyClass(Object object) throws StubNotFoundException { 527 return getProxyClass(object.getClass()); 528 } 529 530 537 private Class getProxyClass(Class clazz) throws StubNotFoundException { 538 String proxyName = clazz.getName() + "__Proxy"; 539 Class proxyClass = null; 540 try { 541 proxyClass = _loader.loadClass(proxyName); 542 if (!Proxy.class.isAssignableFrom(proxyClass)) { 543 throw new StubNotFoundException (proxyName); 544 } 545 } catch (ClassNotFoundException exception) { 546 Class superClass = clazz.getSuperclass(); 547 if (superClass != null && !superClass.isInterface()) { 548 proxyClass = getProxyClass(superClass); 549 } else { 550 throw new StubNotFoundException (proxyName); 551 } 552 } 553 return proxyClass; 554 } 555 556 563 private URI getRoute(URI uri) { 564 URI result = (URI) _routes.get(uri); 565 return (result == null) ? uri : result; 566 } 567 568 } 569 | Popular Tags |