View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.ra;
    19
    20  import javax.jms.Connection;
    21  import javax.jms.ConnectionConsumer;
    22  import javax.jms.ConnectionMetaData;
    23  import javax.jms.Destination;
    24  import javax.jms.ExceptionListener;
    25  import javax.jms.JMSException;
    26  import javax.jms.Queue;
    27  import javax.jms.QueueConnection;
    28  import javax.jms.QueueSession;
    29  import javax.jms.ServerSessionPool;
    30  import javax.jms.Session;
    31  import javax.jms.Topic;
    32  import javax.jms.TopicConnection;
    33  import javax.jms.TopicSession;
    34  import java.util.ArrayList;
    35  import java.util.Iterator;
    36
    37
    38  /***
    39   * Acts as a pass through proxy for a JMS Connection object.
    40   * It intercepts events that are of interest of the ActiveMQManagedConnection.
    41   *
    42   * @version $Revision: 1.8 $
    43   */
    44  public class JMSConnectionProxy implements Connection, QueueConnection, TopicConnection {
    45
    46      private ActiveMQManagedConnection managedConnection;
    47      private ArrayList sessions = new ArrayList();
    48
    49      public JMSConnectionProxy(ActiveMQManagedConnection managedConnection) {
    50          this.managedConnection = managedConnection;
    51      }
    52
    53      /***
    54       * Used to let the ActiveMQManagedConnection that this connection
    55       * handel is not needed by the app.
    56       *
    57       * @throws JMSException
    58       */
    59      public void close() throws JMSException {
    60          managedConnection.proxyClosedEvent(this);
    61      }
    62
    63      /***
    64       * Called by the ActiveMQManagedConnection to invalidate this proxy.
    65       */
    66      public void cleanup() {
    67          managedConnection = null;
    68          for (Iterator iter = sessions.iterator(); iter.hasNext();) {
    69              JMSSessionProxy p = (JMSSessionProxy) iter.next();
    70              p.cleanup();
    71              iter.remove();
    72          }
    73      }
    74
    75      /***
    76       *
    77       */
    78      private Connection getConnection() throws JMSException {
    79          if (managedConnection == null) {
    80              throw new JMSException("Connection is closed.");
    81          }
    82          return managedConnection.getPhysicalConnection();
    83      }
    84
    85      /***
    86       * @param transacted
    87       * @param acknowledgeMode
    88       * @return
    89       * @throws JMSException
    90       */
    91      public Session createSession(boolean transacted, int acknowledgeMode)
    92              throws JMSException {
    93          return createSessionProxy();
    94      }
    95
    96      /***
    97       * @return
    98       */
    99      private JMSSessionProxy createSessionProxy() {
    100         JMSSessionProxy p = new JMSSessionProxy(managedConnection);
    101         sessions.add(p);
    102         return p;
    103     }
    104
    105     /***
    106      * @param transacted
    107      * @param acknowledgeMode
    108      * @return
    109      * @throws JMSException
    110      */
    111     public QueueSession createQueueSession(boolean transacted,
    112                                            int acknowledgeMode) throws JMSException {
    113         return createSessionProxy();
    114     }
    115
    116     /***
    117      * @param transacted
    118      * @param acknowledgeMode
    119      * @return
    120      * @throws JMSException
    121      */
    122     public TopicSession createTopicSession(boolean transacted,
    123                                            int acknowledgeMode) throws JMSException {
    124         return createSessionProxy();
    125     }
    126
    127     /***
    128      * @return
    129      * @throws JMSException
    130      */
    131     public String getClientID() throws JMSException {
    132         return getConnection().getClientID();
    133     }
    134
    135     /***
    136      * @return
    137      * @throws JMSException
    138      */
    139     public ExceptionListener getExceptionListener() throws JMSException {
    140         return getConnection().getExceptionListener();
    141     }
    142
    143     /***
    144      * @return
    145      * @throws JMSException
    146      */
    147     public ConnectionMetaData getMetaData() throws JMSException {
    148         return getConnection().getMetaData();
    149     }
    150
    151     /***
    152      * @param clientID
    153      * @throws JMSException
    154      */
    155     public void setClientID(String clientID) throws JMSException {
    156         //getConnection().setClientID(clientID);
    157     }
    158
    159     /***
    160      * @param listener
    161      * @throws JMSException
    162      */
    163     public void setExceptionListener(ExceptionListener listener)
    164             throws JMSException {
    165         getConnection().setExceptionListener(listener);
    166     }
    167
    168     /***
    169      * @throws JMSException
    170      */
    171     public void start() throws JMSException {
    172         //getConnection().start();
    173     }
    174
    175     /***
    176      * @throws JMSException
    177      */
    178     public void stop() throws JMSException {
    179         //getConnection().stop();
    180     }
    181
    182
    183     /***
    184      * @param queue
    185      * @param messageSelector
    186      * @param sessionPool
    187      * @param maxMessages
    188      * @return
    189      * @throws JMSException
    190      */
    191     public ConnectionConsumer createConnectionConsumer(Queue queue,
    192                                                        String messageSelector, ServerSessionPool sessionPool,
    193                                                        int maxMessages) throws JMSException {
    194         throw new JMSException("Not Supported.");
    195     }
    196
    197     /***
    198      * @param topic
    199      * @param messageSelector
    200      * @param sessionPool
    201      * @param maxMessages
    202      * @return
    203      * @throws JMSException
    204      */
    205     public ConnectionConsumer createConnectionConsumer(Topic topic,
    206                                                        String messageSelector, ServerSessionPool sessionPool,
    207                                                        int maxMessages) throws JMSException {
    208         throw new JMSException("Not Supported.");
    209     }
    210
    211     /***
    212      * @param destination
    213      * @param messageSelector
    214      * @param sessionPool
    215      * @param maxMessages
    216      * @return
    217      * @throws JMSException
    218      */
    219     public ConnectionConsumer createConnectionConsumer(Destination destination,
    220                                                        String messageSelector, ServerSessionPool sessionPool,
    221                                                        int maxMessages) throws JMSException {
    222         throw new JMSException("Not Supported.");
    223     }
    224
    225     /***
    226      * @param topic
    227      * @param subscriptionName
    228      * @param messageSelector
    229      * @param sessionPool
    230      * @param maxMessages
    231      * @return
    232      * @throws JMSException
    233      */
    234     public ConnectionConsumer createDurableConnectionConsumer(Topic topic,
    235                                                               String subscriptionName, String messageSelector,
    236                                                               ServerSessionPool sessionPool, int maxMessages) throws JMSException {
    237         throw new JMSException("Not Supported.");
    238     }
    239
    240     /***
    241      * @return Returns the managedConnection.
    242      */
    243     public ActiveMQManagedConnection getManagedConnection() {
    244         return managedConnection;
    245     }
    246
    247 }