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