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 2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ConfigHelper.java,v 1.5 2003/08/18 10:16:58 tanderson Exp $
44   */
45  package org.exolab.jms.config;
46  
47  import java.net.InetAddress;
48  import java.net.UnknownHostException;
49  
50  import org.exolab.jms.config.types.SchemeType;
51  
52  
53  /***
54   * Helper class for interrogating the configuration
55   *
56   * @version     $Revision: 1.5 $ $Date: 2003/08/18 10:16:58 $
57   * @author      <a href="mailto:tima@intalio.com">Tim Anderson</a>
58   */
59  public class ConfigHelper {
60  
61      /***
62       * Returns the server URL for the specified scheme
63       */
64      public static String getServerURL(SchemeType scheme,
65                                        Configuration config) {
66          String url = null;
67          ServerConfiguration server = config.getServerConfiguration();
68  
69          if (scheme.equals(SchemeType.TCP)) {
70              getServerURL(scheme, server.getHost(),
71                  config.getTcpConfiguration());
72          } else if (scheme.equals(SchemeType.TCPS)) {
73              getServerURL(scheme, server.getHost(),
74                  config.getTcpsConfiguration());
75          } else if (scheme.equals(SchemeType.RMI)) {
76              RmiConfiguration rmi = config.getRmiConfiguration();
77              if (rmi.getEmbeddedRegistry()) {
78                  // if the registry is embedded within the OpenJMS server,
79                  // use the server host
80                  url = getServerURL(scheme, server.getHost(), rmi);
81              } else {
82                  url = getServerURL(scheme, rmi.getRegistryHost(), rmi);
83              }
84          } else if (scheme.equals(SchemeType.HTTP)) {
85              url = getServerURL(scheme, config.getHttpConfiguration());
86          } else if (scheme.equals(SchemeType.HTTPS)) {
87              url = getServerURL(scheme, config.getHttpsConfiguration());
88          } else if (scheme.equals(SchemeType.EMBEDDED)) {
89              url = scheme + "://";
90          }
91          return url;
92      }
93  
94      /***
95       * Returns the embedded JNDI URL for the specified scheme
96       */
97      public static String getJndiURL(SchemeType scheme, Configuration config) {
98          String url = null;
99          ServerConfiguration server = config.getServerConfiguration();
100 
101         if (scheme.equals(SchemeType.TCP)) {
102             url = getJndiURL(scheme, server.getHost(),
103                 config.getTcpConfiguration());
104         } else if (scheme.equals(SchemeType.TCPS)) {
105             url = getJndiURL(scheme, server.getHost(),
106                 config.getTcpsConfiguration());
107         } else if (scheme.equals(SchemeType.HTTP)) {
108             url = getJndiURL(scheme, config.getHttpConfiguration());
109         } else if (scheme.equals(SchemeType.HTTPS)) {
110             url = getJndiURL(scheme, config.getHttpsConfiguration());
111         } else if (scheme.equals(SchemeType.RMI)) {
112             RmiConfiguration rmi = config.getRmiConfiguration();
113             if (rmi.getEmbeddedRegistry()) {
114                 // if the registry is embedded within the OpenJMS server,
115                 // use the server host
116                 url = getJndiURL(scheme, server.getHost(), rmi);
117             } else {
118                 url = getJndiURL(scheme, rmi.getRegistryHost(), rmi);
119             }
120         } else if (scheme.equals(SchemeType.EMBEDDED)) {
121             url = scheme + "://";
122         }
123         return url;
124     }
125 
126     /***
127      * Returns the server administration URL for the specified scheme
128      */
129     public static String getAdminURL(SchemeType scheme, Configuration config) {
130         String url = null;
131         ServerConfiguration server = config.getServerConfiguration();
132 
133         if (scheme.equals(SchemeType.TCP)) {
134             url = getAdminURL(scheme, server.getHost(),
135                 config.getTcpConfiguration());
136         } else if (scheme.equals(SchemeType.TCPS)) {
137             url = getAdminURL(scheme, server.getHost(),
138                 config.getTcpsConfiguration());
139         } else if (scheme.equals(SchemeType.RMI)) {
140             RmiConfiguration rmi = config.getRmiConfiguration();
141             if (rmi.getEmbeddedRegistry()) {
142                 // if the registry is embedded within the OpenJMS server,
143                 // use the server host
144                 url = getAdminURL(scheme, server.getHost(), rmi);
145             } else {
146                 url = getAdminURL(scheme, rmi.getRegistryHost(), rmi);
147             }
148         } else if (scheme.equals(SchemeType.HTTP)) {
149             url = getAdminURL(scheme, config.getHttpConfiguration());
150         } else if (scheme.equals(SchemeType.HTTPS)) {
151             url = getAdminURL(scheme, config.getHttpsConfiguration());
152         } else if (scheme.equals(SchemeType.EMBEDDED)) {
153             url = scheme + "://";
154         }
155         return url;
156     }
157 
158     private static String getServerURL(SchemeType scheme, String host,
159                                        TcpConfigurationType config) {
160         return getURL(scheme, host, config.getPort());
161     }
162 
163     private static String getServerURL(SchemeType scheme, String host,
164                                        RmiConfiguration config) {
165         return getURL(scheme, host, config.getRegistryPort(),
166             config.getServerName());
167     }
168 
169     private static String getServerURL(SchemeType scheme,
170                                        HttpConfigurationType config) {
171         return getURL(scheme, config.getHost(), config.getPort(),
172             config.getAdminServlet());
173     }
174 
175     private static String getJndiURL(SchemeType scheme, String host,
176                                      TcpConfigurationType config) {
177         return getURL(scheme, host, config.getJndiPort());
178     }
179 
180     private static String getJndiURL(SchemeType scheme, String host,
181                                      RmiConfiguration config) {
182         return getURL(scheme, host, config.getRegistryPort(),
183             config.getJndiName());
184     }
185 
186     private static String getJndiURL(SchemeType scheme,
187                                      HttpConfigurationType config) {
188         return getURL(scheme, config.getHost(), config.getPort(),
189             config.getJndiServlet());
190     }
191 
192     private static String getAdminURL(SchemeType scheme, String host,
193                                       TcpConfigurationType config) {
194         return getURL(scheme, host, config.getPort());
195     }
196 
197     private static String getAdminURL(SchemeType scheme, String host,
198                                       RmiConfiguration config) {
199         return getURL(scheme, host, config.getRegistryPort(),
200             config.getAdminName());
201     }
202 
203     private static String getAdminURL(SchemeType scheme,
204                                       HttpConfigurationType config) {
205         return getURL(scheme, config.getHost(), config.getPort(),
206             config.getAdminServlet());
207     }
208 
209     private static String getURL(SchemeType scheme, String host, int port) {
210         return getURL(scheme, host, port, "");
211     }
212 
213     private static String getURL(SchemeType scheme, String host, int port,
214                                  String path) {
215         String result = scheme + "://" + getHost(host) + ":" + port;
216         if (!path.startsWith("/")) {
217             result += "/" + path;
218         } else {
219             result += path;
220         }
221         return result;
222     }
223 
224     /***
225      * Returns the host address, if the supplied host is localhost, else
226      * returns it, unchanged.
227      */
228     private static String getHost(String host) {
229         if (host.equals("localhost")) {
230             try {
231                 host = InetAddress.getLocalHost().getHostAddress();
232             } catch (UnknownHostException ignore) {
233             }
234         }
235         return host;
236     }
237 
238 } //-- ConfigHelper