camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r831222 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ test/java/org/apache/camel/component/file/
Date Fri, 30 Oct 2009 08:00:03 GMT
Author: davsclaus
Date: Fri Oct 30 08:00:03 2009
New Revision: 831222

URL: http://svn.apache.org/viewvc?rev=831222&view=rev
Log:
CAMEL-2121: Added tempFileName as option to file component for fine grained control when writing
using a temporary filename as opposed to using tempPrefix option.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java
      - copied, changed from r831206, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=831222&r1=831221&r2=831222&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
Fri Oct 30 08:00:03 2009
@@ -37,7 +37,6 @@
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
-import org.apache.camel.util.UuidGenerator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -65,6 +64,7 @@
     protected boolean flatten;
     protected int maxMessagesPerPoll;
     protected String tempPrefix;
+    protected Expression tempFileName;
     protected String include;
     protected String exclude;
     protected Expression fileName;
@@ -329,6 +329,19 @@
      */
     public void setTempPrefix(String tempPrefix) {
         this.tempPrefix = tempPrefix;
+        setTempFileName(tempPrefix + "${file:name}");
+    }
+
+    public Expression getTempFileName() {
+        return tempFileName;
+    }
+
+    public void setTempFileName(Expression tempFileName) {
+        this.tempFileName = tempFileName;
+    }
+
+    public void setTempFileName(String tempFileNameExpression) {
+        this.tempFileName = createFileLangugeExpression(tempFileNameExpression);
     }
 
     public GenericFileConfiguration getConfiguration() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java?rev=831222&r1=831221&r2=831222&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
Fri Oct 30 08:00:03 2009
@@ -75,11 +75,15 @@
             preWriteCheck();
 
             // should we write to a temporary name and then afterwards rename to real target
-            boolean writeAsTempAndRename = ObjectHelper.isNotEmpty(endpoint.getTempPrefix());
+            boolean writeAsTempAndRename = ObjectHelper.isNotEmpty(endpoint.getTempFileName());
             String tempTarget = null;
             if (writeAsTempAndRename) {
                 // compute temporary name with the temp prefix
-                tempTarget = createTempFileName(target);
+                tempTarget = createTempFileName(exchange, target);
+
+                if (log.isTraceEnabled()) {
+                    log.trace("Writing using tempNameFile: " + tempTarget);
+                }
 
                 // cater for file exists option on the real target as
                 // the file operations code will work on the temp file
@@ -248,17 +252,28 @@
         return answer;
     }
 
-    protected String createTempFileName(String fileName) {
+    protected String createTempFileName(Exchange exchange, String fileName) {
         // must normalize path to cater for Windows and other OS
         fileName = normalizePath(fileName);
 
+        String tempName;
+        if (exchange.getIn().getHeader(Exchange.FILE_NAME) == null) {
+            // its a generated filename then add it to header so we can evaluate the expression
+            exchange.getIn().setHeader(Exchange.FILE_NAME, FileUtil.stripPath(fileName));
+            tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
+            // and remove it again after evaluation
+            exchange.getIn().removeHeader(Exchange.FILE_NAME);
+        } else {
+            tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
+        }
+
         int path = fileName.lastIndexOf(getFileSeparator());
         if (path == -1) {
             // no path
-            return endpoint.getTempPrefix() + fileName;
+            return tempName;
         } else {
-            StringBuilder sb = new StringBuilder(fileName);
-            sb.insert(path + 1, endpoint.getTempPrefix());
+            StringBuilder sb = new StringBuilder(fileName.substring(0, path + 1));
+            sb.append(tempName);
             return sb.toString();
         }
     }

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java
(from r831206, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java&r1=831206&r2=831222&rev=831222&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempFileNameTest.java
Fri Oct 30 08:00:03 2009
@@ -26,27 +26,31 @@
 /**
  * Unit test for file producer option tempPrefix
  */
-public class FileProduceTempPrefixTest extends ContextTestSupport {
+public class FileProduceTempFileNameTest extends ContextTestSupport {
 
-    private String fileUrl = "file://target/tempandrename/?tempPrefix=inprogress.";
+    private String fileUrl = "file://target/tempandrename/?tempFileName=inprogress-${file:name.noext}.tmp";
 
     public void testCreateTempFileName() throws Exception {
         Endpoint endpoint = context.getEndpoint(fileUrl);
         GenericFileProducer producer = (GenericFileProducer) endpoint.createProducer();
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "claus.txt");
 
-        String tempFileName = producer.createTempFileName("target/tempandrename/claus.txt");
-        assertDirectoryEquals("target/tempandrename/inprogress.claus.txt", tempFileName);
+        String tempFileName = producer.createTempFileName(exchange, "target/tempandrename/claus.txt");
+        assertDirectoryEquals("target/tempandrename/inprogress-claus.tmp", tempFileName);
     }
 
     public void testNoPathCreateTempFileName() throws Exception {
         Endpoint endpoint = context.getEndpoint(fileUrl);
         GenericFileProducer producer = (GenericFileProducer) endpoint.createProducer();
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "claus.txt");
 
-        String tempFileName = producer.createTempFileName("claus.txt");
-        assertDirectoryEquals("inprogress.claus.txt", tempFileName);
+        String tempFileName = producer.createTempFileName(exchange, "./");
+        assertDirectoryEquals("./inprogress-claus.tmp", tempFileName);
     }
 
-    public void testTempPrefix() throws Exception {
+    public void testTempFileName() throws Exception {
         deleteDirectory("target/tempandrename");
 
         template.sendBodyAndHeader("direct:a", "Hello World", Exchange.FILE_NAME, "hello.txt");

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java?rev=831222&r1=831221&r2=831222&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceTempPrefixTest.java
Fri Oct 30 08:00:03 2009
@@ -33,17 +33,21 @@
     public void testCreateTempFileName() throws Exception {
         Endpoint endpoint = context.getEndpoint(fileUrl);
         GenericFileProducer producer = (GenericFileProducer) endpoint.createProducer();
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "claus.txt");
 
-        String tempFileName = producer.createTempFileName("target/tempandrename/claus.txt");
+        String tempFileName = producer.createTempFileName(exchange, "target/tempandrename/claus.txt");
         assertDirectoryEquals("target/tempandrename/inprogress.claus.txt", tempFileName);
     }
 
     public void testNoPathCreateTempFileName() throws Exception {
         Endpoint endpoint = context.getEndpoint(fileUrl);
         GenericFileProducer producer = (GenericFileProducer) endpoint.createProducer();
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "claus.txt");
 
-        String tempFileName = producer.createTempFileName("claus.txt");
-        assertDirectoryEquals("inprogress.claus.txt", tempFileName);
+        String tempFileName = producer.createTempFileName(exchange, "./");
+        assertDirectoryEquals("./inprogress.claus.txt", tempFileName);
     }
 
     public void testTempPrefix() throws Exception {
@@ -57,6 +61,12 @@
         assertEquals("The generated file should exists: " + file, true, file.exists());
     }
 
+    public void testTempPrefixUUIDFilename() throws Exception {
+        deleteDirectory("target/tempandrename");
+
+        template.sendBody("direct:a", "Bye World");
+    }
+
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {



Mime
View raw message