jstrachan    01/08/24 07:20:28

  Added:       messenger .cvsignore PROPOSAL.html README.txt STATUS.html
                        build.properties.sample build.xml
               messenger/src/conf MANIFEST.MF
               messenger/src/java/org/apache/commons/messenger
                        Messenger.java MessengerSupport.java
               messenger/src/test/org/apache/commons/messenger TestAll.java
                        TestMessenger.java
  Log:
  First checkin of JMS facade API to make it easier to work with JMS in servlet and 
taglib environments
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/messenger/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  build.properties
  dist
  
  
  
  1.1                  jakarta-commons-sandbox/messenger/PROPOSAL.html
  
  Index: PROPOSAL.html
  ===================================================================
  <html>
  <head>
  <title>Proposal for Messenger Package</title>
  </head>
  <body bgcolor="white">
  
  <div align="center">
  <h1>Proposal for <em>Messenger</em> Package</h1>
  </div>
  
  <h3>(0) Rationale</h3>
  <p>
     The Messenger library provides a simple facade over the JMS API
     making it easy to use JMS from inside servlets and JSP tag libraries.
  </p>
  <p>
     Messenger will also provide a standard servlet for sending and receiving
     messages from JMS.
  </p>
  
  <h3>(1) Scope of the Package</h3>
  <p>
     The package will create and maintain a number of classes for
     working with JMS and to be distributed under the ASF license.
  </p>
  
  <h3>(1.5) Interaction With Other Packages</h3>
  
  <p><em>Messenger</em> relies on standard JDK 1.2 (or later) APIs for
  production deployment along with the JMS API.  
  
  It utilizes the JUnit unit testing framework for
  developing and executing unit tests, but this is of interest only to
  developers of the component.  
  
  Messenger also currently uses the Jakarta Commons Pool subproject.
  
  <p>No external configuration files are utilized.</p>
  
  <h3>(2) Initial Source of the Package</h3>
  
  <p>
     The source code is new.
  </p>
  
  <p>The proposed package name for the new component is
  <code>org.apache.commons.messenger</code>.</p>
  
  
  <h3>(3)  Required Jakarta-Commons Resources</h3>
  
  <ul>
  <li>CVS Repository - New directory <code>betwixt</code> in the
      <code>jakarta-commons</code> CVS repository.  All initial committers
      are already committers on <code>jakarta-commons</code>, so no
      additional user setups are required.</li>
  <li>Mailing List - Discussions will take place on the general
      <em>[EMAIL PROTECTED]</em> mailing list.  To help
      list subscribers identify messages of interest, it is suggested that
      the message subject of messages about this component be prefixed with
      [Messenger].</li>
  <li>Bugzilla - New component "Messenger" under the "Commons" product
      category, with appropriate version identifiers as needed.</li>
  <li>Jyve FAQ - New category "commons-messenger" (when available).
  </ul>
  
  
  <h3>(4) Initial Committers</h3>
  <ul>
     <li>Colin Crist</li>
     <li>James Strachan</li>
  </ul>
  </body>
  </html>
  
  
  
  1.1                  jakarta-commons-sandbox/messenger/README.txt
  
  Index: README.txt
  ===================================================================
  Jakarta Commons Messenger
  =========================
  
  Welcome to the Messenger component of the Jakarta Commons
  project.
  
  This component requires the excellent Ant utility.  It can 
  be found here :
  
    http://jakarta.apache.org/ant/
  
  For testing the project, you will also need JUnit :
  
    http://www.junit.org/
  
  To let the test process find JUnit, you may make a 
  copy of the build.properties.sample file, rename to
  build.properties,  and modify to reflect
  the location of the junit.jar on your computer.
  
  You will also need the Jakarta commons-pool.jar as well as jms.jar
  
  Once you have Ant propertly installed, and the
  build.properties file correctly reflects the location
  of your junit.jar, you are ready to build and test.
  
  To compile and test the component :
  
  $ ant test
  
  To build a jar :
  
  $ ant dist-jar
  
  To build the API documentation :
  
  $ ant doc
  
  To build the jar and API doc at once :
  
  $ ant dist
  
  
  
  
  
  1.1                  jakarta-commons-sandbox/messenger/STATUS.html
  
  Index: STATUS.html
  ===================================================================
  <html>
  <head>
  <title>Status File for Jakarta Commons "Messenger" Package</title>
  <head>
  <body bgcolor="white">
  
  
  <div align="center">
  <h1>The Jakarta Commons <em>Messenger</em> Package</h1>
  $Id: STATUS.html,v 1.1 2001/08/24 14:20:28 jstrachan Exp $<br>
  <a href="#Introduction">[Introduction]</a>
  <a href="#Dependencies">[Dependencies]</a>
  <a href="#Release Info">[Release Info]</a>
  <a href="#Committers">[Committers]</a>
  <a href="#Action Items">[Action Items]</a>
  <br><br>
  </div>
  
  
  <a name="Introduction"></a>
  <h3>1.  INTRODUCTION</h3>
  
  <p>The <em>Messenger</em> package provides a simple facade over the JMS API
   making it easy to use JMS from inside servlets and JSP tag libraries as
  well as providing a standard servlet for sending and receiving messages on JMS.
  </p>
  <p>
  The following classes are included:</p>
  <ul>
  <li><strong>Messenger</strong> - the main facade interface over JMS.</li>
  </ul>
  
  <a name="Dependencies"></a>
  <h3>2.  DEPENDENCIES</h3>
  
  <p>The <em>Messenger</em> package is dependent upon the following external
  components for development and use:</p>
  <ul>
  <li><a href="http://java.sun.com/j2se";>Java Development Kit</a>
      (Version 1.2 or later)</li>
  <li><a href="http://java.sun.com/products/jms";>JMS</a>
      (Version 1.0.2b or later)</li>
  <li><a href="http://jakarta.apache.org/commons/pool.html";>Jakarta Commons Pool</a>
      (Version 1.0 or later)</li>
  <li><a href="http://www.junit.org";>JUnit Testing Framework</a>
      (Version 3.7 or later) - for unit tests only, not required
      for deployment</li>
  </ul>
  
  
  <a name="Release Info"></a>
  <h3>3.  RELEASE INFO</h3>
  
  <p>Current Release:
  <!--
  <a 
href="http://jakarta.apache.org/builds/jakarta-commons/release/commons-messenger/v1.0";>Version
 1.0</a>
  -->
  
  <p>Planned Next Release:  TBD</p>
  
  <a name="Committers"></a>
  <h3>4.  COMMITTERS</h3>
  
  <p>The following individuals are the primary developers and maintainers of this
  component.  Developers who plan to use <em>Messenger</em> in their own
  projects are encouraged to collaborate on the future development of this
  component to ensure that it continues to meet a variety of needs.</p>
  <ul>
     <li><a href="mailto:[EMAIL PROTECTED]";>Colin Crist</a></li>
     <li><a href="mailto:[EMAIL PROTECTED]";>James Strachan</a></li>
  </ul>
  
  <a name="Action Items"></a>
  <h3>5.  ACTION ITEMS</h3>
  
  <p>Want to help?  Here's some "to do" items the team has identified.</p>
  
  <table border="1">
    <tr>
      <th width="80%">Action Item</th>
      <th width="20%">Volunteer</th>
    </tr>
  
    <tr>
      <td><strong>Improved configuration</strong>.  Support more flexible 
configuration support.</td>
      <td align="center">&nbsp;</td>
    </tr>
  
    <tr>
      <td><strong>Additional Documentation</strong>.  Create simple
          User's Guide, examples, or other documentation for this package.</td>
      <td align="center">&nbsp;</td>
    </tr>
  </table>
  
  </body>
  </html>
  
  
  
  1.1                  jakarta-commons-sandbox/messenger/build.properties.sample
  
  Index: build.properties.sample
  ===================================================================
  # junit.jar - JUnit 3.2+ Classpath
  junit.jar=/java/junit/junit.jar
  
  # jms.jar
  jms.jar=/java/jms/jms.jar
  
  # jakarta-commons-pool.jar
  pool.jar=../../jakarta-commons/collections/dist/commons-pool.jar
  
  
  
  
  1.1                  jakarta-commons-sandbox/messenger/build.xml
  
  Index: build.xml
  ===================================================================
  <!-- $Id: build.xml,v 1.1 2001/08/24 14:20:28 jstrachan Exp $ -->
  <project name="messenger" default="test" basedir=".">
  
     <!-- patternset describing files to be copied from the doc directory -->
     <patternset id="patternset-doc"/>
  
     <!-- patternset describing test classes -->
     <patternset id="patternset-test-classes">
        <include name="**/Test*.class"/>
     </patternset>
  
     <!-- patternset describing non test classes -->
     <patternset id="patternset-non-test-classes">
        <include name="**/*.class"/>
        <exclude name="**/Test*.class"/>
     </patternset>
  
     <!-- patternset describing non test source files (*.java, *html, etc.) -->
     <patternset id="patternset-javadocable-sources">
        <include name="**/*"/>
        <exclude name="**/Test*.java"/>
     </patternset>
  
     <!-- ######################################################### -->
  
     <target name="init">
        <tstamp/>
  
        <!-- read properties from the build.properties, if any -->
        <property name="component-propfile" value="${basedir}/build.properties"/>
        <property file="${component-propfile}"/>
  
        <!-- read properties from the commons build.properties, if any -->
        <property name="commons-propfile" value="${basedir}/../build.properties"/>
        <property file="${commons-propfile}"/>
  
        <!-- read properties from the ${user.home}/propfile, if any -->
        <property name="user-propfile" value="${user.home}/build.properties"/>
        <property file="${user-propfile}"/>
  
        <!-- command line classpath, if any -->
        <property name="cp" value=""/>
  
        <!-- now combine the classpaths -->
        <property name="classpath" value="${cp}:${jms.jar}:${junit.jar}"/>
  
        <property name="name" value="messenger"/>
        <property name="Name" value="Messenger"/>
        <property name="Name-Long" value="Jakarta Commons Messenger Package"/>
  
        <!-- The current version number of this component -->
        <property name="component.version"       value="1.0-dev"/>
  
        <property name="test.entry" value="org.apache.commons.messenger.TestAll"/>
        <property name="test.failonerror" value="true" />
        <property name="test.runner" value="junit.textui.TestRunner" />
  
        <property name="workdir" 
value="${java.io.tmpdir}/buildtemp_${DSTAMP}${TSTAMP}"/>
        <property name="source" value="${basedir}"/>
        <property name="source.src" value="${basedir}/src"/>
        <property name="source.src.java" value="${source.src}/java"/>
        <property name="source.src.test" value="${source.src}/test"/>
        <property name="source.doc" value="${basedir}/doc"/>
        <property name="dest" value="${basedir}/dist"/>
        <property name="dest.classes" value="${dest}/classes"/>
        <property name="dest.doc" value="${dest}/docs"/>
        <property name="dest.doc.api" value="${dest.doc}/api"/>
        <property name="dest.jardir" value="${dest}"/>
        <property name="dest.jardir.jar" value="${dest.jardir}/${name}.jar"/>
  
        <available property="available-doc" file="${source.doc}"/> <!-- does this 
module have docs? -->
        <available property="available-src-java" file="${source.src.java}"/> <!-- does 
this module have java src? -->
        <available property="available-src-test" file="${source.src.test}"/> <!-- does 
this module have test src? -->
  
     </target>
  
     <!-- ######################################################### -->
  
     <target name="copy-javadoc-source" depends="init" if="available-src-java">
        <mkdir dir="${javadoc-source-dir}"/>
        <copy todir="${javadoc-source-dir}" filtering="no">
           <fileset dir="${source.src.java}">
              <patternset refid="patternset-javadocable-sources"/>
           </fileset>
        </copy>
     </target>
  
     <target name="copy-doc" depends="init" if="available-doc">
        <mkdir dir="${doc-source-dir}/${name}"/>
        <copy todir="${doc-source-dir}/${name}" filtering="no">
           <fileset dir="${source.doc}">
              <patternset refid="patternset-doc"/>
           </fileset>
        </copy>
     </target>
  
     <!-- ######################################################### -->
  
     <target name="clean" depends="init" description="removes generated files">
        <delete dir="${dest}"/>
     </target>
  
     <target name="clean-doc" depends="init,clean-javadoc">
        <delete dir="${dest.doc}"/>
     </target>
  
     <target name="clean-javadoc" depends="init">
        <delete dir="${dest.doc.api}"/>
     </target>
  
     <target name="clean-build" depends="init">
        <delete dir="${dest.classes}"/>
     </target>
  
     <target name="clean-dist" depends="init">
        <delete file="${dest.jardir.jar}"/>
     </target>
  
     <!-- ######################################################### -->
  
     <target name="doc" depends="init,doc-top,doc-copy,doc-javadoc" 
description="generates javadocs and other documentation">
     </target>
  
     <target name="doc-top" depends="init">
        <mkdir  dir="${dest}"/>
        <copy todir="${dest}" file="../LICENSE"/>
     </target>
  
     <target name="doc-copy" depends="init" if="available-doc">
        <mkdir dir="${dest.doc}"/>
        <copy todir="${dest.doc}">
        <fileset dir="${source.doc}">
           <patternset refid="patternset-doc"/>
        </fileset>
        </copy>
     </target>
  
     <target name="doc-javadoc" depends="init" if="available-src-java">
        <!-- copy all the non-test sources out to the work directory and javadoc that 
-->
        <mkdir dir="${workdir}"/>
        <copy todir="${workdir}">
          <fileset dir="${source.src.java}">
            <patternset refid="patternset-javadocable-sources"/>
          </fileset>
        </copy>
        <mkdir dir="${dest.doc.api}"/>
        <javadoc packagenames="org.*"
                 sourcepath="${workdir}"
                 destdir="${dest.doc.api}"
                 windowtitle="${Name-Long}"
                 doctitle="${Name-Long}"
                 bottom="&lt;small&gt;Copyright &amp;copy; 2001 Apache Software 
Foundation. Documenation generated ${TODAY}&lt;/small&gt;."
                 public="true"
                 version="true"
                 author="true"
                 splitindex="false"
                 nodeprecated="true"
                 nodeprecatedlist="true"
                 notree="true"
                 noindex="false"
                 nohelp="true"
                 nonavbar="false"
                 serialwarn="false">
            <link href="http://java.sun.com/products/jdk/1.3/docs/api"/>
        </javadoc>
        <delete dir="${workdir}"/>
     </target>
  
     <!-- ######################################################### -->
  
     <target name="build" depends="init,build-java" description="compiles source 
files"/>
  
     <target name="build-java" depends="init" if="available-src-java">
        <mkdir dir="${dest.classes}"/>
        <javac destdir="${dest.classes}"
               srcdir="${source.src.java}"
               classpath="${classpath}"
               debug="false"
               deprecation="true"
               optimize="true"/>
     </target>
  
     <target name="build-test" depends="init,build-java" if="available-src-test">
        <mkdir dir="${dest.classes}"/>
        <javac destdir="${dest.classes}"
               srcdir="${source.src.test}"
               classpath="${classpath}"
               debug="false"
               deprecation="true"
               optimize="true"/>
     </target>
  
     <!-- ######################################################### -->
  
     <target name="test" depends="build-test" if="test.entry" description="runs 
(junit) unit tests">
        <!--
        <junit printsummary="yes" fork="on" haltonfailure="yes">
                <formatter type="plain" usefile="false"/>
                <test name="${test.entry}"/>
                <classpath>
                        <pathelement location="${dest.classes}" />
                        <pathelement path="${classpath}" />
                        <pathelement path="${java.class.path}" />
                </classpath>
        </junit>
        -->
  
        <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
          <arg value="${test.entry}"/>
          <classpath>
            <pathelement location="${dest.classes}" />
            <pathelement path="${classpath}" />
            <pathelement path="${java.class.path}" />
          </classpath>
        </java>
     </target>
  
     <!-- ######################################################### -->
  
     <target name="dist" depends="dist-jar,doc" description="builds binary 
distribution"/>
  
     <target name="dist-jar" depends="build">
        <mkdir dir="${dest.jardir}"/>
        <mkdir dir="${workdir}"/>
        <copy todir="${workdir}">
           <fileset dir="${dest.classes}">
              <patternset refid="patternset-non-test-classes"/>
           </fileset>
        </copy>
        <jar jarfile="${dest.jardir.jar}" manifest="${source.src}/conf/MANIFEST.MF">
           <fileset dir="${workdir}"/>
        </jar>
        <delete dir="${workdir}"/>
        <delete dir="${dest.classes}"/>
     </target>
  
     <!-- ######################################################### -->
  </project>
  
  
  
  1.1                  jakarta-commons-sandbox/messenger/src/conf/MANIFEST.MF
  
  Index: MANIFEST.MF
  ===================================================================
  Extension-Name: org.apache.commons.messenger
  Specification-Vendor: Apache Software Foundation
  Specification-Version: 1.0
  Implementation-Vendor: Apache Software Foundation
  Implementation-Version: 1.0-dev
  
  
  
  
  1.1                  
jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/Messenger.java
  
  Index: Messenger.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: Messenger.java,v 1.1 2001/08/24 14:20:28 jstrachan Exp $
   */
  package org.apache.commons.messenger;
  
  import java.io.Serializable;
  
  import javax.jms.BytesMessage;
  import javax.jms.JMSException;
  import javax.jms.MapMessage;
  import javax.jms.Message;
  import javax.jms.MessageListener;
  import javax.jms.ObjectMessage;
  import javax.jms.StreamMessage;
  import javax.jms.TextMessage;
  
  
  /** <p><code>Messenger</code> a facade over the JMS API making it easy to use JMS
    * and hiding much of the complexity of threading and configuration.</p>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public interface Messenger {
  
      /** Sends a message on the given subject name */
      public void send(String subject, Message message) throws JMSException;
  
      /** Sends a message on the given subject and blocks until a response is returned 
*/
      public Message call(String subject, Message message) throws JMSException;
  
      /** Receives a message on the given subject name, blocking until one is returned 
*/    
      public Message receive(String subject) throws JMSException;
      
      /** Receives a message on the given subject name and message selector, blocking 
until one is returned */    
      public Message receive(String subject, String selector) throws JMSException;
      
      /** Receives a message on the given subject name, blocking for the specified 
timeout */    
      public Message receive(String subject, long timeoutMillis) throws JMSException;
      
      /** Receives a message on the given subject name and selector, blocking for the 
specified timeout */    
      public Message receive(String subject, String selector, long timeoutMillis) 
throws JMSException;
      
      /** Receives a message on the given subject name without blocking or returns 
null */    
      public Message receiveNoWait(String subject) throws JMSException;
  
      /** Receives a message on the given subject name and selector without blocking 
or returns null */    
      public Message receiveNoWait(String subject, String selector) throws 
JMSException;
  
      
      // Listener API
      //-------------------------------------------------------------------------    
      
      /** Adds a message listener on the given subject */
      public void addListener(String subject, MessageListener listener) throws 
JMSException;
      public void addListener(String subject, String selector, MessageListener 
listener) throws JMSException;
  
      public void removeListener(String subject, MessageListener listener) throws 
JMSException;
      public void removeListener(String subject, String selector, MessageListener 
listener) throws JMSException;
      
      // Message factory methods
      //-------------------------------------------------------------------------    
      
      public BytesMessage createBytesMessage() throws JMSException;
      
      public MapMessage createMapMessage() throws JMSException;
      
      public Message createMessage() throws JMSException;    
      
      public ObjectMessage createObjectMessage() throws JMSException;
      public ObjectMessage createObjectMessage(Serializable object) throws 
JMSException;
      
      public StreamMessage createStreamMessage() throws JMSException;
      
      public TextMessage createTextMessage() throws JMSException;
      public TextMessage createTextMessage(String text) throws JMSException;
      
  }
  
  
  
  
  1.1                  
jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/MessengerSupport.java
  
  Index: MessengerSupport.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: MessengerSupport.java,v 1.1 2001/08/24 14:20:28 jstrachan Exp $
   */
  package org.apache.commons.messenger;
  
  import java.io.Serializable;
  
  import javax.jms.BytesMessage;
  import javax.jms.JMSException;
  import javax.jms.MapMessage;
  import javax.jms.Message;
  import javax.jms.MessageConsumer;
  import javax.jms.MessageListener;
  import javax.jms.MessageProducer;
  import javax.jms.ObjectMessage;
  import javax.jms.Queue;
  import javax.jms.QueueRequestor;
  import javax.jms.QueueSender;
  import javax.jms.QueueSession;
  import javax.jms.Session;
  import javax.jms.StreamMessage;
  import javax.jms.TextMessage;
  import javax.jms.Topic;
  import javax.jms.TopicPublisher;
  import javax.jms.TopicRequestor;
  import javax.jms.TopicSession;
  
  
  /** <p><code>MessengerSupport</code> is an abstract base class which implements
    * most of the functionality of Messenger. Derivations need to specify the
    * connection and session creation and the pooling strategy.</p>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public abstract class MessengerSupport implements Messenger {
  
      /** are topic subscribers durable? */
      private boolean durable;
      
      /** the durable name used for durable topic based subscriptions */
      private String durableName;
      
      /** whether local messages are ignored when topic based subscription is used
       * with a message selector */
      private boolean noLocal;
      
      
      public MessengerSupport() {
      }
      
      public void send( String subject, Message message ) throws JMSException {
          Session session = borrowSession();
          try {
              MessageProducer producer = getMessageProducer( session, subject );
              if ( producer instanceof TopicPublisher ) {
                  ((TopicPublisher) producer).publish( message );
              } 
              else {
                  ((QueueSender) producer).send( message );
              }
          }
          finally {
              returnSession( session );
          }
      }
  
      public Message call( String subject, Message message ) throws JMSException {
          Session session = borrowSession();
          try {
              if ( session instanceof TopicSession ) {
                  TopicSession topicSession = (TopicSession) session;
                  TopicRequestor requestor = new TopicRequestor( 
                      topicSession, 
                      getTopic( topicSession, subject ) 
                  );
                  return requestor.request( message );
              }
              else {
                  QueueSession queueSession = (QueueSession) session;
                  QueueRequestor requestor = new QueueRequestor( 
                      queueSession, 
                      getQueue( queueSession, subject ) 
                  );
                  return requestor.request( message );
              }
          }
          finally {
              returnSession( session );
          }
      }
  
      public Message receive(String subject) throws JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = getMessageConsumer( session, subject );
              return consumer.receive();
          }
          finally {
              returnSession( session );
          }
      }
      
      public Message receive(String subject, String selector) throws JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = getMessageConsumer( session, subject, 
selector );
              return consumer.receive();
          }
          finally {
              returnSession( session );
          }
      }
      
      public Message receive(String subject, long timeoutMillis) throws JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = getMessageConsumer( session, subject );
              return consumer.receive(timeoutMillis);
          }
          finally {
              returnSession( session );
          }
      }
      
      public Message receive(String subject, String selector, long timeoutMillis) 
throws JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = getMessageConsumer( session, subject, 
selector );
              return consumer.receive(timeoutMillis);
          }
          finally {
              returnSession( session );
          }
      }
      
      public Message receiveNoWait(String subject) throws JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = getMessageConsumer( session, subject );
              return consumer.receiveNoWait();
          }
          finally {
              returnSession( session );
          }
      }
  
      public Message receiveNoWait(String subject, String selector) throws 
JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = getMessageConsumer( session, subject, 
selector );
              return consumer.receiveNoWait();
          }
          finally {
              returnSession( session );
          }
      }
  
      
      // Listener API
      //-------------------------------------------------------------------------    
      
      public void addListener(String subject, MessageListener listener) throws 
JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = createMessageConsumer( session, subject );
              consumer.setMessageListener( listener );
          }
          finally {
              returnSession( session );
          }
      }
      
      public void addListener(String subject, String selector, MessageListener 
listener) throws JMSException {
          Session session = borrowSession();
          try {
              MessageConsumer consumer = createMessageConsumer( session, subject, 
selector );
              consumer.setMessageListener( listener );
          }
          finally {
              returnSession( session );
          }
      }
      
  
      public void removeListener(String subject, MessageListener listener ) throws 
JMSException {
          // we need to iterate through all sessions to find which one has a listener 
for 
          throw new JMSException( "Not implemented yet" );
      }
  
      public void removeListener(String subject, String selector, MessageListener 
listener ) throws JMSException {
          // we need to iterate through all sessions to find which one has a listener 
for 
          throw new JMSException( "Not implemented yet" );
      }
  
      
      // Message factory methods
      //-------------------------------------------------------------------------    
      
      public BytesMessage createBytesMessage() throws JMSException {
          Session session = borrowSession();
          try {
              return session.createBytesMessage();
          }
          finally {
              returnSession( session );
          }
      }
      
      public MapMessage createMapMessage() throws JMSException {
          Session session = borrowSession();
          try {
              return session.createMapMessage();
          }
          finally {
              returnSession( session );
          }
      }
      
      public Message createMessage() throws JMSException {    
          Session session = borrowSession();
          try {
              return session.createMessage();
          }
          finally {
              returnSession( session );
          }
      }
      
      public ObjectMessage createObjectMessage() throws JMSException {
          Session session = borrowSession();
          try {
              return session.createObjectMessage();
          }
          finally {
              returnSession( session );
          }
      }
      
      public ObjectMessage createObjectMessage(Serializable object) throws 
JMSException {
          Session session = borrowSession();
          try {
              return session.createObjectMessage(object);
          }
          finally {
              returnSession( session );
          }
      }
      
      public StreamMessage createStreamMessage() throws JMSException {
          Session session = borrowSession();
          try {
              return session.createStreamMessage();
          }
          finally {
              returnSession( session );
          }
      }
      
      public TextMessage createTextMessage() throws JMSException {
          Session session = borrowSession();
          try {
              return session.createTextMessage();
          }
          finally {
              returnSession( session );
          }
      }
      
      public TextMessage createTextMessage(String text) throws JMSException {
          Session session = borrowSession();
          try {
              return session.createTextMessage(text);
          }
          finally {
              returnSession( session );
          }
      }
      
      
      // Properties
      //-------------------------------------------------------------------------    
      
      /** Gets whether topic subscribers are durable or not */
      public boolean isDurable() {
          return noLocal;
      }
      
      /** Sets whether topic subscribers are durable or not */
      public void setDurable(boolean durable) {
          this.durable = durable;
      }
      
      /** Returns the durable name used for durable topic based subscriptions */
      public String getDurableName() {
          return durableName;
      }
      
      /** Sets the durable name used for durable topic based subscriptions */
      public void setDurableName(String durableName) {
          this.durableName = durableName;
      }
  
      /** Gets whether local messages are ignored when topic based subscription is 
used
       * with a message selector */
      public boolean isNoLocal() {
          return noLocal;
      }
      
      /** Sets whether local messages are ignored when topic based subscription is 
used
       * with a message selector */
      public void setNoLocal(boolean noLocal) {
          this.noLocal = noLocal;
      }
      
      // Implementation methods
      //-------------------------------------------------------------------------    
      
      /** Borrows a session instance from the pool */
      protected abstract Session borrowSession() throws JMSException;
      
      /** Returns a session instance back to the pool */
      protected abstract void returnSession(Session session) throws JMSException;
  
      /** Returns a message producer for the given session and subject */
      protected MessageProducer getMessageProducer( Session session, String subject ) 
throws JMSException {
          if ( session instanceof TopicSession ) {
              TopicSession topicSession = (TopicSession) session;
              return topicSession.createPublisher( getTopic(topicSession, subject) );
          }
          else {
              QueueSession queueSession = (QueueSession) session;
              return queueSession.createSender( getQueue(queueSession, subject) );
          }
      }
  
      /** Returns a MessageConsumer for the given session and subject */
      protected MessageConsumer getMessageConsumer( Session session, String subject ) 
throws JMSException {
          // could do caching one day
          return createMessageConsumer( session, subject );
      }
      
      /** Returns a MessageConsumer for the given session, subject and selector */
      protected MessageConsumer getMessageConsumer( Session session, String subject, 
String selector ) throws JMSException {
          // could do caching one day
          return createMessageConsumer( session, subject, selector );
      }
      
      /** Returns a new MessageConsumer for the given session and subject */
      protected MessageConsumer createMessageConsumer( Session session, String subject 
) throws JMSException {
          if ( session instanceof TopicSession ) {
              TopicSession topicSession = (TopicSession) session;
              if ( isDurable() ) {
                  return topicSession.createDurableSubscriber( 
                      getTopic(topicSession, subject), 
                      getDurableName() 
                  );
              }
              else {
                  return topicSession.createSubscriber( 
                      getTopic(topicSession, subject) 
                  );
              }
          }
          else {
              QueueSession queueSession = (QueueSession) session;
              return queueSession.createReceiver( getQueue(queueSession, subject) );
          }
      }
      
      /** Returns a new MessageConsumer for the given session, subject and selector */
      protected MessageConsumer createMessageConsumer( Session session, String 
subject, String selector ) throws JMSException {
          if ( session instanceof TopicSession ) {
              TopicSession topicSession = (TopicSession) session;
              if ( isDurable() ) {
                  return topicSession.createDurableSubscriber( 
                      getTopic(topicSession, subject), 
                      getDurableName(), 
                      selector, 
                      isNoLocal() 
                  );
              }
              else {
                  return topicSession.createSubscriber( 
                      getTopic(topicSession, subject), 
                      selector, 
                      isNoLocal()
                  );
              }
          }
          else {
              QueueSession queueSession = (QueueSession) session;
              return queueSession.createReceiver(
                  getQueue(queueSession, subject), 
                  selector 
              );
          }
      }
      
      protected Queue getQueue(Session session, String subject) throws JMSException {
          return getQueue((QueueSession) session, subject);
      }
      
      protected Queue getQueue(QueueSession session, String subject) throws 
JMSException {
          // XXXX: might want to cache
          return session.createQueue( subject );
      }
      
      protected Topic getTopic(Session session, String subject) throws JMSException {
          return getTopic((TopicSession) session, subject);
      }
      
      protected Topic getTopic(TopicSession session, String subject) throws 
JMSException {
          // XXXX: might want to cache
          return session.createTopic( subject );
      }
      
  }
  
  
  
  
  1.1                  
jakarta-commons-sandbox/messenger/src/test/org/apache/commons/messenger/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: TestAll.java,v 1.1 2001/08/24 14:20:28 jstrachan Exp $
   */
  package org.apache.commons.messenger;
  
  import junit.framework.*;
  
  /** Entry point for all JUnit tests.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
    * @version $Revision: 1.1 $
   */
  public class TestAll extends TestCase {
      public TestAll(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          TestSuite suite = new TestSuite();
          suite.addTest(TestMessenger.suite());
          return suite;
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestAll.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/messenger/src/test/org/apache/commons/messenger/TestMessenger.java
  
  Index: TestMessenger.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: TestMessenger.java,v 1.1 2001/08/24 14:20:28 jstrachan Exp $
   */
  package org.apache.commons.messenger;
  
  import java.io.StringWriter;
  
  import junit.framework.*;
  
  
  /** Test harness for Messenger
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class TestMessenger extends TestCase {
      
      public static Test suite() {
          return new TestSuite(TestMessenger.class);
      }
      
      public TestMessenger(String testName) {
          super(testName);
      }
      
      public void testSend() throws Exception {
      }
  }
  
  
  
  

Reply via email to