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 2000-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: HttpJmsServer.java,v 1.7 2003/09/25 12:37:56 tanderson Exp $
44   */
45  
46  package org.exolab.jms.server.http;
47  
48  import java.net.InetAddress;
49  import java.net.UnknownHostException;
50  import java.util.Hashtable;
51  
52  import javax.naming.Context;
53  import javax.naming.NamingException;
54  
55  import org.exolab.jms.client.http.HttpJmsConstants;
56  import org.exolab.jms.client.mipc.IpcJmsConstants;
57  import org.exolab.jms.config.Configuration;
58  import org.exolab.jms.config.ConfigurationManager;
59  import org.exolab.jms.config.Connector;
60  import org.exolab.jms.config.ConnectorHelper;
61  import org.exolab.jms.config.ConnectorResource;
62  import org.exolab.jms.config.HttpConfigurationType;
63  import org.exolab.jms.config.ServerConfiguration;
64  import org.exolab.jms.config.TcpConfigurationType;
65  import org.exolab.jms.config.types.SchemeType;
66  import org.exolab.jms.server.ConnectionFactoryHelper;
67  import org.exolab.jms.server.JmsServerIfc;
68  import org.exolab.jms.server.ServerException;
69  
70  
71  /***
72   * This class enables clients to connect to the JMS server via
73   * a HTTP server.
74   * It must be used in conjunction with the TCP connector.
75   *
76   * @version     $Revision: 1.7 $ $Date: 2003/09/25 12:37:56 $
77   * @author      <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
78   */
79  public class HttpJmsServer implements JmsServerIfc {
80  
81      /***
82       * Construct a new <code>HttpJmsServer</code>
83       */
84      public HttpJmsServer() {
85      }
86  
87      /***
88       * Initialise the server
89       */
90      public void init() throws ServerException {
91          HttpConfigurationType config =
92              ConfigurationManager.getConfig().getHttpConfiguration();
93  
94          if (config.getProxyHost() != null) {
95              System.setProperty("http.proxyHost", config.getProxyHost());
96          }
97          if (config.hasProxyPort()) {
98              System.setProperty("http.proxyPort",
99                  Integer.toString(config.getProxyPort()));
100         }
101     }
102 
103     // implementation of JmsServerIfc.bindConnectionFactories
104     public void bindConnectionFactories(Context context)
105         throws NamingException, ServerException {
106 
107         Configuration config = ConfigurationManager.getConfig();
108 
109         // retrieve the configuration information from the configuration
110         // manager
111         ServerConfiguration server = config.getServerConfiguration();
112         TcpConfigurationType tcp = config.getTcpConfiguration();
113         String host = normalizeHost(server.getHost());
114         String internalHost = normalizeHost(tcp.getInternalHost());
115 
116         // we need to put together a list of parameters that the
117         // connection factories will need to use to connect
118         // to this server
119         Hashtable env = new Hashtable();
120         env.put(getHttpServerProperty(), getHttpServerURL());
121         env.put(IpcJmsConstants.IPC_SERVER_HOST, host);
122         env.put(IpcJmsConstants.IPC_SERVER_PORT,
123             Integer.toString(tcp.getPort()));
124         if (internalHost != null) {
125             env.put(IpcJmsConstants.IPC_INTERNAL_SERVER_HOST, internalHost);
126         }
127         Connector connector = ConfigurationManager.getConnector(getScheme());
128         ConnectorResource resource =
129             ConnectorHelper.getConnectorResource(getScheme());
130 
131         Class proxy;
132         String className = resource.getServer().getProxyClass();
133         try {
134             proxy = Class.forName(className);
135         } catch (ClassNotFoundException exception) {
136             throw new ServerException(
137                 "Failed to locate the server proxy class: " + className);
138         }
139 
140         ConnectionFactoryHelper.bind(
141             context, connector.getConnectionFactories(), proxy, env);
142     }
143 
144     /***
145      * Returns the connector scheme for this server.
146      */
147     protected SchemeType getScheme() {
148         return SchemeType.HTTP;
149     }
150 
151     /***
152      * Returns the HTTP configuration
153      */
154     protected HttpConfigurationType getHttpConfiguration() {
155         return ConfigurationManager.getConfig().getHttpConfiguration();
156     }
157 
158     /***
159      * Returns the HTTP server URL property name, used in the construction
160      * of connection factories
161      */
162     protected String getHttpServerProperty() {
163         return HttpJmsConstants.HTTP_SERVER_URL;
164     }
165 
166     /***
167      * Returns the HTTP server URL, used in the construction of connection
168      * factories
169      */
170     protected String getHttpServerURL() {
171         HttpConfigurationType config = getHttpConfiguration();
172         String host = config.getHost(); 
173         // NOTE: host should not be converted to an IP address
174         // refer to bug 806377 - HTTPS connector shouldn't convert host
175         // names
176         int port = config.getPort();
177         String url = getScheme() + "://" + host + ":" + port;
178         String path = config.getServerServlet();
179         if (!path.startsWith("/")) {
180             url += "/" + path;
181         } else {
182             url += path;
183         }
184         return url;
185     }
186 
187     /***
188      * This method will normalize the host name. If the host is localhost then
189      * it will be converted to the corresponding IP address
190      *
191      * @param host - the raw host name
192      * @return the normalized IP address
193      */
194     protected String normalizeHost(String host) {
195         String server = host;
196 
197         // ensure that the host is not null
198         if (host == null) {
199             return host;
200         }
201 
202         if (host.equals("localhost")) {
203             try {
204                 server = InetAddress.getLocalHost().getHostAddress();
205             } catch (UnknownHostException ignore) {
206                 // ignore
207             }
208         }
209 
210         return server;
211     }
212 
213 } //-- HttpJmsServer