Author: davsclaus
Date: Sat Sep 27 04:35:10 2008
New Revision: 699619

URL: http://svn.apache.org/viewvc?rev=699619&view=rev
Log:
CAMEL-930: File language is now using headers for file information (instead of 
relying on java.io.File) to support other file based component such as FTP.

Modified:
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
    
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java?rev=699619&r1=699618&r2=699619&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
 Sat Sep 27 04:35:10 2008
@@ -17,6 +17,8 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.io.IOException;
+import java.util.Date;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
@@ -35,6 +37,25 @@
         super(camelContext, pattern);
         setIn(new FileMessage(file));
         this.file = file;
+
+        // set additional headers with file information
+        if (file != null) {
+            getIn().setHeader("CamelFileName", file.getName());
+            getIn().setHeader("CamelFileAbsolutePath", file.getAbsolutePath());
+            getIn().setHeader("CamelFileParent", file.getParent());
+            getIn().setHeader("CamelFilePath", file.getPath());
+            try {
+                getIn().setHeader("CamelFileCanonicalPath", 
file.getCanonicalPath());
+            } catch (IOException e) {
+                // ignore
+            }
+            if (file.length() > 0) {
+                getIn().setHeader("CamelFileLength", new Long(file.length()));
+            }
+            if (file.lastModified() > 0) {
+                getIn().setHeader("CamelFileLastModified", new 
Date(file.lastModified()));
+            }
+        }
     }
 
     public FileExchange(DefaultExchange parent, File file) {

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java?rev=699619&r1=699618&r2=699619&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
 Sat Sep 27 04:35:10 2008
@@ -17,35 +17,31 @@
 
 package org.apache.camel.language.simple;
 
-import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 
 import org.apache.camel.Expression;
-import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.ExpressionBuilder;
-import org.apache.camel.component.file.FileExchange;
-import org.apache.camel.language.bean.BeanLanguage;
 
 /**
  * A helper class for working with <a 
href="http://activemq.apache.org/camel/expression.html";>expressions</a> based
- * on FileExchange.
+ * on files.
+ * <p/>
+ * This expression expects the headers from the [EMAIL PROTECTED] 
FileLanguage} on the <b>IN</b> message.
+ *
+ * @see org.apache.camel.language.simple.FileLanguage
  */
 public final class FileExpressionBuilder {
 
-    // TODO: All the file stuff should just be added as
-
     private FileExpressionBuilder() {
         // Helper class
     }
 
-    public static <E extends FileExchange> Expression<E> fileNameExpression() {
+    public static <E extends Exchange> Expression<E> fileNameExpression() {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                if (exchange.getFile() == null) {
-                    return null;
-                }
-                return exchange.getFile().getName();
+                return exchange.getIn().getHeader("CamelFileName", 
String.class);
             }
 
             @Override
@@ -55,14 +51,15 @@
         };
     }
 
-    public static <E extends FileExchange> Expression<E> 
fileNameNoExtensionExpression() {
+    public static <E extends Exchange> Expression<E> 
fileNameNoExtensionExpression() {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                if (exchange.getFile() == null) {
+                String name = exchange.getIn().getHeader("CamelFileName", 
String.class);
+                if (name != null) {
+                    return name.substring(0, name.lastIndexOf('.'));
+                } else {
                     return null;
                 }
-                String name = exchange.getFile().getName();
-                return name.substring(0, name.lastIndexOf('.'));
             }
 
             @Override
@@ -72,13 +69,10 @@
         };
     }
 
-    public static <E extends FileExchange> Expression<E> 
fileParentExpression() {
+    public static <E extends Exchange> Expression<E> fileParentExpression() {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                if (exchange.getFile() == null) {
-                    return null;
-                }
-                return exchange.getFile().getParent();
+                return exchange.getIn().getHeader("CamelFileParent", 
String.class);
             }
 
             @Override
@@ -88,13 +82,10 @@
         };
     }
 
-    public static <E extends FileExchange> Expression<E> filePathExpression() {
+    public static <E extends Exchange> Expression<E> filePathExpression() {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                if (exchange.getFile() == null) {
-                    return null;
-                }
-                return exchange.getFile().getPath();
+                return exchange.getIn().getHeader("CamelFilePath", 
String.class);
             }
 
             @Override
@@ -104,33 +95,23 @@
         };
     }
 
-    public static <E extends FileExchange> Expression<E> 
fileAbsoluteExpression() {
+    public static <E extends Exchange> Expression<E> 
fileAbsolutePathExpression() {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                if (exchange.getFile() == null) {
-                    return null;
-                }
-                return exchange.getFile().getAbsolutePath();
+                return exchange.getIn().getHeader("CamelFileAbsolutePath", 
String.class);
             }
 
             @Override
             public String toString() {
-                return "file:absolute";
+                return "file:absolute.path";
             }
         };
     }
 
-    public static <E extends FileExchange> Expression<E> 
fileCanoicalPathExpression() {
+    public static <E extends Exchange> Expression<E> 
fileCanoicalPathExpression() {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                if (exchange.getFile() == null) {
-                    return null;
-                }
-                try {
-                    return exchange.getFile().getCanonicalPath();
-                } catch (IOException e) {
-                    throw new RuntimeCamelException("Could not get the 
canonical path for file: " + exchange.getFile(), e);
-                }
+                return exchange.getIn().getHeader("CamelFileCanonicalPath", 
String.class);
             }
 
             @Override
@@ -140,55 +121,54 @@
         };
     }
 
-    public static <E extends FileExchange> Expression<E> dateExpression(final 
String command, final String pattern) {
+    public static <E extends Exchange> Expression<E> fileSizeExpression() {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                if ("file".equals(command)) {
-                    if (exchange.getFile() == null) {
-                        return null;
-                    }
-                    Date date = new Date(exchange.getFile().lastModified());
-                    SimpleDateFormat df = new SimpleDateFormat(pattern);
-                    return df.format(date);
-                }
-                // must call evalute to return the nested langauge evaluate 
when evaluating
-                // stacked expressions
-                return ExpressionBuilder.dateExpression(command, 
pattern).evaluate(exchange);
+                return exchange.getIn().getHeader("CamelFileLength", 
Long.class);
             }
 
             @Override
             public String toString() {
-                return "date(" + command + ":" + pattern + ")";
+                return "file:length";
             }
         };
     }
 
-    public static <E extends FileExchange> Expression<E> 
simpleExpression(final String simple) {
+    public static <E extends Exchange> Expression<E> dateExpression(final 
String command, final String pattern) {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
+                if ("file".equals(command)) {
+                    Date date = 
exchange.getIn().getHeader("CamelFileLastModified", Date.class);
+                    if (date != null) {
+                        SimpleDateFormat df = new SimpleDateFormat(pattern);
+                        return df.format(date);
+                    } else {
+                        return null;
+                    }
+                }
                 // must call evalute to return the nested langauge evaluate 
when evaluating
                 // stacked expressions
-                return SimpleLanguage.simple(simple).evaluate(exchange);
+                return ExpressionBuilder.dateExpression(command, 
pattern).evaluate(exchange);
             }
 
             @Override
             public String toString() {
-                return "simple(" + simple + ")";
+                return "date(" + command + ":" + pattern + ")";
             }
         };
     }
 
-    public static <E extends FileExchange> Expression<E> beanExpression(final 
String bean) {
+    public static <E extends Exchange> Expression<E> simpleExpression(final 
String simple) {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
                 // must call evalute to return the nested langauge evaluate 
when evaluating
                 // stacked expressions
-                return BeanLanguage.bean(bean).evaluate(exchange);
+                return SimpleLanguage.simple(simple).evaluate(exchange);
             }
 
             @Override
             public String toString() {
-                return "bean(" + bean + ")";
+                return "simple(" + simple + ")";
             }
         };
     }

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java?rev=699619&r1=699618&r2=699619&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
 Sat Sep 27 04:35:10 2008
@@ -26,13 +26,14 @@
  *
  * Examples of supported file expressions are:
  * <ul>
- * <li>file:name to access the file name</li>
- * <li>file:name.noext to access the file name with no extension</li>
- * <li>file:parent to access the parent file name</li>
- * <li>file:path to access the file path name</li>
- * <li>file:absolute to access the absolute file name</li>
- * <li>file:canonical.path to access the canonical path name</li>
- * <li>date:&lt;command&gt;:&lt;pattern&gt; for date formatting using the 
[EMAIL PROTECTED] java.text.SimpleDateFormat} patterns.
+ * <li><tt>file:name</tt> to access the file name</li>
+ * <li><tt>file:name.noext</tt> to access the file name with no extension</li>
+ * <li><tt>file:parent</tt> to access the parent file name</li>
+ * <li><tt>file:path</tt> to access the file path name</li>
+ * <li><tt>file:absolute.path</tt> to access the absolute file path name</li>
+ * <li><tt>file:canonical.path</tt> to access the canonical path name</li>
+ * <li><tt>file:length</tt> to access the file length as a Long type</li>
+ * <li><tt>date:&lt;command&gt;:&lt;pattern&gt;</tt> for date formatting using 
the [EMAIL PROTECTED] java.text.SimpleDateFormat} patterns.
  *     Additional Supported commands are: <tt>file</tt> for the last modified 
timestamp of the file.
  *     All the commands from [EMAIL PROTECTED] SimpleLanguage} is also 
avaiable.
  * </li>
@@ -62,10 +63,12 @@
                 return FileExpressionBuilder.fileParentExpression();
             } else if (ObjectHelper.equal(remainder, "path")) {
                 return FileExpressionBuilder.filePathExpression();
-            } else if (ObjectHelper.equal(remainder, "absolute")) {
-                return FileExpressionBuilder.fileAbsoluteExpression();
+            } else if (ObjectHelper.equal(remainder, "absolute.path")) {
+                return FileExpressionBuilder.fileAbsolutePathExpression();
             } else if (ObjectHelper.equal(remainder, "canonical.path")) {
                 return FileExpressionBuilder.fileCanoicalPathExpression();
+            } else if (ObjectHelper.equal(remainder, "length")) {
+                return FileExpressionBuilder.fileSizeExpression();
             }
         }
 

Modified: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java?rev=699619&r1=699618&r2=699619&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
 Sat Sep 27 04:35:10 2008
@@ -25,7 +25,9 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.LanguageTestSupport;
+import org.apache.camel.converter.IOConverter;
 import org.apache.camel.component.file.FileExchange;
+import org.apache.camel.component.file.FileComponent;
 import org.apache.camel.impl.JndiRegistry;
 
 /**
@@ -56,8 +58,9 @@
         assertExpression("${file:name.noext}", "hello");
         assertExpression("${file:parent}", file.getParent());
         assertExpression("${file:path}", file.getPath());
-        assertExpression("${file:absolute}", file.getAbsolutePath());
+        assertExpression("${file:absolute.path}", file.getAbsolutePath());
         assertExpression("${file:canonical.path}", file.getCanonicalPath());
+        assertExpression("${file:length}", file.length());
     }
 
     public void testDate() throws Exception {
@@ -92,6 +95,10 @@
     }
 
     public Exchange createExchange() {
+        // create the file
+        template.sendBodyAndHeader("file://target/filelanguage", "Hello 
World", FileComponent.HEADER_FILE_NAME, "hello.txt");
+
+        // get the file handle
         file = new File("target/filelanguage/hello.txt");
         Exchange answer = new FileExchange(context, ExchangePattern.InOut, 
file);
 


Reply via email to