Coverage Report - org.apache.commons.messenger.task.ProducerTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ProducerTask
0%
0/83
0%
0/24
2.05
 
 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: ProducerTask.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messenger.task;
 11  
 
 12  
 import java.io.BufferedReader;
 13  
 import java.io.File;
 14  
 import java.io.FileReader;
 15  
 import java.io.IOException;
 16  
 import java.io.Reader;
 17  
 import java.util.Iterator;
 18  
 import java.util.Vector;
 19  
 
 20  
 import javax.jms.Destination;
 21  
 import javax.jms.JMSException;
 22  
 import javax.jms.TextMessage;
 23  
 
 24  
 import org.apache.commons.messenger.Messenger;
 25  
 import org.apache.commons.messenger.MessengerManager;
 26  
 import org.apache.tools.ant.BuildException;
 27  
 import org.apache.tools.ant.DirectoryScanner;
 28  
 import org.apache.tools.ant.Task;
 29  
 import org.apache.tools.ant.types.FileSet;
 30  
 
 31  
 /** 
 32  
  * <p><code>ProducerTask</code> is an Ant task which will 
 33  
  * publish all of the given text files as a JMS Text Message
 34  
  * using a given JMS Connection (Messenger) and a Destination
 35  
  *
 36  
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 37  
  * @version $Revision: 155459 $
 38  
  */
 39  0
 public class ProducerTask extends Task {
 40  
 
 41  0
     private Vector filesets = new Vector();
 42  
     private Messenger messenger;
 43  
     private String messengerName;
 44  
     private Destination destination;
 45  
     private String subject;
 46  
     private MessengerManager messengerManager;   
 47  
     private File file; 
 48  
 
 49  
     /** Holds value of property sleep. */
 50  
     private long sleep;
 51  
     
 52  
     // Properties
 53  
     //-------------------------------------------------------------------------
 54  
     
 55  
 
 56  
     /**
 57  
      * Adds a set of files (nested fileset attribute).
 58  
      */
 59  
     public void addFileset(FileSet set) {
 60  0
         filesets.addElement(set);
 61  0
     }
 62  
 
 63  
     public Messenger getMessenger() throws JMSException {
 64  0
         if ( messenger == null ) {
 65  0
             messenger = getMessengerManager().getMessenger( getMessengerName() );
 66  
         }
 67  0
         return messenger;
 68  
     }
 69  
     
 70  
     /** Sets the Messenger to be used */
 71  
     public void setMessenger(Messenger messenger) {
 72  0
         this.messenger = messenger;
 73  0
     }
 74  
     
 75  
     /** Getter for property messengerName.
 76  
      * @return Value of property messengerName.
 77  
      */
 78  
     public String getMessengerName() {
 79  0
         return messengerName;
 80  
     }
 81  
 
 82  
     /** Setter for property messengerName.
 83  
      * @param messengerName New value of property messengerName.
 84  
      */
 85  
     public void setMessengerName(String messengerName) {
 86  0
         this.messengerName = messengerName;
 87  0
     }
 88  
 
 89  
     /** Getter for property destination.
 90  
      * @return Value of property destination.
 91  
      */
 92  
     public Destination getDestination() throws JMSException {
 93  0
         if ( destination == null ) {
 94  0
             destination = getMessenger().getDestination( getSubject() );
 95  
         }
 96  0
         return destination;
 97  
     }
 98  
 
 99  
     /** Setter for property destination.
 100  
      * @param destination New value of property destination.
 101  
      */
 102  
     public void setDestination(Destination destination) {
 103  0
         this.destination = destination;
 104  0
     }
 105  
 
 106  
     /** Getter for property subject.
 107  
      * @return Value of property subject.
 108  
      */
 109  
     public String getSubject() {
 110  0
         return subject;
 111  
     }
 112  
 
 113  
     /** Setter for property subject.
 114  
      * @param subject New value of property subject.
 115  
      */
 116  
     public void setSubject(String subject) {
 117  0
         this.subject = subject;
 118  0
     }
 119  
     
 120  
     
 121  
     /** Getter for property messengerManager.
 122  
      * @return Value of property messengerManager.
 123  
      */
 124  
     public MessengerManager getMessengerManager() {
 125  0
         return messengerManager;
 126  
     }
 127  
     
 128  
     /** Setter for property messengerManager.
 129  
      * @param messengerManager New value of property messengerManager.
 130  
      */
 131  
     public void setMessengerManager(MessengerManager messengerManager) {
 132  0
         this.messengerManager = messengerManager;
 133  0
     }
 134  
 
 135  
     /** 
 136  
      * Sets the URI of the Messenger.xml configuration document to use
 137  
      * to configure the messengers to use for this task.
 138  
      */
 139  
     public void setConfiguration(String uri) throws JMSException {
 140  0
         setMessengerManager( MessengerManager.load( uri ) );
 141  0
     }
 142  
     
 143  
     /** Getter for property sleep, which defines the number of milliseconds to 
 144  
      * sleep for before each send.
 145  
      * @return Value of property sleep.
 146  
      */
 147  
     public long getSleep() {
 148  0
         return sleep;
 149  
     }
 150  
     
 151  
     /** Setter for property sleep, which defines the number of milliseconds to 
 152  
      * sleep for before each send.
 153  
      * @param sleep New value of property sleep.
 154  
      */
 155  
     public void setSleep(long sleep) {
 156  0
         this.sleep = sleep;
 157  0
     }
 158  
     
 159  
     /**
 160  
      * Returns the single file to be sent instead of a FileSet
 161  
      * @return File
 162  
      */
 163  
     public File getFile() {
 164  0
         return file;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Allows a single file to be sent via the Ant Task
 169  
      * @param file The file to set
 170  
      */
 171  
     public void setFile(File file) {
 172  0
         this.file = file;
 173  0
     }
 174  
 
 175  
     // Task interface
 176  
     //-------------------------------------------------------------------------
 177  
     
 178  
     /**
 179  
      * Performs the copy operation.
 180  
      */
 181  
     public void execute() throws BuildException {
 182  
         try {
 183  0
             if (filesets.size() == 0) {
 184  0
                 throw new BuildException("Specify at least one source fileset.", location);
 185  
             }
 186  0
             Messenger messenger = getMessenger();
 187  0
             if ( messenger == null ) {
 188  0
                 throw new BuildException("Must specify a valid Messenger", location );
 189  
             }
 190  0
             Destination destination = getDestination();
 191  0
             if ( destination == null ) {
 192  0
                 throw new BuildException("Must specify a valid JMS Destination", location );
 193  
             }
 194  
             
 195  0
             if ( sleep > 0 ) {
 196  0
                 log( "Will sleep for: " + sleep + " (ms) between message sends" );
 197  
             }
 198  
                     
 199  
             // deal with the filesets
 200  0
             boolean first = true;
 201  0
             if (file != null ) {
 202  0
                 sendFile(file, messenger, destination);
 203  
             }
 204  
             else {
 205  0
                 for (Iterator iter = filesets.iterator(); iter.hasNext(); ) {
 206  0
                     FileSet fs = (FileSet) iter.next();
 207  0
                     DirectoryScanner ds = fs.getDirectoryScanner(project);                
 208  0
                     ds.scan();
 209  
     
 210  0
                     File dir = ds.getBasedir();
 211  0
                     String[] files = ds.getIncludedFiles();
 212  
                     
 213  0
                     for (int i = 0; i < files.length; i++) {
 214  0
                         if ( first ) {
 215  0
                             first = false;
 216  
                         }
 217  
                         else {
 218  0
                             sleep();
 219  
                         }
 220  0
                         sendFile( new File( dir, files[i]), messenger, destination );
 221  
                     }
 222  0
                 }
 223  
             }
 224  
         }
 225  0
         catch (IOException e) {
 226  0
             throw new BuildException(e, location);
 227  
         }
 228  0
         catch (JMSException e) {
 229  0
             throw new BuildException(e, location);
 230  
         }
 231  
         finally {        
 232  0
             try {
 233  
                 // close the JMS connection to release any background threads        
 234  0
                 messenger.close();
 235  
             }
 236  0
             catch (Exception e) {
 237  
                 // ignore close exceptions
 238  0
             }
 239  0
         }
 240  0
     }
 241  
 
 242  
     /**
 243  
      * Sends the contents of the given file to the given Destination 
 244  
      * using the given Messenger instance
 245  
      */
 246  
     protected void sendFile(File file, Messenger messenger, Destination destination) throws IOException, JMSException {
 247  0
         log( "Sending message to: " + destination + " from file: " + file );
 248  
         
 249  0
         String text = readText(
 250  
             new BufferedReader( new FileReader( file ) )
 251  
         );
 252  
         
 253  0
         TextMessage message = messenger.createTextMessage( text );
 254  
         
 255  0
         messenger.send( destination, message );
 256  0
     }
 257  
     
 258  
     /** 
 259  
      * Reads the given text stream into a single string 
 260  
      */
 261  
     protected String readText(Reader in) throws IOException {
 262  
         // read all the text into memory
 263  0
         StringBuffer buffer = new StringBuffer();
 264  0
         BufferedReader reader = new BufferedReader( in );
 265  0
         for ( String line; (line = reader.readLine()) != null; ) {
 266  0
             buffer.append( line );
 267  0
             buffer.append( '\n' );
 268  
         }
 269  0
         reader.close();
 270  0
         return buffer.toString();
 271  
     }
 272  
     
 273  
     /**
 274  
      * Sleeps for a configurable amount of time between each message send 
 275  
      */
 276  
     protected void sleep() {
 277  0
         if ( sleep > 0 ) {
 278  
             try {
 279  0
                 Thread.sleep(sleep);
 280  
             }
 281  0
             catch (InterruptedException e) {
 282  
                 // ignore interuptions
 283  0
             }
 284  
         }
 285  0
     }
 286  
 }
 287  
 
 288