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: HttpJmsConnectionStub.java,v 1.5 2003/08/25 03:33:19 tanderson Exp $
44   *
45   * Date             Author      Changes
46   * 12 Oct 2001  	mourikis    Created
47   */
48  package org.exolab.jms.client.http;
49  
50  import java.util.Enumeration;
51  import java.util.Vector;
52  
53  import javax.jms.JMSException;
54  
55  import org.exolab.core.ipc.IpcIfc;
56  import org.exolab.jms.client.JmsConnectionStubIfc;
57  import org.exolab.jms.client.JmsSessionStubIfc;
58  
59  
60  /***
61   * Used to create OpenJmsSessions to the server. It gets the new Session Id
62   * assigned by the server and returns a HttpJmsSessionStub.
63   *
64   * @version     $Revision: 1.5 $ $Date: 2003/08/25 03:33:19 $
65   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
66   * @see 	org.exolab.core.http.HttpClient
67   */
68  public class HttpJmsConnectionStub implements JmsConnectionStubIfc {
69  
70      /***
71       * This is a reference to the Client connection.
72       */
73      private IpcIfc _connection = null;
74  
75      // The client connection id.
76      private String _clientId;
77  
78      // The destination connection id
79      private String _connectionId;
80  
81      // The list of sessions for this connection.
82      Vector _sessions = new Vector();
83  
84  
85      /***
86       * Construct an instance of this class with the specified id's.
87       * These id's are used when creating sessions.
88       *
89       * @param      	connection  The http connection to the server.
90       * @param		clientId This clients unique id.
91       * @param		connectionId This objects connection identifier.
92       * @throws   JMSException If the connection has failed.
93       *
94       */
95      public HttpJmsConnectionStub(IpcIfc connection, String clientId,
96                                   String connectionId)
97          throws JMSException {
98          if (connection != null) {
99              _connection = connection;
100             _clientId = clientId;
101             _connectionId = connectionId;
102         } else {
103             throw new JMSException("Cannot instantiate with a null " +
104                 "connection");
105         }
106     }
107 
108     /***
109      * Send a session connection message. Create a new HttpJmsSessionStub
110      * if this createSession request was successfull.
111      *
112      * @param ackMode - the ack mode for the session
113      * @param transacted - true if the session is transacted
114      * @return JmsSessionStubIfc The Session interface.
115      * @throws JMSException On failure to create a session.
116      */
117     public JmsSessionStubIfc createSession(int ackMode, boolean transacted)
118         throws JMSException {
119         JmsSessionStubIfc stub = null;
120 
121         try {
122             Vector v = pack("createSession", 2);
123             synchronized (_connection) {
124                 v.add(new Integer(ackMode));
125                 v.add(new Boolean(transacted));
126                 _connection.send(v);
127                 v = checkReply("createSession");
128             }
129 
130             Vector state = (Vector) v.get(1);
131             String sessionId = (String) state.get(0);
132             stub = new HttpJmsSessionStub(_connection, _clientId,
133                                           _connectionId, sessionId);
134             _sessions.add(stub);
135         } catch (JMSException exception) {
136             throw exception;
137         } catch (Exception exception) {
138             // rethrow as a JMSException
139             throw new JMSException("Failed to create session: " + exception);
140         }
141 
142         return stub;
143     }
144 
145     /***
146      * Send a close request to the server.
147      *
148      * @throws JMSException On failure to close connection.
149      */
150     public void close() throws JMSException {
151         try {
152             Vector v = pack("close", 0);
153             synchronized (_connection) {
154                 _connection.send(v);
155                 v = checkReply("close");
156             }
157         } catch (JMSException exception) {
158             throw exception;
159         } catch (Exception exception) {
160             // rethrow as a JMSException
161             throw new JMSException("Failed to close: " + exception);
162         } finally {
163             _connection = null;
164             for (Enumeration e = _sessions.elements();
165                  e.hasMoreElements();) {
166                 ((HttpJmsSessionStub) e.nextElement()).stopReceiver();
167             }
168         }
169     }
170 
171     /***
172      * Return the client connection id.
173      *
174      * @return the client connection id.
175      */
176     public String getConnectionId() {
177         return _connectionId;
178     }
179 
180     // implementation of JmsConnectionStubIfc.destroy
181     public void destroy() {
182         _connection = null;
183     }
184 
185     /***
186      * Pack all the data that is required by the server in a vector.
187      * Set the size of the vector to be exactly the right size for efficiency.
188      *
189      * @param method The function to activate on the server.
190      * @param numParams The number of paramaters this method will require.
191      * @return Vector The vector containing all the data.
192      */
193     private Vector pack(String method, int numParams) {
194         Vector v = new Vector(4 + numParams);
195         v.add("org.exolab.jms.server.mipc.IpcJmsServerConnection");
196         v.add(method);
197         v.add(_clientId);
198         v.add(_connectionId);
199         return v;
200     }
201 
202     /***
203      * A convenience method to check the success of operations
204      *
205      * @param method the requested server function.
206      * @throws JMSException On any failure.
207      * @return the result of the request
208      */
209     private Vector checkReply(String method) throws JMSException {
210         Vector v = null;
211         try {
212             v = (Vector) _connection.receive();
213         } catch (Exception exception) {
214             // rethrow as a JMSException
215             throw new JMSException("Operation " + method + " failed: "
216                                    + exception);
217         }
218 
219         if (v != null) {
220             Boolean b = (Boolean) v.get(0);
221             if (!b.booleanValue()) {
222                 if (v.get(1) instanceof JMSException) {
223                     throw (JMSException) v.get(1);
224                 } else {
225                     throw new JMSException("Operation " + method +
226                         " failed: " + v.get(1));
227                 }
228             }
229         } else {
230             throw new JMSException("Unknown connection error for " + method);
231         }
232 
233         return v;
234     }
235 
236 } //-- HttpConnectionStub