Coverage Report - org.apache.commons.messenger.task.ConsumerTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ConsumerTask
0%
0/69
0%
0/14
1.882
 
 1  
 /*
 2  
  * Copyright (C) The Apache Software Foundation. All rights reserved.
 3  
  *
 4  
  * This software is published under the terms of the Apache Software License
 5  
  * version 1.1, a copy of which has been included with this distribution in
 6  
  * the LICENSE file.
 7  
  * 
 8  
  * $Id: ConsumerTask.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messenger.task;
 11  
 
 12  
 import java.io.File;
 13  
 import java.io.FileWriter;
 14  
 import java.io.IOException;
 15  
 
 16  
 import javax.jms.Destination;
 17  
 import javax.jms.JMSException;
 18  
 import javax.jms.Message;
 19  
 import javax.jms.TextMessage;
 20  
 
 21  
 import org.apache.commons.messenger.Messenger;
 22  
 import org.apache.commons.messenger.MessengerManager;
 23  
 import org.apache.tools.ant.BuildException;
 24  
 import org.apache.tools.ant.Project;
 25  
 import org.apache.tools.ant.Task;
 26  
 
 27  
 /** 
 28  
  * <p><code>ConsumerTask</code> is an Ant task which will 
 29  
  * publish all of the given text files as a JMS Text Message
 30  
  * using a given JMS Connection (Messenger) and a Destination
 31  
  *
 32  
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 33  
  * @version $Revision: 155459 $
 34  
  */
 35  0
 public class ConsumerTask extends Task {
 36  
 
 37  
     private Messenger messenger;
 38  
     private String messengerName;
 39  
     private Destination destination;
 40  
     private String subject;
 41  
     private MessengerManager messengerManager;    
 42  
 
 43  
     /** the number of messages to receive */
 44  
     private int count;
 45  
 
 46  
     /** the output directory */
 47  0
     private File dir = new File(".");    
 48  
     
 49  
     // Properties
 50  
     //-------------------------------------------------------------------------
 51  
     
 52  
 
 53  
     /** 
 54  
      * Sets the output directory 
 55  
      */
 56  
     public void setDir(File dir) {
 57  0
         this.dir = dir;
 58  0
     }
 59  
 
 60  
     public Messenger getMessenger() throws JMSException {
 61  0
         if ( messenger == null ) {
 62  0
             messenger = getMessengerManager().getMessenger( getMessengerName() );
 63  
         }
 64  0
         return messenger;
 65  
     }
 66  
     
 67  
     /** Sets the Messenger to be used */
 68  
     public void setMessenger(Messenger messenger) {
 69  0
         this.messenger = messenger;
 70  0
     }
 71  
     
 72  
     /** Getter for property messengerName.
 73  
      * @return Value of property messengerName.
 74  
      */
 75  
     public String getMessengerName() {
 76  0
         return messengerName;
 77  
     }
 78  
 
 79  
     /** Setter for property messengerName.
 80  
      * @param messengerName New value of property messengerName.
 81  
      */
 82  
     public void setMessengerName(String messengerName) {
 83  0
         this.messengerName = messengerName;
 84  0
     }
 85  
 
 86  
     /** Getter for property destination.
 87  
      * @return Value of property destination.
 88  
      */
 89  
     public Destination getDestination() throws JMSException {
 90  0
         if ( destination == null ) {
 91  0
             destination = getMessenger().getDestination( getSubject() );
 92  
         }
 93  0
         return destination;
 94  
     }
 95  
 
 96  
     /** Setter for property destination.
 97  
      * @param destination New value of property destination.
 98  
      */
 99  
     public void setDestination(Destination destination) {
 100  0
         this.destination = destination;
 101  0
     }
 102  
 
 103  
     /** Getter for property subject.
 104  
      * @return Value of property subject.
 105  
      */
 106  
     public String getSubject() {
 107  0
         return subject;
 108  
     }
 109  
 
 110  
     /** Setter for property subject.
 111  
      * @param subject New value of property subject.
 112  
      */
 113  
     public void setSubject(String subject) {
 114  0
         this.subject = subject;
 115  0
     }
 116  
     
 117  
     
 118  
     /** Getter for property messengerManager.
 119  
      * @return Value of property messengerManager.
 120  
      */
 121  
     public MessengerManager getMessengerManager() {
 122  0
         return messengerManager;
 123  
     }
 124  
     
 125  
     /** Setter for property messengerManager.
 126  
      * @param messengerManager New value of property messengerManager.
 127  
      */
 128  
     public void setMessengerManager(MessengerManager messengerManager) {
 129  0
         this.messengerManager = messengerManager;
 130  0
     }
 131  
 
 132  
     /** 
 133  
      * Sets the URI of the Messenger.xml configuration document to use
 134  
      * to configure the messengers to use for this task.
 135  
      */
 136  
     public void setConfiguration(String uri) throws JMSException {
 137  0
         setMessengerManager( MessengerManager.load( uri ) );
 138  0
     }
 139  
     
 140  
     /** 
 141  
      * @return the number of messages to receive.
 142  
      * A number less than or equal to 0 will receive messages forever
 143  
      */
 144  
     public int getCount() {
 145  0
         return count;
 146  
     }
 147  
     
 148  
     /** 
 149  
      * Setter for the number of messages to receive.
 150  
      * A number less than or equal to 0 will receive messages forever
 151  
      */
 152  
     public void setCount(int count) {
 153  0
         this.count = count;
 154  0
     }
 155  
     
 156  
     
 157  
     // Task interface
 158  
     //-------------------------------------------------------------------------
 159  
     
 160  
     /**
 161  
      * Performs the copy operation.
 162  
      */
 163  
     public void execute() throws BuildException {
 164  
         try {
 165  0
             Messenger messenger = getMessenger();
 166  0
             if ( messenger == null ) {
 167  0
                 throw new BuildException("Must specify a valid Messenger", location );
 168  
             }
 169  0
             Destination destination = getDestination();
 170  0
             if ( destination == null ) {
 171  0
                 throw new BuildException("Must specify a valid JMS Destination", location );
 172  
             }
 173  
 
 174  0
             if ( count > 0 ) {
 175  0
                 log( "Will wait until I receive: " + count + " messages and will write to directory: " + dir );
 176  
                 
 177  0
                 for ( int i = 0; i < count; i++ ) {
 178  0
                     Message message = messenger.receive( destination );
 179  0
                     processMessage( message );
 180  
                 }
 181  
                 
 182  0
                 log( "Finished." );
 183  
             }
 184  
             else {
 185  0
                 log( "Infinite loop. Will write to directory: " + dir );
 186  
                 
 187  
                 while (true) {
 188  0
                     Message message = messenger.receive( destination );
 189  0
                     processMessage( message );
 190  0
                 }
 191  
             }
 192  
         }
 193  0
         catch (IOException e) {
 194  0
             log( "Caught exception: " + e, Project.MSG_ERR );
 195  0
             throw new BuildException(e, location);
 196  
         }
 197  0
         catch (JMSException e) {
 198  0
             log( "Caught exception: " + e, Project.MSG_ERR );
 199  0
             throw new BuildException(e, location);
 200  
         }
 201  
         finally {        
 202  0
             try {
 203  
                 // close the JMS connection to release any background threads        
 204  0
                 messenger.close();
 205  
             }
 206  0
             catch (Exception e) {
 207  
                 // ignore close exceptions
 208  0
             }
 209  0
         }
 210  0
     }
 211  
 
 212  
     /**
 213  
      * Processes a given message
 214  
      */
 215  
     protected void processMessage(Message message) throws IOException, JMSException {
 216  0
         log( "Received message to: " + message );
 217  
 
 218  0
         String text = null;
 219  0
         if ( message instanceof TextMessage ) {
 220  0
             TextMessage textMessage = (TextMessage) message;
 221  0
             text = textMessage.toString();
 222  0
         }
 223  
         else {
 224  
             // #### bit of a hack!!!
 225  
             // ideally we need an XML format for message persistence
 226  0
             text = message.toString();
 227  
         }       
 228  0
         processMessageText(text);
 229  0
     }
 230  
     
 231  
     /** 
 232  
      * Writes the given text to a file
 233  
      */
 234  
     protected void processMessageText(String text) throws IOException {
 235  0
         FileWriter writer = new FileWriter( dir );
 236  0
         writer.write ( text );
 237  0
         writer.close();
 238  0
     }
 239  
 }
 240  
 
 241