KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > rmi > RMIManagedConnectionAcceptor


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.3 2005/05/27 14:02:45 tanderson Exp $
44  */

45 package org.exolab.jms.net.rmi;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.rmi.registry.LocateRegistry JavaDoc;
49 import java.rmi.registry.Registry JavaDoc;
50 import java.rmi.server.UnicastRemoteObject JavaDoc;
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.3 $ $Date: 2005/05/27 14:02:45 $
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 JavaDoc _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 JavaDoc registry = null;
132
133         int port = _uri.getPort();
134         if (_embedRegistry) {
135             try {
136                 registry = LocateRegistry.createRegistry(port);
137                 _created = true;
138             } catch (RemoteException JavaDoc 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 JavaDoc 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 JavaDoc exception) {
160             throw new ResourceException("Failed to export object", exception);
161         }
162
163         RegistryHelper.bind(_factory, _uri, registry);
164         _registry = registry;
165     }
166
167     /**
168      * Returns the URI that this acceptor is accepting connections on.
169      *
170      * @return the URI that this acceptor is accepting connections on
171      */

172     public URI getURI() {
173         return _uri;
174     }
175
176     /**
177      * Stop accepting connection requests, and clean up any allocated resources.
178      *
179      * @throws ResourceException if the acceptor cannot be closed
180      */

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                     // if this created the registry, and there are no
191
// other bindings, unexport the registry
192

193                     if (!UnicastRemoteObject.unexportObject(_registry, true)) {
194                         _log.warn("Failed to unexport registry");
195                     }
196                 }
197             } catch (RemoteException JavaDoc 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