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/08/09 18:51:23 UTC

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

Author: davsclaus
Date: Sat Aug  9 09:51:22 2008
New Revision: 684296

URL: http://svn.apache.org/viewvc?rev=684296&view=rev
Log:
CAMEL-94: Optimised FileProducer to handle java.io.File inputs using FileChannel directly than using io streams.

Added a missing package.html for the javadoc

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceAppendTest.java   (contents, props changed)
      - copied, changed from r684215, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java

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=684296&r1=684295&r2=684296&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 Sat Aug  9 09:51:22 2008
@@ -20,6 +20,8 @@
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.RandomAccessFile;
+import java.io.IOException;
+import java.io.FileInputStream;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 
@@ -28,7 +30,7 @@
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.UuidGenerator;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -46,8 +48,11 @@
         this.endpoint = endpoint;
     }
 
+    /**
+     * @deprecated will be removed in Camel 2.0.
+     */
     public FileEndpoint getEndpoint() {
-        return (FileEndpoint) super.getEndpoint();
+        return endpoint;
     }
 
     public void process(Exchange exchange) throws Exception {
@@ -57,23 +62,48 @@
     }
 
     public void process(FileExchange exchange) throws Exception {
-        InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
-        File file = createFileName(exchange.getIn());
-        buildDirectory(file);
+        boolean fileSource = exchange.getIn().getBody() instanceof File;
+        File target = createFileName(exchange.getIn());
+        buildDirectory(target);
 
         if (LOG.isDebugEnabled()) {
-            LOG.debug("About to write to: " + file + " from exchange: " + exchange);
+            LOG.debug("About to write to: " + target + " from exchange: " + exchange);
         }
-        FileChannel fc = null;
+
+        if (fileSource) {
+            File source = ExchangeHelper.getMandatoryInBody(exchange, File.class);
+            writeFileByFile(source, target);
+        } else {
+            InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
+            writeFileByStream(in, target);
+        }
+    }
+
+    private void writeFileByFile(File source, File target) throws IOException {
+        FileChannel in = new FileInputStream(source).getChannel();
+        FileChannel out = null;
         try {
-            if (getEndpoint().isAppend()) {
-                fc = new RandomAccessFile(file, "rw").getChannel();
-                fc.position(fc.size());
-            } else {
-                fc = new FileOutputStream(file).getChannel();
+            out = prepareOutputFileChannel(target, out);
+
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Using FileChannel to transfer from: " + in + " to: " + out);
             }
+            in.transferTo(0, in.size(), out);
+        } finally {
+            ObjectHelper.close(in, source.getName(), LOG);
+            ObjectHelper.close(out, source.getName(), LOG);
+        }
+    }
 
-            int size = getEndpoint().getBufferSize();
+    private void writeFileByStream(InputStream in, File target) throws IOException {
+        FileChannel out = null;
+        try {
+            out = prepareOutputFileChannel(target, out);
+
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Using InputStream to transfer from: " + in + " to: " + out);
+            }
+            int size = endpoint.getBufferSize();
             byte[] buffer = new byte[size];
             ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
             while (true) {
@@ -82,17 +112,31 @@
                     break;
                 } else if (count < size) {
                     byteBuffer = ByteBuffer.wrap(buffer, 0, count);
-                    fc.write(byteBuffer);
+                    out.write(byteBuffer);
                     break;
                 } else {
-                    fc.write(byteBuffer);
+                    out.write(byteBuffer);
                     byteBuffer.clear();
                 }
             }
         } finally {
-            ObjectHelper.close(in, file.getName(), LOG);
-            ObjectHelper.close(fc, file.getName(), LOG);
+            ObjectHelper.close(in, target.getName(), LOG);
+            ObjectHelper.close(out, target.getName(), LOG);
+        }
+    }
+
+    /**
+     * Creates and prepares the output file channel. Will position itself in correct position if eg. it should append
+     * or override any existing content.
+     */
+    private FileChannel prepareOutputFileChannel(File target, FileChannel out) throws IOException {
+        if (endpoint.isAppend()) {
+            out = new RandomAccessFile(target, "rw").getChannel();
+            out = out.position(out.size());
+        } else {
+            out = new FileOutputStream(target).getChannel();
         }
+        return out;
     }
 
     protected File createFileName(Message message) {

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html?rev=684296&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html Sat Aug  9 09:51:22 2008
@@ -0,0 +1,25 @@
+<!--
+    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.
+-->
+<html>
+<head>
+</head>
+<body>
+
+Camel <a href="http://activemq.apache.org/camel/header.html">Header</a> language.
+
+</body>
+</html>

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/header/package.html
------------------------------------------------------------------------------
    svn:mime-type = text/html

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java?rev=684296&r1=684295&r2=684296&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java Sat Aug  9 09:51:22 2008
@@ -24,6 +24,7 @@
  * @version $Revision$
  */
 public class FileConsumerProducerRouteTest extends ContextTestSupport {
+
     public void testFileRoute() throws Exception {
         MockEndpoint result = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
         result.expectedMessageCount(2);

Copied: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceAppendTest.java (from r684215, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceAppendTest.java?p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceAppendTest.java&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java&r1=684215&r2=684296&rev=684296&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerProducerRouteTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProduceAppendTest.java Sat Aug  9 09:51:22 2008
@@ -18,33 +18,57 @@
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.converter.IOConverter;
+
+import java.io.File;
+import java.io.OutputStream;
 
 /**
- * @version $Revision$
+ * Unit test to verify the append option
  */
-public class FileConsumerProducerRouteTest extends ContextTestSupport {
-    public void testFileRoute() throws Exception {
-        MockEndpoint result = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
-        result.expectedMessageCount(2);
-        result.setResultWaitTime(10000);
+public class FileProduceAppendTest extends ContextTestSupport {
+
+    public void testAppendText() throws Exception {
+        template.sendBody("direct:start", " World");
+
+        // give time to write to file
+        Thread.sleep(1000);
+
+        String body = IOConverter.toString(new File("target/test-file-append/hello.txt").getAbsoluteFile());
+        assertEquals("Hello World", body);
+    }
+
+    public void testAppendFile() throws Exception {
+        // create a file with some content we want to append to the existing file
+        File in = new File("target/test-file-append/world.txt").getAbsoluteFile();
+        template.sendBody("direct:start", in);
 
-        result.assertIsSatisfied();
+        // give time to write to file
+        Thread.sleep(1000);
+
+        String body = IOConverter.toString(new File("target/test-file-append/hello.txt").getAbsoluteFile());
+        assertEquals("Hello World", body);
     }
 
     @Override
     protected void setUp() throws Exception {
-        deleteDirectory("target/test-consumer-produer-inbox");
-        super.setUp(); 
+        super.setUp();
+        deleteDirectory("target/test-file-append");
+        template.sendBodyAndHeader("file://target/test-file-append", "Hello", FileComponent.HEADER_FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader("file://target/test-file-append", " World", FileComponent.HEADER_FILE_NAME, "world.txt");
+        // give time to write files
+        Thread.sleep(1000);
     }
 
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from("file:src/main/data?noop=true").to("file:target/test-consumer-produer-inbox");
-                from("file:target/test-consumer-produer-inbox").to("mock:result");
+                from("direct:start")
+                    .setHeader(FileComponent.HEADER_FILE_NAME, constant("hello.txt"))
+                    .to("file://target/test-file-append?append=true");
             }
         };
     }
+
 }
\ No newline at end of file

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

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

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