Author: hadrian Date: Wed Jul 18 02:44:36 2012 New Revision: 1362749 URL: http://svn.apache.org/viewvc?rev=1362749&view=rev Log: CAMEL-3448. Support for CamelContextAware in CDI. Thanks Lukasz for patch
Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/InjectLiteral.java (with props) camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelContextAwareTest.java (with props) camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ContextAwareBean.java (with props) Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java?rev=1362749&r1=1362748&r2=1362749&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java Wed Jul 18 02:44:36 2012 @@ -16,17 +16,22 @@ */ package org.apache.camel.component.cdi.internal; +import java.lang.reflect.Method; + import javax.enterprise.event.Observes; import javax.enterprise.inject.spi.AfterBeanDiscovery; import javax.enterprise.inject.spi.AfterDeploymentValidation; +import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.BeforeShutdown; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.ProcessAnnotatedType; import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; import org.apache.camel.component.cdi.CdiCamelContext; import org.apache.deltaspike.core.api.provider.BeanProvider; +import org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder; /** * Set of camel specific hooks for CDI. @@ -39,6 +44,24 @@ public class CamelExtension implements E private CamelContext camelContext; /** + * Process camel context aware bean definitions. + * + * @param process Annotated type. + * @throws Exception In case of exceptions. + */ + protected void contextAwareness(@Observes ProcessAnnotatedType<CamelContextAware> process) throws Exception { + AnnotatedType<CamelContextAware> annotatedType = process.getAnnotatedType(); + Class<CamelContextAware> javaClass = annotatedType.getJavaClass(); + if (CamelContextAware.class.isAssignableFrom(javaClass)) { + Method method = javaClass.getMethod("setCamelContext", CamelContext.class); + AnnotatedTypeBuilder<CamelContextAware> builder = new AnnotatedTypeBuilder<CamelContextAware>() + .readFromType(javaClass) + .addToMethod(method, new InjectLiteral()); + process.setAnnotatedType(builder.create()); + } + } + + /** * Disable creation of default CamelContext bean and rely on context created * and managed by extension. * Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/InjectLiteral.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/InjectLiteral.java?rev=1362749&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/InjectLiteral.java (added) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/InjectLiteral.java Wed Jul 18 02:44:36 2012 @@ -0,0 +1,28 @@ +/** + * 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 javax.enterprise.util.AnnotationLiteral; +import javax.inject.Inject; + +/** + * Literal used to mark method as injectable. + */ +@SuppressWarnings("all") +class InjectLiteral extends AnnotationLiteral<Inject> implements Inject { + +} \ No newline at end of file Propchange: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/InjectLiteral.java ------------------------------------------------------------------------------ svn:eol-style = native Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelContextAwareTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelContextAwareTest.java?rev=1362749&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelContextAwareTest.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelContextAwareTest.java Wed Jul 18 02:44:36 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 javax.inject.Inject; + +import org.apache.camel.CamelContext; +import org.apache.camel.cdi.support.ContextAwareBean; +import org.junit.Test; + +/** + * Test context awareness. + */ +public class CamelContextAwareTest extends CdiContextTestSupport { + + @Inject + private ContextAwareBean bean; + + @Inject + private CamelContext context; + + @Test + public void shouldInjectCamelContextToBean() throws InterruptedException { + assertNotNull(bean.getCamelContext()); + assertSame(context, bean.getCamelContext()); + } + +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelContextAwareTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ContextAwareBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ContextAwareBean.java?rev=1362749&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ContextAwareBean.java (added) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ContextAwareBean.java Wed Jul 18 02:44:36 2012 @@ -0,0 +1,39 @@ +/** + * 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.CamelContext; +import org.apache.camel.CamelContextAware; + +/** + * Simple POJO interested in getting camel context. + */ +public class ContextAwareBean implements CamelContextAware { + + private CamelContext camelContext; + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + +} Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/ContextAwareBean.java ------------------------------------------------------------------------------ svn:eol-style = native