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 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: HttpJmsSessionConnection.java,v 1.14 2003/08/25 03:38:14 tanderson Exp $
44   */
45  package org.exolab.jms.server.http;
46  
47  import java.io.IOException;
48  import java.rmi.UnknownHostException;
49  import java.util.HashMap;
50  import java.util.Vector;
51  
52  import javax.jms.JMSException;
53  import javax.jms.Message;
54  
55  import org.apache.commons.logging.Log;
56  import org.apache.commons.logging.LogFactory;
57  
58  import org.exolab.core.http.HttpClient;
59  import org.exolab.core.mipc.MultiplexConnectionIfc;
60  import org.exolab.jms.client.JmsQueue;
61  import org.exolab.jms.client.JmsTopic;
62  import org.exolab.jms.config.Configuration;
63  import org.exolab.jms.config.HttpConfiguration;
64  import org.exolab.jms.config.HttpsConfiguration;
65  import org.exolab.jms.server.JmsServerSession;
66  import org.exolab.jms.server.mipc.IpcJmsServer;
67  import org.exolab.jms.server.mipc.IpcJmsSessionConnection;
68  
69  
70  /***
71   * This is the server side receiver for JmsSession requests. All requests are
72   * unpacked and passed on to the appropriate JmsServerSession object.
73   *
74   * @version     $Revision: 1.14 $ $Date: 2003/08/25 03:38:14 $
75   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
76   */
77  public class HttpJmsSessionConnection extends IpcJmsSessionConnection {
78  
79      /***
80       * The list of all http client consumer connections, used to send
81       * JmsMessages on.
82       */
83      private HashMap _consumerList = new HashMap();
84  
85      /***
86       * The logger
87       */
88      private static final Log _log =
89          LogFactory.getLog(HttpJmsSessionConnection.class);
90  
91  
92      /***
93       * Construct a new <code>HttpJmsSessionConnection</code>
94       *
95       * @param server the server instance
96       */
97      public HttpJmsSessionConnection(IpcJmsServer server) {
98          super(server);
99      }
100 
101     /***
102      * Create a new receiver
103      *
104      * @param session The session the request is for.
105      * @param queue The queue to create the reciver for
106      * @param consumerName The unique name of this consumer,
107      * only valid for persitent messages
108      * @param selector The selector to filter messages. This may be null.
109      * @param connection The MultiplexConnection to the machine the consumer is on
110      * @param host The host the client is running on.
111      * @param port The port the client is listening on.
112      * @param url The url for the clients web server.
113      * @return Vector The result of the request.
114      *
115      */
116     protected Vector createReceiver(JmsServerSession session, JmsQueue queue,
117                                     Long consumerId, String selector,
118                                     MultiplexConnectionIfc not_used, String host, String port, String url) {
119         if (session != null) {
120             try {
121                 session.createReceiver
122                     (queue, consumerId.longValue(), selector);
123                 addSession(session, url, host, port);
124             } catch (JMSException error) {
125                 return pack(new Boolean(false), error);
126             } catch (Exception error) {
127                 return pack(new Boolean(false), error.getMessage());
128             }
129         }
130 
131         return pack(new Boolean(true), null);
132     }
133 
134 
135     /***
136      * Create a new queue browser for the specified session and queue.
137      *
138      * @param session session that the request is for
139      * @param queue queue to browse
140      * @param clientId the client identity
141      * @param selector message selector. May be null
142      * @param connection the connection to the remote machine
143      * @param host The host the client is running on.
144      * @param port The port the client is listening on.
145      * @param url The url for the clients web server.
146      * @return      Vector              result of the request
147      *
148      */
149     protected Vector createBrowser(JmsServerSession session,
150                                    JmsQueue queue, Long clientId, String selector,
151                                    MultiplexConnectionIfc not_used, String host, String port, String url) {
152         Vector result = null;
153         if (session == null) {
154             result = pack(new Boolean(true), null);
155         } else {
156             try {
157                 session.createBrowser(queue, clientId.longValue(), selector);
158                 addSession(session, url, host, port);
159                 result = pack(new Boolean(true), null);
160             } catch (JMSException error) {
161                 result = pack(new Boolean(false), error);
162             } catch (Exception error) {
163                 return pack(new Boolean(false), error.getMessage());
164             }
165         }
166 
167         return result;
168     }
169 
170     /***
171      * Create a new subscriber, and connect back to the client through the
172      * MultiplexConnection.
173      *
174      * @param session The session the request is for.
175      * @param topic The topic the subscriber is subscribing on
176      * @param name The unique name of this subscriber,
177      * only valid for persitent messages
178      * @param selector The selector to filter messages. This may be null.
179      * @param connection The MultiplexConnection to the machine the consumer is on
180      * @param host The host the client is running on.
181      * @param port The port the client is listening on.
182      * @param url The url for the clients web server.
183      * @return Vector The result of the request.
184      *
185      */
186     protected Vector createSubscriber(JmsServerSession session, JmsTopic topic,
187                                       String name, Long clientId, String selector, Boolean noLocal,
188                                       MultiplexConnectionIfc not_used, String host, String port, String url) {
189         if (session != null) {
190             try {
191                 session.createSubscriber(topic, name, clientId.longValue(),
192                     selector, noLocal.booleanValue());
193                 addSession(session, url, host, port);
194             } catch (JMSException error) {
195                 return pack(new Boolean(false), error);
196                 // pass JMSExceptions back to the client
197             } catch (Exception error) {
198                 return pack(new Boolean(false), error.getMessage());
199             }
200         }
201 
202         return pack(new Boolean(true), null);
203     }
204 
205     /***
206      * A close request has been received.
207      *
208      * @param session the session to close
209      * @param connection the connection associated with the session
210      * @return the result of the request.
211      *
212      */
213     protected Vector close(JmsServerSession session,
214                            MultiplexConnectionIfc connection) {
215         removeSession(session);
216         return super.close(session, connection);
217     }
218 
219     /***
220      * A disconnection has been detected for this session, by the
221      * session sender, perform any neccessary cleanup, and deregister.
222      */
223     protected void disconnect(JmsServerSession session) {
224         if (session != null) {
225             removeSession(session);
226             try {
227                 session.close();
228             } catch (Exception ignore) {
229             }
230         }
231     }
232 
233     /***
234      * Add a new session for this client.
235      *
236      * @param session The client session
237      * @param url The client url for connecting to the servlet.
238      */
239     private synchronized void addSession(
240         JmsServerSession session, String url, String host, String port)
241         throws UnknownHostException, IOException {
242 
243         if (_log.isDebugEnabled()) {
244             _log.debug("Adding session for client, webserver URL="
245                 + url + ", host=" + host + ", port=" + port);
246         }
247 
248         HttpJmsSessionSender sender = (HttpJmsSessionSender) _consumerList.get
249             (session.getSessionId());
250         if (sender == null) {
251             HttpClient client = new HttpClient(url, "HttpJmsSessionConnection");
252             sender = new HttpJmsSessionSender(
253                 this, client, session, host, port);
254             _consumerList.put(session.getSessionId(), sender);
255             session.setMessageListener(sender);
256         }
257     }
258 
259     /***
260      * Remove this sessions connection
261      *
262      * @param session The client session.
263      */
264     private synchronized void removeSession(JmsServerSession session) {
265         HttpJmsSessionSender sender =
266             (HttpJmsSessionSender) _consumerList.remove(
267                 session.getSessionId());
268         if (sender != null) {
269             sender.close();
270         }
271     }
272 
273 } //-- HttpJmsSessionConnection
274