You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/08/10 09:18:46 UTC

svn commit: r564495 - in /activemq/camel/trunk/camel-core/src: main/data/ main/java/org/apache/camel/component/file/ main/java/org/apache/camel/component/file/strategy/ main/java/org/apache/camel/converter/ test/java/org/apache/camel/component/file/

Author: jstrachan
Date: Fri Aug 10 00:18:45 2007
New Revision: 564495

URL: http://svn.apache.org/viewvc?view=rev&rev=564495
Log:
added test case and fix for CAMEL-95 - many thanks to Aaron Crickenberger!

Added:
    activemq/camel/trunk/camel-core/src/main/data/
    activemq/camel/trunk/camel-core/src/main/data/bar.xml   (with props)
    activemq/camel/trunk/camel-core/src/main/data/foo.xml   (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DefaultFileRenamer.java   (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileProcessStrategy.java   (contents, props changed)
      - copied, changed from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileStrategy.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategy.java   (contents, props changed)
      - copied, changed from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStrategy.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategySupport.java   (contents, props changed)
      - copied, changed from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenamer.java   (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileProcessStrategy.java   (contents, props changed)
      - copied, changed from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileStrategy.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileProcessStrategy.java   (contents, props changed)
      - copied, changed from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileStrategy.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java
      - copied, changed from r564133, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
    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
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRenameRouteTest.java

Added: activemq/camel/trunk/camel-core/src/main/data/bar.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/data/bar.xml?view=auto&rev=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/data/bar.xml (added)
+++ activemq/camel/trunk/camel-core/src/main/data/bar.xml Fri Aug 10 00:18:45 2007
@@ -0,0 +1 @@
+<hello>bar</hello>
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-core/src/main/data/bar.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/data/foo.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/data/foo.xml?view=auto&rev=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/data/foo.xml (added)
+++ activemq/camel/trunk/camel-core/src/main/data/foo.xml Fri Aug 10 00:18:45 2007
@@ -0,0 +1 @@
+<hello>foo</hello>
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-core/src/main/data/foo.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?view=diff&rev=564495&r1=564494&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java Fri Aug 10 00:18:45 2007
@@ -16,14 +16,14 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
-
 import org.apache.camel.Processor;
-import org.apache.camel.component.file.strategy.FileStrategy;
+import org.apache.camel.component.file.strategy.FileProcessStrategy;
 import org.apache.camel.impl.ScheduledPollConsumer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.File;
+
 /**
  * @version $Revision: 523016 $
  */
@@ -47,7 +47,8 @@
     protected void pollFileOrDirectory(File fileOrDirectory, boolean processDir) {
         if (!fileOrDirectory.isDirectory()) {
             pollFile(fileOrDirectory); // process the file
-        } else if (processDir) {
+        }
+        else if (processDir) {
             if (isValidFile(fileOrDirectory)) {
                 LOG.debug("Polling directory " + fileOrDirectory);
                 File[] files = fileOrDirectory.listFiles();
@@ -55,33 +56,48 @@
                     pollFileOrDirectory(files[i], isRecursive()); // self-recursion
                 }
             }
-        } else {
+        }
+        else {
             LOG.debug("Skipping directory " + fileOrDirectory);
         }
     }
 
     protected void pollFile(final File file) {
-        if (file.exists() && file.lastModified() > lastPollTime) {
-            if (isValidFile(file)) {
-                FileStrategy strategy = endpoint.getFileStrategy();
-                FileExchange exchange = endpoint.createExchange(file);
-
-                try {
+        if (!file.exists()) {
+            return;
+        }
+        if (isValidFile(file)) {
+            // we only care about file modified times if we are not deleting/moving files
+            if (endpoint.isNoop()) {
+                long fileModified = file.lastModified();
+                if (fileModified <= lastPollTime) {
                     if (LOG.isDebugEnabled()) {
-                        LOG.debug("About to process file:  " + file + " using exchange: " + exchange);
+                        LOG.debug("Ignoring file: " + file + " as modified time: " + fileModified + " less than last poll time: " + lastPollTime);
                     }
-                    if (strategy.begin(endpoint, exchange, file)) {
-                        getProcessor().process(exchange);
-                        strategy.commit(endpoint, exchange, file);
-                    } else {
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug(endpoint + " cannot process file: " + file);
-                        }
+                    return;
+                }
+            }
+
+            FileProcessStrategy processStrategy = endpoint.getFileStrategy();
+            FileExchange exchange = endpoint.createExchange(file);
+
+            try {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("About to process file:  " + file + " using exchange: " + exchange);
+                }
+                if (processStrategy.begin(endpoint, exchange, file)) {
+                    getProcessor().process(exchange);
+                    processStrategy.commit(endpoint, exchange, file);
+                }
+                else {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug(endpoint + " cannot process file: " + file);
                     }
-                } catch (Throwable e) {
-                    handleException(e);
                 }
             }
+            catch (Throwable e) {
+                handleException(e);
+            }
         }
     }
 
@@ -103,9 +119,19 @@
             }
         }
         String[] prefixes = endpoint.getExcludedNamePrefixes();
-        for (String prefix : prefixes) {
-            if (name.startsWith(prefix)) {
-                return false;
+        if (prefixes != null) {
+            for (String prefix : prefixes) {
+                if (name.startsWith(prefix)) {
+                    return false;
+                }
+            }
+        }
+        String[] postfixes = endpoint.getExcludedNamePostfixes();
+        if (postfixes != null) {
+            for (String postfix : postfixes) {
+                if (name.endsWith(postfix)) {
+                    return false;
+                }
             }
         }
         return true;

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=564495&r1=564494&r2=564495
==============================================================================
--- 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 Fri Aug 10 00:18:45 2007
@@ -16,19 +16,21 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
-
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
-import org.apache.camel.component.file.strategy.DeleteFileStrategy;
-import org.apache.camel.component.file.strategy.FileStrategy;
-import org.apache.camel.component.file.strategy.NoOpFileStrategy;
-import org.apache.camel.component.file.strategy.RenameFileStrategy;
+import org.apache.camel.component.file.strategy.DefaultFileRenamer;
+import org.apache.camel.component.file.strategy.DeleteFileProcessStrategy;
+import org.apache.camel.component.file.strategy.FileProcessStrategy;
+import org.apache.camel.component.file.strategy.FileProcessStrategySupport;
+import org.apache.camel.component.file.strategy.NoOpFileProcessStrategy;
+import org.apache.camel.component.file.strategy.RenameFileProcessStrategy;
 import org.apache.camel.impl.ScheduledPollEndpoint;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.File;
+
 /**
  * A <a href="http://activemq.apache.org/camel/file.html">File Endpoint</a> for
  * working with file systems
@@ -38,7 +40,7 @@
 public class FileEndpoint extends ScheduledPollEndpoint<FileExchange> {
     private static final transient Log LOG = LogFactory.getLog(FileEndpoint.class);
     private File file;
-    private FileStrategy fileStrategy;
+    private FileProcessStrategy fileProcessStrategy;
     private boolean autoCreate = true;
     private boolean lock = true;
     private boolean delete;
@@ -47,6 +49,8 @@
     private String moveNamePrefix;
     private String moveNamePostfix;
     private String[] excludedNamePrefixes = {"."};
+    private String[] excludedNamePostfixes = { FileProcessStrategySupport.DEFAULT_LOCK_FILE_POSTFIX };
+    private int bufferSize = 128 * 1024;
 
     protected FileEndpoint(File file, String endpointUri, FileComponent component) {
         super(endpointUri, component);
@@ -117,22 +121,22 @@
         this.autoCreate = autoCreate;
     }
 
-    public FileStrategy getFileStrategy() {
-        if (fileStrategy == null) {
-            fileStrategy = createFileStrategy();
-            LOG.debug("" + this + " using strategy: " + fileStrategy);
+    public FileProcessStrategy getFileStrategy() {
+        if (fileProcessStrategy == null) {
+            fileProcessStrategy = createFileStrategy();
+            LOG.debug("" + this + " using strategy: " + fileProcessStrategy);
         }
-        return fileStrategy;
+        return fileProcessStrategy;
     }
 
     /**
      * Sets the strategy to be used when the file has been processed such as
      * deleting or renaming it etc.
      * 
-     * @param fileStrategy the new stategy to use
+     * @param fileProcessStrategy the new stategy to use
      */
-    public void setFileStrategy(FileStrategy fileStrategy) {
-        this.fileStrategy = fileStrategy;
+    public void setFileStrategy(FileProcessStrategy fileProcessStrategy) {
+        this.fileProcessStrategy = fileProcessStrategy;
     }
 
     public boolean isDelete() {
@@ -160,7 +164,7 @@
      * the files from * to *.done set this value to ".done"
      * 
      * @param moveNamePostfix
-     * @see RenameFileStrategy#setNamePostfix(String)
+     * @see DefaultFileRenamer#setNamePostfix(String)
      */
     public void setMoveNamePostfix(String moveNamePostfix) {
         this.moveNamePostfix = moveNamePostfix;
@@ -175,7 +179,7 @@
      * processed files into a hidden directory called ".camel" set this value to
      * ".camel/"
      * 
-     * @see RenameFileStrategy#setNamePrefix(String)
+     * @see DefaultFileRenamer#setNamePrefix(String)
      */
     public void setMoveNamePrefix(String moveNamePrefix) {
         this.moveNamePrefix = moveNamePrefix;
@@ -193,13 +197,25 @@
         this.excludedNamePrefixes = excludedNamePrefixes;
     }
 
+    public String[] getExcludedNamePostfixes() {
+        return excludedNamePostfixes;
+    }
+
+    /**
+     * Sets the excluded file name postfixes, such as {@link FileProcessStrategySupport#DEFAULT_LOCK_FILE_POSTFIX}
+     * to ignore lock files by default.
+     */
+    public void setExcludedNamePostfixes(String[] excludedNamePostfixes) {
+        this.excludedNamePostfixes = excludedNamePostfixes;
+    }
+
     public boolean isNoop() {
         return noop;
     }
 
     /**
-     * If set to true then the default {@link FileStrategy} will be to use the
-     * {@link NoOpFileStrategy} to not move or copy processed files
+     * If set to true then the default {@link FileProcessStrategy} will be to use the
+     * {@link NoOpFileProcessStrategy} to not move or copy processed files
      * 
      * @param noop
      */
@@ -221,22 +237,33 @@
         this.append = append;
     }
 
+    public int getBufferSize() {
+        return bufferSize;
+    }
+
+    /**
+     * Sets the buffer size used to read/write files
+     */
+    public void setBufferSize(int bufferSize) {
+        this.bufferSize = bufferSize;
+    }
+
     /**
      * A strategy method to lazily create the file strategy
      */
-    protected FileStrategy createFileStrategy() {
+    protected FileProcessStrategy createFileStrategy() {
         if (isNoop()) {
-            return new NoOpFileStrategy();
+            return new NoOpFileProcessStrategy();
         } else if (moveNamePostfix != null || moveNamePrefix != null) {
             if (isDelete()) {
                 throw new IllegalArgumentException(
                                                    "You cannot set the deleteFiles property and a moveFilenamePostfix or moveFilenamePrefix");
             }
-            return new RenameFileStrategy(isLock(), moveNamePrefix, moveNamePostfix);
+            return new RenameFileProcessStrategy(isLock(), moveNamePrefix, moveNamePostfix);
         } else if (isDelete()) {
-            return new DeleteFileStrategy(isLock());
+            return new DeleteFileProcessStrategy(isLock());
         } else {
-            return new RenameFileStrategy(isLock());
+            return new RenameFileProcessStrategy(isLock());
         }
     }
 }

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=564495&r1=564494&r2=564495
==============================================================================
--- 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 Fri Aug 10 00:18:45 2007
@@ -25,6 +25,7 @@
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
@@ -32,7 +33,7 @@
 
 /**
  * A {@link Producer} implementation for File
- * 
+ *
  * @version $Revision: 523016 $
  */
 public class FileProducer extends DefaultProducer {
@@ -57,6 +58,58 @@
     }
 
     public void process(FileExchange exchange) throws Exception {
+        InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
+        File file = createFileName(exchange);
+        buildDirectory(file);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("About to write to: " + file + " from exchange: " + exchange);
+        }
+        FileChannel fc = null;
+        try {
+            if (getEndpoint().isAppend()) {
+                fc = new RandomAccessFile(file, "rw").getChannel();
+                fc.position(fc.size());
+            }
+            else {
+                fc = new FileOutputStream(file).getChannel();
+            }
+            int size = getEndpoint().getBufferSize();
+            byte[] buffer = new byte[size];
+            ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
+            while (true) {
+                int count = in.read(buffer);
+                if (count <= 0) {
+                    break;
+                }
+                else if (count < size) {
+                    byteBuffer = ByteBuffer.wrap(buffer, 0, count);
+                    fc.write(byteBuffer);
+                    break;
+                }
+                else {
+                    fc.write(byteBuffer);
+                }
+            }
+        }
+        finally {
+            if (in != null) {
+                try {
+                    in.close();
+                }
+                catch (IOException e) {
+                    LOG.warn("Failed to close input: " + e, e);
+                }
+            }
+            if (fc != null) {
+                try {
+                    fc.close();
+                }
+                catch (IOException e) {
+                    LOG.warn("Failed to close output: " + e, e);
+                }
+            }
+        }
+        /*
         ByteBuffer payload = exchange.getIn().getBody(ByteBuffer.class);
         if (payload == null) {
             InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
@@ -87,6 +140,7 @@
                 fc.close();
             }
         }
+        */
     }
 
     protected File createFileName(FileExchange exchange) {
@@ -98,13 +152,15 @@
             File answer = new File(endpointFile, name);
             if (answer.isDirectory()) {
                 return new File(answer, fileName);
-            } else {
+            }
+            else {
                 return answer;
             }
         }
         if (endpointFile != null && endpointFile.isDirectory()) {
             return new File(endpointFile, fileName);
-        } else {
+        }
+        else {
             return new File(fileName);
         }
     }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DefaultFileRenamer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DefaultFileRenamer.java?view=auto&rev=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DefaultFileRenamer.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DefaultFileRenamer.java Fri Aug 10 00:18:45 2007
@@ -0,0 +1,80 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file.strategy;
+
+import java.io.File;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class DefaultFileRenamer implements FileRenamer {
+    private String namePrefix;
+    private String namePostfix;
+
+    public DefaultFileRenamer() {
+    }
+
+    public DefaultFileRenamer(String namePrefix, String namePostfix) {
+        this.namePrefix = namePrefix;
+        this.namePostfix = namePostfix;
+    }
+
+    public File renameFile(File file) {
+        File parent = file.getParentFile();
+        String name = renameFileName(file);
+        return new File(parent, name);
+    }
+
+    public String getNamePostfix() {
+        return namePostfix;
+    }
+
+    /**
+     * Sets the name postfix appended to moved files. For example
+     * to rename all the files from * to *.done set this value to ".done"
+     */
+    public void setNamePostfix(String namePostfix) {
+        this.namePostfix = namePostfix;
+    }
+
+    public String getNamePrefix() {
+        return namePrefix;
+    }
+
+    /**
+     * Sets the name prefix appended to moved files. For example
+     * to move processed files into a hidden directory called ".camel"
+     * set this value to ".camel/"
+     */
+    public void setNamePrefix(String namePrefix) {
+        this.namePrefix = namePrefix;
+    }
+
+
+    protected String renameFileName(File file) {
+        StringBuffer buffer = new StringBuffer();
+        if (namePrefix != null) {
+            buffer.append(namePrefix);
+        }
+        buffer.append(file.getName());
+        if (namePostfix != null) {
+            buffer.append(namePostfix);
+        }
+        return buffer.toString();
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DefaultFileRenamer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileProcessStrategy.java (from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileStrategy.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileProcessStrategy.java?view=diff&rev=564495&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileStrategy.java&r1=564133&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileProcessStrategy.java&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileProcessStrategy.java Fri Aug 10 00:18:45 2007
@@ -28,13 +28,13 @@
  *
  * @version $Revision: 1.1 $
  */
-public class DeleteFileStrategy extends FileStategySupport {
-    private static final transient Log LOG = LogFactory.getLog(DeleteFileStrategy.class);
+public class DeleteFileProcessStrategy extends FileProcessStrategySupport {
+    private static final transient Log LOG = LogFactory.getLog(DeleteFileProcessStrategy.class);
 
-    public DeleteFileStrategy() {
+    public DeleteFileProcessStrategy() {
     }
 
-    public DeleteFileStrategy(boolean lockFile) {
+    public DeleteFileProcessStrategy(boolean lockFile) {
         super(lockFile);
     }
 

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/DeleteFileProcessStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategy.java (from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStrategy.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategy.java?view=diff&rev=564495&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStrategy.java&r1=564133&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategy.java&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategy.java Fri Aug 10 00:18:45 2007
@@ -26,7 +26,7 @@
  *
  * @version $Revision: 1.1 $
  */
-public interface FileStrategy {
+public interface FileProcessStrategy {
     /**
      * Called when work is about to begin on this file. This method may attempt to acquire some file lock before
      * returning true; returning false if the file lock could not be obtained so that the file should be ignored.

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategySupport.java (from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategySupport.java?view=diff&rev=564495&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java&r1=564133&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategySupport.java&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategySupport.java Fri Aug 10 00:18:45 2007
@@ -31,21 +31,30 @@
 /**
  * @version $Revision: 1.1 $
  */
-public abstract class FileStategySupport implements FileStrategy {
-    private static final transient Log LOG = LogFactory.getLog(FileStategySupport.class);
+public abstract class FileProcessStrategySupport implements FileProcessStrategy {
+    public static final String DEFAULT_LOCK_FILE_POSTFIX = ".cameLock";
+    
+    private static final transient Log LOG = LogFactory.getLog(FileProcessStrategySupport.class);
     private boolean lockFile;
+    private FileRenamer lockFileRenamer;
 
-    protected FileStategySupport() {
+    protected FileProcessStrategySupport() {
         this(true);
     }
 
-    protected FileStategySupport(boolean lockFile) {
+    protected FileProcessStrategySupport(boolean lockFile) {
+        this(lockFile, new DefaultFileRenamer(null, DEFAULT_LOCK_FILE_POSTFIX));
+    }
+
+    protected FileProcessStrategySupport(boolean lockFile, FileRenamer lockFileRenamer) {
         this.lockFile = lockFile;
+        this.lockFileRenamer = lockFileRenamer;
     }
 
     public boolean begin(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
         if (isLockFile()) {
-            String lockFileName = file.getAbsoluteFile() + ".lock";
+            File newFile = lockFileRenamer.renameFile(file);
+            String lockFileName = newFile.getAbsolutePath();
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Locking the file: " + file + " using the lock file name: " + lockFileName);
             }
@@ -82,5 +91,13 @@
 
     public void setLockFile(boolean lockFile) {
         this.lockFile = lockFile;
+    }
+
+    public FileRenamer getLockFileRenamer() {
+        return lockFileRenamer;
+    }
+
+    public void setLockFileRenamer(FileRenamer lockFileRenamer) {
+        this.lockFileRenamer = lockFileRenamer;
     }
 }

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileProcessStrategySupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenamer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenamer.java?view=auto&rev=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenamer.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenamer.java Fri Aug 10 00:18:45 2007
@@ -0,0 +1,28 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file.strategy;
+
+import java.io.File;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public interface FileRenamer {
+
+    File renameFile(File file);
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileRenamer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileProcessStrategy.java (from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileStrategy.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileProcessStrategy.java?view=diff&rev=564495&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileStrategy.java&r1=564133&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileProcessStrategy.java&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileProcessStrategy.java Fri Aug 10 00:18:45 2007
@@ -21,8 +21,8 @@
  *
  * @version $Revision: 1.1 $
  */
-public class NoOpFileStrategy extends FileStategySupport {
-    public NoOpFileStrategy() {
+public class NoOpFileProcessStrategy extends FileProcessStrategySupport {
+    public NoOpFileProcessStrategy() {
         super(false);
     }
 

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/NoOpFileProcessStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileProcessStrategy.java (from r564133, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileStrategy.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileProcessStrategy.java?view=diff&rev=564495&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileStrategy.java&r1=564133&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileProcessStrategy.java&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileProcessStrategy.java Fri Aug 10 00:18:45 2007
@@ -29,34 +29,29 @@
  * 
  * @version $Revision: 1.1 $
  */
-public class RenameFileStrategy extends FileStategySupport {
-    private static final transient Log LOG = LogFactory.getLog(RenameFileStrategy.class);
+public class RenameFileProcessStrategy extends FileProcessStrategySupport {
+    private static final transient Log LOG = LogFactory.getLog(RenameFileProcessStrategy.class);
+    private FileRenamer renamer;
 
-    private String namePrefix;
-    private String namePostfix;
-
-    public RenameFileStrategy() {
+    public RenameFileProcessStrategy() {
         this(true);
     }
 
-    public RenameFileStrategy(boolean lock) {
+    public RenameFileProcessStrategy(boolean lock) {
         this(lock, ".camel/", "");
     }
 
-    public RenameFileStrategy(boolean lock, String namePrefix, String namePostfix) {
+    public RenameFileProcessStrategy(boolean lock, String namePrefix, String namePostfix) {
+        this(lock, new DefaultFileRenamer(namePrefix, namePostfix));
+    }
+
+    public RenameFileProcessStrategy(boolean lock, FileRenamer renamer) {
         super(lock);
-        if (namePrefix != null) {
-            this.namePrefix = namePrefix;
-        }
-        if (namePostfix != null) {
-            this.namePostfix = namePostfix;
-        }
+        this.renamer = renamer;
     }
 
     public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
-        File parent = file.getParentFile();
-        String name = getNamePrefix() + file.getName() + getNamePostfix();
-        File newName = new File(parent, name);
+        File newName = renamer.renameFile(file);
         newName.getParentFile().mkdirs();
 
         if (LOG.isDebugEnabled()) {
@@ -69,28 +64,11 @@
         super.commit(endpoint, exchange, file);
     }
 
-    public String getNamePostfix() {
-        return namePostfix;
-    }
-
-    /**
-     * Sets the name postfix appended to moved files. For example
-     * to rename all the files from * to *.done set this value to ".done"
-     */
-    public void setNamePostfix(String namePostfix) {
-        this.namePostfix = namePostfix;
-    }
-
-    public String getNamePrefix() {
-        return namePrefix;
+    public FileRenamer getRenamer() {
+        return renamer;
     }
 
-    /**
-     * Sets the name prefix appended to moved files. For example
-     * to move processed files into a hidden directory called ".camel"
-     * set this value to ".camel/"
-     */
-    public void setNamePrefix(String namePrefix) {
-        this.namePrefix = namePrefix;
+    public void setRenamer(FileRenamer renamer) {
+        this.renamer = renamer;
     }
 }

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/RenameFileProcessStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java?view=diff&rev=564495&r1=564494&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java Fri Aug 10 00:18:45 2007
@@ -18,6 +18,10 @@
 
 import org.apache.camel.Converter;
 
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.nio.ByteBuffer;
 
@@ -51,6 +55,14 @@
         return ByteBuffer.wrap(data);
     }
     
+    @Converter
+    public static ByteBuffer toByteBuffer(File file) throws IOException {
+       byte[] buf = new byte[(int) file.length()];
+       InputStream in = new BufferedInputStream(new FileInputStream(file));
+       in.read(buf);
+       return ByteBuffer.wrap(buf);
+    }
+
     @Converter
     public static ByteBuffer toByteBuffer(String value) {
         ByteBuffer buf = ByteBuffer.allocate(value.length());

Copied: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java (from r564133, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java?view=diff&rev=564495&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java&r1=564133&p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java Fri Aug 10 00:18:45 2007
@@ -23,16 +23,11 @@
 /**
  * @version $Revision: 529902 $
  */
-public class FileRouteTest extends ContextTestSupport {
-    protected Object expectedBody = "Hello there!";
-    protected String uri = "file:target/test-default-inbox";
-
+public class FileConsumerProducerRouteTest extends ContextTestSupport {
     public void testFileRoute() throws Exception {
         MockEndpoint result = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
-        result.expectedBodiesReceived(expectedBody);
-        result.setDefaulResultWaitMillis(5000);
-
-        template.sendBodyAndHeader(uri, expectedBody, "cheese", 123);
+        result.expectedMessageCount(2);
+        result.setDefaulResultWaitMillis(10000);
 
         result.assertIsSatisfied();
     }
@@ -41,8 +36,9 @@
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from(uri).to("mock:result");
+                from("file:src/main/data?noop=true").to("file:target/test-consumer-produer-inbox");
+                from("file:target/test-consumer-produer-inbox").to("mock:result");
             }
         };
     }
-}
+}
\ No newline at end of file

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRenameRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRenameRouteTest.java?view=diff&rev=564495&r1=564494&r2=564495
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRenameRouteTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRenameRouteTest.java Fri Aug 10 00:18:45 2007
@@ -22,7 +22,7 @@
 public class FileRenameRouteTest extends FileRouteTest {
     @Override
     protected void setUp() throws Exception {
-        uri = "file:target/test-rename-inbox?moveNamePrefix=.foo/";
+        uri = "file:target/test-rename-inbox?moveNamePrefix=foo/";
         super.setUp();
     }
 }