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: Caller.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messenger.tool;
11  
12  import java.io.FileReader;
13  import java.io.FileWriter;
14  import java.io.InputStreamReader;
15  import java.io.OutputStreamWriter;
16  import java.io.PrintWriter;
17  import java.io.Reader;
18  import java.io.Writer;
19  
20  import javax.jms.Destination;
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  import javax.jms.TextMessage;
24  
25  import org.apache.commons.messenger.MessengerManager;
26  
27  /** <p><code>Caller</code> is a sample program that 
28    * sends a message to a destination and waits for a response.</p>
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 155459 $
32    */
33  public class Caller extends Producer {
34  
35      public static void main(String[] args) {
36          try {
37              Caller client = new Caller();
38              client.run( args );
39          }
40          catch (JMSException e) {
41              System.out.println( "Caught: " + e );
42              Exception linked = e.getLinkedException();
43              if ( linked != null ) {
44                  System.out.println( "Underlying exception: " + linked );
45                  linked.printStackTrace();
46              }
47              else {
48                  e.printStackTrace();
49              }
50          }
51          catch (Exception e) {
52              System.out.println( "Caught: " + e );
53              e.printStackTrace();
54          }
55      }
56      
57      public void run(String[] args) throws Exception {
58          if ( args.length < 2 ) {
59              System.out.println( "Usage: Caller messengerName destination [inputFileName] [outputFileName]" );
60              System.out.println( "If no input file is used then System.in is used." );
61              System.out.println( "If no output file is used then System.out is used." );
62              return;
63          }
64          String name = args[0];
65          String subject = args[1];
66          messenger = MessengerManager.get( name );
67          if ( messenger == null ) {
68              throw new JMSException( "No such messenger called: " + name );
69          }
70          Destination destination = messenger.getDestination( subject );
71          if ( destination == null ) {
72              throw new JMSException( "Could not find destination: " + subject );
73          }
74  
75          Reader reader = ( args.length > 2 ) 
76              ? new FileReader( args[2] )
77              : new InputStreamReader( System.in );
78              
79          Writer writer = ( args.length > 3 ) 
80              ? new FileWriter( args[3] )
81              : new OutputStreamWriter( System.out );
82           
83          call( destination, reader, writer );
84          
85          // close the JMS connection to release any background threads        
86          messenger.close();
87      }    
88      
89      protected void call( Destination destination, Reader reader, Writer writer ) throws Exception {
90          Message request = createMessage( reader );
91          
92          Message response = messenger.call( destination, request );
93          
94          writeMessage( response, writer );
95      }
96      
97      
98      /** Writes the given message to the Writer */
99      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