Coverage Report - org.apache.commons.messenger.tool.Caller
 
Classes in this File Line Coverage Branch Coverage Complexity
Caller
0%
0/43
0%
0/14
3.5
 
 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  0
 public class Caller extends Producer {
 34  
 
 35  
     public static void main(String[] args) {
 36  
         try {
 37  0
             Caller client = new Caller();
 38  0
             client.run( args );
 39  
         }
 40  0
         catch (JMSException e) {
 41  0
             System.out.println( "Caught: " + e );
 42  0
             Exception linked = e.getLinkedException();
 43  0
             if ( linked != null ) {
 44  0
                 System.out.println( "Underlying exception: " + linked );
 45  0
                 linked.printStackTrace();
 46  
             }
 47  
             else {
 48  0
                 e.printStackTrace();
 49  
             }
 50  
         }
 51  0
         catch (Exception e) {
 52  0
             System.out.println( "Caught: " + e );
 53  0
             e.printStackTrace();
 54  0
         }
 55  0
     }
 56  
     
 57  
     public void run(String[] args) throws Exception {
 58  0
         if ( args.length < 2 ) {
 59  0
             System.out.println( "Usage: Caller messengerName destination [inputFileName] [outputFileName]" );
 60  0
             System.out.println( "If no input file is used then System.in is used." );
 61  0
             System.out.println( "If no output file is used then System.out is used." );
 62  0
             return;
 63  
         }
 64  0
         String name = args[0];
 65  0
         String subject = args[1];
 66  0
         messenger = MessengerManager.get( name );
 67  0
         if ( messenger == null ) {
 68  0
             throw new JMSException( "No such messenger called: " + name );
 69  
         }
 70  0
         Destination destination = messenger.getDestination( subject );
 71  0
         if ( destination == null ) {
 72  0
             throw new JMSException( "Could not find destination: " + subject );
 73  
         }
 74  
 
 75  0
         Reader reader = ( args.length > 2 ) 
 76  
             ? new FileReader( args[2] )
 77  
             : new InputStreamReader( System.in );
 78  
             
 79  0
         Writer writer = ( args.length > 3 ) 
 80  
             ? new FileWriter( args[3] )
 81  
             : new OutputStreamWriter( System.out );
 82  
          
 83  0
         call( destination, reader, writer );
 84  
         
 85  
         // close the JMS connection to release any background threads        
 86  0
         messenger.close();
 87  0
     }    
 88  
     
 89  
     protected void call( Destination destination, Reader reader, Writer writer ) throws Exception {
 90  0
         Message request = createMessage( reader );
 91  
         
 92  0
         Message response = messenger.call( destination, request );
 93  
         
 94  0
         writeMessage( response, writer );
 95  0
     }
 96  
     
 97  
     
 98  
     /** Writes the given message to the Writer */
 99  
     protected void writeMessage(Message message, Writer out) throws Exception {
 100  0
         PrintWriter writer = new PrintWriter(out);
 101  0
         if ( message instanceof TextMessage ) {
 102  0
             TextMessage textMessage = (TextMessage) message;
 103  0
             writer.println( textMessage.getText() );
 104  
         }
 105  0
         writer.close();
 106  0
     }
 107  
 }
 108  
 
 109