001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: ProducerTask.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger.task;
011    
012    import java.io.BufferedReader;
013    import java.io.File;
014    import java.io.FileReader;
015    import java.io.IOException;
016    import java.io.Reader;
017    import java.util.Iterator;
018    import java.util.Vector;
019    
020    import javax.jms.Destination;
021    import javax.jms.JMSException;
022    import javax.jms.TextMessage;
023    
024    import org.apache.commons.messenger.Messenger;
025    import org.apache.commons.messenger.MessengerManager;
026    import org.apache.tools.ant.BuildException;
027    import org.apache.tools.ant.DirectoryScanner;
028    import org.apache.tools.ant.Task;
029    import org.apache.tools.ant.types.FileSet;
030    
031    /** 
032     * <p><code>ProducerTask</code> is an Ant task which will 
033     * publish all of the given text files as a JMS Text Message
034     * using a given JMS Connection (Messenger) and a Destination
035     *
036     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
037     * @version $Revision: 155459 $
038     */
039    public class ProducerTask extends Task {
040    
041        private Vector filesets = new Vector();
042        private Messenger messenger;
043        private String messengerName;
044        private Destination destination;
045        private String subject;
046        private MessengerManager messengerManager;   
047        private File file; 
048    
049        /** Holds value of property sleep. */
050        private long sleep;
051        
052        // Properties
053        //-------------------------------------------------------------------------
054        
055    
056        /**
057         * Adds a set of files (nested fileset attribute).
058         */
059        public void addFileset(FileSet set) {
060            filesets.addElement(set);
061        }
062    
063        public Messenger getMessenger() throws JMSException {
064            if ( messenger == null ) {
065                messenger = getMessengerManager().getMessenger( getMessengerName() );
066            }
067            return messenger;
068        }
069        
070        /** Sets the Messenger to be used */
071        public void setMessenger(Messenger messenger) {
072            this.messenger = messenger;
073        }
074        
075        /** Getter for property messengerName.
076         * @return Value of property messengerName.
077         */
078        public String getMessengerName() {
079            return messengerName;
080        }
081    
082        /** Setter for property messengerName.
083         * @param messengerName New value of property messengerName.
084         */
085        public void setMessengerName(String messengerName) {
086            this.messengerName = messengerName;
087        }
088    
089        /** Getter for property destination.
090         * @return Value of property destination.
091         */
092        public Destination getDestination() throws JMSException {
093            if ( destination == null ) {
094                destination = getMessenger().getDestination( getSubject() );
095            }
096            return destination;
097        }
098    
099        /** 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