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: HttpJmsServerStub.java,v 1.8 2003/08/07 13:32:51 tanderson Exp $
44   *
45   * Date             Author      Changes
46   * 12 Oct 2001	    mourikis    Created
47   */
48  
49  
50  package org.exolab.jms.client.http;
51  
52  import java.io.IOException;
53  import java.util.Hashtable;
54  import java.util.Vector;
55  
56  import javax.jms.ExceptionListener;
57  import javax.jms.JMSException;
58  
59  import org.exolab.core.http.HttpClient;
60  import org.exolab.jms.client.JmsConnectionStubIfc;
61  import org.exolab.jms.client.JmsServerStubIfc;
62  
63  
64  /***
65   * This class is repsonsible for opening an http connection to the server
66   * and creating HttpJmsConnections.
67   *
68   * @version     $Revision: 1.8 $ $Date: 2003/08/07 13:32:51 $
69   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
70   * @see         org.exolab.core.http.HttpClient
71   */
72  public class HttpJmsServerStub implements JmsServerStubIfc {
73  
74      /***
75       * The server URL
76       */
77      private String _url;
78  
79      /***
80       * The connection label. @todo - HttpClient doesn't appear to use this
81       */
82      private String _label = "HttpJmsServerStub";
83  
84      /***
85       * The exception listener is used to communicate server connection
86       * problems
87       */
88      private ExceptionListener _listener = null;
89  
90  
91      /***
92       * Default constructor
93       */
94      public HttpJmsServerStub() {
95      }
96  
97      /***
98       * The constructor has a collection of environment variables
99       * which it uses to construct a connection to the remote
100      * server
101      *
102      * @param env the connection properties
103      * @throws JMSException if required connection properties aren't specified
104      */
105     public HttpJmsServerStub(Hashtable env) throws JMSException {
106         _url = (String) env.get(HttpJmsConstants.HTTP_SERVER_URL);
107         if (_url == null) {
108             throw new JMSException("Required property not specified: " +
109                 HttpJmsConstants.HTTP_SERVER_URL);
110         }
111     }
112 
113     /***
114      * Instantiate an instance of this stub with the specified URL
115      *
116      * @param  url  url of the server hosting the servlet.
117      */
118     public HttpJmsServerStub(String url) {
119         _url = url;
120     }
121 
122     /***
123      * Create a connection to the JMS Server.
124      *
125      * @param clientId the identity of client
126      * @param username the client username
127      * @param password the client password
128      * @throws JMSException if the connection cannot be created
129      */
130     public JmsConnectionStubIfc createConnection(
131         String clientId, String username, String password)
132         throws JMSException {
133         JmsConnectionStubIfc stub = null;
134 
135         try {
136             Vector v = pack("createConnection", clientId, username, password);
137             HttpClient connection = new HttpClient(_url, _label);
138 
139             connection.send(v);
140             String connectionId = (String) checkReply(connection,
141                 "createConnection");
142             stub = new HttpJmsConnectionStub(
143                 connection, clientId, connectionId);
144         } catch (IOException exception) {
145             throw new JMSException("Failed to create connection, URL=" +
146                 _url + ": " + exception);
147         }
148         return stub;
149     }
150 
151     // implementation of JmsServerStubIfc.setExceptionListener
152     public void setExceptionListener(ExceptionListener listener) {
153         _listener = listener;
154     }
155 
156     /***
157      * Set the URL
158      *
159      * @param URL the webserver URL
160      */
161     protected void setURL(String URL) {
162         _url = URL;
163     }
164 
165     /***
166      * The the connection label
167      */
168     protected void setLabel(String label) {
169         _label = label;
170     }
171 
172     /***
173      * A convenience method to check the success of operations which return
174      * a true on sucess.
175      *
176      * @param connection the connection
177      * @param method The requested server function.
178      * @return the result of the call, or <code>null</code> if the call
179      * didn't return a result
180      * @throws JMSException for any failure.
181      */
182     protected Object checkReply(HttpClient connection, String method)
183         throws JMSException {
184         Object result = null;
185         Vector v = null;
186         try {
187             v = (Vector) connection.receive();
188         } catch (Exception err) {
189             // rethrow as a JMSException
190             throw new JMSException("Operation " + method + " failed: " + err);
191         }
192 
193         if (v != null) {
194             Boolean b = (Boolean) v.get(0);
195             if (!b.booleanValue()) {
196                 if (v.get(1) instanceof JMSException) {
197                     throw (JMSException) v.get(1);
198                 } else {
199                     throw new JMSException("Operation " + method +
200                         " failed:\n" + v.get(1));
201                 }
202             }
203             result = v.get(1);
204         } else {
205             throw new JMSException("Unknown connection error for " + method);
206         }
207 
208         return result;
209     }
210 
211     /***
212      * Pack all the data that is required by the server in a vector.
213      * Set the size of the vector to be exactly the right size for efficiency.
214      *
215      * @param method The function to activate on the server.
216      * @param id The unique client id.
217      * @param username
218      * @param password
219      * @return Vector The vector containing all the data.
220      */
221     protected Vector pack(String method, String id, String username,
222                           String password) {
223         Vector v = new Vector(5);
224         v.add("org.exolab.jms.server.mipc.IpcJmsServerConnection");
225         v.add(method);
226         v.add(id);
227         v.add(username);
228         v.add(password);
229         return v;
230     }
231 
232 } //-- HttpJmsServerStub