You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2020/10/05 14:43:00 UTC

[camel-quarkus] 01/02: Introduce RoutesBuilderClassExcludeBuildItem

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

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

commit 13e5bcdf36e3e004d53c93941572e88306ee0101
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Mon Oct 5 14:00:43 2020 +0200

    Introduce RoutesBuilderClassExcludeBuildItem
---
 .../quarkus/core/deployment/CamelProcessor.java    | 23 ++++--
 .../spi/RoutesBuilderClassExcludeBuildItem.java    | 85 ++++++++++++++++++++++
 2 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelProcessor.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelProcessor.java
index fcc0b70..5d4bb2f 100644
--- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelProcessor.java
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelProcessor.java
@@ -24,6 +24,7 @@ import java.nio.file.Path;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.function.Predicate;
 import java.util.stream.Collectors;
 
 import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
@@ -60,6 +61,7 @@ import org.apache.camel.quarkus.core.deployment.spi.CamelServicePatternBuildItem
 import org.apache.camel.quarkus.core.deployment.spi.CamelTypeConverterLoaderBuildItem;
 import org.apache.camel.quarkus.core.deployment.spi.CamelTypeConverterRegistryBuildItem;
 import org.apache.camel.quarkus.core.deployment.spi.ContainerBeansBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.RoutesBuilderClassExcludeBuildItem;
 import org.apache.camel.quarkus.core.deployment.util.CamelSupport;
 import org.apache.camel.quarkus.core.deployment.util.PathFilter;
 import org.apache.camel.quarkus.support.common.CamelCapabilities;
@@ -218,8 +220,8 @@ class CamelProcessor {
         // account even if it should not.
         //
         // TODO: we could add a filter to discard AnnotationTypeConverterLoader but maybe we should introduce
-        //       a marker interface like StaticTypeConverterLoader for loaders that do not require to perform
-        //       any discovery at runtime.
+        // a marker interface like StaticTypeConverterLoader for loaders that do not require to perform
+        // any discovery at runtime.
         //
         for (ApplicationArchive archive : applicationArchives.getAllApplicationArchives()) {
             for (Path root : archive.getRootDirs()) {
@@ -315,7 +317,8 @@ class CamelProcessor {
     @BuildStep(onlyIf = { CamelConfigFlags.RoutesDiscoveryEnabled.class })
     public List<CamelRoutesBuilderClassBuildItem> discoverRoutesBuilderClassNames(
             CombinedIndexBuildItem combinedIndex,
-            CamelConfig config) {
+            CamelConfig config,
+            List<RoutesBuilderClassExcludeBuildItem> routesBuilderClassExcludes) {
 
         final IndexView index = combinedIndex.getIndex();
 
@@ -324,15 +327,21 @@ class CamelProcessor {
         allKnownImplementors.addAll(index.getAllKnownSubclasses(ROUTE_BUILDER_TYPE));
         allKnownImplementors.addAll(index.getAllKnownSubclasses(ADVICE_WITH_ROUTE_BUILDER_TYPE));
 
+        final Predicate<DotName> pathFilter = new PathFilter.Builder()
+                .exclude(
+                        routesBuilderClassExcludes.stream()
+                                .map(RoutesBuilderClassExcludeBuildItem::getPattern)
+                                .collect(Collectors.toList()))
+                .exclude(config.routesDiscovery.excludePatterns)
+                .include(config.routesDiscovery.includePatterns)
+                .build().asDotNamePredicate();
+
         return allKnownImplementors
                 .stream()
                 // public and non-abstract
                 .filter(ci -> ((ci.flags() & (Modifier.ABSTRACT | Modifier.PUBLIC)) == Modifier.PUBLIC))
                 .map(ClassInfo::name)
-                .filter(new PathFilter.Builder()
-                        .exclude(config.routesDiscovery.excludePatterns)
-                        .include(config.routesDiscovery.includePatterns)
-                        .build().asDotNamePredicate())
+                .filter(pathFilter)
                 .map(CamelRoutesBuilderClassBuildItem::new)
                 .collect(Collectors.toList());
     }
diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/spi/RoutesBuilderClassExcludeBuildItem.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/spi/RoutesBuilderClassExcludeBuildItem.java
new file mode 100644
index 0000000..326f829
--- /dev/null
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/spi/RoutesBuilderClassExcludeBuildItem.java
@@ -0,0 +1,85 @@
+/*
+ * 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.quarkus.core.deployment.spi;
+
+import io.quarkus.builder.item.MultiBuildItem;
+import org.apache.camel.quarkus.core.CamelConfig.RoutesDiscoveryConfig;
+
+/**
+ * A {@link MultiBuildItem} holding patterns whose matching classes will be excluded from the set of classes from which
+ * routes will be instantiated. This is a programmatic way of doing the same thing as can be done via
+ * {@link RoutesDiscoveryConfig#excludePatterns}.
+ */
+public final class RoutesBuilderClassExcludeBuildItem extends MultiBuildItem {
+    private final String pattern;
+
+    /**
+     * @param  cl a class to exclude
+     * @return    a new {@link RoutesBuilderClassExcludeBuildItem}
+     */
+    public static RoutesBuilderClassExcludeBuildItem ofClass(Class<?> cl) {
+        return ofClassName(cl.getName());
+    }
+
+    /**
+     * @param  className a class name to exclude
+     * @return           a new {@link RoutesBuilderClassExcludeBuildItem}
+     */
+    public static RoutesBuilderClassExcludeBuildItem ofClassName(String className) {
+        return new RoutesBuilderClassExcludeBuildItem(className.replace('.', '/'));
+    }
+
+    /**
+     * @param  pattern a single pattern to exclude like in {@link RoutesDiscoveryConfig#excludePatterns}; should contain
+     *                 slashes instead of periods; no leading slash, e.g. {@code com/mycompany/bar/*}
+     * @return         a new {@link RoutesBuilderClassExcludeBuildItem}
+     */
+    public static RoutesBuilderClassExcludeBuildItem ofPathPattern(String pattern) {
+        return new RoutesBuilderClassExcludeBuildItem(pattern);
+    }
+
+    RoutesBuilderClassExcludeBuildItem(String pattern) {
+        this.pattern = pattern;
+    }
+
+    public String getPattern() {
+        return pattern;
+    }
+
+    @Override
+    public int hashCode() {
+        return pattern.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        RoutesBuilderClassExcludeBuildItem other = (RoutesBuilderClassExcludeBuildItem) obj;
+        if (pattern == null) {
+            if (other.pattern != null)
+                return false;
+        } else if (!pattern.equals(other.pattern))
+            return false;
+        return true;
+    }
+
+}