Repository: camel Updated Branches: refs/heads/master beb451ca7 -> 281bac41e
CAMEL-10347: camel-scr todos and polish Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/281bac41 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/281bac41 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/281bac41 Branch: refs/heads/master Commit: 281bac41e9de4fdc8a7bbee9c6d9d5fe8bebbaba Parents: beb451c Author: Jyrki Ruuskanen <[email protected]> Authored: Tue Sep 27 12:32:23 2016 +0300 Committer: Claus Ibsen <[email protected]> Committed: Thu Sep 29 09:20:58 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/scr/AbstractCamelRunner.java | 69 ++++++++++---------- .../java/org/apache/camel/scr/ScrHelper.java | 2 +- .../camel/scr/AbstractCamelRunnerTest.java | 8 +++ .../apache/camel/scr/ConcreteCamelRunner.java | 20 +++--- .../org/apache/camel/scr/TestRouteBuilder.java | 7 +- .../org/apache/camel/scr/TestRouteBuilder2.java | 6 ++ tooling/archetypes/camel-archetype-scr/pom.xml | 7 ++ .../src/main/java/__className__.java | 15 ++++- .../main/java/internal/__className__Route.java | 5 ++ .../src/main/resources/default.properties | 18 ----- .../src/test/java/__className__Test.java | 12 ++-- 11 files changed, 94 insertions(+), 75 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java b/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java index 78dc460..741890f 100644 --- a/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java +++ b/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java @@ -38,7 +38,6 @@ import org.apache.camel.RoutesBuilder; import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.core.osgi.OsgiCamelContextPublisher; import org.apache.camel.core.osgi.OsgiDefaultCamelContext; -import org.apache.camel.core.osgi.OsgiServiceRegistry; import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.ExplicitCamelContextNameStrategy; @@ -54,21 +53,20 @@ public abstract class AbstractCamelRunner implements Runnable { public static final int START_DELAY = 5000; public static final String PROPERTY_PREFIX = "camel.scr.properties.prefix"; - + protected Logger log = LoggerFactory.getLogger(getClass()); - protected CamelContext context; - protected Registry registry; // Configured fields private String camelContextId; private boolean active; + private CamelContext context; private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); private ScheduledFuture starter; private volatile boolean activated; private volatile boolean started; - public synchronized void activate(final BundleContext bundleContext, final Map<String, String> props) throws Exception { + public final synchronized void activate(final BundleContext bundleContext, final Map<String, String> props) throws Exception { if (activated) { return; } @@ -81,7 +79,7 @@ public abstract class AbstractCamelRunner implements Runnable { runWithDelay(this); } - public synchronized void prepare(final BundleContext bundleContext, final Map<String, String> props) throws Exception { + public final synchronized void prepare(final BundleContext bundleContext, final Map<String, String> props) throws Exception { createCamelContext(bundleContext, props); // Configure fields from properties @@ -92,27 +90,22 @@ public abstract class AbstractCamelRunner implements Runnable { protected void createCamelContext(final BundleContext bundleContext, final Map<String, String> props) { if (bundleContext != null) { - registry = new OsgiServiceRegistry(bundleContext); - context = new OsgiDefaultCamelContext(bundleContext, registry); + context = new OsgiDefaultCamelContext(bundleContext, createRegistry(bundleContext)); // Setup the application context classloader with the bundle classloader context.setApplicationContextClassLoader(new BundleDelegatingClassLoader(bundleContext.getBundle())); // and make sure the TCCL is our classloader Thread.currentThread().setContextClassLoader(context.getApplicationContextClassLoader()); } else { - registry = new SimpleRegistry(); - context = new DefaultCamelContext(registry); + context = new DefaultCamelContext(createRegistry()); } setupPropertiesComponent(context, props, log); } - + protected void setupCamelContext(final BundleContext bundleContext, final String camelContextId) throws Exception { // Set up CamelContext if (camelContextId != null) { context.setNameStrategy(new ExplicitCamelContextNameStrategy(camelContextId)); } - // TODO: allow to configure these options and not hardcode - context.setUseMDCLogging(true); - context.setUseBreadcrumb(true); // Add routes for (RoutesBuilder route : getRouteBuilders()) { @@ -141,17 +134,11 @@ public abstract class AbstractCamelRunner implements Runnable { Properties initialProps = new Properties(); initialProps.putAll(props); log.debug(String.format("Added %d initial properties", props.size())); - try { - pc.setInitialProperties(initialProps); - } catch (NoSuchMethodError e) { - // For Camel versions without setInitialProperties - pc.setOverrideProperties(initialProps); - pc.setLocation("default.properties"); - } + pc.setInitialProperties(initialProps); } } - protected abstract List<RoutesBuilder> getRouteBuilders(); + protected abstract List<RoutesBuilder> getRouteBuilders() throws Exception; // Run after a delay unless the method is called again private void runWithDelay(final Runnable runnable) { @@ -169,11 +156,11 @@ public abstract class AbstractCamelRunner implements Runnable { } } - public synchronized void run() { + public final synchronized void run() { startCamelContext(); } - public synchronized void deactivate() { + public final synchronized void deactivate() { if (!activated) { return; } @@ -186,7 +173,7 @@ public abstract class AbstractCamelRunner implements Runnable { stop(); } - public synchronized void stop() { + public final synchronized void stop() { stopCamelContext(); } @@ -220,16 +207,16 @@ public abstract class AbstractCamelRunner implements Runnable { } } - public CamelContext getContext() { + public final CamelContext getContext() { return context; } - protected void gotCamelComponent(final ServiceReference serviceReference) { + protected final void gotCamelComponent(final ServiceReference serviceReference) { log.trace("Got a new Camel Component."); runWithDelay(this); } - protected void lostCamelComponent(final ServiceReference serviceReference) { + protected final void lostCamelComponent(final ServiceReference serviceReference) { log.trace("Lost a Camel Component."); } @@ -275,11 +262,9 @@ public abstract class AbstractCamelRunner implements Runnable { } else if (type instanceof Class) { clazz = (Class<?>) type; } - if (null != value) { - if (clazz.isInstance(value)) { - return value; - } else if (clazz == String.class) { - return value; + if (clazz != null && value != null) { + if (clazz.isAssignableFrom(value.getClass())) { + return clazz.cast(value); } else if (clazz == Boolean.class || clazz == boolean.class) { return Boolean.parseBoolean(value); } else if (clazz == Integer.class || clazz == int.class) { @@ -295,10 +280,26 @@ public abstract class AbstractCamelRunner implements Runnable { } else if (clazz == URL.class) { return new URL(value); } else { - throw new IllegalArgumentException("Unsupported type: " + (clazz != null ? clazz.getName() : null)); + throw new IllegalArgumentException("Unsupported type: " + clazz.getName()); } } else { return null; } } + + public static <T extends Registry> T getRegistry(CamelContext context, Class<T> type) throws Exception { + T result = context.getRegistry(type); + if (result == null) { + throw new Exception(type.getName() + " not available in " + context.getName()); + } + return result; + } + + protected Registry createRegistry() { + return new SimpleRegistry(); + } + + protected Registry createRegistry(BundleContext bundleContext) { + return createRegistry(); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java b/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java index fac645a..6ec6558 100644 --- a/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java +++ b/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java @@ -43,7 +43,7 @@ public final class ScrHelper { } public static Map<String, String> getScrProperties(String xmlLocation, String componentName) throws Exception { - Map<String, String> result = new HashMap<String, String>(); + Map<String, String> result = new HashMap<>(); XMLInputFactory inputFactory = XMLInputFactory.newFactory(); inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java b/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java index f8f64c7..87a6f00 100644 --- a/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java +++ b/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java @@ -127,4 +127,12 @@ public class AbstractCamelRunnerTest { fail(); } } + + @Test + public void testConversions() throws Exception { + assertEquals("test", AbstractCamelRunner.convertValue("test", String.class)); + assertEquals(true, AbstractCamelRunner.convertValue("true", boolean.class)); + assertEquals(100, AbstractCamelRunner.convertValue("100", int.class)); + assertEquals(1.1, AbstractCamelRunner.convertValue("1.1", double.class)); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java b/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java index b1a7090..9dd73b2 100644 --- a/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java +++ b/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java @@ -36,15 +36,15 @@ import org.apache.camel.spi.LifecycleStrategy; import org.apache.camel.spi.RouteContext; import org.osgi.framework.BundleContext; -public class ConcreteCamelRunner extends AbstractCamelRunner implements LifecycleStrategy { +class ConcreteCamelRunner extends AbstractCamelRunner implements LifecycleStrategy { - protected int camelContextStarted; - protected int camelContextStopped; - protected int routeAdded; + int camelContextStarted; + int camelContextStopped; + int routeAdded; - public Map<String, String> getDefaultProperties() { + Map<String, String> getDefaultProperties() { // Set default properties - Map<String, String> defaultProps = new HashMap<String, String>(); + Map<String, String> defaultProps = new HashMap<>(); defaultProps.put("camelContextId", "camel-runner"); defaultProps.put("unit.camelContextId", "camel-runner-unitTest"); defaultProps.put("camelRouteId", "test/direct-mock"); @@ -63,13 +63,13 @@ public class ConcreteCamelRunner extends AbstractCamelRunner implements Lifecycl @Override protected void createCamelContext(BundleContext bundleContext, Map<String, String> props) { super.createCamelContext(bundleContext, props); - context.disableJMX(); - context.addLifecycleStrategy(this); + getContext().disableJMX(); + getContext().addLifecycleStrategy(this); } @Override - public List<RoutesBuilder> getRouteBuilders() { - List<RoutesBuilder> routesBuilders = new ArrayList<RoutesBuilder>(); + public List<RoutesBuilder> getRouteBuilders() throws Exception { + List<RoutesBuilder> routesBuilders = new ArrayList<>(); routesBuilders.add(new TestRouteBuilder()); routesBuilders.add(new TestRouteBuilder2()); return routesBuilders; http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder.java b/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder.java index cf1ca16..c54196c 100644 --- a/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder.java +++ b/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder.java @@ -22,15 +22,10 @@ import org.apache.commons.lang.Validate; public class TestRouteBuilder extends RouteBuilder { // Configured fields - @SuppressWarnings("unused") private Integer maximumRedeliveries; - @SuppressWarnings("unused") private Long redeliveryDelay; - @SuppressWarnings("unused") private Double backOffMultiplier; - @SuppressWarnings("unused") private Long maximumRedeliveryDelay; - @SuppressWarnings("unused") private String camelRouteId; @Override @@ -49,7 +44,7 @@ public class TestRouteBuilder extends RouteBuilder { .to("{{to}}"); } - public void checkProperties() { + private void checkProperties() { Validate.notNull(maximumRedeliveries, "maximumRedeliveries property is not set"); Validate.notNull(redeliveryDelay, "redeliveryDelay property is not set"); Validate.notNull(backOffMultiplier, "backOffMultiplier property is not set"); http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder2.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder2.java b/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder2.java index 4987e7a..1be7f54 100644 --- a/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder2.java +++ b/components/camel-scr/src/test/java/org/apache/camel/scr/TestRouteBuilder2.java @@ -17,10 +17,16 @@ package org.apache.camel.scr; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.SimpleRegistry; public class TestRouteBuilder2 extends RouteBuilder { + @Override public void configure() throws Exception { + + // Add a bean to local registry + AbstractCamelRunner.getRegistry(getContext(), SimpleRegistry.class).put("testString", "this is a test"); + from("direct://start2") .routeId("Second") .log("{{messageOk}}") http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/tooling/archetypes/camel-archetype-scr/pom.xml ---------------------------------------------------------------------- diff --git a/tooling/archetypes/camel-archetype-scr/pom.xml b/tooling/archetypes/camel-archetype-scr/pom.xml index 12d13b4..bdd2db6 100644 --- a/tooling/archetypes/camel-archetype-scr/pom.xml +++ b/tooling/archetypes/camel-archetype-scr/pom.xml @@ -70,6 +70,13 @@ <configuration> <skip>${maven.test.skip.exec}</skip> </configuration> + <dependencies> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-invoker</artifactId> + <version>2.2</version> + </dependency> + </dependencies> </plugin> </plugins> </pluginManagement> http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/__className__.java ---------------------------------------------------------------------- diff --git a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/__className__.java b/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/__className__.java index c381dc0..58d2d59 100644 --- a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/__className__.java +++ b/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/__className__.java @@ -27,6 +27,7 @@ import ${package}.internal.${className}Route; import org.apache.camel.RoutesBuilder; import org.apache.camel.spi.ComponentResolver; import org.apache.felix.scr.annotations.*; +import org.osgi.framework.BundleContext; @Component(label = ${className}.COMPONENT_LABEL, description = ${className}.COMPONENT_DESCRIPTION, immediate = true, metatype = true) @Properties({ @@ -47,10 +48,20 @@ public class ${className} extends AbstractCamelRunner { public static final String COMPONENT_DESCRIPTION = "This is the description for ${artifactId}."; @Override - protected List<RoutesBuilder>getRouteBuilders() { - List<RoutesBuilder>routesBuilders = new ArrayList<>(); + protected List<RoutesBuilder> getRouteBuilders() { + List<RoutesBuilder> routesBuilders = new ArrayList<>(); routesBuilders.add(new ${className}Route()); return routesBuilders; } + @Override + protected void setupCamelContext(BundleContext bundleContext, String camelContextId)throws Exception{ + super.setupCamelContext(bundleContext, camelContextId); + + // Use MDC logging + getContext().setUseMDCLogging(true); + + // Use breadcrumb logging + getContext().setUseBreadcrumb(true); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/internal/__className__Route.java ---------------------------------------------------------------------- diff --git a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/internal/__className__Route.java b/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/internal/__className__Route.java index fce8e85..c5ff82c 100644 --- a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/internal/__className__Route.java +++ b/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/java/internal/__className__Route.java @@ -20,6 +20,8 @@ package ${package}.internal; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.SimpleRegistry; +import org.apache.camel.scr.AbstractCamelRunner; public class ${className}Route extends RouteBuilder { @@ -28,6 +30,9 @@ public class ${className}Route extends RouteBuilder { @Override public void configure() throws Exception { + // Add a bean to Camel context registry + AbstractCamelRunner.getRegistry(getContext(), SimpleRegistry.class).put("testString", "this is a test"); + from("{{from}}").routeId(camelRouteId) .to("{{to}}"); } http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/resources/default.properties ---------------------------------------------------------------------- diff --git a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/resources/default.properties b/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/resources/default.properties deleted file mode 100644 index 7056968..0000000 --- a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/main/resources/default.properties +++ /dev/null @@ -1,18 +0,0 @@ -## ------------------------------------------------------------------------ -## 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. -## ------------------------------------------------------------------------ - -dummy = dummy http://git-wip-us.apache.org/repos/asf/camel/blob/281bac41/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/test/java/__className__Test.java ---------------------------------------------------------------------- diff --git a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/test/java/__className__Test.java b/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/test/java/__className__Test.java index f3e2702..c6fee21 100644 --- a/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/test/java/__className__Test.java +++ b/tooling/archetypes/camel-archetype-scr/src/main/resources/archetype-resources/src/test/java/__className__Test.java @@ -21,12 +21,13 @@ package ${package}; import java.util.List; -import org.apache.camel.CamelContext; +import org.apache.camel.scr.AbstractCamelRunner; +import org.apache.camel.scr.ScrHelper; import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.component.mock.MockComponent; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.ModelCamelContext; import org.apache.camel.model.RouteDefinition; -import org.apache.camel.scr.ScrHelper; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -46,7 +47,7 @@ public class ${className}Test { public TestName testName = new TestName(); ${className} integration; - CamelContext context; + ModelCamelContext context; @Before public void setUp() throws Exception { @@ -60,7 +61,10 @@ public class ${className}Test { // Prepare the integration integration = new ${className}(); integration.prepare(null, ScrHelper.getScrProperties(integration.getClass().getName())); - context = integration.getContext(); + context = (ModelCamelContext) integration.getContext(); + + // Configure this + AbstractCamelRunner.configure(context, this, log); // Disable JMX for test context.disableJMX();
