Coverage Report - org.apache.commons.messenger.tool.Producer
 
Classes in this File Line Coverage Branch Coverage Complexity
Producer
0%
0/42
0%
0/12
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: Producer.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messenger.tool;
 11  
 
 12  
 import java.io.BufferedReader;
 13  
 import java.io.FileReader;
 14  
 import java.io.IOException;
 15  
 import java.io.InputStreamReader;
 16  
 import java.io.Reader;
 17  
 
 18  
 import javax.jms.Destination;
 19  
 import javax.jms.JMSException;
 20  
 import javax.jms.Message;
 21  
 import javax.jms.TextMessage;
 22  
 
 23  
 import org.apache.commons.messenger.Messenger;
 24  
 import org.apache.commons.messenger.MessengerManager;
 25  
 
 26  
 /** <p><code>Producer</code> is a sample program that 
 27  
   * creates messages and sends them to a given destination which
 28  
   * could either be a queue or a topc.</p>
 29  
   *
 30  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 31  
   * @version $Revision: 155459 $
 32  
   */
 33  0
 public class Producer {
 34  
 
 35  
     protected Messenger messenger;
 36  
 
 37  
     public static void main(String[] args) {
 38  
         try {
 39  0
             Producer client = new Producer();
 40  0
             client.run( args );
 41  
         }
 42  0
         catch (JMSException e) {
 43  0
             System.out.println( "Caught: " + e );
 44  0
             Exception linked = e.getLinkedException();
 45  0
             if ( linked != null ) {
 46  0
                 System.out.println( "Underlying exception: " + linked );
 47  0
                 linked.printStackTrace();
 48  
             }
 49  
             else {
 50  0
                 e.printStackTrace();
 51  
             }
 52  
         }
 53  0
         catch (Exception e) {
 54  0
             System.out.println( "Caught: " + e );
 55  0
             e.printStackTrace();
 56  0
         }
 57  0
     }
 58  
     
 59  
     public void run(String[] args) throws Exception {
 60  0
         if ( args.length < 2 ) {
 61  0
             System.out.println( "Usage: Producer messengerName destination [fileName]" );
 62  0
             System.out.println( "If no fileName is provided then the current input stream is used" );
 63  0
             return;
 64  
         }
 65  0
         String name = args[0];
 66  0
         String subject = args[1];
 67  0
         messenger = MessengerManager.get( name );
 68  0
         if ( messenger == null ) {
 69  0
             throw new JMSException( "No such messenger called: " + name );
 70  
         }
 71  0
         Destination destination = messenger.getDestination( subject );
 72  0
         if ( destination == null ) {
 73  0
             throw new JMSException( "Could not find destination: " + subject );
 74  
         }
 75  
 
 76  0
         Reader reader = ( args.length > 2 ) 
 77  
             ? new FileReader( args[2] )
 78  
             : new InputStreamReader( System.in );
 79  
             
 80  0
         Message message = createMessage( reader );
 81  
         
 82  0
         messenger.send( destination, message );
 83  
         
 84  
         // close the JMS connection to release any background threads        
 85  0
         messenger.close();
 86  0
     }
 87  
     
 88  
     /** Creates a message from the given text file */
 89  
     protected Message createMessage(Reader in) throws Exception {
 90  0
         String text = readText(in);
 91  0
         TextMessage message = messenger.createTextMessage( text );
 92  0
         return message;
 93  
     }
 94  
     
 95  
     /** Reads the given text stream into a single string */
 96  
     protected String readText(Reader in) throws IOException {
 97  
         // read all the text into memory
 98  0
         StringBuffer buffer = new StringBuffer();
 99  0
         BufferedReader reader = new BufferedReader( in );
 100  0
         for ( String line; (line = reader.readLine()) != null; ) {
 101  0
             buffer.append( line );
 102  0
             buffer.append( '\n' );
 103  
         }
 104  0
         reader.close();
 105  0
         return buffer.toString();
 106  
     }
 107  
         
 108  
 }
 109  
 
 110