1 45 package org.exolab.jms.net.rmi; 46 47 import java.rmi.RemoteException ; 48 import java.rmi.registry.LocateRegistry ; 49 import java.rmi.registry.Registry ; 50 import java.rmi.server.UnicastRemoteObject ; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 55 import org.exolab.jms.net.connector.Authenticator; 56 import org.exolab.jms.net.connector.ManagedConnectionAcceptor; 57 import org.exolab.jms.net.connector.ManagedConnectionAcceptorListener; 58 import org.exolab.jms.net.connector.ResourceException; 59 import org.exolab.jms.net.uri.URI; 60 import org.exolab.jms.net.uri.URIHelper; 61 62 63 71 class RMIManagedConnectionAcceptor implements ManagedConnectionAcceptor { 72 73 76 private final Authenticator _authenticator; 77 78 81 private final URI _uri; 82 83 86 private final boolean _embedRegistry; 87 88 91 private Registry _registry; 92 93 96 private boolean _created = false; 97 98 101 private RMIInvokerFactory _factory; 102 103 106 private static final Log _log = 107 LogFactory.getLog(RMIManagedConnectionAcceptor.class); 108 109 110 116 public RMIManagedConnectionAcceptor(Authenticator authenticator, 117 RMIRequestInfo info) { 118 _authenticator = authenticator; 119 _uri = URIHelper.convertHostToAddress(info.getURI()); 120 _embedRegistry = info.getEmbedRegistry(); 121 } 122 123 129 public void accept(ManagedConnectionAcceptorListener listener) 130 throws ResourceException { 131 Registry registry = null; 132 133 int port = _uri.getPort(); 134 if (_embedRegistry) { 135 try { 136 registry = LocateRegistry.createRegistry(port); 137 _created = true; 138 } catch (RemoteException exception) { 139 if (_log.isDebugEnabled()) { 140 _log.debug("Failed to create registry on port=" + port 141 + ", attempting to locate one", exception); 142 } 143 } 144 } 145 if (registry == null) { 146 try { 147 registry = LocateRegistry.getRegistry(port); 148 } catch (RemoteException nested) { 149 throw new ResourceException( 150 "Failed to create or locate a registry, port=" + port, 151 nested); 152 } 153 } 154 155 _factory = new RMIInvokerFactoryImpl(_authenticator, this, listener); 156 157 try { 158 UnicastRemoteObject.exportObject(_factory, port); 159 } catch (RemoteException exception) { 160 throw new ResourceException("Failed to export object", exception); 161 } 162 163 RegistryHelper.bind(_factory, _uri, registry); 164 _registry = registry; 165 } 166 167 172 public URI getURI() { 173 return _uri; 174 } 175 176 181 public synchronized void close() throws ResourceException { 182 if (_registry != null) { 183 try { 184 RegistryHelper.unbind(_factory, _uri, _registry); 185 if (!UnicastRemoteObject.unexportObject(_factory, true)) { 186 _log.warn("Failed to unexport invoker factory"); 187 } 188 189 if (_created && !RegistryHelper.hasBindings(_registry)) { 190 193 if (!UnicastRemoteObject.unexportObject(_registry, true)) { 194 _log.warn("Failed to unexport registry"); 195 } 196 } 197 } catch (RemoteException exception) { 198 throw new ResourceException( 199 "Failed to close connection acceptor", exception); 200 } finally { 201 _factory = null; 202 _registry = null; 203 } 204 } 205 } 206 207 } 208 | Popular Tags |