camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r564320 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file: FileEndpoint.java FileProducer.java
Date Thu, 09 Aug 2007 17:56:56 GMT
Author: jstrachan
Date: Thu Aug  9 10:56:54 2007
New Revision: 564320

URL: http://svn.apache.org/viewvc?view=rev&rev=564320
Log:
allow the file endpoint to work in replace rather than append mode

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?view=diff&rev=564320&r1=564319&r2=564320
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
Thu Aug  9 10:56:54 2007
@@ -43,6 +43,7 @@
     private boolean lock = true;
     private boolean delete;
     private boolean noop;
+    private boolean append = true;
     private String moveNamePrefix;
     private String moveNamePostfix;
     private String[] excludedNamePrefixes = {"."};
@@ -204,6 +205,20 @@
      */
     public void setNoop(boolean noop) {
         this.noop = noop;
+    }
+
+    public boolean isAppend() {
+        return append;
+    }
+
+    /**
+     * When writing do we append to the end of the file, or replace it?
+     * The default is to append
+     *
+     * @param append whether to append (or replace)
+     */
+    public void setAppend(boolean append) {
+        this.append = append;
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java?view=diff&rev=564320&r1=564319&r2=564320
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
Thu Aug  9 10:56:54 2007
@@ -16,18 +16,20 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
-import java.io.InputStream;
-import java.io.RandomAccessFile;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
-
 import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+
 /**
  * A {@link Producer} implementation for File
  * 
@@ -42,6 +44,10 @@
         this.endpoint = endpoint;
     }
 
+    public FileEndpoint getEndpoint() {
+        return (FileEndpoint) super.getEndpoint();
+    }
+
     /**
      * @param exchange
      * @see org.apache.camel.Processor#process(Exchange)
@@ -62,13 +68,24 @@
         if (LOG.isDebugEnabled()) {
             LOG.debug("Creating file: " + file);
         }
+        FileChannel fc = null;
         try {
-            FileChannel fc = new RandomAccessFile(file, "rw").getChannel();
-            fc.position(fc.size());
+            if (getEndpoint().isAppend()) {
+                fc = new RandomAccessFile(file, "rw").getChannel();
+                fc.position(fc.size());
+            }
+            else {
+                fc = new FileOutputStream(file).getChannel();
+            }
             fc.write(payload);
-            fc.close();
-        } catch (Throwable e) {
+        }
+        catch (Throwable e) {
             LOG.error("Failed to write to File: " + file, e);
+        }
+        finally {
+            if (fc != null) {
+                fc.close();
+            }
         }
     }
 



Mime
View raw message