001    /*
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "JMSQueueAdapter.java".  Description: 
010    "An implementation of JMSDestination that uses an underlying Queue." 
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
013    2004.  All Rights Reserved. 
014    
015    Contributor(s): ______________________________________. 
016    
017    Alternatively, the contents of this file may be used under the terms of the 
018    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
019    applicable instead of those above.  If you wish to allow use of your version of this 
020    file only under the terms of the GPL and not to allow others to use your version 
021    of this file under the MPL, indicate your decision by deleting  the provisions above 
022    and replace  them with the notice and other provisions required by the GPL License.  
023    If you do not delete the provisions above, a recipient may use your version of 
024    this file under either the MPL or the GPL. 
025    */
026    
027    package ca.uhn.hl7v2.protocol.impl;
028    
029    import javax.jms.Connection;
030    import javax.jms.JMSException;
031    import javax.jms.Message;
032    import javax.jms.Queue;
033    import javax.jms.QueueConnection;
034    import javax.jms.QueueReceiver;
035    import javax.jms.QueueSender;
036    import javax.jms.QueueSession;
037    import javax.jms.Session;
038    import javax.jms.TextMessage;
039    
040    import ca.uhn.hl7v2.protocol.JMSDestination;
041    
042    /**
043     * An implementation of <code>JMSDestination</code> that uses an underlying 
044     * Queue.  
045     * 
046     * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
047     * @version $Revision: 1.1 $ updated on $Date: 2007/02/19 02:24:26 $ by $Author: jamesagnew $
048     */
049    public class JMSQueueAdapter implements JMSDestination {
050    
051        private QueueSession mySendingSession;
052        private QueueSession myReceivingSession; 
053        private QueueSender mySender;
054        private QueueReceiver myReceiver;
055        private QueueConnection myConnection;
056        private Queue myQueue;
057        private boolean myIsConnected;
058    
059        /**
060         * @param theConnection
061         * @param theDestination
062         */
063        public JMSQueueAdapter(QueueConnection theConnection, Queue theDestination) {
064            myConnection = theConnection;
065            myQueue = theDestination;
066        }
067    
068        /**
069         * @see ca.uhn.hl7v2.protocol.JMSDestination#getName()
070         */
071        public String getName() throws JMSException {
072            return myQueue.getQueueName();
073        }
074    
075        /**
076         * @see ca.uhn.hl7v2.protocol.JMSDestination#createMessage()
077         */
078        public TextMessage createMessage() throws JMSException {
079            return mySendingSession.createTextMessage();
080        }
081    
082        /** 
083         * @see ca.uhn.hl7v2.protocol.JMSDestination#connect()
084         */
085        public void connect() throws JMSException {
086            boolean transacted = false;
087            int ackMode = Session.AUTO_ACKNOWLEDGE;
088    
089            disconnect();
090    
091            mySendingSession = myConnection.createQueueSession(transacted, ackMode);
092            mySender = mySendingSession.createSender(myQueue);
093    
094            myReceivingSession = myConnection.createQueueSession(transacted, ackMode);
095            myReceiver = myReceivingSession.createReceiver(myQueue);
096            
097            myIsConnected = true;
098        }
099    
100        /** 
101         * @see ca.uhn.hl7v2.protocol.JMSDestination#disconnect()
102         */
103        public void disconnect() throws JMSException {
104            myIsConnected = false;
105            if (mySendingSession != null) {
106                mySendingSession.close();
107            }
108            if (myReceivingSession != null) {
109                myReceivingSession.close();
110            }
111        }
112    
113        /** 
114         * @see ca.uhn.hl7v2.protocol.JMSDestination#send(javax.jms.Message)
115         */
116        public void send(Message theMessage) throws JMSException {
117            mySender.send(theMessage);
118        }
119    
120        /** 
121         * @see ca.uhn.hl7v2.protocol.JMSDestination#receive()
122         */
123        public Message receive() throws JMSException {
124            return myReceiver.receive();
125        }
126    
127        /** 
128         * @see ca.uhn.hl7v2.protocol.JMSDestination#isConnected()
129         */
130        public boolean isConnected() {
131            return myIsConnected;
132        }
133    
134        /**
135         * @see ca.uhn.hl7v2.protocol.JMSDestination#getConnection()
136         */
137        public Connection getConnection() {
138            return myConnection;
139        }
140    
141    }