KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > config > ConfigHelper


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: ConfigHelper.java,v 1.7 2005/06/07 14:15:16 tanderson Exp $
44  */

45 package org.exolab.jms.config;
46
47 import java.net.InetAddress JavaDoc;
48 import java.net.UnknownHostException JavaDoc;
49
50 import org.exolab.jms.config.types.SchemeType;
51
52
53 /**
54  * Helper class for interrogating the configuration.
55  *
56  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
57  * @version $Revision: 1.7 $ $Date: 2005/06/07 14:15:16 $
58  */

59 public class ConfigHelper {
60
61     /**
62      * Returns the server URL for the specified scheme.
63      *
64      * @param scheme the connector scheme
65      * @param config the configuration to use
66      * @return the server URL for the specified scheme
67      */

68     public static String JavaDoc getServerURL(SchemeType scheme,
69                                       Configuration config) {
70         String JavaDoc url = null;
71         ServerConfiguration server = config.getServerConfiguration();
72
73         if (scheme.equals(SchemeType.TCP)) {
74             url = getServerURL(scheme, server.getHost(),
75                                config.getTcpConfiguration());
76         } else if (scheme.equals(SchemeType.TCPS)) {
77             url = getServerURL(scheme, server.getHost(),
78                                config.getTcpsConfiguration());
79         } else if (scheme.equals(SchemeType.RMI)) {
80             RmiConfiguration rmi = config.getRmiConfiguration();
81             if (rmi.getEmbeddedRegistry()) {
82                 // if the registry is embedded within the OpenJMS server,
83
// use the server host
84
url = getServerURL(scheme, server.getHost(), rmi);
85             } else {
86                 url = getServerURL(scheme, rmi.getRegistryHost(), rmi);
87             }
88         } else if (scheme.equals(SchemeType.HTTP)) {
89             url = getServerURL(scheme, config.getHttpConfiguration());
90         } else if (scheme.equals(SchemeType.HTTPS)) {
91             url = getServerURL(scheme, config.getHttpsConfiguration());
92         } else if (scheme.equals(SchemeType.EMBEDDED)) {
93             url = "vm:openjms";
94         }
95         return url;
96     }
97
98     /**
99      * Returns the embedded JNDI URL for the specified scheme.
100      *
101      * @param scheme the connector scheme
102      * @param config the configuration to use
103      * @return embedded JNDI URL for the specified scheme
104      */

105     public static String JavaDoc getJndiURL(SchemeType scheme, Configuration config) {
106         String JavaDoc url = null;
107         ServerConfiguration server = config.getServerConfiguration();
108
109         if (scheme.equals(SchemeType.TCP)) {
110             url = getJndiURL(scheme, server.getHost(),
111                              config.getTcpConfiguration());
112         } else if (scheme.equals(SchemeType.TCPS)) {
113             url = getJndiURL(scheme, server.getHost(),
114                              config.getTcpsConfiguration());
115         } else if (scheme.equals(SchemeType.HTTP)) {
116             url = getJndiURL(scheme, config.getHttpConfiguration());
117         } else if (scheme.equals(SchemeType.HTTPS)) {
118             url = getJndiURL(scheme, config.getHttpsConfiguration());
119         } else if (scheme.equals(SchemeType.RMI)) {
120             RmiConfiguration rmi = config.getRmiConfiguration();
121             if (rmi.getEmbeddedRegistry()) {
122                 // if the registry is embedded within the OpenJMS server,
123
// use the server host
124
url = getJndiURL(scheme, server.getHost(), rmi);
125             } else {
126                 url = getJndiURL(scheme, rmi.getRegistryHost(), rmi);
127             }
128         } else if (scheme.equals(SchemeType.EMBEDDED)) {
129             url = "vm:openjms";
130         }
131         return url;
132     }
133
134     /**
135      * Returns the server administration URL for the specified scheme.
136      *
137      * @param scheme the connector scheme
138      * @param config the configuration to use
139      * @return the server administration URL for the specified scheme
140      */

141     public static String JavaDoc getAdminURL(SchemeType scheme, Configuration config) {
142         String JavaDoc url = null;
143         ServerConfiguration server = config.getServerConfiguration();
144
145         if (scheme.equals(SchemeType.TCP)) {
146             url = getAdminURL(scheme, server.getHost(),
147                               config.getTcpConfiguration());
148         } else if (scheme.equals(SchemeType.TCPS)) {
149             url = getAdminURL(scheme, server.getHost(),
150                               config.getTcpsConfiguration());
151         } else if (scheme.equals(SchemeType.RMI)) {
152             RmiConfiguration rmi = config.getRmiConfiguration();
153             if (rmi.getEmbeddedRegistry()) {
154                 // if the registry is embedded within the OpenJMS server,
155
// use the server host
156
url = getAdminURL(scheme, server.getHost(), rmi);
157             } else {
158                 url = getAdminURL(scheme, rmi.getRegistryHost(), rmi);
159             }
160         } else if (scheme.equals(SchemeType.HTTP)) {
161             url = getAdminURL(scheme, config.getHttpConfiguration());
162         } else if (scheme.equals(SchemeType.HTTPS)) {
163             url = getAdminURL(scheme, config.getHttpsConfiguration());
164         } else if (scheme.equals(SchemeType.EMBEDDED)) {
165             url = "vm:openjms";
166         }
167         return url;
168     }
169
170     /**
171      * Returns the server URL for the TCP/TCPS connector.
172      *
173      * @param scheme the connector scheme
174      * @param host the server host
175      * @param config the TCP/TCPS configuration
176      * @return the server URL for the TCP/TCPS connector
177      */

178     private static String JavaDoc getServerURL(SchemeType scheme, String JavaDoc host,
179                                        TcpConfigurationType config) {
180         return getURL(scheme, host, config.getPort());
181     }
182
183     /**
184      * Returns the server URL for the RMI connector.
185      *
186      * @param scheme the connector scheme
187      * @param host the server host
188      * @param config the RMI configuration
189      * @return the server URL for the RMI connector
190      */

191     private static String JavaDoc getServerURL(SchemeType scheme, String JavaDoc host,
192                                        RmiConfiguration config) {
193         return getURL(scheme, host, config.getRegistryPort());
194     }
195
196     /**
197      * Returns the server URL for the HTTP/HTTPS connector.
198      *
199      * @param scheme the connector scheme
200      * @param config the HTTP/HTTPS configuration
201      * @return the server URL for the HTTP/HTTPS connector
202      */

203     private static String JavaDoc getServerURL(SchemeType scheme,
204                                        HttpConfigurationType config) {
205         return getURL(scheme, config.getWebServerHost(),
206                       config.getWebServerPort(), config.getServlet());
207     }
208
209     /**
210      * Returns the embedded JNDI URL for the TCP/TCPS connector.
211      *
212      * @return the embedded JNDI URL for the TCP/TCPS connector
213      */

214     private static String JavaDoc getJndiURL(SchemeType scheme, String JavaDoc host,
215                                      TcpConfigurationType config) {
216         int port = config.getJndiPort();
217         if (port == 0) {
218             port = config.getPort();
219         }
220         return getURL(scheme, host, port);
221     }
222
223     /**
224      * Returns the embedded JNDI URL for the RMI connector.
225      *
226      * @return the embedded JNDI URL for the RMI connector
227      */

228     private static String JavaDoc getJndiURL(SchemeType scheme, String JavaDoc host,
229                                      RmiConfiguration config) {
230         return getURL(scheme, host, config.getRegistryPort());
231     }
232
233     /**
234      * Returns the embedded JNDI URL for the HTTP/HTTPS connector.
235      *
236      * @return the embedded JNDI URL for the HTTP/HTTPS connector
237      */

238     private static String JavaDoc getJndiURL(SchemeType scheme,
239                                      HttpConfigurationType config) {
240         return getURL(scheme, config.getWebServerHost(),
241                       config.getWebServerPort(), config.getServlet());
242     }
243
244     /**
245      * Returns the admin URL for the TCP/TCPS connector.
246      *
247      * @return the admin URL for the TCP/TCPS connector
248      */

249     private static String JavaDoc getAdminURL(SchemeType scheme, String JavaDoc host,
250                                       TcpConfigurationType config) {
251         int port = config.getAdminPort();
252         if (port == 0) {
253             port = config.getPort();
254         }
255         return getURL(scheme, host, port);
256     }
257
258     /**
259      * Returns the admin URL for the RMI connector.
260      *
261      * @return the admin URL for the RMI connector
262      */

263     private static String JavaDoc getAdminURL(SchemeType scheme, String JavaDoc host,
264                                       RmiConfiguration config) {
265         return getURL(scheme, host, config.getRegistryPort());
266     }
267
268     /**
269      * Returns the admin URL for the HTTP/HTTPS connector.
270      *
271      * @return the admin URL for the HTTP/HTTPS connector
272      */

273     private static String JavaDoc getAdminURL(SchemeType scheme,
274                                       HttpConfigurationType config) {
275         return getURL(scheme, config.getWebServerHost(),
276                       config.getWebServerPort(), config.getServlet());
277     }
278
279     /**
280      * Constructs a URL with no path.
281      *
282      * @param scheme the connector scheme
283      * @param host the host
284      * @param port the port
285      * @return a URL formed from the concatenation of the arguments
286      */

287     private static String JavaDoc getURL(SchemeType scheme, String JavaDoc host, int port) {
288         return getURL(scheme, host, port, "");
289     }
290
291     /**
292      * Constructs a URL with a path.
293      *
294      * @param scheme the connector scheme
295      * @param host the host
296      * @param port the port
297      * @param path the path
298      * @return a URL formed from the concatenation of the arguments
299      */

300     private static String JavaDoc getURL(SchemeType scheme, String JavaDoc host, int port,
301                                  String JavaDoc path) {
302         return getURL(scheme.toString(), host, port, path);
303     }
304
305     /**
306      * Constructs a URL with a path.
307      *
308      * @param scheme the connector scheme
309      * @param host the host
310      * @param port the port
311      * @param path the path
312      * @return a URL formed from the concatenation of the arguments
313      */

314     private static String JavaDoc getURL(String JavaDoc scheme, String JavaDoc host, int port,
315                                  String JavaDoc path) {
316         String JavaDoc result = scheme + "://" + getHost(host) + ":" + port;
317         if (!path.startsWith("/")) {
318             result += "/" + path;
319         } else {
320             result += path;
321         }
322         return result;
323     }
324
325     /**
326      * Returns the host address, if the supplied host is localhost, else returns
327      * it, unchanged.
328      */

329     private static String JavaDoc getHost(String JavaDoc host) {
330         if (host.equals("localhost")) {
331             try {
332                 host = InetAddress.getLocalHost().getHostAddress();
333             } catch (UnknownHostException JavaDoc ignore) {
334             }
335         }
336         return host;
337     }
338
339 }
340
Popular Tags