jstrachan    02/05/02 11:42:08

  Modified:    messenger TODO.txt build.xml
  Added:       messenger/src/java/org/apache/commons/messenger/task
                        ProducerTask.java package.html
  Log:
  Added a jmsSend Ant Task for sending files to a JMS provider via Ant using ants 
filesets. Still needs more work and testing but its almost there
  
  Revision  Changes    Path
  1.8       +1 -0      jakarta-commons-sandbox/messenger/TODO.txt
  
  Index: TODO.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/messenger/TODO.txt,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TODO.txt  26 Feb 2002 04:12:52 -0000      1.7
  +++ TODO.txt  2 May 2002 18:42:08 -0000       1.8
  @@ -1,6 +1,7 @@
   Project To Do List
   ==================
   
  +* add an option to the producer task to allow the messenger.xml to be specified
   
   * Allow the replyTo destination and Messenger to be overridden 
     in the XML deployment configuration for Messagelets and Servlets.
  
  
  
  1.26      +15 -1     jakarta-commons-sandbox/messenger/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/messenger/build.xml,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- build.xml 26 Feb 2002 04:20:57 -0000      1.25
  +++ build.xml 2 May 2002 18:42:08 -0000       1.26
  @@ -3,7 +3,7 @@
   
   <!--
           "messenger" component of the Jakarta Commons Subproject
  -        $Id: build.xml,v 1.25 2002/02/26 04:20:57 jstrachan Exp $
  +        $Id: build.xml,v 1.26 2002/05/02 18:42:08 jstrachan Exp $
   -->
   
   
  @@ -467,6 +467,20 @@
   
         <sysproperty key="org.apache.commons.messenger" value="${messenger.xml}"/>
       </java>
  +  </target>
  +  
  +  <target name="demo.send" depends="compile.tests"
  +    description="Sends a number of messages to a JMS destination using the jmsSend 
task">
  +
  +    <taskdef
  +      name="jmsSend"
  +      classname="org.apache.commons.messenger.task.ProducerTask">
  +      <classpath refid="jms.classpath"/>
  +    </taskdef>
  +
  +    <jmsSend messengerName="queue" subject="jms/Queue">
  +      <fileset dir="src/conf" excludes="**/*.txt"/>
  +    </jmsSend> 
     </target>
     
   
  
  
  
  1.1                  
jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/task/ProducerTask.java
  
  Index: ProducerTask.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: ProducerTask.java,v 1.1 2002/05/02 18:42:08 jstrachan Exp $
   */
  package org.apache.commons.messenger.task;
  
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.Reader;
  import java.util.Iterator;
  import java.util.Vector;
  
  import javax.jms.Destination;
  import javax.jms.JMSException;
  import javax.jms.Message;
  import javax.jms.TextMessage;
  
  import org.apache.commons.messenger.Messenger;
  import org.apache.commons.messenger.MessengerManager;
  
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.DirectoryScanner;
  import org.apache.tools.ant.types.FileSet;
  import org.apache.tools.ant.types.FilterSet;
  import org.apache.tools.ant.util.FileUtils;
  
  /** 
   * <p><code>ProducerTask</code> is an Ant task which will 
   * publish all of the given text files as a JMS Text Message
   * using a given JMS Connection (Messenger) and a Destination
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
   * @version $Revision: 1.1 $
   */
  public class ProducerTask extends Task {
  
      private Vector filesets = new Vector();
      private Messenger messenger;
      private String messengerName;
      private Destination destination;
      private String subject;
  
  
      // Properties
      //-------------------------------------------------------------------------
      
  
      /**
       * Adds a set of files (nested fileset attribute).
       */
      public void addFileset(FileSet set) {
          filesets.addElement(set);
      }
  
      public Messenger getMessenger() throws JMSException {
          if ( messenger == null ) {
              messenger = MessengerManager.get( getMessengerName() );
          }
          return messenger;
      }
      
      /** Sets the Messenger to be used */
      public void setMessenger(Messenger messenger) {
          this.messenger = messenger;
      }
      
      /** Getter for property messengerName.
       * @return Value of property messengerName.
       */
      public String getMessengerName() {
          return messengerName;
      }
  
      /** Setter for property messengerName.
       * @param messengerName New value of property messengerName.
       */
      public void setMessengerName(String messengerName) {
          this.messengerName = messengerName;
      }
  
      /** Getter for property destination.
       * @return Value of property destination.
       */
      public Destination getDestination() throws JMSException {
          if ( destination == null ) {
              destination = getMessenger().getDestination( getSubject() );
          }
          return destination;
      }
  
      /** Setter for property destination.
       * @param destination New value of property destination.
       */
      public void setDestination(Destination destination) {
          this.destination = destination;
      }
  
      /** Getter for property subject.
       * @return Value of property subject.
       */
      public String getSubject() {
          return subject;
      }
  
      /** Setter for property subject.
       * @param subject New value of property subject.
       */
      public void setSubject(String subject) {
          this.subject = subject;
      }
      
      
      // Task interface
      //-------------------------------------------------------------------------
      
      /**
       * Performs the copy operation.
       */
      public void execute() throws BuildException {
          try {
              if (filesets.size() == 0) {
                  throw new BuildException("Specify at least one source fileset.", 
location);
              }
              Messenger messenger = getMessenger();
              if ( messenger == null ) {
                  throw new BuildException("Must specify a valid Messenger", location 
);
              }
              Destination destination = getDestination();
              if ( destination == null ) {
                  throw new BuildException("Must specify a valid JMS Destination", 
location );
              }
  
          // deal with the filesets
              for (Iterator iter = filesets.iterator(); iter.hasNext(); ) {
                  FileSet fs = (FileSet) iter.next();
                  DirectoryScanner ds = fs.getDirectoryScanner(project);               
 
                  ds.scan();
  
                  String[] files = ds.getIncludedFiles();
                  for (int i = 0; i < files.length; i++) {
                      sendFile( files[i], messenger, destination );
                  }
              }
          }
          catch (IOException e) {
              throw new BuildException(e, location);
          }
          catch (JMSException e) {
              throw new BuildException(e, location);
          }
          finally {        
              try {
                  // close the JMS connection to release any background threads        

                  messenger.close();
              }
              catch (Exception e) {
                  // ignore close exceptions
              }
          }
      }
  
      /**
       * Sends the contents of the given file to the given Destination 
       * using the given Messenger instance
       */
      protected void sendFile(String file, Messenger messenger, Destination 
destination) throws IOException, JMSException {
          log( "Sending message to: " + destination + " from file: " + file );
          
          String text = readText(
              new BufferedReader( new FileReader( file ) )
          );
          
          TextMessage message = messenger.createTextMessage( text );
          
          messenger.send( destination, message );
      }
      
      /** 
       * Reads the given text stream into a single string 
       */
      protected String readText(Reader in) throws IOException {
          // read all the text into memory
          StringBuffer buffer = new StringBuffer();
          BufferedReader reader = new BufferedReader( in );
          for ( String line; (line = reader.readLine()) != null; ) {
              buffer.append( line );
              buffer.append( '\n' );
          }
          reader.close();
          return buffer.toString();
      }
  }
  
  
  
  
  
  1.1                  
jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/task/package.html
  
  Index: package.html
  ===================================================================
  <html>
  <head>
  </head>
  <body>
  
    <p>A collection of Ant tasks for working with JMS.</p>
    
  </body>
  </html>
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to