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: Producer.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger.tool;
011    
012    import java.io.BufferedReader;
013    import java.io.FileReader;
014    import java.io.IOException;
015    import java.io.InputStreamReader;
016    import java.io.Reader;
017    
018    import javax.jms.Destination;
019    import javax.jms.JMSException;
020    import javax.jms.Message;
021    import javax.jms.TextMessage;
022    
023    import org.apache.commons.messenger.Messenger;
024    import org.apache.commons.messenger.MessengerManager;
025    
026    /** <p><code>Producer</code> is a sample program that 
027      * creates messages and sends them to a given destination which
028      * could either be a queue or a topc.</p>
029      *
030      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
031      * @version $Revision: 155459 $
032      */
033    public class Producer {
034    
035        protected Messenger messenger;
036    
037        public static void main(String[] args) {
038            try {
039                Producer client = new Producer();
040                client.run( args );
041            }
042            catch (JMSException e) {
043                System.out.println( "Caught: " + e );
044                Exception linked = e.getLinkedException();
045                if ( linked != null ) {
046                    System.out.println( "Underlying exception: " + linked );
047                    linked.printStackTrace();
048                }
049                else {
050                    e.printStackTrace();
051                }
052            }
053            catch (Exception e) {
054                System.out.println( "Caught: " + e );
055                e.printStackTrace();
056            }
057        }
058        
059        public void run(String[] args) throws Exception {
060            if ( args.length < 2 ) {
061                System.out.println( "Usage: Producer messengerName destination [fileName]" );
062                System.out.println( "If no fileName is provided then the current input stream is used" );
063                return;
064            }
065            String name = args[0];
066            String subject = args[1];
067            messenger = MessengerManager.get( name );
068            if ( messenger == null ) {
069                throw new JMSException( "No such messenger called: " + name );
070            }
071            Destination destination = messenger.getDestination( subject );
072            if ( destination == null ) {
073                throw new JMSException( "Could not find destination: " + subject );
074            }
075    
076            Reader reader = ( args.length > 2 ) 
077                ? new FileReader( args[2] )
078                : new InputStreamReader( System.in );
079                
080            Message message = createMessage( reader );
081            
082            messenger.send( destination, message );
083            
084            // close the JMS connection to release any background threads        
085            messenger.close();
086        }
087        
088        /** Creates a message from the given text file */
089        protected Message createMessage(Reader in) throws Exception {
090            String text = readText(in);
091            TextMessage message = messenger.createTextMessage( text );
092            return message;
093        }
094        
095        /** Reads the given text stream into a single string */
096        protected String readText(Reader in) throws IOException {
097            // read all the text into memory
098            StringBuffer buffer = new StringBuffer();
099            BufferedReader reader = new BufferedReader( in );
100            for ( String line; (line = reader.readLine()) != null; ) {
101                buffer.append( line );
102                buffer.append( '\n' );
103            }
104            reader.close();
105            return buffer.toString();
106        }
107            
108    }
109    
110