1 45 package org.exolab.jms.net.vm; 46 47 import java.io.IOException ; 48 import java.rmi.MarshalException ; 49 import java.rmi.MarshalledObject ; 50 import java.security.Principal ; 51 52 import org.exolab.jms.common.uuid.UUIDGenerator; 53 import org.exolab.jms.net.connector.AbstractManagedConnection; 54 import org.exolab.jms.net.connector.Caller; 55 import org.exolab.jms.net.connector.CallerImpl; 56 import org.exolab.jms.net.connector.Connection; 57 import org.exolab.jms.net.connector.IllegalStateException; 58 import org.exolab.jms.net.connector.InvocationHandler; 59 import org.exolab.jms.net.connector.MarshalledInvocation; 60 import org.exolab.jms.net.connector.Request; 61 import org.exolab.jms.net.connector.ResourceException; 62 import org.exolab.jms.net.connector.Response; 63 import org.exolab.jms.net.connector.URIRequestInfo; 64 import org.exolab.jms.net.uri.InvalidURIException; 65 import org.exolab.jms.net.uri.URI; 66 import org.exolab.jms.net.uri.URIHelper; 67 68 69 76 class VMManagedConnection extends AbstractManagedConnection { 77 78 81 private VMInvoker _remoteInvoker; 82 83 86 private InvocationHandler _invoker; 87 88 91 private URI _remoteURI; 92 93 96 private URI _localURI; 97 98 101 private Principal _principal; 102 103 106 private Caller _caller; 107 108 109 116 protected VMManagedConnection(Principal principal, URIRequestInfo info) 117 throws ResourceException { 118 _remoteURI = info.getURI(); 119 try { 120 _localURI = URIHelper.create("vm", null, -1, 121 UUIDGenerator.create()); 122 } catch (InvalidURIException exception) { 123 throw new ResourceException("Failed to generate local URI", 124 exception); 125 } 126 VMInvoker invoker = new VMInvoker(this); 127 _remoteInvoker = VMManagedConnectionAcceptor.connect(principal, info, 128 invoker, 129 _localURI); 130 _principal = principal; 131 } 132 133 142 protected VMManagedConnection(Principal principal, URIRequestInfo info, 143 VMInvoker client, URI uri) { 144 _localURI = info.getURI(); 145 _remoteInvoker = client; 146 _remoteURI = uri; 147 _caller = new CallerImpl(_remoteURI, _localURI); 148 _principal = principal; 149 } 150 151 158 public synchronized Connection getConnection() 159 throws IllegalStateException { 160 if (_invoker == null) { 161 throw new IllegalStateException ("No InvocationHandler registered"); 162 } 163 return new VMConnection(this); 164 } 165 166 174 public synchronized void setInvocationHandler(InvocationHandler handler) 175 throws ResourceException { 176 if (_invoker != null) { 177 throw new IllegalStateException ( 178 "An invocation handler is already registered"); 179 } 180 _invoker = handler; 181 } 182 183 188 public boolean isAlive() { 189 boolean alive = false; 190 VMInvoker invoker = null; 191 synchronized (this) { 192 invoker = _remoteInvoker; 193 } 194 if (invoker != null) { 195 alive = invoker.isAlive(); 196 } 197 return alive; 198 } 199 200 205 public void destroy() throws ResourceException { 206 synchronized (this) { 207 _remoteInvoker = null; 208 } 209 } 210 211 216 public URI getRemoteURI() { 217 return _remoteURI; 218 } 219 220 225 public URI getLocalURI() { 226 return _localURI; 227 } 228 229 237 public boolean hasPrincipal(Principal principal) { 238 boolean result = false; 239 if ((_principal != null && _principal.equals(principal)) 240 || (_principal == null && principal == null)) { 241 result = true; 242 } 243 return result; 244 } 245 246 253 protected Response invoke(Connection connection, Request request) { 254 Response response; 255 try { 256 MarshalledObject wrappedRequest = new MarshalledObject (request); 257 MarshalledObject wrappedResponse = 258 _remoteInvoker.invoke(wrappedRequest); 259 response = (Response) wrappedResponse.get(); 260 } catch (ClassNotFoundException exception) { 261 response = new Response(exception); 262 } catch (IOException exception) { 263 response = new Response(exception); 264 } 265 return response; 266 } 267 268 276 protected MarshalledObject invokeLocal(MarshalledObject request) 277 throws MarshalException { 278 MarshalledInvocation invocation 279 = new MarshalledInvocation(request, _caller); 280 281 _invoker.invoke(invocation); 282 MarshalledObject response = null; 283 try { 284 response = invocation.getMarshalledResponse(); 285 } catch (IOException exception) { 286 throw new MarshalException ("Failed to marshal response", 287 exception); 288 } 289 return response; 290 } 291 292 297 protected boolean isAliveLocal() { 298 boolean alive = false; 299 synchronized (this) { 300 alive = (_remoteInvoker != null); 301 } 302 return alive; 303 } 304 305 } 306 | Popular Tags |