This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new d0eefb5  Move resource discovery to ResourceHelper and allow patch 
mathing against path
d0eefb5 is described below

commit d0eefb540683fdc1e8894f72f1d79b0e65d11bfc
Author: lburgazzoli <[email protected]>
AuthorDate: Fri Jan 3 14:53:30 2020 +0100

    Move resource discovery to ResourceHelper and allow patch mathing against 
path
---
 .../engine/DefaultPackageScanResourceResolver.java | 26 +++------
 .../DefaultPackageScanResourceResolverTest.java    | 68 ++++++++++++++++++++++
 .../apache/camel/impl/engine/ar/camel-dummy.xml    | 25 ++++++++
 .../org/apache/camel/impl/engine/ar/camel-scan.xml | 25 ++++++++
 .../apache/camel/impl/engine/br/camel-dummy.xml    | 25 ++++++++
 .../org/apache/camel/impl/engine/br/camel-scan.xml | 25 ++++++++
 .../org/apache/camel/support/ResourceHelper.java   | 28 +++++++++
 7 files changed, 203 insertions(+), 19 deletions(-)

diff --git 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java
 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java
index f78c604..a42355d 100644
--- 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java
+++ 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java
@@ -27,7 +27,6 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLDecoder;
 import java.nio.file.Files;
-import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.LinkedHashSet;
@@ -35,7 +34,6 @@ import java.util.List;
 import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
-import java.util.stream.Stream;
 
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.NonManagedService;
@@ -83,23 +81,13 @@ public class DefaultPackageScanResourceResolver extends 
BasePackageScanResolver
     }
 
     protected void findInFileSystem(File dir, Set<InputStream> resources, 
String subPattern) throws Exception {
-        try (Stream<Path> path = Files.walk(dir.toPath())) {
-            path.filter(f -> {
-                if (f.toFile().isFile()) {
-                    String shortName = f.toFile().getName();
-                    boolean match = PATH_MATCHER.match(subPattern, shortName);
-                    log.debug("Found resource: {} matching pattern: {} -> {}", 
shortName, subPattern, match);
-                    return match;
-                }
-                return false;
-            }).forEach(f -> {
-                try {
-                    resources.add(new FileInputStream(f.toFile()));
-                } catch (FileNotFoundException e) {
-                    // ignore
-                }
-            });
-        }
+        ResourceHelper.findInFileSystem(dir.toPath(), subPattern).forEach(f -> 
{
+            try {
+                resources.add(Files.newInputStream(f));
+            } catch (IOException e) {
+                // ignore
+            }
+        });
     }
 
     protected void findInClasspath(String packageName, Set<InputStream> 
resources, String subPattern) {
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolverTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolverTest.java
new file mode 100644
index 0000000..dc2ebe9
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolverTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.impl.engine;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.model.ModelHelper;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.model.RoutesDefinition;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class DefaultPackageScanResourceResolverTest {
+    @Test
+    public void testFileResourcesScan() {
+        DefaultCamelContext ctx = new DefaultCamelContext(false);
+
+        assertThat(loadRouteIDs(ctx, 
"file:src/test/resources/org/apache/camel/impl/engine/**/*.xml")).containsOnly("dummy-a",
 "scan-a", "dummy-b", "scan-b");
+        assertThat(loadRouteIDs(ctx, 
"file:src/test/resources/org/apache/camel/impl/engine/a?/*.xml")).containsOnly("dummy-a",
 "scan-a");
+        assertThat(loadRouteIDs(ctx, 
"file:src/test/resources/org/apache/camel/impl/engine/b?/*.xml")).containsOnly("dummy-b",
 "scan-b");
+        assertThat(loadRouteIDs(ctx, 
"file:src/test/resources/org/apache/camel/impl/engine/c?/*.xml")).isEmpty();
+    }
+
+    private static Set<String> loadRouteIDs(CamelContext context, String path) 
{
+        return loadRouteDefinitions(context, 
path).stream().map(RouteDefinition::getId).collect(Collectors.toSet());
+    }
+
+    private static List<RouteDefinition> loadRouteDefinitions(CamelContext 
context, String path) {
+        List<RouteDefinition> answer = new ArrayList<>();
+
+        try {
+            for (InputStream is:  
context.adapt(ExtendedCamelContext.class).getPackageScanResourceResolver().findResources(path))
 {
+                RoutesDefinition routes = 
ModelHelper.loadRoutesDefinition(context, is);
+
+                if (routes != null) {
+                    answer.addAll(routes.getRoutes());
+                }
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        return answer;
+    }
+}
diff --git 
a/core/camel-core/src/test/resources/org/apache/camel/impl/engine/ar/camel-dummy.xml
 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/ar/camel-dummy.xml
new file mode 100644
index 0000000..2b59d15
--- /dev/null
+++ 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/ar/camel-dummy.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<routes id="camel" xmlns="http://camel.apache.org/schema/spring";>
+    <route id="dummy-a">
+        <from uri="direct:dummy"/>
+        <to uri="mock:dummy"/>
+    </route>
+</routes>
diff --git 
a/core/camel-core/src/test/resources/org/apache/camel/impl/engine/ar/camel-scan.xml
 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/ar/camel-scan.xml
new file mode 100644
index 0000000..8e34e79
--- /dev/null
+++ 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/ar/camel-scan.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<routes id="camel" xmlns="http://camel.apache.org/schema/spring";>
+    <route id="scan-a">
+        <from uri="direct:scan"/>
+        <to uri="mock:scan"/>
+    </route>
+</routes>
diff --git 
a/core/camel-core/src/test/resources/org/apache/camel/impl/engine/br/camel-dummy.xml
 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/br/camel-dummy.xml
new file mode 100644
index 0000000..b84a69b
--- /dev/null
+++ 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/br/camel-dummy.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<routes id="camel" xmlns="http://camel.apache.org/schema/spring";>
+    <route id="dummy-b">
+        <from uri="direct:dummy"/>
+        <to uri="mock:dummy"/>
+    </route>
+</routes>
diff --git 
a/core/camel-core/src/test/resources/org/apache/camel/impl/engine/br/camel-scan.xml
 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/br/camel-scan.xml
new file mode 100644
index 0000000..dd1f8a4
--- /dev/null
+++ 
b/core/camel-core/src/test/resources/org/apache/camel/impl/engine/br/camel-scan.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<routes id="camel" xmlns="http://camel.apache.org/schema/spring";>
+    <route id="scan-b">
+        <from uri="direct:scan"/>
+        <to uri="mock:scan"/>
+    </route>
+</routes>
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/ResourceHelper.java 
b/core/camel-support/src/main/java/org/apache/camel/support/ResourceHelper.java
index 686bd7b..50c1b30 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/ResourceHelper.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/ResourceHelper.java
@@ -29,11 +29,18 @@ import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLDecoder;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.ClassResolver;
+import org.apache.camel.util.AntPathMatcher;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
@@ -305,4 +312,25 @@ public final class ResourceHelper {
         return uri;
     }
 
+    /**
+     * Find resources from the file system using Ant-style path patterns.
+     *
+     * @param root the starting file
+     * @param pattern the Ant pattern
+     * @return a list of files matching the given pattern
+     * @throws Exception
+     */
+    public static Set<Path> findInFileSystem(Path root, String pattern) throws 
Exception {
+        try (Stream<Path> path = Files.walk(root)) {
+            return path
+                .filter(Files::isRegularFile)
+                .filter(entry -> {
+                    Path relative = root.relativize(entry);
+                    boolean match = AntPathMatcher.INSTANCE.match(pattern, 
relative.toString());
+                    LOG.debug("Found resource: {} matching pattern: {} -> {}", 
entry.toString(), pattern, match);
+                    return match;
+                })
+                .collect(Collectors.toCollection(LinkedHashSet::new));
+        }
+    }
 }

Reply via email to