KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > orb > AbstractORB


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2003-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: AbstractORB.java,v 1.7 2005/06/04 14:46:00 tanderson Exp $
44  */

45 package org.exolab.jms.net.orb;
46
47 import java.rmi.NoSuchObjectException JavaDoc;
48 import java.rmi.RemoteException JavaDoc;
49 import java.rmi.StubNotFoundException JavaDoc;
50 import java.rmi.server.ExportException JavaDoc;
51 import java.rmi.server.ObjID JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Map JavaDoc;
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 /**
62  * Abstract implementation of the {@link ORB} interface.
63  *
64  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
65  * @version $Revision: 1.7 $ $Date: 2005/06/04 14:46:00 $
66  */

67 public abstract class AbstractORB implements ORB {
68
69     /**
70      * A map of ObjID -> ObjectRef instances.
71      */

72     private HashMap JavaDoc _objIDMap = new HashMap JavaDoc();
73
74     /**
75      * A map of Object -> ObjectRef instances.
76      */

77     private HashMap JavaDoc _objectMap = new HashMap JavaDoc();
78
79     /**
80      * Configuration properties.
81      */

82     private final Map JavaDoc _properties;
83
84     /**
85      * The default URI for exported objects.
86      */

87     private final String JavaDoc _defaultURI;
88
89     /**
90      * A map of routes for exported objects. The key is a URI representing the
91      * URI the objects are listening on. The value is the URI of the router.
92      */

93     private HashMap JavaDoc _routes = new HashMap JavaDoc();
94
95     /**
96      * The class loader used to load proxies.
97      */

98     private ClassLoader JavaDoc _loader;
99
100
101     /**
102      * Construct a new <code>AbstractORB</code>.
103      *
104      * @param loader the class loader to load proxies
105      * @param properties properties to configure this with. May be
106      * <code>null</code>.
107      */

108     public AbstractORB(ClassLoader JavaDoc loader, Map JavaDoc properties) {
109         if (loader == null) {
110             throw new IllegalArgumentException JavaDoc("Argument 'loader' is null");
111         }
112         _loader = loader;
113         if (properties != null) {
114             _properties = properties;
115             _defaultURI = (String JavaDoc) properties.get(PROVIDER_URI);
116         } else {
117             _properties = new HashMap JavaDoc();
118             _defaultURI = null;
119         }
120     }
121
122     /**
123      * Add a route for exported objects.
124      *
125      * @param uri the URI to route
126      * @param toURI the URI to route to
127      * @throws RemoteException for any error
128      */

129     public synchronized void addRoute(String JavaDoc uri, String JavaDoc toURI)
130             throws RemoteException JavaDoc {
131         if (uri == null) {
132             throw new IllegalArgumentException JavaDoc("Argument 'uri' is null");
133         }
134         if (toURI == null) {
135             throw new IllegalArgumentException JavaDoc("Argument 'toURI' is null");
136         }
137         _routes.put(URIHelper.parse(uri), URIHelper.parse(toURI));
138     }
139
140     /**
141      * Returns the proxy associated with the specified object, and URI.
142      *
143      * @param object the object to look up the proxy for
144      * @param uri the URI the object was exported on
145      * @return the proxy corresponding to <code>object</code> and
146      * <code>uri</code>
147      * @throws NoSuchObjectException if the object hasn't been exported on the
148      * specified URI
149      */

150     public synchronized Proxy getProxy(Object JavaDoc object, String JavaDoc uri)
151             throws NoSuchObjectException JavaDoc {
152
153 // if (uri == null) {
154
// throw new IllegalArgumentException("Argument 'uri' is null");
155
// }
156
ObjectRef ref = (ObjectRef) _objectMap.get(object);
157         if (ref == null) {
158             throw new NoSuchObjectException JavaDoc("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 JavaDoc(exception.getMessage());
166             }
167         }
168         return ref.getProxy(parsed);
169     }
170
171     /**
172      * Returns the object associated with the specified ID, and URI.
173      *
174      * @param objID the identifier of the object
175      * @param uri the URI the object was exported on
176      * @return the object corresponding to <code>objID</code> and
177      * <code>uri</code>
178      * @throws NoSuchObjectException if the object hasn't been exported on the
179      * specified URI
180      */

181     public synchronized Object JavaDoc getObject(ObjID JavaDoc objID, String JavaDoc uri)
182             throws NoSuchObjectException JavaDoc {
183
184 // if (uri == null) {
185
// throw new IllegalArgumentException("Argument 'uri' is null");
186
// }
187
ObjectRef ref = (ObjectRef) _objIDMap.get(objID);
188         if (ref == null) {
189             throw new NoSuchObjectException JavaDoc("Object not exported");
190         }
191         // ref.getProxy(uri);
192
// ensures it has been exported on the specified uri
193
return ref.getObject();
194     }
195
196     /**
197      * Export an object on a default URI.
198      *
199      * @param object the object to export
200      * @return a proxy which may be used to invoke methods on the object
201      * @throws ExportException if the object cannot be exported
202      * @throws StubNotFoundException if the proxy class cannot be found
203      */

204     public Proxy exportObject(Object JavaDoc object)
205             throws ExportException JavaDoc, StubNotFoundException JavaDoc {
206         return exportObject(object, _defaultURI);
207     }
208
209     /**
210      * Export an object on a specific URI.
211      *
212      * @param object the object to export
213      * @param uri the URI via which connections to the object are made
214      * @return a proxy which may be used to invoke methods on the object
215      * @throws ExportException if the object cannot be exported
216      * @throws StubNotFoundException if the proxy class cannot be found
217      */

218     public synchronized Proxy exportObject(Object JavaDoc object, String JavaDoc uri)
219             throws ExportException JavaDoc, StubNotFoundException JavaDoc {
220
221         if (object == null) {
222             throw new IllegalArgumentException JavaDoc("Argument 'object' is null");
223         }
224         if (uri == null) {
225             throw new IllegalArgumentException JavaDoc("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 JavaDoc(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 JavaDoc objID = new ObjID JavaDoc();
241             proxy = doExport(object, objID, parsed, getProxyClass(object));
242         }
243
244         return proxy;
245     }
246
247     /**
248      * Export an object with a well known identifier on a default URI.
249      *
250      * @param object the object to export
251      * @param objID the well known object identifier
252      * @return a proxy which may be used to invoke methods on the object
253      * @throws ExportException if the object cannot be exported
254      * @throws StubNotFoundException if the proxy class cannot be found
255      */

256     public Proxy exportObject(Object JavaDoc object, ObjID JavaDoc objID)
257             throws ExportException JavaDoc, StubNotFoundException JavaDoc {
258         return exportObject(object, objID, _defaultURI);
259     }
260
261     /**
262      * Export an object with a well known identifier on a specific URI.
263      *
264      * @param object the object to export
265      * @param objID the well known object identifier
266      * @param uri the URI via which connections to the object are made
267      * @return a proxy which may be used to invoke methods on the object
268      * @throws ExportException if the object cannot be exported
269      * @throws StubNotFoundException if the proxy class cannot be found
270      */

271     public synchronized Proxy exportObject(Object JavaDoc object, ObjID JavaDoc objID,
272                                            String JavaDoc uri)
273             throws ExportException JavaDoc, StubNotFoundException JavaDoc {
274
275         if (object == null) {
276             throw new IllegalArgumentException JavaDoc("Argument 'object' is null");
277         }
278         if (objID == null) {
279             throw new IllegalArgumentException JavaDoc("Argument 'objID' is null");
280         }
281         if (uri == null) {
282             throw new IllegalArgumentException JavaDoc("Argument 'uri' is null");
283         }
284         URI parsed = null;
285         try {
286             parsed = URIHelper.parse(uri);
287         } catch (InvalidURIException exception) {
288             throw new ExportException JavaDoc(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     /**
302      * Export an object to a specific URI.
303      *
304      * @param object the object to export
305      * @param uri the target URI from which connections to the object are
306      * made.
307      * @return a proxy which may be used to invoke methods on the object
308      * @throws ExportException if the object cannot be exported
309      * @throws StubNotFoundException if the proxy class cannot be found
310      */

311     public Proxy exportObjectTo(Object JavaDoc object, String JavaDoc uri)
312             throws ExportException JavaDoc, StubNotFoundException JavaDoc {
313         return exportObjectTo(object, uri, null, null);
314     }
315
316     /**
317      * Export an object to a specific URI. Only callers from the target URI may
318      * perform invocations.
319      *
320      * @param object the object to export
321      * @param uri the target URI from which connections to the object
322      * are made.
323      * @param principal the security principal. May be <code>null</code>
324      * @param credentials the security credentials. May be <code>null</code>
325      * @return a proxy which may be used to invoke methods on the object
326      * @throws ExportException if the object cannot be exported
327      * @throws StubNotFoundException if the proxy class cannot be found
328      */

329     public Proxy exportObjectTo(Object JavaDoc object, String JavaDoc uri, String JavaDoc principal,
330                                 String JavaDoc credentials)
331             throws ExportException JavaDoc, StubNotFoundException JavaDoc {
332         if (object == null) {
333             throw new IllegalArgumentException JavaDoc("Argument 'object' is null");
334         }
335         if (uri == null) {
336             throw new IllegalArgumentException JavaDoc("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 JavaDoc(exception.getMessage(), exception);
344         }
345
346         localURI = connect(remoteURI, principal, credentials);
347
348         return doExportTo(object, localURI);
349     }
350
351     /**
352      * Unexport an object.
353      *
354      * @param object the object to export
355      * @throws NoSuchObjectException if the object isn't exported
356      */

357     public synchronized void unexportObject(Object JavaDoc object)
358             throws NoSuchObjectException JavaDoc {
359
360         ObjectRef ref = (ObjectRef) _objectMap.remove(object);
361         if (ref != null) {
362             _objIDMap.remove(ref.getObjID());
363         } else {
364             throw new NoSuchObjectException JavaDoc("Object not exported");
365         }
366     }
367
368     /**
369      * Connect to the specified URI.
370      *
371      * @param uri the URI to establish a connection with
372      * @param principal specifies the identity of the principal. If
373      * <code>null</code>, indicates to connect anonymously.
374      * @param credentials the credentials of the principal
375      * @return the local address that the connection is bound to
376      * @throws ExportException for any error
377      */

378     protected abstract URI connect(URI uri, String JavaDoc principal,
379                                    String JavaDoc credentials) throws ExportException JavaDoc;
380
381     /**
382      * Accept connections on the specified URI.
383      *
384      * @param uri the URI to accept connections on
385      * @throws ExportException for any error
386      */

387     protected abstract void accept(URI uri) throws ExportException JavaDoc;
388
389     /**
390      * Returns the proxy class loader.
391      *
392      * @return the proxy class loader
393      */

394     protected ClassLoader JavaDoc getProxyClassLoader() {
395         return _loader;
396     }
397
398     /**
399      * Returns the configuration properties.
400      *
401      * @return the configuration properties
402      */

403     protected Map JavaDoc getProperties() {
404         return _properties;
405     }
406
407     /**
408      * Export an object to a specific URI.
409      *
410      * @param object the object to export
411      * @param uri the URI via which connections to the object are made
412      * @throws ExportException if the object cannot be exported
413      * @throws StubNotFoundException if the proxy class cannot be found
414      */

415     protected Proxy doExportTo(Object JavaDoc object, URI uri)
416             throws ExportException JavaDoc, StubNotFoundException JavaDoc {
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 JavaDoc objID = new ObjID JavaDoc();
423             proxy = doExportTo(object, objID, uri, getProxyClass(object));
424         }
425
426         return proxy;
427     }
428     
429     /**
430      * Export an object on a specific URI.
431      *
432      * @param object the object to export
433      * @param objID the identifier of the object
434      * @param uri the URI via which connections to the object are made
435      * @param proxyClass the proxy class
436      * @return a proxy which may be used to invoke methods on the object
437      * @throws ExportException if the object cannot be exported
438      */

439     private Proxy doExport(Object JavaDoc object, ObjID JavaDoc objID, URI uri,
440                            Class JavaDoc proxyClass) throws ExportException JavaDoc {
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     /**
450      * Export an object to a specific URI.
451      *
452      * @param object the object to export
453      * @param objID the identifier of the object
454      * @param uri the URI via which connections to the object are made
455      * @param proxyClass the proxy class
456      * @return a proxy which may be used to invoke methods on the object
457      * @throws ExportException if the object cannot be exported
458      */

459     private Proxy doExportTo(Object JavaDoc object, ObjID JavaDoc objID, URI uri,
460                              Class JavaDoc proxyClass) throws ExportException JavaDoc {
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     /**
469      * Add a proxy for an exported object.
470      *
471      * @param ref a reference to the exported object
472      * @param uri the URI via which connections to the object are made
473      * @param object the exported object
474      * @param proxyClass the proxy class
475      * @return a proxy which may be used to invoke methods on the object
476      * @throws ExportException if the object cannot be exported
477      */

478     private Proxy addProxy(ObjectRef ref, URI uri, Object JavaDoc object,
479                            Class JavaDoc proxyClass) throws ExportException JavaDoc {
480
481         if (object != ref.getObject()) {
482             throw new ExportException JavaDoc("Cannot export object on URI=" + uri
483                                       + ": object mismatch");
484         }
485         if (proxyClass != ref.getProxyClass()) {
486             throw new ExportException JavaDoc("Cannot export object on URI=" + uri
487                                       + ": proxy class mismatch");
488         }
489
490         accept(uri);
491         return ref.addProxy(getRoute(uri));
492     }
493
494     /**
495      * Add a proxy for an exported object.
496      *
497      * @param ref a reference to the exported object
498      * @param uri the URI via which connections to the object are made
499      * @param object the exported object
500      * @param proxyClass the proxy class
501      * @return a proxy which may be used to invoke methods on the object
502      * @throws ExportException if the object cannot be exported
503      */

504     private Proxy addProxyTo(ObjectRef ref, URI uri, Object JavaDoc object,
505                              Class JavaDoc proxyClass) throws ExportException JavaDoc {
506
507         if (object != ref.getObject()) {
508             throw new ExportException JavaDoc("Cannot export object on URI=" + uri
509                                       + ": object mismatch");
510         }
511         if (proxyClass != ref.getProxyClass()) {
512             throw new ExportException JavaDoc("Cannot export object on URI=" + uri
513                                       + ": proxy class mismatch");
514         }
515
516         return ref.addProxy(uri);
517     }
518
519     /**
520      * Loads the proxy class for the supplied object.
521      *
522      * @param object the object to load the proxy for
523      * @return the proxy class corresponding to <code>object</code>
524      * @throws StubNotFoundException if the proxy class cannot be loaded
525      */

526     private Class JavaDoc getProxyClass(Object JavaDoc object) throws StubNotFoundException JavaDoc {
527         return getProxyClass(object.getClass());
528     }
529
530     /**
531      * Loads the proxy class for the supplied class.
532      *
533      * @param clazz the class to load the proxy for
534      * @return the proxy class corresponding to <code>class</code>
535      * @throws StubNotFoundException if the proxy class cannot be loaded
536      */

537     private Class JavaDoc getProxyClass(Class JavaDoc clazz) throws StubNotFoundException JavaDoc {
538         String JavaDoc proxyName = clazz.getName() + "__Proxy";
539         Class JavaDoc proxyClass = null;
540         try {
541             proxyClass = _loader.loadClass(proxyName);
542             if (!Proxy.class.isAssignableFrom(proxyClass)) {
543                 throw new StubNotFoundException JavaDoc(proxyName);
544             }
545         } catch (ClassNotFoundException JavaDoc exception) {
546             Class JavaDoc superClass = clazz.getSuperclass();
547             if (superClass != null && !superClass.isInterface()) {
548                 proxyClass = getProxyClass(superClass);
549             } else {
550                 throw new StubNotFoundException JavaDoc(proxyName);
551             }
552         }
553         return proxyClass;
554     }
555
556     /**
557      * Returns the route address of a URI.
558      *
559      * @param uri the URI
560      * @return the route address of <code>uri</code>, or <code>uri</code> if it
561      * isn't routed
562      */

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