[camel] branch camel-2.22.x updated: CAMEL-12087: camel-core: WARN No CamelContext defined yet so cannot inject into bean

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch camel-2.22.x updated: CAMEL-12087: camel-core: WARN No CamelContext defined yet so cannot inject into bean

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

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


The following commit(s) were added to refs/heads/camel-2.22.x by this push:
     new 763ee7b  CAMEL-12087: camel-core: WARN No CamelContext defined yet so cannot inject into bean
763ee7b is described below

commit 763ee7b2505c9ca038953686c102afed7f544237
Author: lburgazzoli <[hidden email]>
AuthorDate: Tue Oct 9 16:06:13 2018 +0200

    CAMEL-12087: camel-core: WARN No CamelContext defined yet so cannot inject into bean
---
 .../org/apache/camel/DeferredContextBinding.java   | 36 ++++++++++++++++++++++
 .../camel/impl/DefaultCamelBeanPostProcessor.java  | 15 +++++----
 .../impl/health/RoutesHealthCheckRepository.java   |  2 ++
 3 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/DeferredContextBinding.java b/camel-core/src/main/java/org/apache/camel/DeferredContextBinding.java
new file mode 100644
index 0000000..1bf7927
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/DeferredContextBinding.java
@@ -0,0 +1,36 @@
+/**
+ * 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;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Used to indicate that if the target type is {@link CamelContextAware}, the
+ * context does not need to be mandatory injected during bean post processing but
+ * can be injected later on as example during Camel Context configuration.
+ *
+ * See <a href="https://issues.apache.org/jira/browse/CAMEL-12087">CAMEL-12087</a> for additional information.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Target({ElementType.TYPE})
+public @interface DeferredContextBinding {
+}
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
index 2c5261f..4d624e4 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
@@ -22,6 +22,7 @@ import java.lang.reflect.Method;
 import org.apache.camel.BeanInject;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.DeferredContextBinding;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.PropertyInject;
@@ -64,7 +65,7 @@ public class DefaultCamelBeanPostProcessor {
      * initialization callbacks (like <code>afterPropertiesSet</code>
      * or a custom init-method). The bean will already be populated with property values.
      * The returned bean instance may be a wrapper around the original.
-     *
+     *
      * @param bean the new bean instance
      * @param beanName the name of the bean
      * @return the bean instance to use, either the original or a wrapped one; if
@@ -84,10 +85,12 @@ public class DefaultCamelBeanPostProcessor {
 
         if (bean instanceof CamelContextAware && canSetCamelContext(bean, beanName)) {
             CamelContextAware contextAware = (CamelContextAware)bean;
+            DeferredContextBinding deferredBinding = bean.getClass().getAnnotation(DeferredContextBinding.class);
             CamelContext context = getOrLookupCamelContext();
-            if (context == null) {
-                LOG.warn("No CamelContext defined yet so cannot inject into bean: " + beanName);
-            } else {
+
+            if (context == null && deferredBinding == null) {
+                LOG.warn("No CamelContext defined yet so cannot inject into bean: {}", beanName);
+            } else if (context != null) {
                 contextAware.setCamelContext(context);
             }
         }
@@ -100,7 +103,7 @@ public class DefaultCamelBeanPostProcessor {
      * initialization callbacks (like <code>afterPropertiesSet</code>
      * or a custom init-method). The bean will already be populated with property values.
      * The returned bean instance may be a wrapper around the original.
-     *
+     *
      * @param bean the new bean instance
      * @param beanName the name of the bean
      * @return the bean instance to use, either the original or a wrapped one; if
@@ -193,7 +196,7 @@ public class DefaultCamelBeanPostProcessor {
                                Object bean, String beanName) {
         injectField(field, endpointUri, endpointRef, endpointProperty, bean, beanName, true);
     }
-    
+
     public void injectField(Field field, String endpointUri, String endpointRef, String endpointProperty,
                                Object bean, String beanName, boolean binding) {
         ReflectionHelper.setField(field, bean,
diff --git a/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java b/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java
index 637a805..7727b80 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/health/RoutesHealthCheckRepository.java
@@ -29,11 +29,13 @@ import java.util.stream.Stream;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.DeferredContextBinding;
 import org.apache.camel.Route;
 import org.apache.camel.api.management.mbean.ManagedRouteMBean;
 import org.apache.camel.health.HealthCheck;
 import org.apache.camel.health.HealthCheckRepository;
 
+@DeferredContextBinding
 public class RoutesHealthCheckRepository implements CamelContextAware, HealthCheckRepository {
     private final ConcurrentMap<Route, HealthCheck> checks;
     private Set<String> blacklist;