KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > server > net > RemoteServerConnector


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 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: RemoteServerConnector.java,v 1.5 2005/06/07 14:33:15 tanderson Exp $
44  */

45 package org.exolab.jms.server.net;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.util.Map JavaDoc;
50 import javax.naming.Context JavaDoc;
51 import javax.naming.NamingException JavaDoc;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55 import org.codehaus.spice.jndikit.NamingProvider;
56
57 import org.exolab.jms.authentication.AuthenticationMgr;
58 import org.exolab.jms.client.net.JmsServerStubImpl;
59 import org.exolab.jms.config.Configuration;
60 import org.exolab.jms.config.types.SchemeType;
61 import org.exolab.jms.net.orb.ORB;
62 import org.exolab.jms.net.orb.ORBFactory;
63 import org.exolab.jms.net.registry.LocalRegistry;
64 import org.exolab.jms.server.EmbeddedNameService;
65 import org.exolab.jms.server.ServerConnector;
66 import org.exolab.jms.server.ServerException;
67
68
69 /**
70  * Implementation of the {@link ServerConnector} interface, that provides
71  * remoting via an {@link ORB}.
72  *
73  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
74  * @version $Revision: 1.5 $ $Date: 2005/06/07 14:33:15 $
75  */

76 public class RemoteServerConnector implements ServerConnector {
77
78     /**
79      * The configuration.
80      */

81     private final Configuration _config;
82
83     /**
84      * The connector configuration.
85      */

86     private final ConnectorCfg _connector;
87
88     /**
89      * The URI to export the server on. This is the URI that the server accepts
90      * connections from clients.
91      */

92     private final String JavaDoc _exportURI;
93
94     /**
95      * The URI that clients connect to the server on. This is different to the
96      * {@link _exportURI) if clients connect via a webserver.
97      */

98     private final String JavaDoc _connectURI;
99
100     /**
101      * The ORB.
102      */

103     private ORB _orb;
104
105     /**
106      * The logger.
107      */

108     private static final Log _log = LogFactory.getLog(
109             RemoteServerConnector.class);
110
111
112     /**
113      * Construct a new <code>RemoteServerConnector</code>.
114      *
115      * @param scheme the type of the connector to use
116      * @param config the server configuration
117      */

118     public RemoteServerConnector(SchemeType scheme, Configuration config) {
119         if (scheme == null) {
120             throw new IllegalArgumentException JavaDoc("Argument 'scheme' is null");
121         }
122         if (config == null) {
123             throw new IllegalArgumentException JavaDoc("Argument 'config' is null");
124         }
125         _connector = ConnectorCfgFactory.create(scheme, config);
126         _config = config;
127         _exportURI = _connector.getExportURI();
128         _connectURI = _connector.getConnectURI();
129     }
130
131     /**
132      * Initialises the server interface for the specified connector.
133      *
134      * @throws ServerException if the interface cannot be initialised
135      */

136     public void init() throws ServerException {
137         try {
138             Map JavaDoc properties = _connector.getAcceptProperties();
139             _orb = ORBFactory.createORB(AuthenticationMgr.instance(),
140                                         properties);
141             if (!_connectURI.equals(_exportURI)) {
142                 _orb.addRoute(_exportURI, _connectURI);
143             }
144         } catch (RemoteException JavaDoc exception) {
145             throw new ServerException(
146                     "Failed to create ORB for URI:" + _exportURI, exception);
147         }
148         try {
149             LocalRegistry registry = _orb.getRegistry();
150
151             RemoteServerConnectionFactory server =
152                     new RemoteServerConnectionFactory(_orb, _exportURI);
153             registry.bind("server", server.getProxy());
154             if (_log.isInfoEnabled()) {
155                 _log.info("Server accepting connections on " + _exportURI);
156             }
157
158             if (_config.getServerConfiguration().getEmbeddedJNDI()) {
159                 NamingProvider provider =
160                         EmbeddedNameService.getInstance().getNamingProvider();
161                 RemoteNamingProvider jndi = new RemoteNamingProvider(
162                         provider, _orb, _connector.getJNDIExportURI());
163                 registry.bind("jndi", jndi.getProxy());
164                 if (_log.isInfoEnabled()) {
165                     _log.info("JNDI service accepting connections on "
166                               + _connector.getJNDIExportURI());
167                 }
168             }
169
170             JmsAdminServerImpl admin = new JmsAdminServerImpl(
171                     _orb, _connector.getAdminExportURI());
172             registry.bind("admin", admin.getProxy());
173             if (_log.isInfoEnabled()) {
174                 _log.info("Admin service accepting connections on "
175                           + _connector.getAdminExportURI());
176             }
177
178             registry.setReadOnly(true);
179         } catch (Exception JavaDoc exception) {
180             throw new ServerException(
181                     "Failed to initialise the server interface", exception);
182         }
183     }
184
185     /**
186      * Bind any factory object specified in the configuration file to the
187      * specified JNDI context.
188      *
189      * @param context context to bind factory objects
190      * @throws NamingException if a naming error occurs
191      */

192     public void bindConnectionFactories(Context JavaDoc context)
193             throws NamingException JavaDoc {
194         // put together a list of parameters that the connection factories will
195
// need to use to connect to this server
196
Map JavaDoc properties = _connector.getConnectProperties();
197         Hashtable JavaDoc env = new Hashtable JavaDoc();
198         env.putAll(properties);
199         ConnectionFactoryHelper.bind(context,
200                                      _connector.getConnectionFactories(),
201                                      JmsServerStubImpl.class, env);
202     }
203
204     /**
205      * Close the interface, releasing any resources.
206      *
207      * @throws ServerException if the interface cannot be closed
208      */

209     public void close() throws ServerException {
210         try {
211             _orb.shutdown();
212         } catch (RemoteException JavaDoc exception) {
213             throw new ServerException(exception.getMessage(), exception);
214         }
215     }
216
217 }
218
Popular Tags