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: RMIManagedConnectionAcceptor.java,v 1.4 2006/12/16 12:37:17 tanderson Exp $
44 */
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 /***
64 * <code>RMIManagedConnectionAcceptor</code> is responsible for accepting
65 * connections, and constructing new <code>RMIManagedConnection</code> instances
66 * to serve them.
67 *
68 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
69 * @version $Revision: 1.4 $ $Date: 2006/12/16 12:37:17 $
70 */
71 class RMIManagedConnectionAcceptor implements ManagedConnectionAcceptor {
72
73 /***
74 * The connection authenticator.
75 */
76 private final Authenticator _authenticator;
77
78 /***
79 * The URI of this acceptor.
80 */
81 private final URI _uri;
82
83 /***
84 * Determines if the registry is embedded or not.
85 */
86 private final boolean _embedRegistry;
87
88 /***
89 * The registry.
90 */
91 private Registry _registry;
92
93 /***
94 * Determines if the registry was created by this.
95 */
96 private boolean _created = false;
97
98 /***
99 * The factory for invokers.
100 */
101 private RMIInvokerFactory _factory;
102
103 /***
104 * The logger.
105 */
106 private static final Log _log =
107 LogFactory.getLog(RMIManagedConnectionAcceptor.class);
108
109
110 /***
111 * Construct a new <code>RMIManagedConnectionAcceptor</code>.
112 *
113 * @param authenticator the connection authenticator
114 * @param info the connection request info
115 */
116 public RMIManagedConnectionAcceptor(Authenticator authenticator,
117 RMIRequestInfo info) {
118 _authenticator = authenticator;
119 _uri = URIHelper.convertHostToAddress(info.getURI());
120 _embedRegistry = info.getEmbedRegistry();
121 }
122
123 /***
124 * Start accepting connections.
125 *
126 * @param listener the listener to delegate accepted connections to
127 * @throws ResourceException if connections cannot be accepted
128 */
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 port = 0;
149 } catch (RemoteException nested) {
150 throw new ResourceException(
151 "Failed to create or locate a registry, port=" + port,
152 nested);
153 }
154 }
155
156 _factory = new RMIInvokerFactoryImpl(_authenticator, this, listener);
157
158 try {
159 UnicastRemoteObject.exportObject(_factory, port);
160 } catch (RemoteException exception) {
161 throw new ResourceException("Failed to export object", exception);
162 }
163
164 RegistryHelper.bind(_factory, _uri, registry);
165 _registry = registry;
166 }
167
168 /***
169 * Returns the URI that this acceptor is accepting connections on.
170 *
171 * @return the URI that this acceptor is accepting connections on
172 */
173 public URI getURI() {
174 return _uri;
175 }
176
177 /***
178 * Stop accepting connection requests, and clean up any allocated resources.
179 *
180 * @throws ResourceException if the acceptor cannot be closed
181 */
182 public synchronized void close() throws ResourceException {
183 if (_registry != null) {
184 try {
185 RegistryHelper.unbind(_factory, _uri, _registry);
186 if (!UnicastRemoteObject.unexportObject(_factory, true)) {
187 _log.warn("Failed to unexport invoker factory");
188 }
189
190 if (_created && !RegistryHelper.hasBindings(_registry)) {
191
192
193
194 if (!UnicastRemoteObject.unexportObject(_registry, true)) {
195 _log.warn("Failed to unexport registry");
196 }
197 }
198 } catch (RemoteException exception) {
199 throw new ResourceException(
200 "Failed to close connection acceptor", exception);
201 } finally {
202 _factory = null;
203 _registry = null;
204 }
205 }
206 }
207
208 }