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 2017/09/03 14:26:57 UTC

[1/4] camel git commit: CAMEL-11742: File consumer - Delete orphan lock files on startup may not match a lock file when using include/antInclude filtering

Repository: camel
Updated Branches:
  refs/heads/camel-2.19.x 68bc99394 -> 5778d99dc
  refs/heads/master 64b16bd2c -> b7481fb72


CAMEL-11742: File consumer - Delete orphan lock files on startup may not match a lock file when using include/antInclude filtering


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/439f853d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/439f853d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/439f853d

Branch: refs/heads/master
Commit: 439f853d6c52ee49cee8d382a21562743d6b3238
Parents: 64b16bd
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Sep 3 16:24:27 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Sep 3 16:24:27 2017 +0200

----------------------------------------------------------------------
 .../MarkerFileExclusiveReadLockStrategy.java    | 22 +++++-
 ...siveFilterDeleteOldLockFilesIncludeTest.java | 72 ++++++++++++++++++++
 ...leRecursiveFilterDeleteOldLockFilesTest.java |  4 ++
 3 files changed, 97 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/439f853d/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java b/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
index 9d79094..845e27d 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
@@ -167,7 +167,21 @@ public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusive
 
             // filter unwanted files and directories to avoid traveling everything
             if (filter != null || antFilter != null || excludePattern != null || includePattern != null) {
-                if (!acceptFile(file, endpointPath, filter, antFilter, excludePattern, includePattern)) {
+
+                File targetFile = file;
+
+                // if its a lock file then check if we accept its target file to know if we should delete the orphan lock file
+                if (file.getName().endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) {
+                    String target = file.getName().substring(0, file.getName().length() - FileComponent.DEFAULT_LOCK_FILE_POSTFIX.length());
+                    if (file.getParent() != null) {
+                        targetFile = new File(file.getParent(), target);
+                    } else {
+                        targetFile = new File(target);
+                    }
+                }
+
+                boolean accept = acceptFile(targetFile, endpointPath, filter, antFilter, excludePattern, includePattern);
+                if (!accept) {
                     continue;
                 }
             }
@@ -219,11 +233,17 @@ public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusive
         gf.setFileName(gf.getRelativeFilePath());
 
         if (filter != null) {
+            // a custom filter can also filter directories
             if (!filter.accept(gf)) {
                 return false;
             }
         }
 
+        // the following filters only works on files so allow any directory from this point
+        if (file.isDirectory()) {
+            return true;
+        }
+
         if (antFilter != null) {
             if (!antFilter.accept(gf)) {
                 return false;

http://git-wip-us.apache.org/repos/asf/camel/blob/439f853d/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
new file mode 100644
index 0000000..5dd7277
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
@@ -0,0 +1,72 @@
+/**
+ * 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 org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version 
+ */
+public class FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/oldlock");
+        super.setUp();
+    }
+
+    public void testDeleteOldLockOnStartup() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedBodiesReceived("Bye World", "Hi World");
+        mock.message(0).header(Exchange.FILE_NAME_ONLY).isEqualTo("bye.txt");
+        mock.message(1).header(Exchange.FILE_NAME_ONLY).isEqualTo("gooday.txt");
+        mock.expectedFileExists("target/oldlock/bar/davs.txt");
+        mock.expectedFileExists("target/oldlock/bar/davs.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+
+        template.sendBodyAndHeader("file:target/oldlock", "locked", Exchange.FILE_NAME, "hello.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock", "Bye World", Exchange.FILE_NAME, "bye.txt");
+        template.sendBodyAndHeader("file:target/oldlock/foo", "locked", Exchange.FILE_NAME, "gooday.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock/foo", "Hi World", Exchange.FILE_NAME, "gooday.txt");
+        template.sendBodyAndHeader("file:target/oldlock/bar", "locked", Exchange.FILE_NAME, "davs.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock/bar", "Davs World", Exchange.FILE_NAME, "davs.txt");
+
+        // start the route
+        context.startRoute("foo");
+
+        assertMockEndpointsSatisfied();
+
+        // the lock files should be gone
+        assertFileNotExists("target/oldlock/hello.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        assertFileNotExists("target/oldlock/foo/hegooddayllo.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/oldlock?initialDelay=0&delay=10&recursive=true&sortBy=file:name&include=.*(hello.txt|bye.txt|gooday.txt)$").routeId("foo").noAutoStartup()
+                        .convertBodyTo(String.class).to("mock:result");
+            }
+        };
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/439f853d/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
index f77fd70..76b7cd6 100644
--- a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
@@ -62,6 +62,10 @@ public class FileMarkerFileRecursiveFilterDeleteOldLockFilesTest extends Context
         context.startRoute("foo");
 
         assertMockEndpointsSatisfied();
+
+        // the lock files should be gone
+        assertFileNotExists("target/oldlock/hello.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        assertFileNotExists("target/oldlock/foo/hegooddayllo.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
     }
 
     @Override


[4/4] camel git commit: CAMEL-11743: camel-ldap - Make it possible to use ENV when configuring DirContext

Posted by da...@apache.org.
CAMEL-11743: camel-ldap - Make it possible to use ENV when configuring DirContext


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5778d99d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5778d99d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5778d99d

Branch: refs/heads/camel-2.19.x
Commit: 5778d99dc1fa3d6ed25faf83a05fa86babe7274d
Parents: aa79e7b
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Sep 3 16:25:03 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Sep 3 16:26:48 2017 +0200

----------------------------------------------------------------------
 .../src/main/docs/ldap-component.adoc           |   2 +-
 .../camel/component/ldap/LdapEndpoint.java      |   6 +-
 .../camel/component/ldap/LdapProducer.java      |  24 +++-
 .../camel/component/ldap/LdapRouteTwoTest.java  | 142 +++++++++++++++++++
 4 files changed, 169 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5778d99d/components/camel-ldap/src/main/docs/ldap-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/main/docs/ldap-component.adoc b/components/camel-ldap/src/main/docs/ldap-component.adoc
index ec7f452..51f7b91 100644
--- a/components/camel-ldap/src/main/docs/ldap-component.adoc
+++ b/components/camel-ldap/src/main/docs/ldap-component.adoc
@@ -58,7 +58,7 @@ with the following path and query parameters:
 [width="100%",cols="2,5,^1,2",options="header"]
 |=======================================================================
 | Name | Description | Default | Type
-| **dirContextName** | *Required* Name of javax.naming.directory.DirContext bean to lookup in the registry. |  | String
+| **dirContextName** | *Required* Name of either a javax.naming.directory.DirContext or java.util.Hashtable or Map bean to lookup in the registry. If the bean is either a Hashtable or Map then a new javax.naming.directory.DirContext instance is created for each use. If the bean is a javax.naming.directory.DirContext then the bean is used as given. The latter may not be possible in all situations where the javax.naming.directory.DirContext must not be shared and in those situations it can be better to use java.util.Hashtable or Map instead. |  | String
 |=======================================================================
 
 #### Query Parameters (5 parameters):

http://git-wip-us.apache.org/repos/asf/camel/blob/5778d99d/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
index 131c1b3..7ba8138 100644
--- a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
+++ b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.ldap;
 
 import java.net.URISyntaxException;
+import java.util.Map;
 import javax.naming.directory.SearchControls;
 
 import org.apache.camel.Consumer;
@@ -78,7 +79,10 @@ public class LdapEndpoint extends DefaultEndpoint {
     }
 
     /**
-     * Name of {@link javax.naming.directory.DirContext} bean to lookup in the registry.
+     * Name of either a {@link javax.naming.directory.DirContext}, or {@link java.util.Hashtable}, or {@link Map} bean to lookup in the registry.
+     * If the bean is either a Hashtable or Map then a new {@link javax.naming.directory.DirContext} instance is created for each use. If the bean
+     * is a {@link javax.naming.directory.DirContext} then the bean is used as given. The latter may not be possible in all situations where
+     * the {@link javax.naming.directory.DirContext} must not be shared, and in those situations it can be better to use {@link java.util.Hashtable} or {@link Map} instead.
      */
     public void setDirContextName(String dirContextName) {
         this.dirContextName = dirContextName;

http://git-wip-us.apache.org/repos/asf/camel/blob/5778d99d/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
index cbde266..8e1e67b 100644
--- a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
+++ b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
@@ -18,10 +18,13 @@ package org.apache.camel.component.ldap;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
 import javax.naming.ldap.Control;
@@ -30,6 +33,7 @@ import javax.naming.ldap.PagedResultsControl;
 import javax.naming.ldap.PagedResultsResponseControl;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.impl.DefaultProducer;
 
 /**
@@ -85,7 +89,7 @@ public class LdapProducer extends DefaultProducer {
         }
     }
 
-    protected DirContext getDirContext() {
+    protected DirContext getDirContext() throws NamingException {
         // Obtain our ldap context. We do this by looking up the context in our registry.
         // Note though that a new context is expected each time. Therefore if spring is
         // being used then use prototype="scope". If you do not then you might experience
@@ -93,8 +97,22 @@ public class LdapProducer extends DefaultProducer {
         // On the other hand if you have a DirContext that is able to support concurrency
         // then using the default singleton scope is entirely sufficient. Most DirContext
         // classes will require prototype scope though.
-        DirContext dirContext = (DirContext) getEndpoint().getCamelContext().getRegistry().lookupByName(remaining);
-        return dirContext;
+        // if its a Map/Hashtable then we create a new context per time
+
+        DirContext answer = null;
+        Object context = getEndpoint().getCamelContext().getRegistry().lookupByName(remaining);
+        if (context instanceof Hashtable) {
+            answer = new InitialDirContext((Hashtable<?, ?>) context);
+        } else if (context instanceof Map) {
+            Hashtable hash = new Hashtable((Map) context);
+            answer = new InitialDirContext(hash);
+        } else if (context instanceof DirContext) {
+            answer = (DirContext) context;
+        } else if (context != null) {
+            String msg = "Found bean: " + remaining + " in Registry of type: " + answer.getClass().getName() + " expected type was: " + DirContext.class.getName();
+            throw new NoSuchBeanException(msg);
+        }
+        return answer;
     }
 
     private List<SearchResult> simpleSearch(DirContext ldapContext, String searchFilter) throws NamingException {

http://git-wip-us.apache.org/repos/asf/camel/blob/5778d99d/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java b/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java
new file mode 100644
index 0000000..b262b69
--- /dev/null
+++ b/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java
@@ -0,0 +1,142 @@
+/**
+ * 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.ldap;
+
+import java.util.Collection;
+import java.util.Hashtable;
+import javax.naming.directory.SearchResult;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.SimpleRegistry;
+import org.apache.directory.api.util.Network;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.annotations.ApplyLdifFiles;
+import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
+import org.apache.directory.server.core.integ.FrameworkRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(FrameworkRunner.class)
+@CreateLdapServer(transports = {@CreateTransport(protocol = "LDAP")})
+@ApplyLdifFiles("org/apache/camel/component/ldap/LdapRouteTest.ldif")
+public class LdapRouteTwoTest extends AbstractLdapTestUnit {
+
+    private CamelContext camel;
+    private ProducerTemplate template;
+    private int port;
+
+    @Before
+    public void setup() throws Exception {
+        // you can assign port number in the @CreateTransport annotation
+        port = super.getLdapServer().getPort();
+
+        // use ENV variables
+        Hashtable<String, String> env = new Hashtable();
+        env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
+        env.put("java.naming.provider.url", Network.ldapLoopbackUrl(ldapServer.getPort()));
+        env.put("java.naming.security.principal", "uid=admin,ou=system");
+        env.put("java.naming.security.credentials", "secret");
+        env.put("java.naming.security.authentication", "simple");
+
+        SimpleRegistry reg = new SimpleRegistry();
+        reg.put("localhost:" + port, env);
+        camel = new DefaultCamelContext(reg);
+        template = camel.createProducerTemplate();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        camel.stop();
+    }
+    
+    @Test
+    public void testLdapRouteStandardTwo() throws Exception {
+        camel.addRoutes(createRouteBuilder("ldap:localhost:" + port + "?base=ou=system"));
+        camel.start();
+
+        Endpoint endpoint = camel.getEndpoint("direct:start");
+        Exchange exchange = endpoint.createExchange();
+        // then we set the LDAP filter on the in body
+        exchange.getIn().setBody("(!(ou=test1))");
+
+        // now we send the exchange to the endpoint, and receives the response from Camel
+        Exchange out = template.send(endpoint, exchange);
+        Collection<SearchResult> searchResults = defaultLdapModuleOutAssertions(out);
+
+        assertFalse(contains("uid=test1,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=test2,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=testNoOU,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=tcruise,ou=actors,ou=system", searchResults));
+
+        // call again
+        endpoint = camel.getEndpoint("direct:start");
+        exchange = endpoint.createExchange();
+        // then we set the LDAP filter on the in body
+        exchange.getIn().setBody("(!(ou=test1))");
+
+        // now we send the exchange to the endpoint, and receives the response from Camel
+        out = template.send(endpoint, exchange);
+        searchResults = defaultLdapModuleOutAssertions(out);
+
+        assertFalse(contains("uid=test1,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=test2,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=testNoOU,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=tcruise,ou=actors,ou=system", searchResults));
+    }
+
+    @SuppressWarnings("unchecked")
+    private Collection<SearchResult> defaultLdapModuleOutAssertions(Exchange out) {
+        // assertions of the response
+        assertNotNull(out);
+        assertNotNull(out.getOut());
+        Collection<SearchResult> data = out.getOut().getBody(Collection.class);
+        assertNotNull("out body could not be converted to a Collection - was: " + out.getOut().getBody(), data);
+        return data;
+    }
+
+    protected boolean contains(String dn, Collection<SearchResult> results) {
+        for (SearchResult result : results) {
+            if (result.getNameInNamespace().equals(dn)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    protected RouteBuilder createRouteBuilder(final String ldapEndpointUrl) throws Exception {
+        return new RouteBuilder() {
+            // START SNIPPET: route
+            public void configure() throws Exception {
+                from("direct:start").to(ldapEndpointUrl);
+            }
+            // END SNIPPET: route
+        };
+    }
+}


[3/4] camel git commit: CAMEL-11742: File consumer - Delete orphan lock files on startup may not match a lock file when using include/antInclude filtering

Posted by da...@apache.org.
CAMEL-11742: File consumer - Delete orphan lock files on startup may not match a lock file when using include/antInclude filtering


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/aa79e7b5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/aa79e7b5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/aa79e7b5

Branch: refs/heads/camel-2.19.x
Commit: aa79e7b5bee0531fa4216407636a281bc421faf1
Parents: 68bc993
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Sep 3 16:24:27 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Sep 3 16:26:41 2017 +0200

----------------------------------------------------------------------
 .../MarkerFileExclusiveReadLockStrategy.java    | 22 +++++-
 ...siveFilterDeleteOldLockFilesIncludeTest.java | 72 ++++++++++++++++++++
 ...leRecursiveFilterDeleteOldLockFilesTest.java |  4 ++
 3 files changed, 97 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/aa79e7b5/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java b/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
index 9d79094..845e27d 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
@@ -167,7 +167,21 @@ public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusive
 
             // filter unwanted files and directories to avoid traveling everything
             if (filter != null || antFilter != null || excludePattern != null || includePattern != null) {
-                if (!acceptFile(file, endpointPath, filter, antFilter, excludePattern, includePattern)) {
+
+                File targetFile = file;
+
+                // if its a lock file then check if we accept its target file to know if we should delete the orphan lock file
+                if (file.getName().endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) {
+                    String target = file.getName().substring(0, file.getName().length() - FileComponent.DEFAULT_LOCK_FILE_POSTFIX.length());
+                    if (file.getParent() != null) {
+                        targetFile = new File(file.getParent(), target);
+                    } else {
+                        targetFile = new File(target);
+                    }
+                }
+
+                boolean accept = acceptFile(targetFile, endpointPath, filter, antFilter, excludePattern, includePattern);
+                if (!accept) {
                     continue;
                 }
             }
@@ -219,11 +233,17 @@ public class MarkerFileExclusiveReadLockStrategy implements GenericFileExclusive
         gf.setFileName(gf.getRelativeFilePath());
 
         if (filter != null) {
+            // a custom filter can also filter directories
             if (!filter.accept(gf)) {
                 return false;
             }
         }
 
+        // the following filters only works on files so allow any directory from this point
+        if (file.isDirectory()) {
+            return true;
+        }
+
         if (antFilter != null) {
             if (!antFilter.accept(gf)) {
                 return false;

http://git-wip-us.apache.org/repos/asf/camel/blob/aa79e7b5/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
new file mode 100644
index 0000000..5dd7277
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest.java
@@ -0,0 +1,72 @@
+/**
+ * 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 org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version 
+ */
+public class FileMarkerFileRecursiveFilterDeleteOldLockFilesIncludeTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/oldlock");
+        super.setUp();
+    }
+
+    public void testDeleteOldLockOnStartup() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedBodiesReceived("Bye World", "Hi World");
+        mock.message(0).header(Exchange.FILE_NAME_ONLY).isEqualTo("bye.txt");
+        mock.message(1).header(Exchange.FILE_NAME_ONLY).isEqualTo("gooday.txt");
+        mock.expectedFileExists("target/oldlock/bar/davs.txt");
+        mock.expectedFileExists("target/oldlock/bar/davs.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+
+        template.sendBodyAndHeader("file:target/oldlock", "locked", Exchange.FILE_NAME, "hello.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock", "Bye World", Exchange.FILE_NAME, "bye.txt");
+        template.sendBodyAndHeader("file:target/oldlock/foo", "locked", Exchange.FILE_NAME, "gooday.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock/foo", "Hi World", Exchange.FILE_NAME, "gooday.txt");
+        template.sendBodyAndHeader("file:target/oldlock/bar", "locked", Exchange.FILE_NAME, "davs.txt" + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        template.sendBodyAndHeader("file:target/oldlock/bar", "Davs World", Exchange.FILE_NAME, "davs.txt");
+
+        // start the route
+        context.startRoute("foo");
+
+        assertMockEndpointsSatisfied();
+
+        // the lock files should be gone
+        assertFileNotExists("target/oldlock/hello.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        assertFileNotExists("target/oldlock/foo/hegooddayllo.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/oldlock?initialDelay=0&delay=10&recursive=true&sortBy=file:name&include=.*(hello.txt|bye.txt|gooday.txt)$").routeId("foo").noAutoStartup()
+                        .convertBodyTo(String.class).to("mock:result");
+            }
+        };
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/aa79e7b5/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
index cd20b3e..8060682 100644
--- a/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileMarkerFileRecursiveFilterDeleteOldLockFilesTest.java
@@ -61,6 +61,10 @@ public class FileMarkerFileRecursiveFilterDeleteOldLockFilesTest extends Context
         context.startRoute("foo");
 
         assertMockEndpointsSatisfied();
+
+        // the lock files should be gone
+        assertFileNotExists("target/oldlock/hello.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+        assertFileNotExists("target/oldlock/foo/hegooddayllo.txt." + FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
     }
 
     @Override


[2/4] camel git commit: CAMEL-11743: camel-ldap - Make it possible to use ENV when configuring DirContext

Posted by da...@apache.org.
CAMEL-11743: camel-ldap - Make it possible to use ENV when configuring DirContext


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b7481fb7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b7481fb7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b7481fb7

Branch: refs/heads/master
Commit: b7481fb7209a7914980c267d3751747d13f7b4af
Parents: 439f853
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Sep 3 16:25:03 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Sep 3 16:25:03 2017 +0200

----------------------------------------------------------------------
 .../src/main/docs/ldap-component.adoc           |   2 +-
 .../camel/component/ldap/LdapEndpoint.java      |   6 +-
 .../camel/component/ldap/LdapProducer.java      |  24 +++-
 .../camel/component/ldap/LdapRouteTwoTest.java  | 142 +++++++++++++++++++
 4 files changed, 169 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b7481fb7/components/camel-ldap/src/main/docs/ldap-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/main/docs/ldap-component.adoc b/components/camel-ldap/src/main/docs/ldap-component.adoc
index ec7f452..51f7b91 100644
--- a/components/camel-ldap/src/main/docs/ldap-component.adoc
+++ b/components/camel-ldap/src/main/docs/ldap-component.adoc
@@ -58,7 +58,7 @@ with the following path and query parameters:
 [width="100%",cols="2,5,^1,2",options="header"]
 |=======================================================================
 | Name | Description | Default | Type
-| **dirContextName** | *Required* Name of javax.naming.directory.DirContext bean to lookup in the registry. |  | String
+| **dirContextName** | *Required* Name of either a javax.naming.directory.DirContext or java.util.Hashtable or Map bean to lookup in the registry. If the bean is either a Hashtable or Map then a new javax.naming.directory.DirContext instance is created for each use. If the bean is a javax.naming.directory.DirContext then the bean is used as given. The latter may not be possible in all situations where the javax.naming.directory.DirContext must not be shared and in those situations it can be better to use java.util.Hashtable or Map instead. |  | String
 |=======================================================================
 
 #### Query Parameters (5 parameters):

http://git-wip-us.apache.org/repos/asf/camel/blob/b7481fb7/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
index 131c1b3..7ba8138 100644
--- a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
+++ b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.ldap;
 
 import java.net.URISyntaxException;
+import java.util.Map;
 import javax.naming.directory.SearchControls;
 
 import org.apache.camel.Consumer;
@@ -78,7 +79,10 @@ public class LdapEndpoint extends DefaultEndpoint {
     }
 
     /**
-     * Name of {@link javax.naming.directory.DirContext} bean to lookup in the registry.
+     * Name of either a {@link javax.naming.directory.DirContext}, or {@link java.util.Hashtable}, or {@link Map} bean to lookup in the registry.
+     * If the bean is either a Hashtable or Map then a new {@link javax.naming.directory.DirContext} instance is created for each use. If the bean
+     * is a {@link javax.naming.directory.DirContext} then the bean is used as given. The latter may not be possible in all situations where
+     * the {@link javax.naming.directory.DirContext} must not be shared, and in those situations it can be better to use {@link java.util.Hashtable} or {@link Map} instead.
      */
     public void setDirContextName(String dirContextName) {
         this.dirContextName = dirContextName;

http://git-wip-us.apache.org/repos/asf/camel/blob/b7481fb7/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
index cbde266..8e1e67b 100644
--- a/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
+++ b/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
@@ -18,10 +18,13 @@ package org.apache.camel.component.ldap;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
 import javax.naming.ldap.Control;
@@ -30,6 +33,7 @@ import javax.naming.ldap.PagedResultsControl;
 import javax.naming.ldap.PagedResultsResponseControl;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.impl.DefaultProducer;
 
 /**
@@ -85,7 +89,7 @@ public class LdapProducer extends DefaultProducer {
         }
     }
 
-    protected DirContext getDirContext() {
+    protected DirContext getDirContext() throws NamingException {
         // Obtain our ldap context. We do this by looking up the context in our registry.
         // Note though that a new context is expected each time. Therefore if spring is
         // being used then use prototype="scope". If you do not then you might experience
@@ -93,8 +97,22 @@ public class LdapProducer extends DefaultProducer {
         // On the other hand if you have a DirContext that is able to support concurrency
         // then using the default singleton scope is entirely sufficient. Most DirContext
         // classes will require prototype scope though.
-        DirContext dirContext = (DirContext) getEndpoint().getCamelContext().getRegistry().lookupByName(remaining);
-        return dirContext;
+        // if its a Map/Hashtable then we create a new context per time
+
+        DirContext answer = null;
+        Object context = getEndpoint().getCamelContext().getRegistry().lookupByName(remaining);
+        if (context instanceof Hashtable) {
+            answer = new InitialDirContext((Hashtable<?, ?>) context);
+        } else if (context instanceof Map) {
+            Hashtable hash = new Hashtable((Map) context);
+            answer = new InitialDirContext(hash);
+        } else if (context instanceof DirContext) {
+            answer = (DirContext) context;
+        } else if (context != null) {
+            String msg = "Found bean: " + remaining + " in Registry of type: " + answer.getClass().getName() + " expected type was: " + DirContext.class.getName();
+            throw new NoSuchBeanException(msg);
+        }
+        return answer;
     }
 
     private List<SearchResult> simpleSearch(DirContext ldapContext, String searchFilter) throws NamingException {

http://git-wip-us.apache.org/repos/asf/camel/blob/b7481fb7/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java b/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java
new file mode 100644
index 0000000..b262b69
--- /dev/null
+++ b/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTwoTest.java
@@ -0,0 +1,142 @@
+/**
+ * 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.ldap;
+
+import java.util.Collection;
+import java.util.Hashtable;
+import javax.naming.directory.SearchResult;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.SimpleRegistry;
+import org.apache.directory.api.util.Network;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.annotations.ApplyLdifFiles;
+import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
+import org.apache.directory.server.core.integ.FrameworkRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(FrameworkRunner.class)
+@CreateLdapServer(transports = {@CreateTransport(protocol = "LDAP")})
+@ApplyLdifFiles("org/apache/camel/component/ldap/LdapRouteTest.ldif")
+public class LdapRouteTwoTest extends AbstractLdapTestUnit {
+
+    private CamelContext camel;
+    private ProducerTemplate template;
+    private int port;
+
+    @Before
+    public void setup() throws Exception {
+        // you can assign port number in the @CreateTransport annotation
+        port = super.getLdapServer().getPort();
+
+        // use ENV variables
+        Hashtable<String, String> env = new Hashtable();
+        env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
+        env.put("java.naming.provider.url", Network.ldapLoopbackUrl(ldapServer.getPort()));
+        env.put("java.naming.security.principal", "uid=admin,ou=system");
+        env.put("java.naming.security.credentials", "secret");
+        env.put("java.naming.security.authentication", "simple");
+
+        SimpleRegistry reg = new SimpleRegistry();
+        reg.put("localhost:" + port, env);
+        camel = new DefaultCamelContext(reg);
+        template = camel.createProducerTemplate();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        camel.stop();
+    }
+    
+    @Test
+    public void testLdapRouteStandardTwo() throws Exception {
+        camel.addRoutes(createRouteBuilder("ldap:localhost:" + port + "?base=ou=system"));
+        camel.start();
+
+        Endpoint endpoint = camel.getEndpoint("direct:start");
+        Exchange exchange = endpoint.createExchange();
+        // then we set the LDAP filter on the in body
+        exchange.getIn().setBody("(!(ou=test1))");
+
+        // now we send the exchange to the endpoint, and receives the response from Camel
+        Exchange out = template.send(endpoint, exchange);
+        Collection<SearchResult> searchResults = defaultLdapModuleOutAssertions(out);
+
+        assertFalse(contains("uid=test1,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=test2,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=testNoOU,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=tcruise,ou=actors,ou=system", searchResults));
+
+        // call again
+        endpoint = camel.getEndpoint("direct:start");
+        exchange = endpoint.createExchange();
+        // then we set the LDAP filter on the in body
+        exchange.getIn().setBody("(!(ou=test1))");
+
+        // now we send the exchange to the endpoint, and receives the response from Camel
+        out = template.send(endpoint, exchange);
+        searchResults = defaultLdapModuleOutAssertions(out);
+
+        assertFalse(contains("uid=test1,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=test2,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=testNoOU,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=tcruise,ou=actors,ou=system", searchResults));
+    }
+
+    @SuppressWarnings("unchecked")
+    private Collection<SearchResult> defaultLdapModuleOutAssertions(Exchange out) {
+        // assertions of the response
+        assertNotNull(out);
+        assertNotNull(out.getOut());
+        Collection<SearchResult> data = out.getOut().getBody(Collection.class);
+        assertNotNull("out body could not be converted to a Collection - was: " + out.getOut().getBody(), data);
+        return data;
+    }
+
+    protected boolean contains(String dn, Collection<SearchResult> results) {
+        for (SearchResult result : results) {
+            if (result.getNameInNamespace().equals(dn)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    protected RouteBuilder createRouteBuilder(final String ldapEndpointUrl) throws Exception {
+        return new RouteBuilder() {
+            // START SNIPPET: route
+            public void configure() throws Exception {
+                from("direct:start").to(ldapEndpointUrl);
+            }
+            // END SNIPPET: route
+        };
+    }
+}