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 2019/04/25 10:33:18 UTC

[camel] 13/17: CAMEL-13449: camel3 - Move bean component out of camel-core

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

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

commit 41dfd8972a68e828fbb54c97dde2abcab025a0ce
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Apr 25 11:02:15 2019 +0200

    CAMEL-13449: camel3 - Move bean component out of camel-core
---
 .../component/bean/DefaultBeanProxyFactory.java    |  3 +
 .../services/org/apache/camel/bean-proxy-factory   | 18 ++++++
 .../main/java/org/apache/camel/CamelContext.java   |  6 ++
 .../org/apache/camel/builder/ProxyBuilder.java     |  4 +-
 .../apache/camel/impl/AbstractCamelContext.java    | 22 +++++++
 .../camel/impl/BeanProxyFactoryResolver.java       | 71 ++++++++++++++++++++++
 .../camel/impl/CamelPostProcessorHelper.java       |  4 +-
 .../org/apache/camel/impl/DefaultCamelContext.java |  5 ++
 8 files changed, 129 insertions(+), 4 deletions(-)

diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProxyFactory.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProxyFactory.java
index 421c86d..b2eeaa6 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProxyFactory.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProxyFactory.java
@@ -21,6 +21,9 @@ import org.apache.camel.spi.BeanProxyFactory;
 
 public final class DefaultBeanProxyFactory implements BeanProxyFactory {
 
+    public DefaultBeanProxyFactory() {
+    }
+
     @SafeVarargs
     @Override
     public final <T> T createProxy(Endpoint endpoint, boolean binding, Class<T>... interfaceClasses) throws Exception {
diff --git a/components/camel-bean/src/main/resources/META-INF/services/org/apache/camel/bean-proxy-factory b/components/camel-bean/src/main/resources/META-INF/services/org/apache/camel/bean-proxy-factory
new file mode 100644
index 0000000..58e2e9a
--- /dev/null
+++ b/components/camel-bean/src/main/resources/META-INF/services/org/apache/camel/bean-proxy-factory
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+class=org.apache.camel.component.bean.DefaultBeanProxyFactory
diff --git a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
index e6b4a66..80aaa76 100644
--- a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
@@ -25,6 +25,7 @@ import java.util.concurrent.ScheduledExecutorService;
 
 import org.apache.camel.spi.AnnotationBasedProcessorFactory;
 import org.apache.camel.spi.AsyncProcessorAwaitManager;
+import org.apache.camel.spi.BeanProxyFactory;
 import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.CamelContextNameStrategy;
 import org.apache.camel.spi.ClassResolver;
@@ -1485,4 +1486,9 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration {
      */
     AnnotationBasedProcessorFactory getAnnotationBasedProcessorFactory();
 
+    /**
+     * Gets the {@link BeanProxyFactory} to use.
+     */
+    BeanProxyFactory getBeanProxyFactory();
+
 }
diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java b/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java
index 7e0e759..263135a 100644
--- a/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java
+++ b/core/camel-core/src/main/java/org/apache/camel/builder/ProxyBuilder.java
@@ -81,9 +81,9 @@ public final class ProxyBuilder {
     public <T> T build(Class<T>... interfaceClasses) throws Exception {
         ObjectHelper.notNull(endpoint, "endpoint");
         // use proxy service
-        BeanProxyFactory factory = camelContext.hasService(BeanProxyFactory.class);
+        BeanProxyFactory factory = camelContext.getBeanProxyFactory();
         if (factory == null) {
-            throw new IllegalArgumentException("Cannot find BeanProxyFactory service. Make sure camel-bean is on the classpath.");
+            throw new IllegalArgumentException("Cannot find BeanProxyFactory. Make sure camel-bean is on the classpath.");
         }
         return factory.createProxy(endpoint, binding, interfaceClasses);
     }
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java
index 98e84bb..748554d 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java
@@ -105,6 +105,7 @@ import org.apache.camel.reifier.RouteReifier;
 import org.apache.camel.runtimecatalog.RuntimeCamelCatalog;
 import org.apache.camel.spi.AnnotationBasedProcessorFactory;
 import org.apache.camel.spi.AsyncProcessorAwaitManager;
+import org.apache.camel.spi.BeanProxyFactory;
 import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.CamelContextNameStrategy;
 import org.apache.camel.spi.CamelContextTracker;
@@ -254,6 +255,7 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Mod
     private volatile ManagementMBeanAssembler managementMBeanAssembler;
     private volatile RestRegistry restRegistry;
     private volatile HeadersMapFactory headersMapFactory;
+    private volatile BeanProxyFactory beanProxyFactory;
     private volatile ClassResolver classResolver;
     private volatile PackageScanClassResolver packageScanClassResolver;
     private volatile ServicePool<Producer> producerServicePool;
@@ -2561,6 +2563,12 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Mod
         // Start runtime catalog
         getExtension(RuntimeCamelCatalog.class);
 
+        // if camel-bean is on classpath then we can load its bean proxy facory
+        BeanProxyFactory beanProxyFactory = new BeanProxyFactoryResolver().resolve(this);
+        if (beanProxyFactory != null) {
+            addService(beanProxyFactory);
+        }
+
         // re-create endpoint registry as the cache size limit may be set after the constructor of this instance was called.
         // and we needed to create endpoints up-front as it may be accessed before this context is started
         endpoints = doAddService(createEndpointRegistry(endpoints));
@@ -3936,6 +3944,18 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Mod
         return annotationBasedProcessorFactory;
     }
 
+    @Override
+    public BeanProxyFactory getBeanProxyFactory() {
+        if (beanProxyFactory == null) {
+            synchronized (lock) {
+                if (beanProxyFactory == null) {
+                    beanProxyFactory = createBeanProxyFactory();
+                }
+            }
+        }
+        return beanProxyFactory;
+    }
+
     protected Map<String, RouteService> getRouteServices() {
         return routeServices;
     }
@@ -4037,6 +4057,8 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Mod
 
     protected abstract HeadersMapFactory createHeadersMapFactory();
 
+    protected abstract BeanProxyFactory createBeanProxyFactory();
+
     protected abstract LanguageResolver createLanguageResolver();
 
     protected abstract RestRegistry createRestRegistry();
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/BeanProxyFactoryResolver.java b/core/camel-core/src/main/java/org/apache/camel/impl/BeanProxyFactoryResolver.java
new file mode 100644
index 0000000..de002e1
--- /dev/null
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/BeanProxyFactoryResolver.java
@@ -0,0 +1,71 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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;
+
+import java.io.IOException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.BeanProxyFactory;
+import org.apache.camel.spi.FactoryFinder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Factory resolver to find the {@link BeanProxyFactory} from the classpath in camel-bean.
+ */
+public class BeanProxyFactoryResolver {
+
+    public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/";
+
+    private static final Logger LOG = LoggerFactory.getLogger(BeanProxyFactoryResolver.class);
+
+    private FactoryFinder factoryFinder;
+
+    public BeanProxyFactory resolve(CamelContext context) {
+        // use factory finder to find a custom implementations
+        Class<?> type = null;
+        try {
+            type = findFactory("bean-proxy-factory", context);
+        } catch (Exception e) {
+            // ignore
+        }
+
+        if (type != null) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Found BeanProxyFactory: {} via: {}{}", type.getName(), factoryFinder.getResourcePath(), "bean-proxy-factory");
+            }
+            if (BeanProxyFactory.class.isAssignableFrom(type)) {
+                BeanProxyFactory answer = (BeanProxyFactory) context.getInjector().newInstance(type);
+                LOG.info("Detected and using custom BeanProxyFactory: {}", answer);
+                return answer;
+            } else {
+                throw new IllegalArgumentException("Type is not a BeanProxyFactory implementation. Found: " + type.getName());
+            }
+        }
+
+        LOG.debug("Cannot find BeanProxyFactory. Make sure camel-bean is on the classpath.");
+        return null;
+    }
+
+    private Class<?> findFactory(String name, CamelContext context) throws ClassNotFoundException, IOException {
+        if (factoryFinder == null) {
+            factoryFinder = context.getFactoryFinder(RESOURCE_PATH);
+        }
+        return factoryFinder.findClass(name);
+    }
+
+}
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java b/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
index 467b85f..130854f 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
@@ -251,9 +251,9 @@ public class CamelPostProcessorHelper implements CamelContextAware {
                     // lets create a proxy
                     try {
                         // use proxy service
-                        BeanProxyFactory factory = camelContext.hasService(BeanProxyFactory.class);
+                        BeanProxyFactory factory = camelContext.getBeanProxyFactory();
                         if (factory == null) {
-                            throw new IllegalArgumentException("Cannot find BeanProxyFactory service. Make sure camel-bean is on the classpath.");
+                            throw new IllegalArgumentException("Cannot find BeanProxyFactory. Make sure camel-bean is on the classpath.");
                         }
                         return factory.createProxy(endpoint, binding, type);
                     } catch (Exception e) {
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 4c64ba9..f635af3 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -36,6 +36,7 @@ import org.apache.camel.model.validator.ValidatorDefinition;
 import org.apache.camel.runtimecatalog.RuntimeCamelCatalog;
 import org.apache.camel.runtimecatalog.impl.DefaultRuntimeCamelCatalog;
 import org.apache.camel.spi.AsyncProcessorAwaitManager;
+import org.apache.camel.spi.BeanProxyFactory;
 import org.apache.camel.spi.BeanRepository;
 import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.CamelContextNameStrategy;
@@ -276,6 +277,10 @@ public class DefaultCamelContext extends AbstractCamelContext {
         return new HeadersMapFactoryResolver().resolve(this);
     }
 
+    protected BeanProxyFactory createBeanProxyFactory() {
+        return new BeanProxyFactoryResolver().resolve(this);
+    }
+
     protected LanguageResolver createLanguageResolver() {
         return new DefaultLanguageResolver();
     }