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