1 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 65 class ObjectRef { 66 67 70 private ObjID _objID; 71 72 75 private Object _object; 76 77 81 private Class _proxyClass; 82 83 86 private HashMap _proxies = new HashMap (); 87 88 91 private static final Class [] PROXY_ARGS = new Class []{Delegate.class}; 92 93 101 public ObjectRef(ObjID objID, Object object, Class proxyClass) { 102 _objID = objID; 103 _object = object; 104 _proxyClass = proxyClass; 105 } 106 107 112 public ObjID getObjID() { 113 return _objID; 114 } 115 116 121 public Object getObject() { 122 return _object; 123 } 124 125 131 public Class getProxyClass() { 132 return _proxyClass; 133 } 134 135 142 public synchronized Proxy addProxy(URI uri) throws ExportException { 143 Proxy proxy; 144 try { 145 Delegate delegate = new UnicastDelegate(_objID, uri.toString(), 146 null); 147 Constructor constructor = _proxyClass.getConstructor(PROXY_ARGS); 148 proxy = (Proxy) constructor.newInstance(new Object []{delegate}); 149 } catch (InvocationTargetException exception) { 150 if (exception.getTargetException() instanceof Exception ) { 151 Exception nested = (Exception ) exception.getTargetException(); 152 throw new ExportException (nested.getMessage(), nested); 153 } else { 154 throw new ExportException (exception.getMessage(), exception); 155 } 156 } catch (Exception exception) { 157 throw new ExportException (exception.getMessage(), exception); 158 } 159 _proxies.put(uri, proxy); 160 return proxy; 161 } 162 163 171 public synchronized Proxy getProxy(URI uri) 172 throws NoSuchObjectException { 173 174 Proxy proxy = (Proxy) _proxies.get(uri); 175 if (proxy == null) { 176 throw new NoSuchObjectException ( 177 "Object not exported on URI=" + uri); 178 } 179 return proxy; 180 } 181 182 187 public synchronized URI[] getURIs() { 188 return (URI[]) _proxies.keySet().toArray(new URI[0]); 189 } 190 191 197 public boolean equals(Object 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 210 public int hashCode() { 211 return _objID.hashCode(); 212 } 213 214 } 215 | Popular Tags |