Author: davsclaus
Date: Thu Oct 18 19:42:34 2012
New Revision: 1399815

URL: http://svn.apache.org/viewvc?rev=1399815&view=rev
Log:
CAMEL-5712: Fixed camel-blueprint to use a service listener to defer starting 
BlueprintCamelContext until after the BlueprintContainer is fully finished and 
has enlisted itself as service. This fixes among others using packageScan with 
blueprint.

Modified:
    camel/branches/camel-2.9.x/   (props changed)
    
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
    
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1399808
  Merged /camel/branches/camel-2.10.x:r1399809

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java?rev=1399815&r1=1399814&r2=1399815&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
 (original)
+++ 
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
 Thu Oct 18 19:42:34 2012
@@ -29,11 +29,13 @@ import org.apache.camel.impl.DefaultCame
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.Registry;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
 import org.osgi.service.blueprint.container.BlueprintContainer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class BlueprintCamelContext extends DefaultCamelContext {
+public class BlueprintCamelContext extends DefaultCamelContext implements 
ServiceListener {
 
     private static final transient Logger LOG = 
LoggerFactory.getLogger(BlueprintCamelContext.class);
 
@@ -73,30 +75,34 @@ public class BlueprintCamelContext exten
     }
 
     public void init() throws Exception {
-        final ClassLoader original = 
Thread.currentThread().getContextClassLoader();
-        try {
-            // let's set a more suitable TCCL while starting the context
-            
Thread.currentThread().setContextClassLoader(getApplicationContextClassLoader());
-            maybeStart();
-        } finally {
-            Thread.currentThread().setContextClassLoader(original);
-        }
-    }
-
-    private void maybeStart() throws Exception {
-        if (!isStarted() && !isStarting()) {
-            start();
-        } else {
-            // ignore as Camel is already started
-            LOG.trace("Ignoring maybeStart() as Apache Camel is already 
started");
-        }
+        // add service listener so we can be notified when blueprint container 
is done
+        // and we would be ready to start CamelContext
+        bundleContext.addServiceListener(this);
     }
 
     public void destroy() throws Exception {
+        // remove listener and stop this CamelContext
+        bundleContext.removeServiceListener(this);
         stop();
     }
 
     @Override
+    public void serviceChanged(ServiceEvent event) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Service {} changed to {}", event, event.getType());
+        }
+        // look for blueprint container to be registered, and then we can 
start the CamelContext
+        if (event.getType() == ServiceEvent.REGISTERED && 
event.getServiceReference().isAssignableTo(bundleContext.getBundle(),
+                "org.osgi.service.blueprint.container.BlueprintContainer")) {
+            try {
+                maybeStart();
+            } catch (Exception e) {
+                LOG.warn("Error occurred during starting " + this, e);
+            }
+        }
+    }
+
+    @Override
     protected TypeConverter createTypeConverter() {
         // CAMEL-3614: make sure we use a bundle context which imports 
org.apache.camel.impl.converter package
         BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
@@ -113,4 +119,20 @@ public class BlueprintCamelContext exten
         return OsgiCamelContextHelper.wrapRegistry(this, reg, bundleContext);
     }
 
+    private void maybeStart() throws Exception {
+        if (!isStarted() && !isStarting()) {
+            final ClassLoader original = 
Thread.currentThread().getContextClassLoader();
+            try {
+                // let's set a more suitable TCCL while starting the context
+                
Thread.currentThread().setContextClassLoader(getApplicationContextClassLoader());
+                LOG.debug("Starting {}", this);
+                start();
+            } finally {
+                Thread.currentThread().setContextClassLoader(original);
+            }
+        } else {
+            // ignore as Camel is already started
+            LOG.trace("Ignoring maybeStart() as {} is already started", this);
+        }
+    }
 }

Modified: 
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java?rev=1399815&r1=1399814&r2=1399815&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
 (original)
+++ 
camel/branches/camel-2.9.x/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
 Thu Oct 18 19:42:34 2012
@@ -247,7 +247,7 @@ public class CamelContextFactoryBean ext
         getContext().getPackageScanClassResolver().addFilter(filter);
         ClassLoader classLoader = new 
BundleDelegatingClassLoader(((ExtendedBlueprintContainer) 
blueprintContainer).getBundleContext().getBundle());
         PackageScanRouteBuilderFinder finder = new 
PackageScanRouteBuilderFinder(getContext(), packages, classLoader,
-                                                                               
  /*getBeanPostProcessor(),*/ getContext().getPackageScanClassResolver());
+                                                                               
  getContext().getPackageScanClassResolver());
         finder.appendBuilders(builders);
 
         // and remove the filter


Reply via email to