Author: gertv
Date: Wed Dec 23 10:10:44 2009
New Revision: 893454

URL: http://svn.apache.org/viewvc?rev=893454&view=rev
Log:
SMXCOMP-687: Add an endpoint to interact with the CamelContext and manage its 
lifecycle

Added:
    
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java
    
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java
Removed:
    
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelControlBus.java
Modified:
    
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
    
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java

Added: 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java?rev=893454&view=auto
==============================================================================
--- 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java
 (added)
+++ 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java
 Wed Dec 23 10:10:44 2009
@@ -0,0 +1,82 @@
+/*
+ * 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.servicemix.camel;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessageExchange.Role;
+import javax.jbi.messaging.MessagingException;
+import javax.xml.namespace.QName;
+
+import org.apache.camel.CamelContext;
+import org.apache.servicemix.common.endpoints.ProviderEndpoint;
+
+/**
+ * JBI endpoint that provides access to an underlying {...@link 
org.apache.camel.CamelContext} and
+ * manages the lifecycle for that endpoint
+ */
+public class CamelContextEndpoint extends ProviderEndpoint {
+
+    /**
+     * The default {...@link org.apache.servicemix.camel.CamelContextEndpoint} 
service name
+     */
+    public static final QName SERVICE_NAME = new 
QName("http://camel.apache.org/schema/jbi";, "camelcontext");
+
+    private final CamelContext camelContext;
+
+    public CamelContextEndpoint(CamelContext camelContext, String su) {
+        this.camelContext = camelContext;
+        setService(SERVICE_NAME);
+        setEndpoint(su + "-controlbus");
+    }
+
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void process(MessageExchange exchange) throws Exception {
+        if (exchange.getRole() == Role.PROVIDER) {
+            if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+                //TODO: actually implement some control bus operations
+                fail(exchange, new UnsupportedOperationException("No control 
bus operations available"));
+            }
+        } else {
+            throw new MessagingException(
+                    "Unexpected exchange role: CamelContextEndpoint is not 
capable of handling " + exchange.getRole());
+        }
+
+    }
+
+    @Override
+    public void start() throws Exception {
+        super.start();
+
+        // no need to check for current context state - start() method will 
perform that check
+        camelContext.start();
+    }
+
+    @Override
+    public void stop() throws Exception {
+        super.stop();
+
+        // no need to check for current context state - stop() method will 
perform that check
+        camelContext.stop();
+
+    }
+
+}

Modified: 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java?rev=893454&r1=893453&r2=893454&view=diff
==============================================================================
--- 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
 (original)
+++ 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
 Wed Dec 23 10:10:44 2009
@@ -24,7 +24,6 @@
 import javax.jbi.management.DeploymentException;
 
 import org.apache.camel.Endpoint;
-import org.apache.camel.component.bean.BeanComponent;
 import org.apache.camel.spring.SpringCamelContext;
 import org.apache.servicemix.common.ServiceUnit;
 import org.apache.servicemix.common.xbean.AbstractXBeanDeployer;
@@ -103,15 +102,11 @@
                     
services.add(jbiComponent.createJbiEndpointFromCamel(endpoint));
                 }
             }
-            // lets add a control bus endpoint to ensure we have at least one 
endpoint to deploy
-            BeanComponent beanComponent = camelContext.getComponent("bean", 
BeanComponent.class);
-            Endpoint endpoint = beanComponent.createEndpoint(new 
CamelControlBus(camelContext),
-                                                             "camel:" + 
serviceUnitName + "-controlBus");
-            services.add(jbiComponent.createJbiEndpointFromCamel(endpoint));
-        }
-
-
 
+            // Here we just add a CamelContextEndpoint to delegate the 
servicemix start and stop lifecycle call
+            CamelContextEndpoint camelContextEndpoint = new 
CamelContextEndpoint(camelContext, serviceUnitName);
+            services.add(camelContextEndpoint);            
+        }
         return services;
     }
 

Added: 
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java?rev=893454&view=auto
==============================================================================
--- 
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java
 (added)
+++ 
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java
 Wed Dec 23 10:10:44 2009
@@ -0,0 +1,76 @@
+/*
+ * 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.servicemix.camel;
+
+import java.io.File;
+import java.net.URI;
+import java.net.URL;
+import java.util.Collection;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.ServiceSupport;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.common.Endpoint;
+import org.apache.servicemix.common.ServiceUnit;
+
+/**
+ * Test cases to ensure that {...@link 
org.apache.servicemix.camel.CamelContextEndpoint} correctly
+ * manages the lifecycle of the underlying {...@link 
org.apache.camel.CamelContext}
+ */
+public class CamelContextEndpointLifeCycleTest extends 
NonJbiCamelEndpointsIntegrationTest {
+    private static final transient Log LOG = 
LogFactory.getLog(CamelContextEndpointLifeCycleTest.class);
+
+    public void testComponentInstallation() throws Exception {
+        String serviceUnitConfiguration = suName + "-src/camel-context.xml";
+
+        CamelJbiComponent component = new CamelJbiComponent();
+        container.activateComponent(component, "#ServiceMixComponent#");
+        URL url = getClass().getResource(serviceUnitConfiguration);
+        File path = new File(new URI(url.toString()));
+        path = path.getParentFile();
+        try {
+            // Deploy and start su
+            component.getServiceUnitManager().deploy(suName, 
path.getAbsolutePath());
+            component.getServiceUnitManager().init(suName, 
path.getAbsolutePath());
+            component.getServiceUnitManager().start(suName);
+
+            ServiceUnit su = component.getRegistry().getServiceUnit(suName);
+            Collection<Endpoint> endpoints = su.getEndpoints();
+            assertEquals("There should have one Endpoint", 1, 
endpoints.size());
+            Endpoint endpoint = endpoints.iterator().next();
+            assertTrue("It should be CamelContextEndpoint", endpoint 
instanceof CamelContextEndpoint);
+            CamelContext camelContext = 
((CamelContextEndpoint)endpoint).getCamelContext();
+            // check the CamelContextEndpoint status
+            assertTrue("The CamelContext should be started", 
((ServiceSupport)camelContext).isStarted());
+            // Stop
+            component.getServiceUnitManager().stop(suName);
+            // check the CamelContextEndpoint status
+            assertTrue("The CamelContext should be stopped", 
((ServiceSupport)camelContext).isStopped());
+            // reStart
+            component.getServiceUnitManager().start(suName);
+            assertTrue("The CamelContext should be started", 
((ServiceSupport)camelContext).isStarted());
+
+            component.getServiceUnitManager().stop(suName);
+            component.getServiceUnitManager().shutDown(suName);
+            component.getServiceUnitManager().undeploy(suName, 
path.getAbsolutePath());
+        } catch (Exception e) {
+            LOG.error("Caught: " + e, e);
+            throw e;
+        }
+    }
+}

Modified: 
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java?rev=893454&r1=893453&r2=893454&view=diff
==============================================================================
--- 
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java
 (original)
+++ 
servicemix/components/engines/servicemix-camel/trunk/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java
 Wed Dec 23 10:10:44 2009
@@ -152,7 +152,7 @@
     protected void configureExchange(ServiceMixClient client,
             MessageExchange exchange) {
         ServiceEndpoint endpoint = client.getContext().getEndpoint(
-                CamelProviderEndpoint.SERVICE_NAME, "camel:su1-controlBus");
+                CamelContextEndpoint.SERVICE_NAME, "su1-controlbus");
         assertNotNull("Should have a Camel endpoint exposed in JBI!", 
endpoint);
         exchange.setEndpoint(endpoint);
     }


Reply via email to