Author: jstrachan Date: Mon Sep 3 15:45:30 2012 New Revision: 1380275 URL: http://svn.apache.org/viewvc?rev=1380275&view=rev Log: @Mock and @Uri are now qualifiers. Added an example of Endpoint injection via @Named. Also simplified the use of @Inject with camel injection; it now requires a qualifier if using @Inject to inject Endpoint/ProducerTemplate of either @Mock, @Uri, @Named - otherwise don't use @Inject and just use the vanilla Camel injection annotations (@Produce or @EndpointInject). The use of qualifiers avoids all kinds of CDI issues when trying to mix and match @Inject @EndpointInject and @Inject @Named in the same application; for Endpoint/ProducerTemplate injection there must be a unique qualifier used now. Note @Consume doesn't do injection per se so @Inject is never used directly with it. Finally @Mock() now takes an optional URI for the MockEndpoint
Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointNamedInjectTest.java (with props) camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/NamedEndpointBean.java (with props) Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Mock.java camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Uri.java camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointPropertyInjectTest.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointUriInjectTest.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriInjectedBean.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriPropertyInjectedBean.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Mock.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Mock.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Mock.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Mock.java Mon Sep 3 15:45:30 2012 @@ -20,6 +20,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import javax.enterprise.util.Nonbinding; import javax.inject.Qualifier; /** @@ -29,4 +30,10 @@ import javax.inject.Qualifier; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) public @interface Mock { + + /** + * Returns an optional URI used to create the MockEndpoint + */ + @Nonbinding + String value() default ""; } Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Uri.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Uri.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Uri.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/cdi/Uri.java Mon Sep 3 15:45:30 2012 @@ -20,6 +20,8 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; /** * An injection annotation to define the <a href="http://camel.apache.org/uris.html">Camel URI</a> used @@ -37,6 +39,7 @@ import java.lang.annotation.Target; * } * </code> */ +@Qualifier @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) public @interface Uri { @@ -44,5 +47,6 @@ public @interface Uri { /** * Returns the <a href="http://camel.apache.org/uris.html">Camel URI</a> of the endpoint */ + @Nonbinding String value(); } Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java Mon Sep 3 15:45:30 2012 @@ -18,7 +18,6 @@ package org.apache.camel.component.cdi.i import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.Annotated; -import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject; @@ -26,15 +25,12 @@ import javax.inject.Inject; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.EndpointInject; -import org.apache.camel.Produce; import org.apache.camel.ProducerTemplate; import org.apache.camel.cdi.Mock; import org.apache.camel.cdi.Uri; import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.impl.CamelPostProcessorHelper; import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.ObjectHelper; -import org.apache.deltaspike.core.api.provider.BeanProvider; /** * produces {@link Endpoint} and {@link org.apache.camel.ProducerTemplate} instances for injection into beans @@ -46,80 +42,34 @@ public class CamelFactory { @Produces @Mock public MockEndpoint createMockEndpoint(InjectionPoint point) { - String uri = ""; - String ref = ""; - EndpointInject annotation = point.getAnnotated().getAnnotation(EndpointInject.class); - if (annotation != null) { - uri = annotation.uri(); - ref = annotation.ref(); - } - if (ObjectHelper.isEmpty(ref)) { - ref = point.getMember().getName(); - } + Mock annotation = point.getAnnotated().getAnnotation(Mock.class); + ObjectHelper.notNull(annotation, "Should be annotated with @Mock"); + String uri = annotation.value(); if (ObjectHelper.isEmpty(uri)) { - uri = "mock:" + ref; + uri = "mock:" + point.getMember().getName(); } return CamelContextHelper.getMandatoryEndpoint(camelContext, uri, MockEndpoint.class); } @SuppressWarnings("unchecked") @Produces + @Uri("") public Endpoint createEndpoint(InjectionPoint point, BeanManager beanManager) { Annotated annotated = point.getAnnotated(); Uri uri = annotated.getAnnotation(Uri.class); - if (uri != null) { - return CamelContextHelper.getMandatoryEndpoint(camelContext, uri.value()); - } - EndpointInject annotation = annotated.getAnnotation(EndpointInject.class); - ObjectHelper.notNull(annotation, "Must be annotated with @EndpointInject"); - return getEndpoint(point, annotation.uri(), annotation.ref(), annotation.property()); + ObjectHelper.notNull(uri, "Should be annotated with @Uri"); + return CamelContextHelper.getMandatoryEndpoint(camelContext, uri.value()); } @Produces + @Uri("") public ProducerTemplate createProducerTemplate(InjectionPoint point) { ProducerTemplate producerTemplate = camelContext.createProducerTemplate(); Annotated annotated = point.getAnnotated(); Uri uri = annotated.getAnnotation(Uri.class); - Endpoint endpoint = null; - if (uri != null) { - endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri.value()); - } else { - Produce annotation = annotated.getAnnotation(Produce.class); - if (annotation != null) { - endpoint = getEndpoint(point, annotation.uri(), annotation.ref(), annotation.property()); - } - } - if (endpoint != null) { - producerTemplate.setDefaultEndpoint(endpoint); - } + ObjectHelper.notNull(uri, "Should be annotated with @Uri"); + Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri.value()); + producerTemplate.setDefaultEndpoint(endpoint); return producerTemplate; } - - protected Endpoint getEndpoint(InjectionPoint point, String uri, String ref, String property) { - String injectName = getInjectionPointName(point); - if (ObjectHelper.isEmpty(property)) { - return resolveEndpoint(uri, ref, injectName); - } else { - throw new UnsupportedOperationException(); -/* - TODO this code won't work in CDI as we've not yet created the bean being injected yet - so cannot evaluate the property yet! - - CamelPostProcessorHelper helper = new CamelPostProcessorHelper(camelContext); - Bean<?> bean = point.getBean(); - Class<?> beanClass = bean.getBeanClass(); - Object instance = BeanProvider.getContextualReference((Class)beanClass, bean); - return helper.getEndpointInjection(instance, uri, ref, property, injectName, true); -*/ - } - } - - private Endpoint resolveEndpoint(String uri, String ref, String injectionName) { - return CamelContextHelper.getEndpointInjection(camelContext, uri, ref, injectionName, true); - } - - private String getInjectionPointName(InjectionPoint point) { - // TODO is there a better name? - return point.toString(); - } } Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java Mon Sep 3 15:45:30 2012 @@ -32,7 +32,7 @@ public class ConsumeTest extends CdiTest @Inject @Mock private MockEndpoint result; - @Inject @Produce(uri = "seda:start") + @Inject @Uri("seda:start") private ProducerTemplate producer; @Consume(uri = "seda:start") Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java Mon Sep 3 15:45:30 2012 @@ -37,6 +37,7 @@ public class EndpointInjectTest extends Endpoint endpoint = bean.getEndpoint(); assertNotNull("Could not find injected endpoint!", endpoint); assertTrue("Endpoint should be a MockEndpoint but was " + endpoint, endpoint instanceof MockEndpoint); + assertEquals("Endpoint URI", "mock://blah", endpoint.getEndpointUri()); } } Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointNamedInjectTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointNamedInjectTest.java?rev=1380275&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointNamedInjectTest.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointNamedInjectTest.java Mon Sep 3 15:45:30 2012 @@ -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.cdi; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.apache.camel.Endpoint; +import org.apache.camel.cdi.support.EndpointInjectedBean; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +/** + * Test endpoint injection by @Named + */ +public class EndpointNamedInjectTest extends CdiTestSupport { + + @Inject @Named("myNamedEndpoint") + private Endpoint endpoint; + + @Test + public void shouldInjectEndpoint() { + assertNotNull("Could not find injected endpoint!", endpoint); + assertEquals("Endpoint URI", "mock:nameInjected", endpoint.getEndpointUri()); + } + +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointNamedInjectTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointPropertyInjectTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointPropertyInjectTest.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointPropertyInjectTest.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointPropertyInjectTest.java Mon Sep 3 15:45:30 2012 @@ -32,8 +32,7 @@ public class EndpointPropertyInjectTest @Inject private EndpointUriPropertyInjectedBean bean; - // TODO not supported yet! - @Ignore + @Test public void shouldInjectEndpointByProperty() { assertNotNull(bean); Endpoint endpoint = bean.getEndpoint(); Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointUriInjectTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointUriInjectTest.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointUriInjectTest.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointUriInjectTest.java Mon Sep 3 15:45:30 2012 @@ -39,6 +39,10 @@ public class EndpointUriInjectTest exten assertNotNull("Could not find injected endpoint!", endpoint); assertTrue("Endpoint should be a MockEndpoint but was " + endpoint, endpoint instanceof MockEndpoint); assertEquals("Endpoint URI", "mock://uriInjected", endpoint.getEndpointUri()); + + Endpoint endpoint2 = bean.getEndpoint2(); + assertNotNull("Could not find injected endpoint2!", endpoint2); + assertEquals("Endpoint URI", "mock://anotherEndpoint", endpoint2.getEndpointUri()); } } Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java Mon Sep 3 15:45:30 2012 @@ -25,7 +25,6 @@ import org.apache.camel.EndpointInject; */ public class EndpointInjectedBean { - @Inject @EndpointInject(uri = "mock:blah") private Endpoint endpoint; Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriInjectedBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriInjectedBean.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriInjectedBean.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriInjectedBean.java Mon Sep 3 15:45:30 2012 @@ -29,6 +29,9 @@ public class EndpointUriInjectedBean { @Inject @Uri("mock:uriInjected") private Endpoint endpoint; + @Inject @Uri("mock:anotherEndpoint") + private Endpoint endpoint2; + public Endpoint getEndpoint() { return endpoint; } @@ -36,4 +39,12 @@ public class EndpointUriInjectedBean { public void setEndpoint(Endpoint endpoint) { this.endpoint = endpoint; } + + public Endpoint getEndpoint2() { + return endpoint2; + } + + public void setEndpoint2(Endpoint endpoint2) { + this.endpoint2 = endpoint2; + } } Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriPropertyInjectedBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriPropertyInjectedBean.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriPropertyInjectedBean.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointUriPropertyInjectedBean.java Mon Sep 3 15:45:30 2012 @@ -26,7 +26,7 @@ import org.apache.camel.cdi.Uri; */ public class EndpointUriPropertyInjectedBean { - @Inject @EndpointInject(property = "injectUri") + @EndpointInject(property = "injectUri") private Endpoint endpoint; public Endpoint getEndpoint() { Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java?rev=1380275&r1=1380274&r2=1380275&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java Mon Sep 3 15:45:30 2012 @@ -24,27 +24,10 @@ import org.apache.camel.component.mock.M public class MockEndpointInjectedBean { - /* - - TODO - cannot currently figure out how to be able to inject both Endpoint and MockEndpoint - using a @Produces plugin with a single method without using explicit qualifier annotations - to separate the two scenarios which is a bit ugly. - - See discussion here: - https://issues.apache.org/jira/browse/CAMEL-5553 - - Ideally it would be nice to be able to do this: - - @Inject - @EndpointInject(uri = "mock:blah") - private MockEndpoint endpoint; - - */ - @Inject @Mock private MockEndpoint foo; - @Inject @Mock @EndpointInject(uri = "mock:something") + @Inject @Mock("mock:something") private MockEndpoint bar; public MockEndpoint getBar() { Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/NamedEndpointBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/NamedEndpointBean.java?rev=1380275&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/NamedEndpointBean.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/NamedEndpointBean.java Mon Sep 3 15:45:30 2012 @@ -0,0 +1,34 @@ +/** + * + * 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.cdi.support; + +import javax.enterprise.inject.Produces; +import javax.inject.Named; + +import org.apache.camel.Endpoint; +import org.apache.camel.component.mock.MockComponent; +import org.apache.camel.component.mock.MockEndpoint; + +/** + */ +public class NamedEndpointBean { + @Produces @Named("myNamedEndpoint") + public Endpoint createEndpoint() { + return new MockEndpoint("mock:nameInjected", new MockComponent()); + } +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/NamedEndpointBean.java ------------------------------------------------------------------------------ svn:eol-style = native