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: ObjectRef.java,v 1.2 2005/11/16 12:32:49 tanderson Exp $
44 */
45 package org.exolab.jms.net.orb;
46
47 import java.lang.reflect.Constructor;
48 import java.lang.reflect.InvocationTargetException;
49 import java.rmi.NoSuchObjectException;
50 import java.rmi.server.ExportException;
51 import java.rmi.server.ObjID;
52 import java.util.HashMap;
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.2 $ $Date: 2005/11/16 12:32:49 $
64 */
65 class ObjectRef {
66
67 /***
68 * The identity of the object.
69 */
70 private ObjID _objID;
71
72 /***
73 * The exported object.
74 */
75 private Object _object;
76
77 /***
78 * The proxy class of the exported object, implementing the {@link Proxy}
79 * interface.
80 */
81 private Class _proxyClass;
82
83 /***
84 * The set of {@link UnicastDelegate} instances, keyed on URI.
85 */
86 private HashMap _proxies = new HashMap();
87
88 /***
89 * Helper for Proxy constructor resolution.
90 */
91 private static final Class[] PROXY_ARGS = new Class[]{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 objID, Object object, Class 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 getObjID() {
113 return _objID;
114 }
115
116 /***
117 * Returns the exported object.
118 *
119 * @return the exported object
120 */
121 public Object 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 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 {
143 Proxy proxy;
144 try {
145 Delegate delegate = new UnicastDelegate(_objID, uri.toString());
146 Constructor constructor = _proxyClass.getConstructor(PROXY_ARGS);
147 proxy = (Proxy) constructor.newInstance(new Object[]{delegate});
148 } catch (InvocationTargetException exception) {
149 if (exception.getTargetException() instanceof Exception) {
150 Exception nested = (Exception) exception.getTargetException();
151 throw new ExportException(nested.getMessage(), nested);
152 } else {
153 throw new ExportException(exception.getMessage(), exception);
154 }
155 } catch (Exception exception) {
156 throw new ExportException(exception.getMessage(), exception);
157 }
158 _proxies.put(uri, proxy);
159 return proxy;
160 }
161
162 /***
163 * Returns the proxy for the specified URI.
164 *
165 * @param uri the connection URI
166 * @return a proxy for the object
167 * @throws NoSuchObjectException if the object isn't exported on the
168 * specified URI
169 */
170 public synchronized Proxy getProxy(URI uri)
171 throws NoSuchObjectException {
172
173 Proxy proxy = (Proxy) _proxies.get(uri);
174 if (proxy == null) {
175 throw new NoSuchObjectException(
176 "Object not exported on URI=" + uri);
177 }
178 return proxy;
179 }
180
181 /***
182 * Returns a list of URIs that the object is exported on.
183 *
184 * @return a list of URIs that the object is exported on
185 */
186 public synchronized URI[] getURIs() {
187 return (URI[]) _proxies.keySet().toArray(new URI[0]);
188 }
189
190 /***
191 * Determines if this equals another object.
192 *
193 * @param object the object to compare
194 * @return <code>true</code> if this is equal, otherwise <code>false</code>
195 */
196 public boolean equals(Object object) {
197 boolean equal = (this == object);
198 if (!equal && (object instanceof ObjectRef)) {
199 equal = _objID.equals(((ObjectRef) object)._objID);
200 }
201 return equal;
202 }
203
204 /***
205 * Returns the hash code of this.
206 *
207 * @return the hash code of this
208 */
209 public int hashCode() {
210 return _objID.hashCode();
211 }
212
213 }