KickJava   Java API By Example, From Geeks To Geeks.

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


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 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: AbstractConnectorCfg.java,v 1.1 2005/05/03 13:47:22 tanderson Exp $
44  */

45 package org.exolab.jms.server.net;
46
47 import java.net.InetAddress JavaDoc;
48 import java.net.UnknownHostException JavaDoc;
49 import java.util.Map JavaDoc;
50
51 import org.exolab.jms.config.ConfigHelper;
52 import org.exolab.jms.config.Configuration;
53 import org.exolab.jms.config.ConnectionFactories;
54 import org.exolab.jms.config.Connector;
55 import org.exolab.jms.config.types.SchemeType;
56 import org.exolab.jms.net.connector.AbstractConnectionFactory;
57 import org.exolab.jms.net.orb.ORB;
58 import org.exolab.jms.net.uri.InvalidURIException;
59 import org.exolab.jms.net.uri.URI;
60 import org.exolab.jms.net.uri.URIHelper;
61 import org.exolab.jms.net.util.Properties;
62
63
64 /**
65  * Abstract implementation of the {@link ConnectorCfg} interface.
66  *
67  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
68  * @version $Revision: 1.1 $ $Date: 2005/05/03 13:47:22 $
69  */

70 abstract class AbstractConnectorCfg implements ConnectorCfg {
71
72     /**
73      * The connector scheme.
74      */

75     private final SchemeType _scheme;
76
77     /**
78      * The underlying configuration.
79      */

80     private final Configuration _config;
81
82
83     /**
84      * Construct a new <code>AbstractConnectorCfg</code>.
85      *
86      * @param scheme the connector scheme
87      * @param config the configuration to use
88      */

89     public AbstractConnectorCfg(SchemeType scheme, Configuration config) {
90         if (scheme == null) {
91             throw new IllegalArgumentException JavaDoc("Argument 'scheme' is null");
92         }
93         if (config == null) {
94             throw new IllegalArgumentException JavaDoc("Argument 'config' is null");
95         }
96         _scheme = scheme;
97         _config = config;
98     }
99
100     /**
101      * Returns the connector scheme.
102      *
103      * @return the connector scheme
104      */

105     public SchemeType getScheme() {
106         return _scheme;
107     }
108
109     /**
110      * Returns the URI used to establsh connections to remote services.
111      * <p/>
112      * This implementation returns {@link #getExportURI}
113      *
114      * @return the URI used to establish connections to remote services
115      */

116     public String JavaDoc getConnectURI() {
117         return getExportURI();
118     }
119
120     /**
121      * Returns the URI that services are exported on.
122      *
123      * @return the URI for exporting services
124      */

125     public String JavaDoc getExportURI() {
126         return ConfigHelper.getServerURL(_scheme, _config);
127     }
128
129     /**
130      * Returns the URI that JNDI service is exported on.
131      *
132      * @return the JNDI service URI
133      */

134     public String JavaDoc getJNDIExportURI() {
135         return ConfigHelper.getJndiURL(_scheme, _config);
136     }
137
138     /**
139      * Returns the URI the administration service is exported on.
140      * <p/>
141      * Typically, this will be the same as that returned by {@link
142      * #getExportURI}.
143      *
144      * @return the administration service URI
145      */

146     public String JavaDoc getAdminExportURI() {
147         return ConfigHelper.getAdminURL(_scheme, _config);
148     }
149
150     /**
151      * Returns properties to configure the ORB to enable it to establish a
152      * connection to the remote ORB.
153      *
154      * @return a map of String properties
155      */

156     public Map JavaDoc getConnectProperties() {
157         Properties properties = getProperties();
158         populateConnectProperties(properties);
159         return properties.getProperties();
160     }
161
162     /**
163      * Returns properties to configure the ORB to enable it to accept
164      * connections from remote ORBs.
165      *
166      * @return a map of String properties
167      */

168     public Map JavaDoc getAcceptProperties() {
169         Properties properties = getProperties();
170         populateAcceptProperties(properties);
171         return properties.getProperties();
172     }
173
174     /**
175      * Returns connection factories associated with this configuration.
176      *
177      * @return associated connection factories.
178      */

179     public ConnectionFactories getConnectionFactories() {
180         ConnectionFactories result = null;
181         Connector[] connectors = _config.getConnectors().getConnector();
182         for (int i = 0; i < connectors.length; ++i) {
183             if (connectors[i].getScheme().equals(_scheme)) {
184                 result = connectors[i].getConnectionFactories();
185                 break;
186             }
187         }
188         return result;
189     }
190
191     /**
192      * Returns the underlying configuration.
193      *
194      * @return the underlying configuration
195      */

196     protected Configuration getConfiguration() {
197         return _config;
198     }
199
200     /**
201      * Populates the supplied properties with connect properties.
202      *
203      * @param properties the properties to populate
204      */

205     protected void populateConnectProperties(Properties properties) {
206         properties.set(ORB.PROVIDER_URI, getConnectURI());
207     }
208
209     /**
210      * Populates the supplied properties with connection accept properties.
211      *
212      * @param properties the properties to populate
213      */

214     protected void populateAcceptProperties(Properties properties) {
215         properties.set(ORB.PROVIDER_URI, getExportURI());
216     }
217
218     /**
219      * Helper to create a new {@link Properties} instance.
220      *
221      * @return a new <code>Properties</code> instance
222      */

223     protected Properties getProperties() {
224         String JavaDoc prefix = AbstractConnectionFactory.PROPERTY_PREFIX;
225         if (_scheme.equals(SchemeType.EMBEDDED)) {
226             prefix += "vm";
227         } else {
228             prefix += _scheme.toString();
229         }
230         prefix += ".";
231         return new Properties(prefix);
232     }
233
234     /**
235      * Constructs a URI with no path.
236      *
237      * @param scheme the connector scheme
238      * @param host the host
239      * @param port the port
240      * @return a new <code>URI</code>
241      */

242     protected URI getURI(String JavaDoc scheme, String JavaDoc host, int port) {
243         URI result;
244         try {
245             result = URIHelper.create(scheme, getHost(host), port);
246         } catch (InvalidURIException exception) {
247             throw new IllegalStateException JavaDoc("Failed to create URI: "
248                                             + exception);
249         }
250         return result;
251     }
252
253     /**
254      * Constructs a URI with a path.
255      *
256      * @param scheme the connector scheme
257      * @param host the host
258      * @param port the port
259      * @param path the path
260      * @return a new <ocde>URI</ocde>
261      */

262     protected URI getURI(String JavaDoc scheme, String JavaDoc host, int port,
263                          String JavaDoc path) {
264         URI result;
265         try {
266             result = URIHelper.create(scheme, getHost(host), port, path);
267         } catch (InvalidURIException exception) {
268             throw new IllegalStateException JavaDoc("Failed to create URI: "
269                                             + exception);
270         }
271         return result;
272     }
273
274     /**
275      * Helper to change the supplied host to its address, iff it is
276      * <em>localhost</em>.
277      *
278      * @param host the host
279      * @return the host address if <code>host</code> is <em>localhost</em>
280      * otherwise returns <code>host</code> unchanged
281      */

282     protected String JavaDoc getHost(String JavaDoc host) {
283         if (host.equals("localhost")) {
284             try {
285                 host = InetAddress.getLocalHost().getHostAddress();
286             } catch (UnknownHostException JavaDoc ignore) {
287             }
288         }
289         return host;
290     }
291
292     /**
293      * Helper to parse a URI.
294      *
295      * @param uri the URI to parse.
296      * @return the parsed URI
297      * @throws IllegalStateException if the URI is invalid
298      */

299     protected URI getURI(String JavaDoc uri) {
300         URI result;
301         try {
302             result = URIHelper.parse(uri);
303         } catch (InvalidURIException exception) {
304             throw new IllegalStateException JavaDoc("Failed to parse URI: " + uri);
305         }
306         return result;
307     }
308
309 }
310
Popular Tags