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

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

commit 76bece6ceacf1c796ea523807278067651463fd2
Author: Maciej Swiderski <swiderski.mac...@gmail.com>
AuthorDate: Wed Dec 5 12:41:59 2018 +0100

    CAMEL-12978 - Add support to configure CamelContext created by KIE-Server 
extension
---
 .../apache/camel/component/jbpm/JBPMConstants.java |   2 +
 .../component/jbpm/config/CamelContextBuilder.java |  35 ++++
 .../jbpm/server/CamelKieServerExtension.java       |  73 +++++++-
 .../jbpm/server/CamelKieServerExtensionTest.java   | 197 +++++++++++++++++++++
 .../camel-jbpm/src/test/resources/camel-routes.xml |   7 +
 .../src/test/resources/global-camel-routes.xml     |   7 +
 6 files changed, 316 insertions(+), 5 deletions(-)

diff --git 
a/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/JBPMConstants.java
 
b/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/JBPMConstants.java
index 94ff0ca..b984ebf 100644
--- 
a/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/JBPMConstants.java
+++ 
b/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/JBPMConstants.java
@@ -49,6 +49,8 @@ public interface JBPMConstants {
     String CAMEL_ENDPOINT_ID_WI_PARAM = "CamelEndpointId";
     String RESPONSE_WI_PARAM = "Response";
     String MESSAGE_WI_PARAM = "Message";
+    
+    String CAMEL_CONTEXT_BUILDER_KEY = "CamelContextBuilder";
 
     
 }
diff --git 
a/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/config/CamelContextBuilder.java
 
b/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/config/CamelContextBuilder.java
new file mode 100644
index 0000000..badac07
--- /dev/null
+++ 
b/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/config/CamelContextBuilder.java
@@ -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.component.jbpm.config;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ * Allows to specify alternative configuration for CamelContext
+ */
+public interface CamelContextBuilder {
+
+    /**
+     * Default implementation that allows to simply return 
<code>DefaultCamelContext</code> instance
+     * @return returns <code>DefaultCamelContext</code> instance
+     */
+    default CamelContext buildCamelContext() {
+        
+        return new DefaultCamelContext();
+    }
+}
diff --git 
a/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/server/CamelKieServerExtension.java
 
b/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/server/CamelKieServerExtension.java
index 1783b04..f338aed 100644
--- 
a/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/server/CamelKieServerExtension.java
+++ 
b/components/camel-jbpm/src/main/java/org/apache/camel/component/jbpm/server/CamelKieServerExtension.java
@@ -19,15 +19,21 @@ package org.apache.camel.component.jbpm.server;
 import java.io.InputStream;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.ServiceLoader;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.component.jbpm.JBPMConstants;
+import org.apache.camel.component.jbpm.config.CamelContextBuilder;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.model.FromDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RoutesDefinition;
 import org.jbpm.services.api.service.ServiceRegistry;
+import org.kie.internal.runtime.manager.InternalRuntimeManager;
+import org.kie.internal.runtime.manager.RuntimeManagerRegistry;
 import org.kie.server.services.api.KieContainerInstance;
 import org.kie.server.services.api.KieServerExtension;
 import org.kie.server.services.api.KieServerRegistry;
@@ -48,14 +54,29 @@ public class CamelKieServerExtension implements 
KieServerExtension {
     protected boolean managedCamel;
 
     protected Map<String, DefaultCamelContext> camelContexts = new HashMap<>();
+    
+    protected CamelContextBuilder camelContextBuilder;
 
     public CamelKieServerExtension() {
         this.managedCamel = true;
+        this.camelContextBuilder = discoverCamelContextBuilder();
+    }
+    
+    public CamelKieServerExtension(CamelContextBuilder camelContextBuilder) {
+        this.managedCamel = true;
+        this.camelContextBuilder = camelContextBuilder;
     }
 
     public CamelKieServerExtension(DefaultCamelContext camelContext) {
         this.camelContext = camelContext;
         this.managedCamel = false;
+        this.camelContextBuilder = discoverCamelContextBuilder();
+    }
+
+    public CamelKieServerExtension(DefaultCamelContext camelContext, 
CamelContextBuilder camelContextBuilder) {
+        this.camelContext = camelContext;
+        this.managedCamel = false;
+        this.camelContextBuilder = camelContextBuilder;
     }
 
     @Override
@@ -71,7 +92,7 @@ public class CamelKieServerExtension implements 
KieServerExtension {
     @Override
     public void init(KieServerImpl kieServer, KieServerRegistry registry) {
         if (this.managedCamel && this.camelContext == null) {
-            this.camelContext = new DefaultCamelContext();
+            this.camelContext = (DefaultCamelContext) buildGlobalContext();
             this.camelContext.setName("KIE Server Camel context");
 
             try (InputStream is = 
this.getClass().getResourceAsStream("/global-camel-routes.xml")) {
@@ -108,7 +129,7 @@ public class CamelKieServerExtension implements 
KieServerExtension {
         try (InputStream is = 
classloader.getResourceAsStream("camel-routes.xml")) {
             if (is != null) {
 
-                DefaultCamelContext context = new DefaultCamelContext();
+                DefaultCamelContext context = (DefaultCamelContext) 
buildDeploymentContext(id);
                 context.setName("KIE Server Camel context for container " + 
kieContainerInstance.getContainerId());
 
                 RoutesDefinition routes = context.loadRoutesDefinition(is);
@@ -196,6 +217,14 @@ public class CamelKieServerExtension implements 
KieServerExtension {
     public String toString() {
         return EXTENSION_NAME + " KIE Server extension";
     }
+    
+    public DefaultCamelContext getCamelContext() {
+        return camelContext;
+    }
+    
+    public CamelContextBuilder getCamelContextBuilder() {
+        return camelContextBuilder;
+    }
 
     protected void annotateKJarRoutes(RoutesDefinition routes, String 
deploymentId) {
         for (RouteDefinition route : routes.getRoutes()) {
@@ -215,10 +244,44 @@ public class CamelKieServerExtension implements 
KieServerExtension {
                     }
                     uri.append("deploymentId=").append(deploymentId);
                     from.setUri(uri.toString());
-                }
-
-                System.out.println(from.getUri());
+                }                
             }
         }
     }
+    
+    protected CamelContext buildGlobalContext() {
+        if (camelContextBuilder != null) {
+            return camelContextBuilder.buildCamelContext();
+        }
+        
+        return new CamelContextBuilder(){}.buildCamelContext();
+    }
+    
+    protected CamelContext buildDeploymentContext(String identifier) {
+        
+        InternalRuntimeManager runtimeManager = (InternalRuntimeManager) 
RuntimeManagerRegistry.get().getManager(identifier);
+        
+        if (runtimeManager != null) {
+            
+            CamelContextBuilder deploymentContextBuilder = 
(CamelContextBuilder) runtimeManager.getEnvironment()
+                                                                               
         .getEnvironment()
+                                                                               
         .get(JBPMConstants.CAMEL_CONTEXT_BUILDER_KEY);
+            if (deploymentContextBuilder != null) {
+                return deploymentContextBuilder.buildCamelContext();
+            }
+        }
+        
+        return new CamelContextBuilder(){}.buildCamelContext();
+    }
+    
+    protected CamelContextBuilder discoverCamelContextBuilder() {
+        
+        ServiceLoader<CamelContextBuilder> builders = 
ServiceLoader.load(CamelContextBuilder.class);
+        Iterator<CamelContextBuilder> it = builders.iterator();
+        if (it.hasNext()) {
+            return it.next();
+        }
+        
+        return null;
+    }
 }
diff --git 
a/components/camel-jbpm/src/test/java/org/apache/camel/component/jbpm/server/CamelKieServerExtensionTest.java
 
b/components/camel-jbpm/src/test/java/org/apache/camel/component/jbpm/server/CamelKieServerExtensionTest.java
new file mode 100644
index 0000000..fbbb7ed
--- /dev/null
+++ 
b/components/camel-jbpm/src/test/java/org/apache/camel/component/jbpm/server/CamelKieServerExtensionTest.java
@@ -0,0 +1,197 @@
+/**
+ * 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.component.jbpm.server;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.when;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.jbpm.JBPMConstants;
+import org.apache.camel.component.jbpm.config.CamelContextBuilder;
+import org.apache.camel.model.RouteDefinition;
+import org.jbpm.services.api.service.ServiceRegistry;
+import org.junit.After;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.kie.api.KieServices;
+import org.kie.api.runtime.Environment;
+import org.kie.api.runtime.KieContainer;
+import org.kie.internal.runtime.manager.InternalRuntimeManager;
+import org.kie.internal.runtime.manager.RuntimeEnvironment;
+import org.kie.internal.runtime.manager.RuntimeManagerRegistry;
+import org.kie.server.services.api.KieContainerInstance;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class CamelKieServerExtensionTest {
+    
+    private String identifier = "test";
+    
+    @Mock
+    InternalRuntimeManager runtimeManager;
+    
+    @Mock
+    RuntimeEnvironment runtimeEnvironment;
+
+    @Mock
+    private KieContainerInstance kieContainerInstance;
+    
+    @Mock
+    private KieContainer kieContainer;
+
+    @After
+    public void cleanup() {
+        RuntimeManagerRegistry.get().remove(identifier);
+    }
+    
+    
+    @Test
+    public void testInit() {
+        CamelKieServerExtension extension = new CamelKieServerExtension();
+        extension.init(null, null);
+        CamelContext globalCamelContext = (CamelContext) 
ServiceRegistry.get().service(JBPMConstants.GLOBAL_CAMEL_CONTEXT_SERVICE_KEY);
+        List<RouteDefinition> globalRestDefinitions = 
globalCamelContext.getRouteDefinitions();
+        assertThat(globalRestDefinitions.size(), equalTo(1));
+        assertThat(globalCamelContext.getRouteDefinition("unitTestRoute"), 
is(notNullValue()));
+    }
+    
+    @Test
+    public void testCreateContainer() {
+        CamelKieServerExtension extension = new CamelKieServerExtension();
+        final String containerId = "testContainer";
+        
+        when(kieContainerInstance.getKieContainer()).thenReturn(kieContainer);
+        
when(kieContainer.getClassLoader()).thenReturn(this.getClass().getClassLoader());
+        
+        extension.createContainer(containerId, kieContainerInstance, new 
HashMap<String, Object>());
+        
+        CamelContext camelContext = (CamelContext) 
ServiceRegistry.get().service("testContainer" + 
JBPMConstants.DEPLOYMENT_CAMEL_CONTEXT_SERVICE_KEY_POSTFIX);
+        List<RouteDefinition> restDefinitions = 
camelContext.getRouteDefinitions();
+        assertThat(restDefinitions.size(), equalTo(1));
+        
+        assertThat(camelContext.getRoute("unitTestRoute"), is(notNullValue()));
+    }
+    
+    @Test
+    public void testDefaultSetup() {
+        
+        CamelKieServerExtension extension = new CamelKieServerExtension();
+        
+        assertNull(extension.getCamelContextBuilder());
+    }
+    
+    @Test
+    public void testDefaultSetupCustomDiscovery() {
+        
+        CamelKieServerExtension extension = new CamelKieServerExtension() {
+
+            @Override
+            protected CamelContextBuilder discoverCamelContextBuilder() {
+                return new CamelContextBuilder() {
+
+                    @Override
+                    public CamelContext buildCamelContext() {
+                        // for test purpose return simply null as camel context
+                        return null;
+                    }
+                    
+                };
+            }
+            
+        };
+        
+        assertNotNull(extension.getCamelContextBuilder());
+        assertNull(extension.getCamelContextBuilder().buildCamelContext());
+    }
+    
+    @Test
+    public void testBuildGlobalCamelContext() throws Exception {
+        
+        CamelKieServerExtension extension = new CamelKieServerExtension();
+        CamelContext context = extension.buildGlobalContext();
+        assertNotNull(context);
+        
+        context.stop();
+    }
+    
+    @Test
+    public void testBuildGlobalCamelContextCustomBuilder() throws Exception {
+        
+        CamelKieServerExtension extension = new CamelKieServerExtension(new 
CamelContextBuilder() {
+
+            @Override
+            public CamelContext buildCamelContext() {
+                // for test purpose return simply null as camel context
+                return null;
+            }
+            
+        });
+        CamelContext context = extension.buildGlobalContext();
+        assertNull(context);        
+    }
+    
+    @Test
+    public void testBuildDeploymentCamelContext() throws Exception {
+        
+        when(runtimeManager.getIdentifier()).thenReturn(identifier);
+        when(runtimeManager.getEnvironment()).thenReturn(runtimeEnvironment);
+        
+        Environment environment = KieServices.get().newEnvironment();
+        when(runtimeEnvironment.getEnvironment()).thenReturn(environment);
+        
+        RuntimeManagerRegistry.get().register(runtimeManager);
+        
+        CamelKieServerExtension extension = new CamelKieServerExtension();
+        CamelContext context = extension.buildDeploymentContext(identifier);
+        assertNotNull(context);
+        
+        context.stop();
+    }
+    
+    @Test
+    public void testBuildDeploymentCamelContextCustomBuilder() throws 
Exception {
+        
+        when(runtimeManager.getIdentifier()).thenReturn(identifier);
+        when(runtimeManager.getEnvironment()).thenReturn(runtimeEnvironment);
+        
+        Environment environment = KieServices.get().newEnvironment();
+        environment.set(JBPMConstants.CAMEL_CONTEXT_BUILDER_KEY, new 
CamelContextBuilder() {
+
+            @Override
+            public CamelContext buildCamelContext() {
+                // for test purpose return simply null as camel context
+                return null;
+            }
+            
+        });
+        when(runtimeEnvironment.getEnvironment()).thenReturn(environment);
+        
+        RuntimeManagerRegistry.get().register(runtimeManager);
+        
+        CamelKieServerExtension extension = new CamelKieServerExtension();
+        CamelContext context = extension.buildDeploymentContext(identifier);
+        assertNull(context);
+        
+    }
+}
diff --git a/components/camel-jbpm/src/test/resources/camel-routes.xml 
b/components/camel-jbpm/src/test/resources/camel-routes.xml
new file mode 100644
index 0000000..710d40a
--- /dev/null
+++ b/components/camel-jbpm/src/test/resources/camel-routes.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<routes id="routes-c6e723e3-4841-48cd-aefe-dfd8cada3e7e" 
xmlns="http://camel.apache.org/schema/spring";>
+  <route id="unitTestRoute">
+    <from id="direct-unitTestRoute" uri="direct:unitTest" />
+    <to uri="log:org.apache.camel.com?level=WARN" />
+  </route>
+</routes>
\ No newline at end of file
diff --git a/components/camel-jbpm/src/test/resources/global-camel-routes.xml 
b/components/camel-jbpm/src/test/resources/global-camel-routes.xml
new file mode 100644
index 0000000..710d40a
--- /dev/null
+++ b/components/camel-jbpm/src/test/resources/global-camel-routes.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<routes id="routes-c6e723e3-4841-48cd-aefe-dfd8cada3e7e" 
xmlns="http://camel.apache.org/schema/spring";>
+  <route id="unitTestRoute">
+    <from id="direct-unitTestRoute" uri="direct:unitTest" />
+    <to uri="log:org.apache.camel.com?level=WARN" />
+  </route>
+</routes>
\ No newline at end of file

Reply via email to