camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-8926: camel-spring - contextScan should allow to exclude non-singletons
Date Wed, 01 Jul 2015 19:27:32 GMT
Repository: camel
Updated Branches:
  refs/heads/master 614a4f43d -> c94530132


CAMEL-8926: camel-spring - contextScan should allow to exclude non-singletons


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

Branch: refs/heads/master
Commit: c945301320db78bcf139964031faf62ee6eb0aab
Parents: 614a4f4
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Jul 1 21:10:45 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Jul 1 21:10:45 2015 +0200

----------------------------------------------------------------------
 .../camel/model/ContextScanDefinition.java      | 24 ++++++++++++++++----
 .../blueprint/BlueprintContainerRegistry.java   | 11 +++++++++
 .../blueprint/CamelContextFactoryBean.java      |  4 ++--
 .../ContextScanRouteBuilderFinder.java          |  6 +++--
 .../xml/AbstractCamelContextFactoryBean.java    |  6 +++--
 .../camel/spring/CamelContextFactoryBean.java   |  4 ++--
 .../spring/ContextScanRouteBuilderFinder.java   |  6 +++--
 7 files changed, 47 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c9453013/camel-core/src/main/java/org/apache/camel/model/ContextScanDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/ContextScanDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ContextScanDefinition.java
index 19d1a41..cffc3dc 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ContextScanDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ContextScanDefinition.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
@@ -32,6 +33,8 @@ import org.apache.camel.spi.Metadata;
 @XmlRootElement(name = "contextScan")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class ContextScanDefinition {
+    @XmlAttribute
+    private Boolean includeNonSingletons;
     @XmlElement(name = "excludes")
     private List<String> excludes = new ArrayList<String>();
     @XmlElement(name = "includes")
@@ -40,12 +43,21 @@ public class ContextScanDefinition {
     public ContextScanDefinition() {
     }
 
-    public List<String> getExcludes() {
-        return excludes;
+    public Boolean getIncludeNonSingletons() {
+        return includeNonSingletons;
     }
 
-    public List<String> getIncludes() {
-        return includes;
+    /**
+     * Whether to include non-singleton beans (prototypes)
+     * <p/>
+     * By default only singleton beans is included in the context scan
+     */
+    public void setIncludeNonSingletons(Boolean includeNonSingletons) {
+        this.includeNonSingletons = includeNonSingletons;
+    }
+
+    public List<String> getExcludes() {
+        return excludes;
     }
 
     /**
@@ -55,6 +67,10 @@ public class ContextScanDefinition {
         this.excludes = excludes;
     }
 
+    public List<String> getIncludes() {
+        return includes;
+    }
+
     /**
      * Include finding route builder from these java package names.
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/c9453013/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
index 26900a1..84f9c93 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintContainerRegistry.java
@@ -97,6 +97,10 @@ public class BlueprintContainerRegistry implements Registry {
     }
 
     public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer,
Class<T> type) {
+        return lookupByType(blueprintContainer, type, true);
+    }
+
+    public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer,
Class<T> type, boolean includeNonSingletons) {
         Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
         Map<String, T> objects = new LinkedHashMap<String, T>();
         Set<String> ids = blueprintContainer.getComponentIds();
@@ -106,6 +110,13 @@ public class BlueprintContainerRegistry implements Registry {
                 Class<?> cl = null;
                 if (metadata instanceof BeanMetadata) {
                     BeanMetadata beanMetadata = (BeanMetadata)metadata;
+                    // should we skip the bean if its prototype and we are only looking for
singletons?
+                    if (!includeNonSingletons) {
+                        String scope = beanMetadata.getScope();
+                        if (BeanMetadata.SCOPE_PROTOTYPE.equals(scope)) {
+                            continue;
+                        }
+                    }
                     cl = bundle.loadClass(beanMetadata.getClassName());
                 } else if (metadata instanceof ReferenceMetadata) {
                     ReferenceMetadata referenceMetadata = (ReferenceMetadata)metadata;

http://git-wip-us.apache.org/repos/asf/camel/blob/c9453013/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
index b8500cd..2ae5729 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
@@ -289,8 +289,8 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu
     }
 
     @Override
-    protected void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder>
builders) throws Exception {
-        ContextScanRouteBuilderFinder finder = new ContextScanRouteBuilderFinder(getContext(),
filter);
+    protected void findRouteBuildersByContextScan(PackageScanFilter filter, boolean includeNonSingletons,
List<RoutesBuilder> builders) throws Exception {
+        ContextScanRouteBuilderFinder finder = new ContextScanRouteBuilderFinder(getContext(),
filter, includeNonSingletons);
         finder.appendBuilders(builders);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/c9453013/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/ContextScanRouteBuilderFinder.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/ContextScanRouteBuilderFinder.java
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/ContextScanRouteBuilderFinder.java
index a2ed6ce..f4a938d 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/ContextScanRouteBuilderFinder.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/ContextScanRouteBuilderFinder.java
@@ -36,17 +36,19 @@ public class ContextScanRouteBuilderFinder {
     private static final Logger LOG = LoggerFactory.getLogger(ContextScanRouteBuilderFinder.class);
     private final BlueprintContainer blueprintContainer;
     private final PackageScanFilter filter;
+    private final boolean includeNonSingletons;
 
-    public ContextScanRouteBuilderFinder(BlueprintCamelContext camelContext, PackageScanFilter
filter) {
+    public ContextScanRouteBuilderFinder(BlueprintCamelContext camelContext, PackageScanFilter
filter, boolean includeNonSingletons) {
         this.blueprintContainer = camelContext.getBlueprintContainer();
         this.filter = filter;
+        this.includeNonSingletons = includeNonSingletons;
     }
 
     /**
      * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be
found in the context
      */
     public void appendBuilders(List<RoutesBuilder> list) {
-        Map<String, RoutesBuilder> beans = BlueprintContainerRegistry.lookupByType(blueprintContainer,
RoutesBuilder.class);
+        Map<String, RoutesBuilder> beans = BlueprintContainerRegistry.lookupByType(blueprintContainer,
RoutesBuilder.class, includeNonSingletons);
 
         for (Entry<String, RoutesBuilder> entry : beans.entrySet()) {
             String key = entry.getKey();

http://git-wip-us.apache.org/repos/asf/camel/blob/c9453013/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
index cc77527..4caefee 100644
--- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
@@ -897,13 +897,15 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex
                 exclude = getContext().resolvePropertyPlaceholders(exclude);
                 filter.addExcludePattern(exclude);
             }
-            findRouteBuildersByContextScan(filter, builders);
+            // lets be false by default, to skip prototype beans
+            boolean includeNonSingletons = contextScanDef.getIncludeNonSingletons() != null
? contextScanDef.getIncludeNonSingletons() : false;
+            findRouteBuildersByContextScan(filter, includeNonSingletons, builders);
         }
     }
 
     protected abstract void findRouteBuildersByPackageScan(String[] packages, PackageScanFilter
filter, List<RoutesBuilder> builders) throws Exception;
 
-    protected abstract void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder>
builders) throws Exception;
+    protected abstract void findRouteBuildersByContextScan(PackageScanFilter filter, boolean
includeNonSingletons, List<RoutesBuilder> builders) throws Exception;
 
     private void addPackageElementContentsToScanDefinition() {
         PackageScanDefinition packageScanDef = getPackageScan();

http://git-wip-us.apache.org/repos/asf/camel/blob/c9453013/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
index 1c02e5f..1725227 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
@@ -228,8 +228,8 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr
     }
 
     @Override
-    protected void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder>
builders) throws Exception {
-        ContextScanRouteBuilderFinder finder = new ContextScanRouteBuilderFinder(getContext(),
filter);
+    protected void findRouteBuildersByContextScan(PackageScanFilter filter, boolean includeNonSingletons,
List<RoutesBuilder> builders) throws Exception {
+        ContextScanRouteBuilderFinder finder = new ContextScanRouteBuilderFinder(getContext(),
filter, includeNonSingletons);
         finder.appendBuilders(builders);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/c9453013/components/camel-spring/src/main/java/org/apache/camel/spring/ContextScanRouteBuilderFinder.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/ContextScanRouteBuilderFinder.java
b/components/camel-spring/src/main/java/org/apache/camel/spring/ContextScanRouteBuilderFinder.java
index aa2a098..cf60c17 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/ContextScanRouteBuilderFinder.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/ContextScanRouteBuilderFinder.java
@@ -36,17 +36,19 @@ public class ContextScanRouteBuilderFinder {
     private static final Logger LOG = LoggerFactory.getLogger(ContextScanRouteBuilderFinder.class);
     private final ApplicationContext applicationContext;
     private final PackageScanFilter filter;
+    private final boolean includeNonSingletons;
 
-    public ContextScanRouteBuilderFinder(SpringCamelContext camelContext, PackageScanFilter
filter) {
+    public ContextScanRouteBuilderFinder(SpringCamelContext camelContext, PackageScanFilter
filter, boolean includeNonSingletons) {
         this.applicationContext = camelContext.getApplicationContext();
         this.filter = filter;
+        this.includeNonSingletons = includeNonSingletons;
     }
 
     /**
      * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be
found in the context
      */
     public void appendBuilders(List<RoutesBuilder> list) {
-        Map<String, RoutesBuilder> beans = applicationContext.getBeansOfType(RoutesBuilder.class,
true, true);
+        Map<String, RoutesBuilder> beans = applicationContext.getBeansOfType(RoutesBuilder.class,
includeNonSingletons, true);
 
         for (Entry<String, RoutesBuilder> entry : beans.entrySet()) {
             Object bean = entry.getValue();


Mime
View raw message