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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new a9c3dba  CAMEL-16983: camel-spring-main - Add option to allow multiple 
camelContext when migrating from legacy Spring XML files
a9c3dba is described below

commit a9c3dba8cd60091f99b1ea8414c5e47c4fc2600a
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Sep 18 10:01:54 2021 +0200

    CAMEL-16983: camel-spring-main - Add option to allow multiple camelContext 
when migrating from legacy Spring XML files
---
 .../main/java/org/apache/camel/spring/Main.java    | 22 ++++++++-
 .../camel/spring/MultipleCamelContextTest.java     | 41 +++++++++++++++++
 .../camel/spring/MultipleCamelContextTest.xml      | 53 ++++++++++++++++++++++
 3 files changed, 114 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-spring-main/src/main/java/org/apache/camel/spring/Main.java 
b/components/camel-spring-main/src/main/java/org/apache/camel/spring/Main.java
index aa386c7..5aa82e8 100644
--- 
a/components/camel-spring-main/src/main/java/org/apache/camel/spring/Main.java
+++ 
b/components/camel-spring-main/src/main/java/org/apache/camel/spring/Main.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.Enumeration;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
@@ -54,7 +55,7 @@ public class Main extends MainCommandLineSupport {
 
     public static final String LOCATION_PROPERTIES = 
"META-INF/camel-spring/location.properties";
     protected static Main instance;
-    private static final Charset UTF8 = Charset.forName("UTF-8");
+    private static final Charset UTF8 = StandardCharsets.UTF_8;
 
     private String applicationContextUri = "META-INF/spring/*.xml";
     private String fileApplicationContextUri;
@@ -62,6 +63,7 @@ public class Main extends MainCommandLineSupport {
     private AbstractApplicationContext parentApplicationContext;
     private AbstractApplicationContext additionalApplicationContext;
     private String parentApplicationContextUri;
+    private boolean allowMultipleCamelContexts;
 
     public Main() {
     }
@@ -152,6 +154,18 @@ public class Main extends MainCommandLineSupport {
         this.parentApplicationContextUri = parentApplicationContextUri;
     }
 
+    public boolean isAllowMultipleCamelContexts() {
+        return allowMultipleCamelContexts;
+    }
+
+    /**
+     * Enable this to allow multiple CamelContexts to be loaded by this Main 
class.
+     * By default only a single CamelContext is allowed.
+     */
+    public void setAllowMultipleCamelContexts(boolean 
allowMultipleCamelContexts) {
+        this.allowMultipleCamelContexts = allowMultipleCamelContexts;
+    }
+
     // Implementation methods
     // 
-------------------------------------------------------------------------
 
@@ -159,8 +173,12 @@ public class Main extends MainCommandLineSupport {
     protected CamelContext createCamelContext() {
         Map<String, SpringCamelContext> camels = 
applicationContext.getBeansOfType(SpringCamelContext.class);
         if (camels.size() > 1) {
+            if (isAllowMultipleCamelContexts()) {
+                // just grab the first
+                return camels.values().iterator().next();
+            }
             throw new IllegalArgumentException(
-                    "Multiple CamelContext detected. This Main class only 
supports single CamelContext");
+                    "Multiple CamelContext detected. Set 
allowMultipleCamelContexts=true to allow multiple CamelContexts");
         } else if (camels.size() == 1) {
             return camels.values().iterator().next();
         }
diff --git 
a/components/camel-spring-main/src/test/java/org/apache/camel/spring/MultipleCamelContextTest.java
 
b/components/camel-spring-main/src/test/java/org/apache/camel/spring/MultipleCamelContextTest.java
new file mode 100644
index 0000000..b3456f7
--- /dev/null
+++ 
b/components/camel-spring-main/src/test/java/org/apache/camel/spring/MultipleCamelContextTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.spring;
+
+import org.apache.camel.CamelContext;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class MultipleCamelContextTest {
+
+    @Test
+    public void testMain() throws Exception {
+        Main main = new Main();
+        main.setAllowMultipleCamelContexts(true);
+        
main.setApplicationContextUri("classpath:org/apache/camel/spring/MultipleCamelContextTest.xml");
+        main.start();
+
+        CamelContext camel1 = main.getApplicationContext().getBean("camel1", 
CamelContext.class);
+        CamelContext camel2 = main.getApplicationContext().getBean("camel2", 
CamelContext.class);
+
+        Assertions.assertNotSame(camel1, camel2);
+        Assertions.assertEquals(2, camel1.getRoutesSize());
+        Assertions.assertEquals(3, camel2.getRoutesSize());
+
+        main.stop();
+    }
+}
diff --git 
a/components/camel-spring-main/src/test/resources/org/apache/camel/spring/MultipleCamelContextTest.xml
 
b/components/camel-spring-main/src/test/resources/org/apache/camel/spring/MultipleCamelContextTest.xml
new file mode 100644
index 0000000..847a6b1
--- /dev/null
+++ 
b/components/camel-spring-main/src/test/resources/org/apache/camel/spring/MultipleCamelContextTest.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <camelContext id="camel1" xmlns="http://camel.apache.org/schema/spring";>
+    <route>
+      <from uri="direct:foo"/>
+      <to uri="mock:result"/>
+    </route>
+    <route>
+      <from uri="direct:foo2"/>
+      <to uri="mock:result"/>
+    </route>
+  </camelContext>
+
+  <camelContext id="camel2" xmlns="http://camel.apache.org/schema/spring";>
+    <route>
+      <from uri="direct:bar"/>
+      <to uri="mock:result"/>
+    </route>
+    <route>
+      <from uri="direct:bar2"/>
+      <to uri="mock:result"/>
+    </route>
+    <route>
+      <from uri="direct:bar3"/>
+      <to uri="mock:result"/>
+    </route>
+  </camelContext>
+
+</beans>

Reply via email to