View Javadoc

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.2 2005/07/22 23:40:38 tanderson Exp $
44   */
45  package org.exolab.jms.server.net;
46  
47  import java.net.InetAddress;
48  import java.net.UnknownHostException;
49  import java.util.Map;
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.ServerConfiguration;
56  import org.exolab.jms.config.types.SchemeType;
57  import org.exolab.jms.net.connector.AbstractConnectionFactory;
58  import org.exolab.jms.net.orb.ORB;
59  import org.exolab.jms.net.uri.InvalidURIException;
60  import org.exolab.jms.net.uri.URI;
61  import org.exolab.jms.net.uri.URIHelper;
62  import org.exolab.jms.net.util.Properties;
63  
64  
65  /***
66   * Abstract implementation of the {@link ConnectorCfg} interface.
67   *
68   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
69   * @version $Revision: 1.2 $ $Date: 2005/07/22 23:40:38 $
70   */
71  abstract class AbstractConnectorCfg implements ConnectorCfg {
72  
73      /***
74       * The connector scheme.
75       */
76      private final SchemeType _scheme;
77  
78      /***
79       * The underlying configuration.
80       */
81      private final Configuration _config;
82  
83  
84      /***
85       * Construct a new <code>AbstractConnectorCfg</code>.
86       *
87       * @param scheme the connector scheme
88       * @param config the configuration to use
89       */
90      public AbstractConnectorCfg(SchemeType scheme, Configuration config) {
91          if (scheme == null) {
92              throw new IllegalArgumentException("Argument 'scheme' is null");
93          }
94          if (config == null) {
95              throw new IllegalArgumentException("Argument 'config' is null");
96          }
97          _scheme = scheme;
98          _config = config;
99      }
100 
101     /***
102      * Returns the connector scheme.
103      *
104      * @return the connector scheme
105      */
106     public SchemeType getScheme() {
107         return _scheme;
108     }
109 
110     /***
111      * Returns the URI used to establsh connections to remote services.
112      * <p/>
113      * This implementation returns {@link #getExportURI}
114      *
115      * @return the URI used to establish connections to remote services
116      */
117     public String getConnectURI() {
118         return getExportURI();
119     }
120 
121     /***
122      * Returns the URI that services are exported on.
123      *
124      * @return the URI for exporting services
125      */
126     public String getExportURI() {
127         return ConfigHelper.getServerURL(_scheme, _config);
128     }
129 
130     /***
131      * Returns the URI that JNDI service is exported on.
132      *
133      * @return the JNDI service URI
134      */
135     public String getJNDIExportURI() {
136         return ConfigHelper.getJndiURL(_scheme, _config);
137     }
138 
139     /***
140      * Returns the URI the administration service is exported on.
141      * <p/>
142      * Typically, this will be the same as that returned by {@link
143      * #getExportURI}.
144      *
145      * @return the administration service URI
146      */
147     public String getAdminExportURI() {
148         return ConfigHelper.getAdminURL(_scheme, _config);
149     }
150 
151     /***
152      * Returns properties to configure the ORB to enable it to establish a
153      * connection to the remote ORB.
154      *
155      * @return a map of String properties
156      */
157     public Map getConnectProperties() {
158         Properties properties = getProperties();
159         populateConnectProperties(properties);
160         return properties.getProperties();
161     }
162 
163     /***
164      * Returns properties to configure the ORB to enable it to accept
165      * connections from remote ORBs.
166      *
167      * @return a map of String properties
168      */
169     public Map getAcceptProperties() {
170         Properties properties = getProperties();
171         populateAcceptProperties(properties);
172         return properties.getProperties();
173     }
174 
175     /***
176      * Returns connection factories associated with this configuration.
177      *
178      * @return associated connection factories.
179      */
180     public ConnectionFactories getConnectionFactories() {
181         ConnectionFactories result = null;
182         Connector[] connectors = _config.getConnectors().getConnector();
183         for (int i = 0; i < connectors.length; ++i) {
184             if (connectors[i].getScheme().equals(_scheme)) {
185                 result = connectors[i].getConnectionFactories();
186                 break;
187             }
188         }
189         return result;
190     }
191 
192     /***
193      * Returns the underlying configuration.
194      *
195      * @return the underlying configuration
196      */
197     protected Configuration getConfiguration() {
198         return _config;
199     }
200 
201     /***
202      * Populates the supplied properties with connect properties.
203      *
204      * @param properties the properties to populate
205      */
206     protected void populateConnectProperties(Properties properties) {
207         properties.set(ORB.PROVIDER_URI, getConnectURI());
208     }
209 
210     /***
211      * Populates the supplied properties with connection accept properties.
212      *
213      * @param properties the properties to populate
214      */
215     protected void populateAcceptProperties(Properties properties) {
216         properties.set(ORB.PROVIDER_URI, getExportURI());
217 
218         ServerConfiguration server = _config.getServerConfiguration();
219         properties.set("org.exolab.jms.net.orb.threads.max",
220                        server.getMaxThreads());
221     }
222 
223     /***
224      * Helper to create a new {@link Properties} instance.
225      *
226      * @return a new <code>Properties</code> instance
227      */
228     protected Properties getProperties() {
229         String prefix = AbstractConnectionFactory.PROPERTY_PREFIX;
230         if (_scheme.equals(SchemeType.EMBEDDED)) {
231             prefix += "vm";
232         } else {
233             prefix += _scheme.toString();
234         }
235         prefix += ".";
236         return new Properties(prefix);
237     }
238 
239     /***
240      * Constructs a URI with no path.
241      *
242      * @param scheme the connector scheme
243      * @param host   the host
244      * @param port   the port
245      * @return a new <code>URI</code>
246      */
247     protected URI getURI(String scheme, String host, int port) {
248         URI result;
249         try {
250             result = URIHelper.create(scheme, getHost(host), port);
251         } catch (InvalidURIException exception) {
252             throw new IllegalStateException("Failed to create URI: "
253                                             + exception);
254         }
255         return result;
256     }
257 
258     /***
259      * Constructs a URI with a path.
260      *
261      * @param scheme the connector scheme
262      * @param host   the host
263      * @param port   the port
264      * @param path   the path
265      * @return a new <ocde>URI</ocde>
266      */
267     protected URI getURI(String scheme, String host, int port,
268                          String path) {
269         URI result;
270         try {
271             result = URIHelper.create(scheme, getHost(host), port, path);
272         } catch (InvalidURIException exception) {
273             throw new IllegalStateException("Failed to create URI: "
274                                             + exception);
275         }
276         return result;
277     }
278 
279     /***
280      * Helper to change the supplied host to its address, iff it is
281      * <em>localhost</em>.
282      *
283      * @param host the host
284      * @return the host address if <code>host</code> is <em>localhost</em>
285      *         otherwise returns <code>host</code> unchanged
286      */
287     protected String getHost(String host) {
288         if (host.equals("localhost")) {
289             try {
290                 host = InetAddress.getLocalHost().getHostAddress();
291             } catch (UnknownHostException ignore) {
292             }
293         }
294         return host;
295     }
296 
297     /***
298      * Helper to parse a URI.
299      *
300      * @param uri the URI to parse.
301      * @return the parsed URI
302      * @throws IllegalStateException if the URI is invalid
303      */
304     protected URI getURI(String uri) {
305         URI result;
306         try {
307             result = URIHelper.parse(uri);
308         } catch (InvalidURIException exception) {
309             throw new IllegalStateException("Failed to parse URI: " + uri);
310         }
311         return result;
312     }
313 
314 }