View Javadoc

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  public class ProducerTask extends Task {
40  
41      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          filesets.addElement(set);
61      }
62  
63      public Messenger getMessenger() throws JMSException {
64          if ( messenger == null ) {
65              messenger = getMessengerManager().getMessenger( getMessengerName() );
66          }
67          return messenger;
68      }
69      
70      /** Sets the Messenger to be used */
71      public void setMessenger(Messenger messenger) {
72          this.messenger = messenger;
73      }
74      
75      /** Getter for property messengerName.
76       * @return Value of property messengerName.
77       */
78      public String getMessengerName() {
79          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          this.messengerName = messengerName;
87      }
88  
89      /** Getter for property destination.
90       * @return Value of property destination.
91       */
92      public Destination getDestination() throws JMSException {
93          if ( destination == null ) {
94              destination = getMessenger().getDestination( getSubject() );
95          }
96          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         this.destination = destination;
104     }
105 
106     /** Getter for property subject.
107      * @return Value of property subject.
108      */
109     public String getSubject() {
110         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         this.subject = subject;
118     }
119     
120     
121     /** Getter for property messengerManager.
122      * @return Value of property messengerManager.
123      */
124     public MessengerManager getMessengerManager() {
125         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         this.messengerManager = messengerManager;
133     }
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         setMessengerManager( MessengerManager.load( uri ) );
141     }
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         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         this.sleep = sleep;
157     }
158     
159     /**
160      * Returns the single file to be sent instead of a FileSet
161      * @return File
162      */
163     public File getFile() {
164         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         this.file = file;
173     }
174 
175     // Task interface
176     //-------------------------------------------------------------------------
177     
178     /**
179      * Performs the copy operation.
180      */
181     public void execute() throws BuildException {
182         try {
183             if (filesets.size() == 0) {
184                 throw new BuildException("Specify at least one source fileset.", location);
185             }
186             Messenger messenger = getMessenger();
187             if ( messenger == null ) {
188                 throw new BuildException("Must specify a valid Messenger", location );
189             }
190             Destination destination = getDestination();
191             if ( destination == null ) {
192                 throw new BuildException("Must specify a valid JMS Destination", location );
193             }
194             
195             if ( sleep > 0 ) {
196                 log( "Will sleep for: " + sleep + " (ms) between message sends" );
197             }
198                     
199             // deal with the filesets
200             boolean first = true;
201             if (file != null ) {
202                 sendFile(file, messenger, destination);
203             }
204             else {
205                 for (Iterator iter = filesets.iterator(); iter.hasNext(); ) {
206                     FileSet fs = (FileSet) iter.next();
207                     DirectoryScanner ds = fs.getDirectoryScanner(project);                
208                     ds.scan();
209     
210                     File dir = ds.getBasedir();
211                     String[] files = ds.getIncludedFiles();
212                     
213                     for (int i = 0; i < files.length; i++) {
214                         if ( first ) {
215                             first = false;
216                         }
217                         else {
218                             sleep();
219                         }
220                         sendFile( new File( dir, files[i]), messenger, destination );
221                     }
222                 }
223             }
224         }
225         catch (IOException e) {
226             throw new BuildException(e, location);
227         }
228         catch (JMSException e) {
229             throw new BuildException(e, location);
230         }
231         finally {        
232             try {
233                 // close the JMS connection to release any background threads        
234                 messenger.close();
235             }
236             catch (Exception e) {
237                 // ignore close exceptions
238             }
239         }
240     }
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         log( "Sending message to: " + destination + " from file: " + file );
248         
249         String text = readText(
250             new BufferedReader( new FileReader( file ) )
251         );
252         
253         TextMessage message = messenger.createTextMessage( text );
254         
255         messenger.send( destination, message );
256     }
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         StringBuffer buffer = new StringBuffer();
264         BufferedReader reader = new BufferedReader( in );
265         for ( String line; (line = reader.readLine()) != null; ) {
266             buffer.append( line );
267             buffer.append( '\n' );
268         }
269         reader.close();
270         return buffer.toString();
271     }
272     
273     /**
274      * Sleeps for a configurable amount of time between each message send 
275      */
276     protected void sleep() {
277         if ( sleep > 0 ) {
278             try {
279                 Thread.sleep(sleep);
280             }
281             catch (InterruptedException e) {
282                 // ignore interuptions
283             }
284         }
285     }
286 }
287 
288