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

davsclaus 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 d5fbaf8  CAMEL-13736: Camel main - Support bean post processing on 
@BindToRegistry
d5fbaf8 is described below

commit d5fbaf8f56041eb6107076f488aa0acd96637333
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Jul 17 11:29:53 2019 +0200

    CAMEL-13736: Camel main - Support bean post processing on @BindToRegistry
---
 .../main/java/org/apache/camel/BindToRegistry.java |  7 ++-
 .../impl/engine/DefaultCamelBeanPostProcessor.java | 39 ++++++++++---
 .../impl/BindToRegistryBeanPostProcessorTest.java  | 67 ++++++++++++++++++++++
 3 files changed, 105 insertions(+), 8 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/BindToRegistry.java 
b/core/camel-api/src/main/java/org/apache/camel/BindToRegistry.java
index ab00b82..c9191b4 100644
--- a/core/camel-api/src/main/java/org/apache/camel/BindToRegistry.java
+++ b/core/camel-api/src/main/java/org/apache/camel/BindToRegistry.java
@@ -23,7 +23,7 @@ import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 /**
- * Used for binding a bean to the registry
+ * Used for binding a bean to the registry.
  *
  * If no name is specified then the bean will have its name auto computed 
based on the
  * class name, field name, or method name where the annotation is configured.
@@ -42,4 +42,9 @@ public @interface BindToRegistry {
      * Id of {@link CamelContext} to use
      */
     String context() default "";
+
+    /**
+     * Whether to perform bean post processing (dependency injection) on the 
bean
+     */
+    boolean beanPostProcess() default false;
 }
diff --git 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
index 6899089..72019f7 100644
--- 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
+++ 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
@@ -30,6 +30,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.DeferredContextBinding;
 import org.apache.camel.EndpointInject;
+import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.Produce;
 import org.apache.camel.PropertyInject;
@@ -195,7 +196,7 @@ public class DefaultCamelBeanPostProcessor implements 
CamelBeanPostProcessor {
             public void doWith(Field field) throws IllegalArgumentException, 
IllegalAccessException {
                 BindToRegistry bind = 
field.getAnnotation(BindToRegistry.class);
                 if (bind != null && 
getPostProcessorHelper().matchContext(bind.context())) {
-                    bindToRegistry(field, bind.value(), bean, beanName);
+                    bindToRegistry(field, bind.value(), bean, beanName, 
bind.beanPostProcess());
                 }
             }
         });
@@ -278,7 +279,7 @@ public class DefaultCamelBeanPostProcessor implements 
CamelBeanPostProcessor {
         // bind each method
         methods.forEach(method -> {
             BindToRegistry bind = method.getAnnotation(BindToRegistry.class);
-            bindToRegistry(method, bind.value(), bean, beanName);
+            bindToRegistry(method, bind.value(), bean, beanName, 
bind.beanPostProcess());
         });
     }
 
@@ -286,7 +287,7 @@ public class DefaultCamelBeanPostProcessor implements 
CamelBeanPostProcessor {
         Class<?> clazz = bean.getClass();
         BindToRegistry ann = clazz.getAnnotation(BindToRegistry.class);
         if (ann != null && 
getPostProcessorHelper().matchContext(ann.context())) {
-            bindToRegistry(clazz, ann.value(), bean, beanName);
+            bindToRegistry(clazz, ann.value(), bean, beanName, 
ann.beanPostProcess());
         }
     }
 
@@ -296,7 +297,7 @@ public class DefaultCamelBeanPostProcessor implements 
CamelBeanPostProcessor {
                 BindToRegistry ann = (BindToRegistry) 
clazz.getAnnotation(BindToRegistry.class);
                 if (ann != null && 
getPostProcessorHelper().matchContext(ann.context())) {
                     // its a nested class so we dont have a bean instance for 
it
-                    bindToRegistry(clazz, ann.value(), null, null);
+                    bindToRegistry(clazz, ann.value(), null, null, 
ann.beanPostProcess());
                 }
             }
         });
@@ -360,7 +361,7 @@ public class DefaultCamelBeanPostProcessor implements 
CamelBeanPostProcessor {
         }
     }
 
-    private void bindToRegistry(Class<?> clazz, String name, Object bean, 
String beanName) {
+    private void bindToRegistry(Class<?> clazz, String name, Object bean, 
String beanName, boolean beanPostProcess) {
         if (isEmpty(name)) {
             name = clazz.getSimpleName();
         }
@@ -368,20 +369,36 @@ public class DefaultCamelBeanPostProcessor implements 
CamelBeanPostProcessor {
             // no bean so then create an instance from its type
             bean = camelContext.getInjector().newInstance(clazz);
         }
+        if (beanPostProcess) {
+            try {
+                
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor().postProcessBeforeInitialization(bean,
 beanName);
+                
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor().postProcessAfterInitialization(bean,
 beanName);
+            } catch (Exception e) {
+                throw RuntimeCamelException.wrapRuntimeException(e);
+            }
+        }
         camelContext.getRegistry().bind(name, bean);
     }
 
-    private void bindToRegistry(Field field, String name, Object bean, String 
beanName) {
+    private void bindToRegistry(Field field, String name, Object bean, String 
beanName, boolean beanPostProcess) {
         if (isEmpty(name)) {
             name = field.getName();
         }
         Object value = ReflectionHelper.getField(field, bean);
         if (value != null) {
+            if (beanPostProcess) {
+                try {
+                    
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor().postProcessBeforeInitialization(value,
 beanName);
+                    
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor().postProcessAfterInitialization(value,
 beanName);
+                } catch (Exception e) {
+                    throw RuntimeCamelException.wrapRuntimeException(e);
+                }
+            }
             camelContext.getRegistry().bind(name, value);
         }
     }
 
-    private void bindToRegistry(Method method, String name, Object bean, 
String beanName) {
+    private void bindToRegistry(Method method, String name, Object bean, 
String beanName, boolean beanPostProcess) {
         if (isEmpty(name)) {
             name = method.getName();
         }
@@ -399,6 +416,14 @@ public class DefaultCamelBeanPostProcessor implements 
CamelBeanPostProcessor {
             value = invokeMethod(method, bean);
         }
         if (value != null) {
+            if (beanPostProcess) {
+                try {
+                    
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor().postProcessBeforeInitialization(value,
 beanName);
+                    
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor().postProcessAfterInitialization(value,
 beanName);
+                } catch (Exception e) {
+                    throw RuntimeCamelException.wrapRuntimeException(e);
+                }
+            }
             camelContext.getRegistry().bind(name, value);
         }
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/BindToRegistryBeanPostProcessorTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/BindToRegistryBeanPostProcessorTest.java
new file mode 100644
index 0000000..ac17277
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/BindToRegistryBeanPostProcessorTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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;
+
+import org.apache.camel.BeanInject;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.junit.Test;
+
+public class BindToRegistryBeanPostProcessorTest extends ContextTestSupport {
+
+    // field
+    @BindToRegistry(beanPostProcess = true)
+    private FooService foo = new FooService();
+
+    // method
+    @BindToRegistry(beanPostProcess = true)
+    public FooService myOtherFoo() {
+        return new FooService();
+    }
+
+    @Test
+    public void testPostProcessor() throws Exception {
+        // bean post processing dont run on ContextTestSupport
+        CamelBeanPostProcessor cbpp = 
context.adapt(ExtendedCamelContext.class).getBeanPostProcessor();
+        cbpp.postProcessBeforeInitialization(this, "this");
+        cbpp.postProcessAfterInitialization(this, "this");
+
+        assertNotNull(foo);
+        assertSame(context, foo.getCamelContext());
+
+        FooService other = (FooService) 
context.getRegistry().lookupByName("myOtherFoo");
+        assertNotNull(other);
+        assertSame(context, other.getCamelContext());
+    }
+
+    public class FooService {
+
+        @BeanInject
+        private CamelContext camelContext;
+
+        public CamelContext getCamelContext() {
+            return camelContext;
+        }
+
+        public void setCamelContext(CamelContext camelContext) {
+            this.camelContext = camelContext;
+        }
+    }
+}

Reply via email to