KickJava   Java API By Example, From Geeks To Geeks.

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


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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ObjectRef.java,v 1.1 2004/11/26 01:51:04 tanderson Exp $
44  */

45 package org.exolab.jms.net.orb;
46
47 import java.lang.reflect.Constructor JavaDoc;
48 import java.lang.reflect.InvocationTargetException JavaDoc;
49 import java.rmi.NoSuchObjectException JavaDoc;
50 import java.rmi.server.ExportException JavaDoc;
51 import java.rmi.server.ObjID JavaDoc;
52 import java.util.HashMap JavaDoc;
53
54 import org.exolab.jms.net.proxy.Delegate;
55 import org.exolab.jms.net.proxy.Proxy;
56 import org.exolab.jms.net.uri.URI;
57
58
59 /**
60  * Maintains state information for an exported object
61  *
62  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
63  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:04 $
64  */

65 class ObjectRef {
66
67     /**
68      * The identity of the object
69      */

70     private ObjID JavaDoc _objID;
71
72     /**
73      * The exported object
74      */

75     private Object JavaDoc _object;
76
77     /**
78      * The proxy class of the exported object, implementing the {@link Proxy}
79      * interface
80      */

81     private Class JavaDoc _proxyClass;
82
83     /**
84      * The set of {@link UnicastDelegate} instances, keyed on URI
85      */

86     private HashMap JavaDoc _proxies = new HashMap JavaDoc();
87
88     /**
89      * Helper for Proxy constructor resolution
90      */

91     private static final Class JavaDoc[] PROXY_ARGS = new Class JavaDoc[]{Delegate.class};
92
93     /**
94      * Construct a new <code>ObjectRef</code>
95      *
96      * @param objID the object identity
97      * @param object the exported object
98      * @param proxyClass the proxy class of the exported object, implementing
99      * the {@link Proxy} interface
100      */

101     public ObjectRef(ObjID JavaDoc objID, Object JavaDoc object, Class JavaDoc proxyClass) {
102         _objID = objID;
103         _object = object;
104         _proxyClass = proxyClass;
105     }
106
107     /**
108      * Returns the identity of the exported object
109      *
110      * @return the object identity
111      */

112     public ObjID JavaDoc getObjID() {
113         return _objID;
114     }
115
116     /**
117      * Returns the exported object
118      *
119      * @return the exported object
120      */

121     public Object JavaDoc getObject() {
122         return _object;
123     }
124
125     /**
126      * Returns the proxy class of the exported object. This implements the
127      * {@link Proxy} interface
128      *
129      * @return the proxy class
130      */

131     public Class JavaDoc getProxyClass() {
132         return _proxyClass;
133     }
134
135     /**
136      * Add a proxy for the object
137      *
138      * @param uri the connection URI
139      * @return a proxy for the object
140      * @throws ExportException if the proxy can't be constructed
141      */

142     public synchronized Proxy addProxy(URI uri) throws ExportException JavaDoc {
143         Proxy proxy;
144         try {
145             Delegate delegate = new UnicastDelegate(_objID, uri.toString(),
146                                                     null);
147             Constructor JavaDoc constructor = _proxyClass.getConstructor(PROXY_ARGS);
148             proxy = (Proxy) constructor.newInstance(new Object JavaDoc[]{delegate});
149         } catch (InvocationTargetException JavaDoc exception) {
150             if (exception.getTargetException() instanceof Exception JavaDoc) {
151                 Exception JavaDoc nested = (Exception JavaDoc) exception.getTargetException();
152                 throw new ExportException JavaDoc(nested.getMessage(), nested);
153             } else {
154                 throw new ExportException JavaDoc(exception.getMessage(), exception);
155             }
156         } catch (Exception JavaDoc exception) {
157             throw new ExportException JavaDoc(exception.getMessage(), exception);
158         }
159         _proxies.put(uri, proxy);
160         return proxy;
161     }
162
163     /**
164      * Returns the proxy for the specified URI
165      *
166      * @param uri the connection URI
167      * @return a proxy for the object
168      * @throws NoSuchObjectException if the object isn't exported on the
169      * specified URI
170      */

171     public synchronized Proxy getProxy(URI uri)
172             throws NoSuchObjectException JavaDoc {
173
174         Proxy proxy = (Proxy) _proxies.get(uri);
175         if (proxy == null) {
176             throw new NoSuchObjectException JavaDoc(
177                     "Object not exported on URI=" + uri);
178         }
179         return proxy;
180     }
181
182     /**
183      * Returns a list of URIs that the object is exported on
184      *
185      * @return a list of URIs that the object is exported on
186      */

187     public synchronized URI[] getURIs() {
188         return (URI[]) _proxies.keySet().toArray(new URI[0]);
189     }
190
191     /**
192      * Determines if this equals another object.
193      *
194      * @param object the object to compare
195      * @return <code>true</code> if this is equal, otherwise <code>false</code>
196      */

197     public boolean equals(Object JavaDoc object) {
198         boolean equal = (this == object);
199         if (!equal && (object instanceof ObjectRef)) {
200             equal = _objID.equals(((ObjectRef) object)._objID);
201         }
202         return equal;
203     }
204
205     /**
206      * Returns the hash code of this
207      *
208      * @return the hash code of this
209      */

210     public int hashCode() {
211         return _objID.hashCode();
212     }
213
214 }
215
Popular Tags