Author: davsclaus
Date: Mon Dec 29 07:21:01 2008
New Revision: 729916

URL: http://svn.apache.org/viewvc?rev=729916&view=rev
Log:
CAMEL-1198: Introduced AntPathMatcherFileFilter for file filtering using ant 
paths. Requires camel-spring as it uses Spring AntPathMathcer.

Added:
    
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/
    
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/AntPathMatcherFileFilter.java
   (with props)
    
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html
   (with props)
    
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest.java
   (contents, props changed)
      - copied, changed from r729464, 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileRouteTest.java
    
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml
   (contents, props changed)
      - copied, changed from r729464, 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileRouteTest-context.xml

Added: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/AntPathMatcherFileFilter.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/AntPathMatcherFileFilter.java?rev=729916&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/AntPathMatcherFileFilter.java
 (added)
+++ 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/AntPathMatcherFileFilter.java
 Mon Dec 29 07:21:01 2008
@@ -0,0 +1,107 @@
+/**
+ * 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;
+
+import java.io.File;
+import java.io.FileFilter;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.util.AntPathMatcher;
+import org.springframework.util.StringUtils;
+
+/**
+ * File filter using Spring's {...@link AntPathMatcher}.
+ * <p/>
+ * Exclude take precedence over includes. If a file match both exclude and 
include it will be regarded as excluded.
+ */
+public class AntPathMatcherFileFilter implements FileFilter {
+    private static final transient Log LOG = 
LogFactory.getLog(AntPathMatcherFileFilter.class);
+
+    private AntPathMatcher matcher = new AntPathMatcher();
+    private String[] excludes;
+    private String[] includes;
+
+    public boolean accept(File pathname) {
+        String path = pathname.getPath();
+        // must use single / as path seperators
+        path = StringUtils.replace(path, File.separator, "/");
+
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Filtering file: " + path);
+        }
+
+        // excludes take precedence
+        if (excludes != null) {
+            for (String exclude : excludes) {
+                if (matcher.match(exclude, path)) {
+                    // something to exclude so we cant accept it
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("File is excluded: " + path);
+                    }
+                    return false;
+                }
+            }
+        }
+
+        if (includes != null) {
+            for (String include : includes) {
+                if (matcher.match(include, path)) {
+                    // something to include so we accept it
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("File is included: " + path);
+                    }
+                    return true;
+                }
+            }
+        }
+
+        // nothing to include so we cant accept it
+        return false;
+    }
+
+    public String[] getExcludes() {
+        return excludes;
+    }
+
+    public void setExcludes(String[] excludes) {
+        this.excludes = excludes;
+    }
+
+    public String[] getIncludes() {
+        return includes;
+    }
+
+    public void setIncludes(String[] includes) {
+        this.includes = includes;
+    }
+
+    /**
+     * Sets excludes using a single string where each element can be separated 
with comma
+     */
+    public void setExcludes(String excludes) {
+        setExcludes(excludes.split(","));
+    }
+
+    /**
+     * Sets includes using a single string where each element can be separated 
with comma
+     */
+    public void setIncludes(String includes) {
+        setIncludes(includes.split(","));
+    }
+
+}

Propchange: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/AntPathMatcherFileFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/AntPathMatcherFileFilter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html?rev=729916&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html
 (added)
+++ 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html
 Mon Dec 29 07:21:01 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>
+
+The <a href="http://activemq.apache.org/camel/file.html";>File Component</a> 
for working with file systems.
+
+</body>
+</html>

Propchange: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/file/package.html
------------------------------------------------------------------------------
    svn:mime-type = text/html

Copied: 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest.java
 (from r729464, 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileRouteTest.java)
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest.java?p2=activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest.java&p1=activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileRouteTest.java&r1=729464&r2=729916&rev=729916&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileRouteTest.java
 (original)
+++ 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest.java
 Mon Dec 29 07:21:01 2008
@@ -16,14 +16,11 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
-
 import org.apache.camel.Endpoint;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.TestSupport;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.hamcrest.Assertions;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import 
org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
@@ -32,31 +29,30 @@
  * @version $Revision$
  */
 @ContextConfiguration
-public class SpringFileRouteTest extends AbstractJUnit38SpringContextTests {
-    protected String expectedBody = "Hello World!";
+public class SpringFileAntPathMatcherFileFilterTest extends 
AbstractJUnit38SpringContextTests {
+    protected String expectedBody = "Godday World";
     @Autowired
     protected ProducerTemplate template;
-    @Autowired
+    @EndpointInject(name = "myFileEndpoint")
     protected Endpoint inputFile;
     @EndpointInject(uri = "mock:result")
     protected MockEndpoint result;
 
-    public void testMocksAreValid() throws Exception {
-        // lets check that our injected endpoint is valid
-        FileEndpoint fileEndpoint = Assertions.assertInstanceOf(inputFile, 
FileEndpoint.class);
-        assertEquals("File", new File("target/test-default-inbox"), 
fileEndpoint.getFile());
-
+    public void testAntPatchMatherFilter() throws Exception {
         result.expectedBodiesReceived(expectedBody);
-        result.setResultWaitTime(5000);
 
-        template.sendBodyAndHeader(inputFile, expectedBody, 
FileComponent.HEADER_FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader(inputFile, "Hello World", 
FileComponent.HEADER_FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader(inputFile, "Bye World", 
FileComponent.HEADER_FILE_NAME, "bye.xml");
+        template.sendBodyAndHeader(inputFile, expectedBody, 
FileComponent.HEADER_FILE_NAME, "subfolder/foo/godday.txt");
+        template.sendBodyAndHeader(inputFile, "Bad world", 
FileComponent.HEADER_FILE_NAME, "subfolder/badday.txt");
+        template.sendBodyAndHeader(inputFile, "Day world", 
FileComponent.HEADER_FILE_NAME, "day.xml");
 
         result.assertIsSatisfied();
     }
 
     @Override
     protected void setUp() throws Exception {
-        TestSupport.deleteDirectory("target/test-default-inbox");
+        TestSupport.deleteDirectory("target/antpathmatcher");
         super.setUp();
     }
-}
+}
\ No newline at end of file

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

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

Copied: 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml
 (from r729464, 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileRouteTest-context.xml)
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml?p2=activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml&p1=activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileRouteTest-context.xml&r1=729464&r2=729916&rev=729916&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileRouteTest-context.xml
 (original)
+++ 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml
 Mon Dec 29 07:21:01 2008
@@ -22,19 +22,26 @@
        http://activemq.apache.org/camel/schema/spring 
http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
     ">
 
-  <!-- START SNIPPET: example -->
-  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring";>
-    <template id="camelTemplate"/>
-    
-    <route>
-      <from ref="inputFile"/>
-        <to uri="mock:result"/>
-    </route>
-  </camelContext>
+    <!-- START SNIPPET: example -->
+    <camelContext xmlns="http://activemq.apache.org/camel/schema/spring";>
+        <template id="camelTemplate"/>
 
-  <bean id="inputFile" class="org.apache.camel.component.file.FileEndpoint">
-    <property name="file" value="target/test-default-inbox"/>
-  </bean>
-  <!-- END SNIPPET: example -->
+        <!-- use myFilter as filter to allow setting ANT paths for which files 
to scan for -->
+        <endpoint id="myFileEndpoint" 
uri="file://target/antpathmatcher?consumer.recursive=true&amp;filter=#myAntFilter"/>
+
+        <route>
+            <from ref="myFileEndpoint"/>
+            <to uri="mock:result"/>
+        </route>
+    </camelContext>
+
+    <!-- we use the AntPathMatcherFileFilter to use ant paths for includes and 
exlucde -->
+    <bean id="myAntFilter" 
class="org.apache.camel.component.file.AntPathMatcherFileFilter">
+        <!-- include and file in the subfolder that has day in the name -->
+        <property name="includes" value="**/subfolder/**/*day*"/>
+        <!-- exclude all files with bad in name or .xml files. Use comma to 
seperate multiple excludes -->
+        <property name="excludes" value="**/*bad*,**/*.xml"/>
+    </bean>
+    <!-- END SNIPPET: example -->
 
 </beans>

Propchange: 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/file/SpringFileAntPathMatcherFileFilterTest-context.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml


Reply via email to