Author: jstrachan Date: Fri Aug 31 13:02:01 2012 New Revision: 1379437 URL: http://svn.apache.org/viewvc?rev=1379437&view=rev Log: initial spike of CAMEL-5553 support for injecting Endpoint instances using @EndpointInject annotation; though there are issues (e.g. no way to inject both Endpoint and MockEndpoint types explicitly without some kind of explicit qualifier for MockEndpoint injection points or something
Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java (with props) camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java (with props) camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java (with props) camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java (with props) camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java (with props) Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java?rev=1379437&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java (added) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java Fri Aug 31 13:02:01 2012 @@ -0,0 +1,75 @@ +/** + * + * 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.component.cdi.internal; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.EndpointInject; +import org.apache.camel.util.ObjectHelper; + +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.inject.Inject; +import java.lang.reflect.Type; + +/** + * Injects endpoints into beans + */ +public class EndpointInjector { + @Inject + private CamelContext camelContext; + + +/* + @Produces + public Endpoint createEndpoint(InjectionPoint point) { + return createEndpoint(point, Endpoint.class); + } + + // Note that there does not appear to be a way in CDI to say we can inject + // all types from Endpoint onwards so lets make it easy to also inject mock endpoints too + @Produces + protected MockEndpoint createMockEndpoint(InjectionPoint point) { + return createEndpoint(point, MockEndpoint.class); + } +*/ + + @Produces + public Endpoint createEndpoint(InjectionPoint point) { + Class<? extends Endpoint> endpointType = Endpoint.class; + Type pointType = point.getType(); + if (pointType instanceof Class<?>) { + endpointType = (Class<? extends Endpoint>) pointType; + } + + EndpointInject annotation = point.getAnnotated().getAnnotation(EndpointInject.class); + if (annotation != null) { + if (annotation != null) { + String uri = annotation.uri(); + if (ObjectHelper.isNotEmpty(uri)) { + return camelContext.getEndpoint(uri, endpointType); + } + String ref = annotation.ref(); + if (ObjectHelper.isNotEmpty(ref)) { + return camelContext.getEndpoint("ref:" + ref, endpointType); + } + } + } + return null; + } +} Propchange: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java ------------------------------------------------------------------------------ svn:eol-style = native Added: 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=1379437&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java Fri Aug 31 13:02:01 2012 @@ -0,0 +1,42 @@ +/** + * 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 org.apache.camel.Endpoint; +import org.apache.camel.cdi.support.EndpointInjectedBean; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +import javax.inject.Inject; + +/** + * Test endpoint injection + */ +public class EndpointInjectTest extends CdiTestSupport { + + @Inject + private EndpointInjectedBean bean; + + @Test + public void shouldInjectEndpoint() { + assertNotNull(bean); + Endpoint endpoint = bean.getEndpoint(); + assertNotNull("Could not find injected endpoint!", endpoint); + assertTrue("Endpoint should be a MockEndpoint but was " + endpoint, endpoint instanceof MockEndpoint); + } + +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/EndpointInjectTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java?rev=1379437&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java Fri Aug 31 13:02:01 2012 @@ -0,0 +1,47 @@ +/** + * 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 org.apache.camel.cdi.support.EndpointInjectedBean; +import org.apache.camel.cdi.support.MockEndpointInjectedBean; +import org.junit.Ignore; +import org.junit.Test; + +import javax.inject.Inject; + +/** + * Test endpoint injection + */ +public class MockEndpointInjectTest extends CdiTestSupport { + + + @Inject + private MockEndpointInjectedBean bean; + + @Ignore + public void shouldInjectMockEndpoint() { + assertNotNull(bean); + /* + TODO + assertNotNull("Could not find injected endpoint!", bean.getEndpoint()); + + cannot currently figure out how to be able to inject both Endpoint and MockEndpoint using a @Produces + plugin without using explicit qualifier annotations to separate the two scenarios which is a bit ugly + */ + } + +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: 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=1379437&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java Fri Aug 31 13:02:01 2012 @@ -0,0 +1,40 @@ +/** + * + * 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 org.apache.camel.Endpoint; +import org.apache.camel.EndpointInject; + +import javax.inject.Inject; + +/** + */ +public class EndpointInjectedBean { + + @Inject + @EndpointInject(uri = "mock:blah") + private Endpoint endpoint; + + public Endpoint getEndpoint() { + return endpoint; + } + + public void setEndpoint(Endpoint endpoint) { + this.endpoint = endpoint; + } +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/EndpointInjectedBean.java ------------------------------------------------------------------------------ svn:eol-style = native Added: 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=1379437&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java Fri Aug 31 13:02:01 2012 @@ -0,0 +1,43 @@ +/** + * + * 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 org.apache.camel.EndpointInject; +import org.apache.camel.component.mock.MockEndpoint; + +import javax.inject.Inject; + +public class MockEndpointInjectedBean { + + /* + + TODO disabled - see the TODO in MockEndpointInjectTest.java + + @Inject + @EndpointInject(uri = "mock:blah") + private MockEndpoint endpoint; + + public MockEndpoint getEndpoint() { + return endpoint; + } + + public void setEndpoint(MockEndpoint endpoint) { + this.endpoint = endpoint; + } + */ +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java ------------------------------------------------------------------------------ svn:eol-style = native