📄 httpjmsconnectionstub.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: HttpJmsConnectionStub.java,v 1.5 2003/08/25 03:33:19 tanderson Exp $
*
* Date Author Changes
* 12 Oct 2001 mourikis Created
*/
package org.exolab.jms.client.http;
import java.util.Enumeration;
import java.util.Vector;
import javax.jms.JMSException;
import org.exolab.core.ipc.IpcIfc;
import org.exolab.jms.client.JmsConnectionStubIfc;
import org.exolab.jms.client.JmsSessionStubIfc;
/**
* Used to create OpenJmsSessions to the server. It gets the new Session Id
* assigned by the server and returns a HttpJmsSessionStub.
*
* @version $Revision: 1.5 $ $Date: 2003/08/25 03:33:19 $
* @author <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
* @see org.exolab.core.http.HttpClient
*/
public class HttpJmsConnectionStub implements JmsConnectionStubIfc {
/**
* This is a reference to the Client connection.
*/
private IpcIfc _connection = null;
// The client connection id.
private String _clientId;
// The destination connection id
private String _connectionId;
// The list of sessions for this connection.
Vector _sessions = new Vector();
/**
* Construct an instance of this class with the specified id's.
* These id's are used when creating sessions.
*
* @param connection The http connection to the server.
* @param clientId This clients unique id.
* @param connectionId This objects connection identifier.
* @throws JMSException If the connection has failed.
*
*/
public HttpJmsConnectionStub(IpcIfc connection, String clientId,
String connectionId)
throws JMSException {
if (connection != null) {
_connection = connection;
_clientId = clientId;
_connectionId = connectionId;
} else {
throw new JMSException("Cannot instantiate with a null " +
"connection");
}
}
/**
* Send a session connection message. Create a new HttpJmsSessionStub
* if this createSession request was successfull.
*
* @param ackMode - the ack mode for the session
* @param transacted - true if the session is transacted
* @return JmsSessionStubIfc The Session interface.
* @throws JMSException On failure to create a session.
*/
public JmsSessionStubIfc createSession(int ackMode, boolean transacted)
throws JMSException {
JmsSessionStubIfc stub = null;
try {
Vector v = pack("createSession", 2);
synchronized (_connection) {
v.add(new Integer(ackMode));
v.add(new Boolean(transacted));
_connection.send(v);
v = checkReply("createSession");
}
Vector state = (Vector) v.get(1);
String sessionId = (String) state.get(0);
stub = new HttpJmsSessionStub(_connection, _clientId,
_connectionId, sessionId);
_sessions.add(stub);
} catch (JMSException exception) {
throw exception;
} catch (Exception exception) {
// rethrow as a JMSException
throw new JMSException("Failed to create session: " + exception);
}
return stub;
}
/**
* Send a close request to the server.
*
* @throws JMSException On failure to close connection.
*/
public void close() throws JMSException {
try {
Vector v = pack("close", 0);
synchronized (_connection) {
_connection.send(v);
v = checkReply("close");
}
} catch (JMSException exception) {
throw exception;
} catch (Exception exception) {
// rethrow as a JMSException
throw new JMSException("Failed to close: " + exception);
} finally {
_connection = null;
for (Enumeration e = _sessions.elements();
e.hasMoreElements();) {
((HttpJmsSessionStub) e.nextElement()).stopReceiver();
}
}
}
/**
* Return the client connection id.
*
* @return the client connection id.
*/
public String getConnectionId() {
return _connectionId;
}
// implementation of JmsConnectionStubIfc.destroy
public void destroy() {
_connection = null;
}
/**
* Pack all the data that is required by the server in a vector.
* Set the size of the vector to be exactly the right size for efficiency.
*
* @param method The function to activate on the server.
* @param numParams The number of paramaters this method will require.
* @return Vector The vector containing all the data.
*/
private Vector pack(String method, int numParams) {
Vector v = new Vector(4 + numParams);
v.add("org.exolab.jms.server.mipc.IpcJmsServerConnection");
v.add(method);
v.add(_clientId);
v.add(_connectionId);
return v;
}
/**
* A convenience method to check the success of operations
*
* @param method the requested server function.
* @throws JMSException On any failure.
* @return the result of the request
*/
private Vector checkReply(String method) throws JMSException {
Vector v = null;
try {
v = (Vector) _connection.receive();
} catch (Exception exception) {
// rethrow as a JMSException
throw new JMSException("Operation " + method + " failed: "
+ exception);
}
if (v != null) {
Boolean b = (Boolean) v.get(0);
if (!b.booleanValue()) {
if (v.get(1) instanceof JMSException) {
throw (JMSException) v.get(1);
} else {
throw new JMSException("Operation " + method +
" failed: " + v.get(1));
}
}
} else {
throw new JMSException("Unknown connection error for " + method);
}
return v;
}
} //-- HttpConnectionStub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -