Author: hadrian Date: Tue Apr 24 02:30:55 2012 New Revision: 1329532 URL: http://svn.apache.org/viewvc?rev=1329532&view=rev Log: CAMEL-5145. Patches applied with thanks to Lukasz (files missed in previous commit)
Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java?rev=1329532&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java (added) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java Tue Apr 24 02:30:55 2012 @@ -0,0 +1,57 @@ +/** + * 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; + +import org.apache.camel.IsSingleton; +import org.apache.camel.spi.Injector; +import org.apache.deltaspike.core.api.provider.BeanProvider; + +/** + * Injector implementation which performs injection with CDI bean provider. + */ +public class CdiInjector implements Injector { + + /** + * Fallback injector used when there is bean of given type registered in CDI. + */ + private Injector injector; + + public CdiInjector(Injector parent) { + this.injector = parent; + } + + @Override + public <T> T newInstance(Class<T> type) { + T bean = BeanProvider.getContextualReference(type, true); + if (bean != null) { + return type.cast(bean); + } + return injector.newInstance(type); + } + + @Override + public <T> T newInstance(Class<T> type, Object instance) { + if (instance instanceof IsSingleton) { + boolean singleton = ((IsSingleton) instance).isSingleton(); + if (singleton) { + return type.cast(instance); + } + } + return newInstance(type); + } + +} Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java?rev=1329532&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java Tue Apr 24 02:30:55 2012 @@ -0,0 +1,54 @@ +/** + * 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.CamelContext; +import org.apache.camel.component.cdi.CdiCamelContext; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.deltaspike.cdise.api.CdiContainer; +import org.apache.deltaspike.cdise.api.CdiContainerLoader; +import org.junit.After; +import org.junit.Before; + +/** + * Base class for cdi tests. + */ +public abstract class CamelCdiTestContainer extends CamelTestSupport { + + private CdiContainer cdiContainer; + + @Before + public void setUp() throws Exception { + // set up CDI container before camel context start up + cdiContainer = CdiContainerLoader.getCdiContainer(); + cdiContainer.boot(); + + super.setUp(); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + context.stop(); + cdiContainer.shutdown(); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + return new CdiCamelContext(); + } +} \ No newline at end of file Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java?rev=1329532&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java Tue Apr 24 02:30:55 2012 @@ -0,0 +1,94 @@ +/** + * 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 java.util.ArrayList; +import java.util.List; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.store.Item; +import org.apache.camel.cdi.store.ShoppingBean; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +public class RegistryLookupAndInjectorTest extends CamelCdiTestContainer { + + private MockEndpoint resultEndpoint; + + @Override + public void setUp() throws Exception { + super.setUp(); + + resultEndpoint = getMockEndpoint("mock:result"); + } + + @Test + public void shouldLookupBeanByName() throws InterruptedException { + resultEndpoint.expectedMessageCount(1); + template.sendBody("direct:injectByName", "hello"); + + assertMockEndpointsSatisfied(); + + Exchange exchange = resultEndpoint.getExchanges().get(0); + List<?> results = exchange.getIn().getBody(List.class); + List<Item> expected = itemsExpected(); + assertNotNull(results); + assertNotNull(expected); + assertEquals(expected.size(), results.size()); + assertEquals(expected, results); + } + + @Test + public void shouldLookupBeanByTypeAndInjectFields() throws InterruptedException { + resultEndpoint.expectedMessageCount(1); + template.sendBody("direct:injectByType", "hello"); + + assertMockEndpointsSatisfied(); + + Exchange exchange = resultEndpoint.getExchanges().get(0); + List<?> results = exchange.getIn().getBody(List.class); + List<Item> expected = itemsExpected(); + assertNotNull(results); + assertNotNull(expected); + assertEquals(expected.size(), results.size()); + assertEquals(expected, results); + } + + private List<Item> itemsExpected() { + List<Item> products = new ArrayList<Item>(); + for (int i = 1; i < 10; i++) { + products.add(new Item("Item-" + i, 1500L * i)); + } + return products; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:injectByName") + .beanRef("shoppingBean", "listAllProducts") + .to("mock:result"); + from("direct:injectByType") + .bean(ShoppingBean.class, "listAllProducts") + .to("mock:result"); + } + }; + } +}