[jboss-svn-commits] JBL Code SVN: r6298 - in labs/jbossesb/trunk/product/core/listeners: src/org/jboss/soa/esb/actions tests/src/org/jboss/soa/esb/actions

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 19 11:12:46 EDT 2006


Author: tfennelly
Date: 2006-09-19 11:12:41 -0400 (Tue, 19 Sep 2006)
New Revision: 6298

Modified:
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/ObjectToFileWriter.java
   labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/ObjectToFileWriterUnitTest.java
Log:
add support for passing byte[] Object instances

Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/ObjectToFileWriter.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/ObjectToFileWriter.java	2006-09-19 14:19:00 UTC (rev 6297)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/ObjectToFileWriter.java	2006-09-19 15:12:41 UTC (rev 6298)
@@ -23,6 +23,7 @@
 package org.jboss.soa.esb.actions;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Serializable;
@@ -42,12 +43,17 @@
  * on the return from a call to {@link java.util.UUID#randomUUID()} and the file extension value specified in
  * the "ext" property (default of "obj").
  * <p/>
+ * If the message is a byte array it's writen to the file as is, otherwise the toString method is called on the
+ * message object and the resulting String bytes are writen to the file based on the "encoding" specified in the action
+ * configuration (see below).
+ * <p/>
  * Sample Action Configuration:
  * <pre>
  * &lt;Action name="Write-To-X-File" processor="ObjectToFileWriter"&gt;
  *     &lt;property name="file" value="file:///..." /&gt; &lt;!-- File/Directory name. Supports URI or non-URI based names. --&gt;
  *     &lt;property name="append" value="true/false" /&gt; &lt;!-- Only relevant if the file is not a directory. Default of "false". --&gt;
  *     &lt;property name="ext" value="dat" /&gt; &lt;!-- Only relevant if the file is a directory. Default of "obj". --&gt;
+ *     &lt;property name="encoding" value="UTF-8" /&gt; &lt;!-- Default of "UTF-8". --&gt;
  * &lt;/Action&gt;
  * </pre>
  * 
@@ -59,12 +65,14 @@
     public static final String FILE_NAME = "file";
     public static final String FILE_APPEND = "append";
     public static final String FILE_EXT = "ext";
+    public static final String FILE_ENC = "encoding";
     private static Logger logger = Logger.getLogger(ObjectToFileWriter.class);
     
     private String actionName;
     private File file;
     private boolean append;
     private String ext;
+    private String encoding;
     
     public ObjectToFileWriter(String actionName, List<KeyValuePair> properties) throws ConfigurationException {
         String fileName = KeyValuePair.getValue(FILE_NAME, properties);
@@ -83,6 +91,7 @@
         }
         append = KeyValuePair.getBooleanValue(FILE_APPEND, properties, false);
         ext = KeyValuePair.getValue(FILE_EXT, properties, "obj");
+        encoding = KeyValuePair.getValue(FILE_ENC, properties, "UTF-8");
     }
 
     /* (non-Javadoc)
@@ -91,19 +100,25 @@
     public Object process(Object message) throws ActionProcessingException {
         synchronized(file) {
             File outputFile = getOutputFile();
+            FileOutputStream fileOutputStream = null;
             FileWriter fileWriter = null;
 
             try {
                 fileWriter = new FileWriter(outputFile, append);
+                fileOutputStream = new FileOutputStream(outputFile, append);
             } catch (IOException e) {
                 throw new ActionProcessingException("Action " + actionName + " failed.  Unable to open output file " + outputFile.getAbsolutePath());
             }
             
             try {
-                fileWriter.write(message.toString());
-                fileWriter.flush();
+                if(message instanceof byte[]) {
+                    fileOutputStream.write((byte[])message);
+                } else {
+                    fileOutputStream.write(message.toString().getBytes(encoding));
+                }
+                fileOutputStream.flush();
             } catch (IOException e) {
-                throw new ActionProcessingException("Action " + actionName + " failed.  Unable to write to output file " + outputFile.getAbsolutePath());
+                throw new ActionProcessingException("Action " + actionName + " failed.  Unable to write to output file " + outputFile.getAbsolutePath(), e);
             } finally {
                 try {
                     fileWriter.close();

Modified: labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/ObjectToFileWriterUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/ObjectToFileWriterUnitTest.java	2006-09-19 14:19:00 UTC (rev 6297)
+++ labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/ObjectToFileWriterUnitTest.java	2006-09-19 15:12:41 UTC (rev 6298)
@@ -23,6 +23,7 @@
 package org.jboss.soa.esb.actions;
 
 import java.io.File;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -39,7 +40,7 @@
  */
 public class ObjectToFileWriterUnitTest extends TestCase {
     
-    private static final String FILE_CONTENTS = "write something to file";
+    private static final String A_STRING = "write something to file";
     private File file = new File("./ObjectToFileWriter.tst");
     private List<KeyValuePair> properties;
 
@@ -57,26 +58,33 @@
         properties.add(new KeyValuePair("file", file.getPath()));
         
         // Write something fo file and check it was written..
-        writeAndCheck(FILE_CONTENTS);
+        writeAndCheck(A_STRING, A_STRING);
         // And do it all again to make sure the contents are not appended i.e. the file is overwritten...
-        writeAndCheck(FILE_CONTENTS);
+        writeAndCheck(A_STRING, A_STRING);
     }
     
+    public void test_writeWithBytes() throws ConfigurationException, ActionProcessingException, UnsupportedEncodingException {
+        properties.add(new KeyValuePair("file", file.getPath()));
+        
+        // Write bytes to the file...
+        writeAndCheck(A_STRING.getBytes("UTF-8"), A_STRING);
+    }
+    
     public void test_writeWithAppend() throws ConfigurationException, ActionProcessingException {
         properties.add(new KeyValuePair("file", file.getPath()));
         properties.add(new KeyValuePair("append", "true"));
 
         // Write something fo file and check it was written..
-        writeAndCheck(FILE_CONTENTS);
+        writeAndCheck(A_STRING, A_STRING);
         // And do it all again to make sure the contents are appended i.e. the file is not overwritten...
-        writeAndCheck(FILE_CONTENTS + FILE_CONTENTS);
+        writeAndCheck(A_STRING, A_STRING + A_STRING);
     }
 
 
-    private void writeAndCheck(String expected) throws ConfigurationException, ActionProcessingException {
+    private void writeAndCheck(Object objectToWrite, String expected) throws ConfigurationException, ActionProcessingException {
         // Use the ObjectToFileWriter to write something to file...
         ObjectToFileWriter fileWriter = new ObjectToFileWriter("ObjectToFileWriter-Test", properties);
-        fileWriter.process(FILE_CONTENTS);
+        fileWriter.process(objectToWrite);
 
         // Use the FileToByteArray processor to read the file contents back...
         FileToByteArray fileToByteArray = new FileToByteArray();




More information about the jboss-svn-commits mailing list