Camel CDI DeltaSpike properties example
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/83d0d1b0 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/83d0d1b0 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/83d0d1b0 Branch: refs/heads/master Commit: 83d0d1b01db6a6df7953a2a14342367d0775a80c Parents: 461ecd5 Author: Antonin Stefanutti <anto...@stefanutti.fr> Authored: Thu Jan 28 15:12:20 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Jan 28 18:56:19 2016 +0100 ---------------------------------------------------------------------- examples/README.md | 1 + examples/camel-example-cdi-properties/README.md | 53 +++++++ examples/camel-example-cdi-properties/pom.xml | 140 +++++++++++++++++++ .../example/cdi/properties/Application.java | 76 ++++++++++ .../META-INF/apache-deltaspike.properties | 2 + .../src/main/resources/META-INF/beans.xml | 8 ++ .../src/main/resources/log4j.properties | 27 ++++ .../example/cdi/properties/ApplicationTest.java | 72 ++++++++++ examples/pom.xml | 1 + 9 files changed, 380 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/README.md ---------------------------------------------------------------------- diff --git a/examples/README.md b/examples/README.md index 80dca64..9eac90e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -50,6 +50,7 @@ All examples have been sort by type/category * [camel-example-cdi](camel-example-cdi/README.md) * [camel-example-metrics-cdi](camel-example-metrics-cdi/README.md) +* [camel-example-cdi-properties](camel-example-cdi-properties/README.md) * [camel-example-widget-gadget-java](camel-example-widget-gadget-cdi/README.md) ##### OSGi http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/camel-example-cdi-properties/README.md ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-properties/README.md b/examples/camel-example-cdi-properties/README.md new file mode 100644 index 0000000..fe7b498 --- /dev/null +++ b/examples/camel-example-cdi-properties/README.md @@ -0,0 +1,53 @@ +# Properties Example - CDI + +### Introduction + +This example illustrates the integration between Camel, DeltaSpike and CDI +for configuration properties. + +The example uses DeltaSpike to source configuration properties and creates +a `PropertiesComponent` bean that Camel uses to resolve property placeholders +in endpoint URIs. Besides, the application uses DeltaSpike's `@ConfigProperty` +qualifier to directly inject configuration property values. More information +can be found in [DeltaSpike configuration mechanism][] documentation. + +The example is implemented in Java with CDI dependency injection. +It uses JBoss Weld as the minimal CDI container to run the application, +though you can run the application in any CDI compliant container. + +[DeltaSpike configuration mechanism]: http://deltaspike.apache.org/documentation/configuration.html + +### Build + +You will need to build this example first: + + mvn install + +### Run + +You can run this example using: + + mvn compile camel:run + +When the Camel application runs, you should see the following messages +being logged to the console, e.g.: +``` +2016-01-28 15:02:46,223 [cdi.Main.main()] INFO CdiCamelExtension - Camel CDI is starting Camel context [hello] +2016-01-28 15:02:46,223 [cdi.Main.main()] INFO DefaultCamelContext - Apache Camel 2.17-SNAPSHOT (CamelContext: hello) is starting +... +2016-01-28 15:02:46,460 [cdi.Main.main()] INFO DefaultCamelContext - Route: route1 started and consuming from: Endpoint[direct://hello] +2016-01-28 15:02:46,461 [cdi.Main.main()] INFO DefaultCamelContext - Total 1 routes, of which 1 is started. +2016-01-28 15:02:46,461 [cdi.Main.main()] INFO DefaultCamelContext - Apache Camel 2.17-SNAPSHOT (CamelContext: hello) started in 0.238 seconds +2016-01-28 15:02:46,661 [cdi.Main.main()] INFO route1 - Hello from CamelContext (hello) +``` + +The Camel application can be stopped pressing `ctrl + c` in the shell. + +### Forum, Help, etc + +If you hit an problems please let us know on the Camel Forums + <http://camel.apache.org/discussion-forums.html> + +Please help us make Apache Camel better - we appreciate any feedback you may have. Enjoy! + +The Camel riders! http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/camel-example-cdi-properties/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-properties/pom.xml b/examples/camel-example-cdi-properties/pom.xml new file mode 100755 index 0000000..c80c179 --- /dev/null +++ b/examples/camel-example-cdi-properties/pom.xml @@ -0,0 +1,140 @@ +<?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. +--> +<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>examples</artifactId> + <version>2.17-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-cdi-properties</artifactId> + <name>Camel :: Example :: CDI :: Properties</name> + <description>DeltaSpike configuration properties CDI example</description> + <packaging>jar</packaging> + + <dependencies> + + <!-- Camel --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-cdi</artifactId> + </dependency> + + <!-- CDI API --> + <dependency> + <groupId>javax.enterprise</groupId> + <artifactId>cdi-api</artifactId> + <version>${cdi-api-1.2-version}</version> + <scope>provided</scope> + </dependency> + + <!-- DeltaSpike --> + <dependency> + <groupId>org.apache.deltaspike.core</groupId> + <artifactId>deltaspike-core-api</artifactId> + <version>${deltaspike-version}</version> + </dependency> + <dependency> + <groupId>org.apache.deltaspike.core</groupId> + <artifactId>deltaspike-core-impl</artifactId> + <version>${deltaspike-version}</version> + </dependency> + + <!-- need to use a CDI container such as JBoss Weld --> + <!-- lets use weld for CDI --> + <dependency> + <groupId>org.jboss.weld.se</groupId> + <artifactId>weld-se</artifactId> + <version>${weld2-version}</version> + </dependency> + <dependency> + <groupId>org.jboss.weld</groupId> + <artifactId>weld-core</artifactId> + <version>${weld2-version}</version> + </dependency> + <dependency> + <groupId>org.apache.deltaspike.cdictrl</groupId> + <artifactId>deltaspike-cdictrl-weld</artifactId> + <version>${deltaspike-version}</version> + <scope>runtime</scope> + </dependency> + + <!-- logging --> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + </dependency> + + <!-- test --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + <version>${hamcrest-version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.arquillian.junit</groupId> + <artifactId>arquillian-junit-container</artifactId> + <version>${arquillian-version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.shrinkwrap.descriptors</groupId> + <artifactId>shrinkwrap-descriptors-depchain</artifactId> + <version>${shrinkwrap-descriptors-version}</version> + <type>pom</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.arquillian.container</groupId> + <artifactId>arquillian-weld-se-embedded-1.1</artifactId> + <version>${arquillian-weld-se-embedded-version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + + <plugin> + <groupId>org.apache.camel</groupId> + <artifactId>camel-maven-plugin</artifactId> + <version>${project.version}</version> + </plugin> + + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/camel-example-cdi-properties/src/main/java/org/apache/camel/example/cdi/properties/Application.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-properties/src/main/java/org/apache/camel/example/cdi/properties/Application.java b/examples/camel-example-cdi-properties/src/main/java/org/apache/camel/example/cdi/properties/Application.java new file mode 100644 index 0000000..8113bc7 --- /dev/null +++ b/examples/camel-example-cdi-properties/src/main/java/org/apache/camel/example/cdi/properties/Application.java @@ -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.camel.example.cdi.properties; + +import java.util.Properties; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Produces; +import javax.inject.Named; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.ContextName; +import org.apache.camel.cdi.Uri; +import org.apache.camel.component.properties.DefaultPropertiesParser; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.component.properties.PropertiesParser; +import org.apache.camel.management.event.CamelContextStartedEvent; +import org.apache.deltaspike.core.api.config.ConfigProperty; +import org.apache.deltaspike.core.api.config.ConfigResolver; + +public class Application { + + @ContextName("hello") + static class HelloRoute extends RouteBuilder { + + @Override + public void configure() { + // Property placeholders in endpoint URIs are resolved + // based on configuration properties + from("{{destination}}").log("${body} from CamelContext (${camelContext.name})"); + } + } + + void hello(@Observes CamelContextStartedEvent event, + // Configuration properties can be injected with @ConfigProperty + @ConfigProperty(name = "message") String message, + // Property placeholders in @Uri qualifier are also resolved + @Uri("{{destination}}") ProducerTemplate producer) { + producer.sendBody(message); + } + + @Produces + @ApplicationScoped + @Named("properties") + // "properties" component bean that Camel uses to lookup properties + PropertiesComponent properties(PropertiesParser parser) { + PropertiesComponent component = new PropertiesComponent(); + // Use DeltaSpike as configuration source for Camel CDI + component.setPropertiesParser(parser); + return component; + } + + // PropertiesParser bean that uses DeltaSpike to resolve properties + static class DeltaSpikeParser extends DefaultPropertiesParser { + + @Override + public String parseProperty(String key, String value, Properties properties) { + return ConfigResolver.getPropertyValue(key); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/camel-example-cdi-properties/src/main/resources/META-INF/apache-deltaspike.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-properties/src/main/resources/META-INF/apache-deltaspike.properties b/examples/camel-example-cdi-properties/src/main/resources/META-INF/apache-deltaspike.properties new file mode 100644 index 0000000..358e7d4 --- /dev/null +++ b/examples/camel-example-cdi-properties/src/main/resources/META-INF/apache-deltaspike.properties @@ -0,0 +1,2 @@ +destination=direct:hello +message=Hello \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/camel-example-cdi-properties/src/main/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-properties/src/main/resources/META-INF/beans.xml b/examples/camel-example-cdi-properties/src/main/resources/META-INF/beans.xml new file mode 100755 index 0000000..e81becb --- /dev/null +++ b/examples/camel-example-cdi-properties/src/main/resources/META-INF/beans.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee + http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" + version="1.1" bean-discovery-mode="all"> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/camel-example-cdi-properties/src/main/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-properties/src/main/resources/log4j.properties b/examples/camel-example-cdi-properties/src/main/resources/log4j.properties new file mode 100755 index 0000000..f5aae04 --- /dev/null +++ b/examples/camel-example-cdi-properties/src/main/resources/log4j.properties @@ -0,0 +1,27 @@ +## ------------------------------------------------------------------------ +## 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. +## ------------------------------------------------------------------------ + +# default properties to initialise log4j +log4j.rootLogger=INFO, console + +# settings for specific packages +#log4j.logger.org.apache.camel.cdi=DEBUG + +# Console appender +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/camel-example-cdi-properties/src/test/java/org/apache/camel/example/cdi/properties/ApplicationTest.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-properties/src/test/java/org/apache/camel/example/cdi/properties/ApplicationTest.java b/examples/camel-example-cdi-properties/src/test/java/org/apache/camel/example/cdi/properties/ApplicationTest.java new file mode 100644 index 0000000..c092a43 --- /dev/null +++ b/examples/camel-example-cdi-properties/src/test/java/org/apache/camel/example/cdi/properties/ApplicationTest.java @@ -0,0 +1,72 @@ +/** + * 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.example.cdi.properties; + +import javax.enterprise.event.Observes; + +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.management.event.CamelContextStartingEvent; +import org.apache.camel.model.ModelCamelContext; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; + +@RunWith(Arquillian.class) +public class ApplicationTest { + + @Deployment + public static Archive deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // DeltaSpike + .addPackages(true, "org.apache.deltaspike.core.impl") + // Test classes + .addPackage(Application.class.getPackage()) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + static void advice(@Observes CamelContextStartingEvent event, ModelCamelContext context) throws Exception { + // Add a mock endpoint to the end of the route + context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() { + weaveAddLast().to("mock:outbound"); + } + }); + } + + @Test + public void testMessage(@Uri("mock:outbound") MockEndpoint outbound) { + assertThat("Exchange count is incorrect!", outbound.getExchanges(), hasSize(1)); + assertThat("Exchange body is incorrect!", outbound.getExchanges().get(0).getIn().getBody(String.class), is(equalTo("Hello"))); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/83d0d1b0/examples/pom.xml ---------------------------------------------------------------------- diff --git a/examples/pom.xml b/examples/pom.xml index e9ce100..6669ec3 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -37,6 +37,7 @@ <module>camel-example-box-osgi</module> <module>camel-example-cafe</module> <module>camel-example-cdi</module> + <module>camel-example-cdi-properties</module> <module>camel-example-cxf</module> <module>camel-example-cxf-blueprint</module> <module>camel-example-cxf-osgi</module>