This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new bfe9fc4 CAMEL-13243: Main class should be more easier to use, so lets drop the confusing multiple CamelContext stuff that was not really intended/working. bfe9fc4 is described below commit bfe9fc4ab9a187c674db2e7f5a0972c6b7eb52e7 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Feb 28 10:28:01 2019 +0100 CAMEL-13243: Main class should be more easier to use, so lets drop the confusing multiple CamelContext stuff that was not really intended/working. --- MIGRATION.md | 4 + .../src/main/java/org/apache/camel/cdi/Main.java | 29 +- .../boot/CamelSpringBootApplicationController.java | 6 +- .../main/java/org/apache/camel/spring/Main.java | 40 ++- .../java/org/apache/camel/spring/MainTest.java | 5 +- components/camel-test-blueprint/pom.xml | 333 +++++++++++---------- .../java/org/apache/camel/test/blueprint/Main.java | 41 ++- .../apache/camel/test/blueprint/MyMainAppTest.java | 15 +- .../src/main/java/org/apache/camel/main/Main.java | 43 +-- .../java/org/apache/camel/main/MainSupport.java | 24 +- .../org/apache/camel/impl/MainSupportTest.java | 6 +- .../test/java/org/apache/camel/main/MainTest.java | 24 +- 12 files changed, 259 insertions(+), 311 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 0c1bff5..001c310 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -126,6 +126,10 @@ In Camel 2.x we have deprecated `getProperties` on `CamelContext` in favour of ` </globalOptions> +### Main class + +The `Main` class from `camel-core`, `camel-spring` and `camel-cdi` has been modified to only support a single `CamelContext` which was really its intention, but there was some old crufy code for multiple Camels. The method `getCamelContextMap` has been removed, and there is just a `getCamelContext` method now. + ### Moved APIs The following API changes may affect your existing Camel applications, which needs to be migrated. diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/Main.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/Main.java index aeabac3..8b14ebc 100644 --- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/Main.java +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/Main.java @@ -18,11 +18,6 @@ package org.apache.camel.cdi; import java.util.Map; import java.util.Set; - -import static java.util.function.Function.identity; -import static java.util.stream.Collectors.toMap; - -import javax.enterprise.inject.UnsatisfiedResolutionException; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; @@ -33,9 +28,10 @@ import org.apache.deltaspike.cdise.api.CdiContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.toMap; import static org.apache.camel.cdi.AnyLiteral.ANY; import static org.apache.camel.cdi.BeanManagerHelper.getReference; -import static org.apache.camel.cdi.BeanManagerHelper.getReferenceByType; import static org.apache.deltaspike.cdise.api.CdiContainerLoader.getCdiContainer; /** @@ -75,19 +71,24 @@ public class Main extends MainSupport { @Override protected ProducerTemplate findOrCreateCamelTemplate() { - return getReferenceByType(cdiContainer.getBeanManager(), CamelContext.class) - .orElseThrow( - () -> new UnsatisfiedResolutionException("No default Camel context is deployed, " - + "cannot create default ProducerTemplate!")) - .createProducerTemplate(); + if (getCamelContext() == null) { + throw new IllegalArgumentException("No CamelContext are available so cannot create a ProducerTemplate!"); + } + return getCamelContext().createProducerTemplate(); } @Override - protected Map<String, CamelContext> getCamelContextMap() { + protected CamelContext createCamelContext() { BeanManager manager = cdiContainer.getBeanManager(); - return manager.getBeans(CamelContext.class, ANY).stream() + Map<String, CamelContext> camels = manager.getBeans(CamelContext.class, ANY).stream() .map(bean -> getReference(manager, CamelContext.class, bean)) .collect(toMap(CamelContext::getName, identity())); + if (camels.size() > 1) { + throw new IllegalArgumentException("Multiple CamelContext detected. This Main class only supports single CamelContext"); + } else if (camels.size() == 1) { + return camels.values().iterator().next(); + } + return null; } @Override @@ -98,7 +99,7 @@ public class Main extends MainSupport { container.getContextControl().startContexts(); cdiContainer = container; super.doStart(); - postProcessContext(); + initCamelContext(); warnIfNoCamelFound(); } diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java index f4d71ac..9d51657 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationController.java @@ -16,8 +16,6 @@ */ package org.apache.camel.spring.boot; -import java.util.Collections; -import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.PreDestroy; @@ -45,8 +43,8 @@ public class CamelSpringBootApplicationController { } @Override - protected Map<String, CamelContext> getCamelContextMap() { - return Collections.singletonMap("camelContext", camelContext); + protected CamelContext createCamelContext() { + return camelContext; } @Override diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java index de747a0..b0a1631 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java @@ -22,7 +22,6 @@ import java.io.InputStreamReader; import java.net.URL; import java.nio.charset.Charset; import java.util.Enumeration; -import java.util.HashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Map; @@ -145,6 +144,17 @@ public class Main extends org.apache.camel.main.MainSupport { // ------------------------------------------------------------------------- @Override + protected CamelContext createCamelContext() { + Map<String, SpringCamelContext> camels = applicationContext.getBeansOfType(SpringCamelContext.class); + if (camels.size() > 1) { + throw new IllegalArgumentException("Multiple CamelContext detected. This Main class only supports single CamelContext"); + } else if (camels.size() == 1) { + return camels.values().iterator().next(); + } + return null; + } + + @Override protected void doStart() throws Exception { try { super.doStart(); @@ -164,13 +174,11 @@ public class Main extends org.apache.camel.main.MainSupport { LOG.debug("Starting Spring ApplicationContext: {}", applicationContext.getId()); applicationContext.start(); - postProcessContext(); + initCamelContext(); } finally { - if (camelContexts != null && !camelContexts.isEmpty()) { - // if we were veto started then mark as completed - if (getCamelContexts().get(0).isVetoStarted()) { - completed(); - } + // if we were veto started then mark as completed + if (getCamelContext() != null && getCamelContext().isVetoStarted()) { + completed(); } } } @@ -192,10 +200,10 @@ public class Main extends org.apache.camel.main.MainSupport { if (names != null && names.length > 0) { return getApplicationContext().getBean(names[0], ProducerTemplate.class); } - if (getCamelContexts().isEmpty()) { - throw new IllegalArgumentException("No CamelContexts are available so cannot create a ProducerTemplate!"); + if (getCamelContext() == null) { + throw new IllegalArgumentException("No CamelContext are available so cannot create a ProducerTemplate!"); } - return getCamelContexts().get(0).createProducerTemplate(); + return getCamelContext().createProducerTemplate(); } protected AbstractApplicationContext createDefaultApplicationContext() throws IOException { @@ -221,18 +229,6 @@ public class Main extends org.apache.camel.main.MainSupport { } } - protected Map<String, CamelContext> getCamelContextMap() { - Map<String, SpringCamelContext> map = applicationContext.getBeansOfType(SpringCamelContext.class); - Set<Map.Entry<String, SpringCamelContext>> entries = map.entrySet(); - Map<String, CamelContext> answer = new HashMap<>(); - for (Map.Entry<String, SpringCamelContext> entry : entries) { - String name = entry.getKey(); - CamelContext camelContext = entry.getValue(); - answer.put(name, camelContext); - } - return answer; - } - protected AbstractApplicationContext createAdditionalLocationsFromClasspath() throws IOException { Set<String> locations = new LinkedHashSet<>(); findLocations(locations, Main.class.getClassLoader()); diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java index 25bbe3b..429dbb1 100644 --- a/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/MainTest.java @@ -43,10 +43,7 @@ public class MainTest extends Assert { }); main.start(); - List<CamelContext> contextList = main.getCamelContexts(); - assertNotNull(contextList); - assertEquals("size", 1, contextList.size()); - CamelContext camelContext = contextList.get(0); + CamelContext camelContext = main.getCamelContext(); MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class); // in case we add more files in src/test/data diff --git a/components/camel-test-blueprint/pom.xml b/components/camel-test-blueprint/pom.xml index 249ffa7..ecdec58 100644 --- a/components/camel-test-blueprint/pom.xml +++ b/components/camel-test-blueprint/pom.xml @@ -17,179 +17,180 @@ limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.camel</groupId> - <artifactId>components</artifactId> - <version>3.0.0-SNAPSHOT</version> - </parent> + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>3.0.0-SNAPSHOT</version> + </parent> - <artifactId>camel-test-blueprint</artifactId> - <packaging>jar</packaging> - <name>Camel :: Test :: Blueprint</name> - <description>Camel unit testing with OSGi Blueprint</description> + <artifactId>camel-test-blueprint</artifactId> + <packaging>jar</packaging> + <name>Camel :: Test :: Blueprint</name> + <description>Camel unit testing with OSGi Blueprint</description> - <properties> - <firstVersion>2.10.0</firstVersion> - <label>testing,java,osgi</label> + <properties> + <firstVersion>2.10.0</firstVersion> + <label>testing,java,osgi</label> - </properties> + </properties> - <dependencies> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-test</artifactId> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-core</artifactId> - <type>test-jar</type> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-blueprint</artifactId> - <!-- exclude aries blueprint as we add these dependencies explicit --> - <exclusions> - <exclusion> - <groupId>org.apache.aries.blueprint</groupId> - <artifactId>org.apache.aries.blueprint.core</artifactId> - </exclusion> - </exclusions> - </dependency> + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-blueprint</artifactId> + <!-- exclude aries blueprint as we add these dependencies explicit --> + <exclusions> + <exclusion> + <groupId>org.apache.aries.blueprint</groupId> + <artifactId>org.apache.aries.blueprint.core</artifactId> + </exclusion> + </exclusions> + </dependency> - <!-- the ordering of the dependencies can matter as we load the dependencies from the classpath - with felix-connect, and you may get a weird error if wrong order --> - <dependency> - <groupId>org.apache.aries.proxy</groupId> - <artifactId>org.apache.aries.proxy</artifactId> - <version>${aries-blueprint-proxy-version}</version> - </dependency> - <dependency> - <groupId>org.apache.aries.blueprint</groupId> - <artifactId>org.apache.aries.blueprint.api</artifactId> - </dependency> - <dependency> - <groupId>org.apache.aries.blueprint</groupId> - <artifactId>org.apache.aries.blueprint.core</artifactId> - </dependency> - <dependency> - <groupId>org.apache.aries.blueprint</groupId> - <artifactId>org.apache.aries.blueprint.cm</artifactId> - </dependency> - <dependency> - <groupId>org.apache.aries</groupId> - <artifactId>org.apache.aries.util</artifactId> - <version>${aries-util-version}</version> - </dependency> - <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.connect</artifactId> - <version>${felix-connect-version}</version> - <exclusions> - <!-- felix-connect includes osgi.core and osgi.compendium version 5.0.0 --> - <exclusion> - <groupId>org.osgi</groupId> - <artifactId>osgi.core</artifactId> - </exclusion> - <exclusion> - <groupId>org.osgi</groupId> - <artifactId>osgi.cmpn</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.ops4j.pax.swissbox</groupId> - <artifactId>pax-swissbox-tinybundles</artifactId> - </dependency> - <dependency> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - </dependency> - <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.configadmin</artifactId> - <!-- exclude the following dependency which otherwise would pop up a lot of compilation - errors both by this and the camel-maven-plugin modules under eclipse. --> - <exclusions> - <exclusion> - <groupId>org.apache.felix</groupId> - <artifactId>org.osgi.foundation</artifactId> - </exclusion> - <exclusion> - <groupId>org.apache.felix</groupId> - <artifactId>osgi.core</artifactId> - </exclusion> - <exclusion> - <groupId>org.apache.felix</groupId> - <artifactId>osgi.cmpn</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.fileinstall</artifactId> - <version>${felix-fileinstall-version}</version> - <!-- exclude the following dependency which otherwise would pop up a lot of compilation - errors both by this and the camel-maven-plugin modules under eclipse. --> - <exclusions> - <exclusion> - <groupId>org.apache.felix</groupId> - <artifactId>org.osgi.foundation</artifactId> - </exclusion> - <exclusion> - <groupId>org.apache.felix</groupId> - <artifactId>osgi.core</artifactId> - </exclusion> - <exclusion> - <groupId>org.apache.felix</groupId> - <artifactId>osgi.cmpn</artifactId> - </exclusion> - </exclusions> - </dependency> + <!-- the ordering of the dependencies can matter as we load the dependencies from the classpath + with felix-connect, and you may get a weird error if wrong order --> + <dependency> + <groupId>org.apache.aries.proxy</groupId> + <artifactId>org.apache.aries.proxy</artifactId> + <version>${aries-blueprint-proxy-version}</version> + </dependency> + <dependency> + <groupId>org.apache.aries.blueprint</groupId> + <artifactId>org.apache.aries.blueprint.api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.aries.blueprint</groupId> + <artifactId>org.apache.aries.blueprint.core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.aries.blueprint</groupId> + <artifactId>org.apache.aries.blueprint.cm</artifactId> + </dependency> + <dependency> + <groupId>org.apache.aries</groupId> + <artifactId>org.apache.aries.util</artifactId> + <version>${aries-util-version}</version> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.connect</artifactId> + <version>${felix-connect-version}</version> + <exclusions> + <!-- felix-connect includes osgi.core and osgi.compendium version 5.0.0 --> + <exclusion> + <groupId>org.osgi</groupId> + <artifactId>osgi.core</artifactId> + </exclusion> + <exclusion> + <groupId>org.osgi</groupId> + <artifactId>osgi.cmpn</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.ops4j.pax.swissbox</groupId> + <artifactId>pax-swissbox-tinybundles</artifactId> + </dependency> + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.configadmin</artifactId> + <!-- exclude the following dependency which otherwise would pop up a lot of compilation + errors both by this and the camel-maven-plugin modules under eclipse. --> + <exclusions> + <exclusion> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.foundation</artifactId> + </exclusion> + <exclusion> + <groupId>org.apache.felix</groupId> + <artifactId>osgi.core</artifactId> + </exclusion> + <exclusion> + <groupId>org.apache.felix</groupId> + <artifactId>osgi.cmpn</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.fileinstall</artifactId> + <version>${felix-fileinstall-version}</version> + <!-- exclude the following dependency which otherwise would pop up a lot of compilation + errors both by this and the camel-maven-plugin modules under eclipse. --> + <exclusions> + <exclusion> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.foundation</artifactId> + </exclusion> + <exclusion> + <groupId>org.apache.felix</groupId> + <artifactId>osgi.core</artifactId> + </exclusion> + <exclusion> + <groupId>org.apache.felix</groupId> + <artifactId>osgi.cmpn</artifactId> + </exclusion> + </exclusions> + </dependency> - <!-- optional dependencies for running tests --> - - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-api</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-core</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-slf4j-impl</artifactId> - <scope>test</scope> - </dependency> + <!-- optional dependencies for running tests --> - </dependencies> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <scope>test</scope> + </dependency> - <profiles> - <profile> - <id>jdk9+-build</id> - <activation> - <jdk>[9,)</jdk> - </activation> - <build> - <plugins> - <plugin> - <artifactId>maven-surefire-plugin</artifactId> - <configuration> - <argLine>--add-modules java.xml.bind,java.xml.ws --add-opens java.base/java.lang=ALL-UNNAMED</argLine> - </configuration> - </plugin> - </plugins> - </build> - </profile> - </profiles> + </dependencies> + + <profiles> + <profile> + <id>jdk9+-build</id> + <activation> + <jdk>[9,)</jdk> + </activation> + <build> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <argLine>--add-modules java.xml.bind,java.xml.ws --add-opens java.base/java.lang=ALL-UNNAMED</argLine> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> </project> diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java index 7a190ea..a837083 100644 --- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java +++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java @@ -16,9 +16,8 @@ */ package org.apache.camel.test.blueprint; -import java.util.HashMap; import java.util.LinkedList; -import java.util.Map; + import org.apache.camel.CamelContext; import org.apache.camel.ProducerTemplate; import org.apache.camel.main.MainSupport; @@ -32,7 +31,6 @@ public class Main extends MainSupport { protected static Main instance; private BundleContext bundleContext; private String descriptors = "OSGI-INF/blueprint/*.xml"; - private CamelContext camelContext; private String bundleName = "MyBundle"; private boolean includeSelfAsBundle; private String configAdminPid; @@ -88,8 +86,12 @@ public class Main extends MainSupport { } @Override + protected CamelContext createCamelContext() { + return CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class); + } + + @Override protected void doStart() throws Exception { - super.doStart(); if (bundleContext == null) { String descriptors = getDescriptors(); if (descriptors == null) { @@ -102,10 +104,14 @@ public class Main extends MainSupport { } else { bundleContext = createBundleContext(bundleName); } - - camelContext = CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class); - if (camelContext == null) { - throw new IllegalArgumentException("Cannot find CamelContext in blueprint XML file: " + descriptors); + } + try { + super.doStart(); + initCamelContext(); + } finally { + // if we were veto started then mark as completed + if (getCamelContext() != null && getCamelContext().isVetoStarted()) { + completed(); } } } @@ -113,11 +119,11 @@ public class Main extends MainSupport { @Override protected void doStop() throws Exception { // stop camel context - if (camelContext != null) { - camelContext.stop(); + if (getCamelContext() != null) { + getCamelContext().stop(); } // and then stop blueprint - LOG.debug("Stopping Blueprint XML file: " + descriptors); + LOG.debug("Stopping Blueprint XML file: {}", descriptors); CamelBlueprintHelper.disposeBundleContext(bundleContext); // call completed to properly stop as we count down the waiting latch completed(); @@ -125,8 +131,8 @@ public class Main extends MainSupport { @Override protected ProducerTemplate findOrCreateCamelTemplate() { - if (camelContext != null) { - return camelContext.createProducerTemplate(); + if (getCamelContext() != null) { + return getCamelContext().createProducerTemplate(); } else { return null; } @@ -146,15 +152,6 @@ public class Main extends MainSupport { loader, configAdminPidFiles); } - @Override - protected Map<String, CamelContext> getCamelContextMap() { - Map<String, CamelContext> map = new HashMap<>(1); - if (camelContext != null) { - map.put(camelContext.getName(), camelContext); - } - return map; - } - public String getDescriptors() { return descriptors; } diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java index 3576ae0..8624575 100644 --- a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java @@ -16,15 +16,10 @@ */ package org.apache.camel.test.blueprint; - - -import java.util.Map; - import org.apache.camel.CamelContext; import org.junit.Test; -import static org.junit.Assert.assertTrue; - +import static org.junit.Assert.assertNotNull; public class MyMainAppTest { @@ -38,15 +33,11 @@ public class MyMainAppTest { Main main = new Main(); run(main); - Map<String, CamelContext> contexts = main.getCamelContextMap(); - // we should get at least one CamelContext - assertTrue("We should get at least one camelcontext", contexts.size() >= 1); - - + CamelContext camelContext = main.getCamelContext(); + assertNotNull(camelContext); } public void run(Main main) throws Exception { - main.setBundleName("MyMainBundle"); // as we run this test without packing ourselves as bundle, then include ourselves main.setIncludeSelfAsBundle(true); diff --git a/core/camel-core/src/main/java/org/apache/camel/main/Main.java b/core/camel-core/src/main/java/org/apache/camel/main/Main.java index 5c03beb..184be9c 100644 --- a/core/camel-core/src/main/java/org/apache/camel/main/Main.java +++ b/core/camel-core/src/main/java/org/apache/camel/main/Main.java @@ -16,7 +16,6 @@ */ package org.apache.camel.main; -import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; @@ -96,35 +95,19 @@ public class Main extends MainSupport { return registry.findByTypeWithName(type); } - /** - * Gets or creates the {@link org.apache.camel.CamelContext} this main class is using. - * - * It just create a new CamelContextMap per call, please don't use it to access the camel context that will be ran by main. - * If you want to setup the CamelContext please use MainListener to get the new created camel context. - */ - public CamelContext getOrCreateCamelContext() { - // force init - Map<String, CamelContext> map = getCamelContextMap(); - if (map.size() >= 1) { - return map.values().iterator().next(); - } else { - throw new IllegalStateException("Error creating CamelContext"); - } - } - // Implementation methods // ------------------------------------------------------------------------- @Override protected void doStart() throws Exception { super.doStart(); - postProcessContext(); - if (getCamelContexts().size() > 0) { + initCamelContext(); + if (getCamelContext() != null) { try { - getCamelContexts().get(0).start(); // if we were veto started then mark as completed + getCamelContext().start(); } finally { - if (getCamelContexts().get(0).isVetoStarted()) { + if (getCamelContext().isVetoStarted()) { completed(); } } @@ -133,28 +116,20 @@ public class Main extends MainSupport { protected void doStop() throws Exception { super.doStop(); - if (getCamelContexts().size() > 0) { - getCamelContexts().get(0).stop(); + if (getCamelContext() != null) { + getCamelContext().stop(); } } protected ProducerTemplate findOrCreateCamelTemplate() { - if (getCamelContexts().size() > 0) { - return getCamelContexts().get(0).createProducerTemplate(); + if (getCamelContext() != null) { + return getCamelContext().createProducerTemplate(); } else { return null; } } - protected Map<String, CamelContext> getCamelContextMap() { - Map<String, CamelContext> answer = new HashMap<>(); - - CamelContext camelContext = createContext(); - answer.put("camel-1", camelContext); - return answer; - } - - protected CamelContext createContext() { + protected CamelContext createCamelContext() { return new DefaultCamelContext(registry); } diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java index 32a9306..0ff50ac 100644 --- a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java +++ b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java @@ -20,8 +20,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -61,11 +59,12 @@ public abstract class MainSupport extends ServiceSupport { protected int durationMaxMessages; protected TimeUnit timeUnit = TimeUnit.SECONDS; protected boolean trace; + + protected CamelContext camelContext; protected List<RouteBuilder> routeBuilders = new ArrayList<>(); protected String routeBuilderClasses; protected String fileWatchDirectory; protected boolean fileWatchDirectoryRecursively; - protected final List<CamelContext> camelContexts = new ArrayList<>(); protected ProducerTemplate camelTemplate; protected boolean hangupInterceptorEnabled = true; protected int durationHitExitCode = DEFAULT_EXIT_CODE; @@ -520,8 +519,8 @@ public abstract class MainSupport extends ServiceSupport { System.out.println(); } - public List<CamelContext> getCamelContexts() { - return camelContexts; + public CamelContext getCamelContext() { + return camelContext; } public List<RouteBuilder> getRouteBuilders() { @@ -534,7 +533,7 @@ public abstract class MainSupport extends ServiceSupport { public List<RouteDefinition> getRouteDefinitions() { List<RouteDefinition> answer = new ArrayList<>(); - for (CamelContext camelContext : camelContexts) { + if (camelContext != null) { answer.addAll(camelContext.adapt(ModelCamelContext.class).getRouteDefinitions()); } return answer; @@ -549,16 +548,11 @@ public abstract class MainSupport extends ServiceSupport { protected abstract ProducerTemplate findOrCreateCamelTemplate(); - protected abstract Map<String, CamelContext> getCamelContextMap(); + protected abstract CamelContext createCamelContext(); - protected void postProcessContext() throws Exception { - Map<String, CamelContext> map = getCamelContextMap(); - Set<Map.Entry<String, CamelContext>> entries = map.entrySet(); - for (Map.Entry<String, CamelContext> entry : entries) { - CamelContext camelContext = entry.getValue(); - camelContexts.add(camelContext); - postProcessCamelContext(camelContext); - } + protected void initCamelContext() throws Exception { + camelContext = createCamelContext(); + postProcessCamelContext(camelContext); } public ModelJAXBContextFactory getModelJAXBContextFactory() { diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java index 4bc3644..c6a6e2c 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/MainSupportTest.java @@ -16,8 +16,6 @@ */ package org.apache.camel.impl; -import java.util.Map; - import org.apache.camel.CamelContext; import org.apache.camel.ContextTestSupport; import org.apache.camel.ProducerTemplate; @@ -28,11 +26,13 @@ public class MainSupportTest extends ContextTestSupport { private class MyMainSupport extends MainSupport { + @Override protected ProducerTemplate findOrCreateCamelTemplate() { return context.createProducerTemplate(); } - protected Map<String, CamelContext> getCamelContextMap() { + @Override + protected CamelContext createCamelContext() { return null; } } diff --git a/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java b/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java index 4fad442..799914a 100644 --- a/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java @@ -32,13 +32,12 @@ public class MainTest extends Assert { Main main = new Main(); main.addRouteBuilder(new MyRouteBuilder()); main.enableTrace(); - main.bind("foo", new Integer(31)); + main.bind("foo", 31); main.start(); - List<CamelContext> contextList = main.getCamelContexts(); - assertNotNull(contextList); - assertEquals("Did not get the expected count of Camel contexts", 1, contextList.size()); - CamelContext camelContext = contextList.get(0); + CamelContext camelContext = main.getCamelContext(); + + assertNotNull(camelContext); assertEquals("Could not find the registry bound object", 31, camelContext.getRegistry().lookupByName("foo")); MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class); @@ -58,13 +57,11 @@ public class MainTest extends Assert { main.addRouteBuilder(new MyRouteBuilder()); main.disableHangupSupport(); main.enableTrace(); - main.bind("foo", new Integer(31)); + main.bind("foo", 31); main.start(); - List<CamelContext> contextList = main.getCamelContexts(); - assertNotNull(contextList); - assertEquals("Did not get the expected count of Camel contexts", 1, contextList.size()); - CamelContext camelContext = contextList.get(0); + CamelContext camelContext = main.getCamelContext(); + assertEquals("Could not find the registry bound object", 31, camelContext.getRegistry().lookupByName("foo")); MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class); @@ -84,11 +81,8 @@ public class MainTest extends Assert { main.parseArguments(new String[]{"-r", "org.apache.camel.main.MainTest$MyRouteBuilder"}); main.start(); - List<CamelContext> contextList = main.getCamelContexts(); - assertNotNull(contextList); - assertEquals("Did not get the expected count of Camel contexts", 1, contextList.size()); - CamelContext camelContext = contextList.get(0); - + CamelContext camelContext = main.getCamelContext(); + MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class); endpoint.expectedMinimumMessageCount(1);