001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JMSClientConnection.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.proxy.jms;
030:
031: import com.sun.jbi.binding.proxy.LocalStringKeys;
032:
033: import com.sun.jbi.binding.proxy.util.Translator;
034:
035: import com.sun.jbi.binding.proxy.connection.ClientConnection;
036:
037: import javax.jms.BytesMessage;
038: import javax.jms.MessageProducer;
039: import javax.jms.Queue;
040: import javax.jms.QueueSender;
041: import javax.jms.Session;
042:
043: /**
044: * Connection as a JMS Client.
045: * @author Sun Microsystems, Inc
046: */
047: public class JMSClientConnection implements ClientConnection {
048: private MessageProducer mProducer;
049: private BytesMessage mSendBytes;
050: private BytesMessage mReceiveBytes;
051: private String mId;
052: private int mByteCount;
053:
054: /**
055: * Creates a new instance of JMSClientConnection
056: * @param session to be used to create any JMS resources.
057: * @param id to be assigned to this connection.
058: * @throws com.sun.jbi.binding.proxy.connection.ConnectionException for any JMS problems.
059: */
060: JMSClientConnection(JMSServerConnection reply, Session session,
061: Queue queue)
062: throws com.sun.jbi.binding.proxy.connection.ConnectionException {
063: try {
064: mId = queue.getQueueName();
065: mSendBytes = session.createBytesMessage();
066: mSendBytes.setJMSReplyTo(reply.getReplyQueue());
067: mProducer = session.createProducer(queue);
068: } catch (javax.jms.JMSException jEx) {
069: throw new com.sun.jbi.binding.proxy.connection.ConnectionException(
070: Translator
071: .translate(LocalStringKeys.CANT_CREATE_CLIENTCONNECTION),
072: jEx);
073: }
074: }
075:
076: /**
077: * Send array of bytes on the JMS connection.
078: * @param bytes to be sent
079: * @throws com.sun.jbi.binding.proxy.connection.ConnectionException for any JMS problems.
080: */
081: public void send(byte[] bytes)
082: throws com.sun.jbi.binding.proxy.connection.ConnectionException {
083: try {
084: mSendBytes.writeBytes(bytes);
085: mProducer.send(mSendBytes);
086: mSendBytes.clearBody();
087: mSendBytes.clearProperties();
088: } catch (javax.jms.JMSException jEx) {
089: throw new com.sun.jbi.binding.proxy.connection.ConnectionException(
090: Translator
091: .translate(LocalStringKeys.CANT_SEND_CLIENTCONNECTION),
092: jEx);
093: }
094: }
095:
096: /**
097: * Receive array of bytes on the JMS connection.
098: * @return bytes to be returned
099: * @throws com.sun.jbi.binding.proxy.connection.ConnectionException for any JMS problems.
100: */
101: public byte[] receive()
102: throws com.sun.jbi.binding.proxy.connection.ConnectionException {
103: int count;
104: byte[] bytes;
105:
106: //
107: // Create a ME from the byte stream.
108: //
109: try {
110: mByteCount = (int) mReceiveBytes.getBodyLength();
111: bytes = new byte[mByteCount];
112: mReceiveBytes.readBytes(bytes);
113: mReceiveBytes = null;
114: return (bytes);
115: } catch (javax.jms.JMSException jEx) {
116: throw new com.sun.jbi.binding.proxy.connection.ConnectionException(
117: Translator
118: .translate(LocalStringKeys.CANT_RECEIVE_CLIENTCONNECTION),
119: jEx);
120: }
121:
122: }
123:
124: /**
125: * Return the instanceId of this connection.
126: * @return String containing instanceId.
127: */
128: public String getInstanceId() {
129: return (mId);
130: }
131:
132: void setMessage(BytesMessage bytes) {
133: mReceiveBytes = bytes;
134: }
135:
136: public int receivedByteCount() {
137: return (mByteCount);
138: }
139: }
|