You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/11/27 21:40:45 UTC

svn commit: r721271 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ test/java/org/apache/camel/component/file/

Author: davsclaus
Date: Thu Nov 27 12:40:45 2008
New Revision: 721271

URL: http://svn.apache.org/viewvc?rev=721271&view=rev
Log:
CAMEL-828: Added tempPrefix option to FileProducer for the write as temp and rename to real file strategy

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java   (contents, props changed)
      - copied, changed from r721150, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceGeneratedFileNameTest.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

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?rev=721271&r1=721270&r2=721271&view=diff
==============================================================================
--- 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 Thu Nov 27 12:40:45 2008
@@ -25,7 +25,6 @@
 
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.Processor;
-import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.impl.ScheduledPollConsumer;
 import org.apache.camel.processor.DeadLetterChannel;
 import org.apache.camel.util.ObjectHelper;
@@ -57,7 +56,7 @@
     private boolean exclusiveReadLock = true;
 
     public FileConsumer(final FileEndpoint endpoint, Processor processor) {
-        super((DefaultEndpoint)endpoint, processor);
+        super(endpoint, processor);
         this.endpoint = endpoint;
     }
 
@@ -349,27 +348,11 @@
                 return false;
             }
         }
-        String[] prefixes = endpoint.getExcludedNamePrefixes();
-        if (prefixes != null) {
-            for (String prefix : prefixes) {
-                if (name.startsWith(prefix)) {
-                    return false;
-                }
-            }
-        }
         if (endpoint.getExcludedNamePostfix() != null) {
             if (name.endsWith(endpoint.getExcludedNamePostfix())) {
                 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?rev=721271&r1=721270&r2=721271&view=diff
==============================================================================
--- 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 Nov 27 12:40:45 2008
@@ -59,13 +59,12 @@
     private boolean append = true;
     private String moveNamePrefix;
     private String moveNamePostfix;
-    private String[] excludedNamePrefixes;
-    private String[] excludedNamePostfixes;
     private String excludedNamePrefix;
     private String excludedNamePostfix;
     private int bufferSize = 128 * 1024;
     private boolean ignoreFileNameHeader;
     private Expression expression;
+    private String tempPrefix;
 
     protected FileEndpoint(File file, String endpointUri, FileComponent component) {
         super(endpointUri, component);
@@ -217,14 +216,6 @@
         this.moveNamePrefix = moveNamePrefix;
     }
 
-    public String[] getExcludedNamePrefixes() {
-        return excludedNamePrefixes;
-    }
-
-    public String[] getExcludedNamePostfixes() {
-        return excludedNamePostfixes;
-    }
-
     public boolean isNoop() {
         return noop;
     }
@@ -304,6 +295,17 @@
         this.expression = FileLanguage.file(fileLanguageExpression);
     }
 
+    public String getTempPrefix() {
+        return tempPrefix;
+    }
+
+    /**
+     * Enables and uses temporary prefix when writing files, after write it will be renamed to the correct name.
+     */
+    public void setTempPrefix(String tempPrefix) {
+        this.tempPrefix = tempPrefix;
+    }
+
     /**
      * A strategy method to lazily create the file strategy
      */

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?rev=721271&r1=721270&r2=721271&view=diff
==============================================================================
--- 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 Nov 27 12:40:45 2008
@@ -25,10 +25,8 @@
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 
-import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
-import org.apache.camel.Message;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.language.simple.FileLanguage;
 import org.apache.camel.util.ExchangeHelper;
@@ -50,28 +48,57 @@
         this.endpoint = endpoint;
     }
 
-    /**
-     * @deprecated will be removed in Camel 2.0.
-     */
-    public Endpoint getEndpoint() {
-        return endpoint;
-    }
-
     public void process(Exchange exchange) throws Exception {
         FileExchange fileExchange = (FileExchange) endpoint.createExchange(exchange);
         process(fileExchange);
         ExchangeHelper.copyResults(exchange, fileExchange);
     }
 
-    public void process(FileExchange exchange) throws Exception {
-        boolean fileSource = exchange.getIn().getBody() instanceof File;
-        File target = createFileName(exchange.getIn());
+    protected void process(FileExchange exchange) throws Exception {
+        File target = createFileName(exchange);
+
+        // should we write to a temporary name and then afterwards rename to real target
+        boolean writeAsTempAndRename = ObjectHelper.isNotNullAndNonEmpty(endpoint.getTempPrefix());
+        File tempTarget = null;
+        if (writeAsTempAndRename) {
+            // compute temporary name reusing the camel renamer
+            tempTarget = new File(target.getParent(), endpoint.getTempPrefix() + target.getName());
+        }
+
+        // write the file
+        writeFile(exchange, tempTarget != null ? tempTarget : target);
+
+        // if we did write to a temporary name then rename it to the real name after we have written the file
+        if (tempTarget != null) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Renaming file: " + tempTarget + " to: " + target);
+            }
+            boolean renamed = tempTarget.renameTo(target);
+            if (!renamed) {
+                throw new IOException("Can not rename file from: " + tempTarget + " to: " + target);
+            }
+
+        }
+
+        // lets store the name we really used in the header, so end-users can retrieve it
+        exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME_PRODUCED, target.getAbsolutePath());
+    }
+
+    /**
+     * Writes the given exchanges to the target file.
+     *
+     * @param exchange  the current exchange
+     * @param target  the target file
+     * @throws Exception can be thrown if not possible to write
+     */
+    protected void writeFile(Exchange exchange, File target) throws Exception {
         buildDirectory(target);
 
         if (LOG.isDebugEnabled()) {
             LOG.debug("About to write to: " + target + " from exchange: " + exchange);
         }
 
+        boolean fileSource = exchange.getIn().getBody() instanceof File;
         if (fileSource) {
             File source = ExchangeHelper.getMandatoryInBody(exchange, File.class);
             writeFileByFile(source, target);
@@ -141,12 +168,18 @@
         return out;
     }
 
-    protected File createFileName(Message message) {
+    /**
+     * Creates the target filename to write.
+     *
+     * @param exchange  the current exchange
+     * @return the target file
+     */
+    protected File createFileName(Exchange exchange) {
         File answer;
 
         String name = null;
         if (!endpoint.isIgnoreFileNameHeader()) {
-            name = message.getHeader(FileComponent.HEADER_FILE_NAME, String.class);
+            name = exchange.getIn().getHeader(FileComponent.HEADER_FILE_NAME, String.class);
         }
 
         // expression support
@@ -164,8 +197,8 @@
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Filename evaluated as expression: " + expression);
             }
-            Object result = expression.evaluate(message.getExchange());
-            name = message.getExchange().getContext().getTypeConverter().convertTo(String.class, result);
+            Object result = expression.evaluate(exchange);
+            name = exchange.getContext().getTypeConverter().convertTo(String.class, result);
         }
 
         File endpointFile = endpoint.getFile();
@@ -173,10 +206,10 @@
             if (name != null) {
                 answer = new File(endpointFile, name);
                 if (answer.isDirectory()) {
-                    answer = new File(answer, endpoint.getGeneratedFileName(message));
+                    answer = new File(answer, endpoint.getGeneratedFileName(exchange.getIn()));
                 }
             } else {
-                answer = new File(endpointFile, endpoint.getGeneratedFileName(message));
+                answer = new File(endpointFile, endpoint.getGeneratedFileName(exchange.getIn()));
             }
         } else {
             if (name == null) {
@@ -186,13 +219,10 @@
             }
         }
 
-        // lets store the name we really used in the header, so end-users can retrieve it
-        message.setHeader(FileComponent.HEADER_FILE_NAME_PRODUCED, answer.getAbsolutePath());
-
         return answer;
     }
 
-    private void buildDirectory(File file) {
+    private static void buildDirectory(File file) {
         String dirName = file.getAbsolutePath();
         int index = dirName.lastIndexOf(File.separatorChar);
         if (index > 0) {

Copied: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java (from r721150, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceGeneratedFileNameTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java?p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceGeneratedFileNameTest.java&r1=721150&r2=721271&rev=721271&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceGeneratedFileNameTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java Thu Nov 27 12:40:45 2008
@@ -19,26 +19,21 @@
 import java.io.File;
 
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 
 /**
- * Unit test that FileProducer can use message id as the filename.
+ * Unit test for file producer option tempPrefix
  */
-public class FileProduceGeneratedFileNameTest extends ContextTestSupport {
+public class FileProduceTempPrefixTest extends ContextTestSupport {
 
-    public void testGeneratedFileName() throws Exception {
-        Endpoint endpoint = context.getEndpoint("direct:a");
-        FileEndpoint fileEndpoint = resolveMandatoryEndpoint("file://target", FileEndpoint.class);
+    public void testTempPrefix() throws Exception {
+        deleteDirectory("target/tempandrename");
 
-        Exchange exchange = endpoint.createExchange();
-        exchange.getIn().setBody("Hello World");
+        template.sendBodyAndHeader("direct:a", "Hello World", FileComponent.HEADER_FILE_NAME, "hello.txt");
 
-        String id = fileEndpoint.getGeneratedFileName(exchange.getIn());
-        template.send(endpoint, exchange);
+        Thread.sleep(250);
 
-        File file = new File("target/" + id);
+        File file = new File("target/tempandrename/hello.txt");
         // use absolute file to let unittest pass on all platforms
         file = file.getAbsoluteFile();
         assertEquals("The generated file should exists: " + file, true, file.exists());
@@ -47,8 +42,8 @@
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("direct:a").to("file://target");
+                from("direct:a").to("file://target/tempandrename/?tempPrefix=inprogress.");
             }
         };
     }
-}
+}
\ No newline at end of file

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

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java
------------------------------------------------------------------------------
    svn:mergeinfo =