You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/09/18 20:47:17 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms SubscribeTag.java MessageOperationTag.java ConnectionTag.java DestinationTag.java JMSTagLibrary.java

jstrachan    2002/09/18 11:47:17

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/jms
                        MessageOperationTag.java ConnectionTag.java
                        DestinationTag.java JMSTagLibrary.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/jms
                        SubscribeTag.java
  Log:
  Patched the JMS tag library to better support easy destinations via an optional subject="a.b.c" attribute.
  
  Also improved the error reporting somewhat
  
  Finally added a subscribe tag that can be useful for Messagelets (from Commons Messenger) style subscriptions from inside Jelly. e.g.
  
  <connection name="foo">
  
    <subscribe subject="my.queue" messageListener="${whatever}"/>
  
    <subscribe subject="my.queue" selector="a='xyz'" messageListener="${whatever}"/>
  
    <subscribe subject="my.queue">
  	<myCustomTagThatMakesAMessageListener/>
  
    </subscribe>
  
  
  etc.
  
  Revision  Changes    Path
  1.2       +30 -2     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/MessageOperationTag.java
  
  Index: MessageOperationTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/MessageOperationTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MessageOperationTag.java	22 Jun 2002 16:55:50 -0000	1.1
  +++ MessageOperationTag.java	18 Sep 2002 18:47:17 -0000	1.2
  @@ -82,6 +82,9 @@
       
       /** The Destination */
       private Destination destination;
  +
  +    /** The String subject used to find a destination */
  +    private String subject;
       
       public MessageOperationTag() {
       }
  @@ -102,24 +105,49 @@
           this.connection = connection;
       }
       
  -    public Destination getDestination() {
  +    public Destination getDestination() throws JellyException, JMSException {
  +        if (destination == null) {
  +            // if we have a subject defined, lets use it to find the destination
  +            if (subject != null) {
  +                destination = findDestination(subject);
  +            }
  +        }
           return destination;
       }
       
       /**
  -     * Sets the JMS destination to be used by this tag
  +     * Sets the JMS Destination to be used by this tag
        */
       public void setDestination(Destination destination) {
           this.destination = destination;
       }
   
  +    /**
  +     * Sets the subject as a String which is used to create the 
  +     * JMS Destination to be used by this tag
  +     */
  +    public void setSubject(String subject) {
  +        this.subject = subject;
  +    }
  +
       // Implementation methods
       //-------------------------------------------------------------------------                            
  +    
  +    /**
  +     * Strategy Method allowing derived classes to change this behaviour
  +     */
       protected Messenger findConnection() throws JellyException, JMSException {
           ConnectionContext messengerTag = (ConnectionContext) findAncestorWithClass( ConnectionContext.class );
           if ( messengerTag == null ) {
               throw new JellyException("This tag must be within a <jms:connection> tag or the 'connection' attribute should be specified");
           }
           return messengerTag.getConnection();
  +    }
  +
  +    /**
  +     * Strategy Method allowing derived classes to change this behaviour
  +     */
  +    protected Destination findDestination(String subject) throws JellyException, JMSException {
  +        return getConnection().getDestination(subject);
       }
   }    
  
  
  
  1.3       +5 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/ConnectionTag.java
  
  Index: ConnectionTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/ConnectionTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ConnectionTag.java	26 Jun 2002 09:24:35 -0000	1.2
  +++ ConnectionTag.java	18 Sep 2002 18:47:17 -0000	1.3
  @@ -96,7 +96,11 @@
       //-------------------------------------------------------------------------                    
       public void doTag(XMLOutput output) throws Exception {        
           connection = MessengerManager.get( name );
  -        
  +
  +        if (connection == null) {        
  +            throw new JellyException( "Could not find a JMS connection called: " + name );
  +        }
  +
           if ( var != null ) {
               context.setVariable( var, connection );
           }
  
  
  
  1.2       +3 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/DestinationTag.java
  
  Index: DestinationTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/DestinationTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DestinationTag.java	22 Jun 2002 16:55:50 -0000	1.1
  +++ DestinationTag.java	18 Sep 2002 18:47:17 -0000	1.2
  @@ -91,6 +91,9 @@
               throw new JellyException("<jms:destination> tag must be within a <jms:connection> or <jms:send> or <jms:receive> tag");
           }
           Messenger messenger = messengerTag.getConnection();
  +        if (messenger == null) {
  +            throw new JellyException("No JMS Connection could be found!" );            
  +        }
           String subject = (name != null) ? name : getBodyText();
           Destination destination = messenger.getDestination( subject );
           if ( var != null ) {
  
  
  
  1.3       +1 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/JMSTagLibrary.java
  
  Index: JMSTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/JMSTagLibrary.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JMSTagLibrary.java	22 Jun 2002 23:54:41 -0000	1.2
  +++ JMSTagLibrary.java	18 Sep 2002 18:47:17 -0000	1.3
  @@ -80,6 +80,7 @@
           registerTag("property", PropertyTag.class);
           registerTag("receive", ReceiveTag.class);
           registerTag("send", SendTag.class);
  +        registerTag("subscribe", SubscribeTag.class);
           registerTag("textMessage", TextMessageTag.class);
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jms/SubscribeTag.java
  
  Index: SubscribeTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/taglibs/beanshell/src/java/org/apache/commons/jelly/tags/beanshell/BeanShellExpressionFactory.java,v 1.1 2002/05/21 07:58:55 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2002/05/21 07:58:55 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 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 end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``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 THE APACHE SOFTWARE FOUNDATION 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.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: BeanShellExpressionFactory.java,v 1.1 2002/05/21 07:58:55 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.jms;
  
  import javax.jms.Destination;
  import javax.jms.Message;
  import javax.jms.MessageListener;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.XMLOutput;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /** 
   * Performs a subscription to some JMS connection to a destination maybe with a selector.
   * A JMS MessageListener can be specified, or a special child tag can explicitly set it on 
   * its parent (so a special tag could construct a MessageListener object and register it with this tag).
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.1 $
   */
  public class SubscribeTag extends MessageOperationTag {
  
      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(SubscribeTag.class);
  
      /** the JMS Selector for the subscription */
      private String selector;
      
      /** The JMS MessageListener used to create the subscription */
      private MessageListener messageListener;
      
      public SubscribeTag() {
      }
      
      // Tag interface
      //-------------------------------------------------------------------------                    
      public void doTag(XMLOutput output) throws Exception {
  
          // evaluate body as it may contain child tags to register a MessageListener
          invokeBody(output);
          
          MessageListener listener = getMessageListener();
          if (listener == null) {
              throw new JellyException( "No messageListener attribute is specified so could not subscribe" );
          }
  
          // clear the listener for the next tag invocation, if caching is employed
          setMessageListener(null);
  
          
          Destination destination = getDestination();
          if ( destination == null ) {
              throw new JellyException( "No destination specified. Either specify a 'destination' attribute or use a nested <jms:destination> tag" );
          }
          
          if ( log.isDebugEnabled() ) {
              log.debug( "About to consume to: " + destination + " with listener: " + listener );
          }
              
          log.info( "About to consume to: " + destination + " with listener: " + listener );
              
          if (selector == null ) {            
              getConnection().addListener( destination, listener );
          }
          else {
              getConnection().addListener( destination, selector, listener );
          }
      }
      
      // Properties
      //-------------------------------------------------------------------------   
      
      /**
       * Sets the optional JMS Message selector for the subscription
       */
      public void setSelector(String selector) {
          this.selector = selector;
      }                             
      
  
      /**
       * Returns the messageListener.
       * @return MessageListener
       */
      public MessageListener getMessageListener() {
          return messageListener;
      }
  
      
      /**
       * Sets the JMS messageListener used ot consume JMS messages on the given destination
       */
      public void setMessageListener(MessageListener messageListener) {
          this.messageListener = messageListener;
      }
  
  }    
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>