camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1399808 - in /camel/trunk/components: camel-blueprint/src/main/java/org/apache/camel/blueprint/ camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/ ca...
Date Thu, 18 Oct 2012 19:30:12 GMT
Author: davsclaus
Date: Thu Oct 18 19:30:12 2012
New Revision: 1399808

URL: http://svn.apache.org/viewvc?rev=1399808&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.

Added:
    camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintPackageScanTest.java
  (with props)
    camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/
    camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/MyCoolRoute.java
  (with props)
    camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/packagescan.xml
      - copied, changed from r1399509, camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml
Modified:
    camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
    camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java

Modified: camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java?rev=1399808&r1=1399807&r2=1399808&view=diff
==============================================================================
--- camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
(original)
+++ camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
Thu Oct 18 19:30:12 2012
@@ -26,11 +26,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);
 
@@ -71,30 +73,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());
@@ -111,4 +117,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/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java?rev=1399808&r1=1399807&r2=1399808&view=diff
==============================================================================
--- camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
(original)
+++ camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
Thu Oct 18 19:30:12 2012
@@ -261,7 +261,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

Added: camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintPackageScanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintPackageScanTest.java?rev=1399808&view=auto
==============================================================================
--- camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintPackageScanTest.java
(added)
+++ camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintPackageScanTest.java
Thu Oct 18 19:30:12 2012
@@ -0,0 +1,44 @@
+/**
+ * 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.test.blueprint;
+
+import org.junit.Test;
+
+/**
+ *
+ */
+public class BlueprintPackageScanTest extends CamelBlueprintTestSupport {
+
+    @Override
+    protected String getBlueprintDescriptor() {
+        return "org/apache/camel/test/blueprint/packagescan.xml";
+    }
+
+    // here we have regular Junit @Test method
+    @Test
+    public void testRoute() throws Exception {
+        // set mock expectations
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+
+        // send a message
+        template.sendBody("direct:start", "World");
+
+        // assert mocks
+        assertMockEndpointsSatisfied();
+    }
+
+}

Propchange: camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintPackageScanTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintPackageScanTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/MyCoolRoute.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/MyCoolRoute.java?rev=1399808&view=auto
==============================================================================
--- camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/MyCoolRoute.java
(added)
+++ camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/MyCoolRoute.java
Thu Oct 18 19:30:12 2012
@@ -0,0 +1,35 @@
+/**
+ * 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.test.blueprint.scan;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class MyCoolRoute extends RouteBuilder {
+
+    @EndpointInject(ref = "foo")
+    private Endpoint foo;
+
+    @Override
+    public void configure() throws Exception {
+        from(foo).to("mock:a");
+    }
+}

Propchange: camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/MyCoolRoute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/scan/MyCoolRoute.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/packagescan.xml
(from r1399509, camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/packagescan.xml?p2=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/packagescan.xml&p1=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml&r1=1399509&r2=1399808&rev=1399808&view=diff
==============================================================================
--- camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml
(original)
+++ camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/packagescan.xml
Thu Oct 18 19:30:12 2012
@@ -15,23 +15,17 @@
     See the License for the specific language governing permissions and
     limitations under the License.
 -->
-<!-- START SNIPPET: example -->
 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
              http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
 
   <camelContext xmlns="http://camel.apache.org/schema/blueprint">
+	  <packageScan>
+		  <package>org.apache.camel.test.blueprint.scan</package>
+	  </packageScan>
 
-    <route>
-      <from uri="direct:start"/>
-      <transform>
-        <simple>Hello ${body}</simple>
-      </transform>
-      <to uri="mock:a"/>
-    </route>
-
+	  <endpoint id="foo" uri="direct:start"/>
   </camelContext>
 
 </blueprint>
-<!-- END SNIPPET: example -->



Mime
View raw message