Skip to content

Commit

Permalink
Support CDI Camel context customization
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Aug 28, 2014
1 parent db5caac commit 120cd46
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/astefanutti/camel/cdi/AnyLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import javax.enterprise.inject.Any;
import javax.enterprise.util.AnnotationLiteral;

public final class AnyLiteral extends AnnotationLiteral<Any> implements Any {
final class AnyLiteral extends AnnotationLiteral<Any> implements Any {

private static final long serialVersionUID = 1L;

public static final Any INSTANCE = new AnyLiteral();
static final Any INSTANCE = new AnyLiteral();

private AnyLiteral() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import java.util.Set;

@Vetoed
final class BeanManagerUtil {

private BeanManagerUtil() {
}
final class BeanManagerHelper {

@SuppressWarnings("unchecked")
static <T> Set<T> getContextualReferences(BeanManager beanManager, Class<T> type, Annotation... qualifiers) {
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/io/astefanutti/camel/cdi/CdiBeanRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,32 @@ final class CdiBeanRegistry implements Registry {
@Override
public Object lookupByName(String name) {
ObjectHelper.notEmpty(name, "name");
return BeanManagerUtil.getContextualReference(beanManager, name, true, Object.class);
return BeanManagerHelper.getContextualReference(beanManager, name, true, Object.class);
}

@Override
public <T> T lookupByNameAndType(String name, Class<T> type) {
ObjectHelper.notEmpty(name, "name");
ObjectHelper.notNull(type, "type");
return BeanManagerUtil.getContextualReference(beanManager, name, true, type);
return BeanManagerHelper.getContextualReference(beanManager, name, true, type);
}

@Override
public <T> Map<String, T> findByTypeWithName(Class<T> type) {
ObjectHelper.notNull(type, "type");
Map<String, T> references = new HashMap<>();
Set<Bean<?>> beans = beanManager.getBeans(type, AnyLiteral.INSTANCE);

if (beans == null)
return references;

for (Bean<?> bean : beans)
for (Bean<?> bean : beanManager.getBeans(type, AnyLiteral.INSTANCE))
// FIXME: check if the bean has the @Named qualifier instead
if (bean.getName() != null)
references.put(bean.getName(), BeanManagerUtil.getContextualReference(beanManager, type, bean));
references.put(bean.getName(), BeanManagerHelper.getContextualReference(beanManager, type, bean));

return references;
}

@Override
public <T> Set<T> findByType(Class<T> type) {
ObjectHelper.notNull(type, "type");
return BeanManagerUtil.getContextualReferences(beanManager, type, AnyLiteral.INSTANCE);
return BeanManagerHelper.getContextualReferences(beanManager, type, AnyLiteral.INSTANCE);
}

@Override
Expand Down
37 changes: 6 additions & 31 deletions src/main/java/io/astefanutti/camel/cdi/CdiCamelContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,21 @@
*/
package io.astefanutti.camel.cdi;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.util.ObjectHelper;

import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Typed;
import javax.enterprise.inject.Vetoed;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

@ApplicationScoped
@Typed({CamelContext.class, ModelCamelContext.class})
class CdiCamelContext extends DefaultCamelContext {
@Vetoed
public class CdiCamelContext extends DefaultCamelContext {

CdiCamelContext() {
protected CdiCamelContext() {
}

@Inject
private CdiCamelContext(BeanManager beanManager) {
super(new CdiBeanRegistry(beanManager));
protected CdiCamelContext(BeanManager beanManager) {
setRegistry(new CdiBeanRegistry(beanManager));
setInjector(new CdiInjector(getInjector(), beanManager));
}

@Override
public void start() {
try {
super.start();
} catch (Exception cause) {
throw ObjectHelper.wrapRuntimeCamelException(cause);
}
}

@Override
@PreDestroy
public void stop() {
try {
super.stop();
} catch (Exception cause) {
throw ObjectHelper.wrapRuntimeCamelException(cause);
}
}
}
114 changes: 114 additions & 0 deletions src/main/java/io/astefanutti/camel/cdi/CdiCamelContextBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright (C) 2014 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed 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 io.astefanutti.camel.cdi;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.PassivationCapable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

final class CdiCamelContextBean implements Bean<CdiCamelContext>, PassivationCapable {

private final Set<Type> types;

private final InjectionTarget<CdiCamelContext> target;

CdiCamelContextBean(BeanManager manager) {
AnnotatedType<CdiCamelContext> annotatedType = manager.createAnnotatedType(CdiCamelContext.class);
this.types = annotatedType.getTypeClosure();
this.target = manager.createInjectionTarget(annotatedType);
}

@Override
public Class<? extends Annotation> getScope() {
return ApplicationScoped.class;
}

@Override
public Set<Annotation> getQualifiers() {
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(DefaultLiteral.INSTANCE, AnyLiteral.INSTANCE)));
}

@Override
public CdiCamelContext create(CreationalContext<CdiCamelContext> creational) {
CdiCamelContext context = target.produce(creational);
target.postConstruct(context);
creational.push(context);
return context;
}

@Override
public void destroy(CdiCamelContext instance, CreationalContext<CdiCamelContext> creational) {
target.preDestroy(instance);
target.dispose(instance);
creational.release();
}

@Override
public Class<CdiCamelContext> getBeanClass() {
return CdiCamelContext.class;
}

@Override
public Set<InjectionPoint> getInjectionPoints() {
return Collections.emptySet();
}

@Override
public String getName() {
return "cdi-camel-context";
}

@Override
public String toString() {
return "Default CdiCamelContext Bean";
}

@Override
public Set<Class<? extends Annotation>> getStereotypes() {
return Collections.emptySet();
}

@Override
public Set<Type> getTypes() {
return Collections.unmodifiableSet(types);
}

@Override
public boolean isAlternative() {
return false;
}

@Override
public boolean isNullable() {
return false;
}

@Override
public String getId() {
return getClass().getName();
}
}
10 changes: 8 additions & 2 deletions src/main/java/io/astefanutti/camel/cdi/CdiCamelExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.camel.RoutesBuilder;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
Expand All @@ -37,8 +38,13 @@ void processTypeConverters(@Observes @WithAnnotations(Converter.class) ProcessAn
typeConverters.add(event.getAnnotatedType().getJavaClass());
}

private void addDefaultCamelContext(@Observes AfterBeanDiscovery abd, BeanManager manager) {
if (manager.getBeans(CamelContext.class, AnyLiteral.INSTANCE, DefaultLiteral.INSTANCE).isEmpty())
abd.addBean(new CdiCamelContextBean(manager));
}

void configureCamelContext(@Observes AfterDeploymentValidation event, BeanManager manager) {
CamelContext context = BeanManagerUtil.getContextualReference(manager, CamelContext.class, false);
CamelContext context = BeanManagerHelper.getContextualReference(manager, CamelContext.class, false);

// add type converter beans to the Camel context
if (!typeConverters.isEmpty()) {
Expand All @@ -48,7 +54,7 @@ void configureCamelContext(@Observes AfterDeploymentValidation event, BeanManage
}

// instantiate route builders and add them to the Camel context
for (RoutesBuilder builder : BeanManagerUtil.getContextualReferences(manager, RoutesBuilder.class)) {
for (RoutesBuilder builder : BeanManagerHelper.getContextualReferences(manager, RoutesBuilder.class)) {
try {
context.addRoutes(builder);
} catch (Exception exception) {
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/io/astefanutti/camel/cdi/CdiInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.astefanutti.camel.cdi;

import org.apache.camel.IsSingleton;
import org.apache.camel.spi.Injector;

import javax.enterprise.inject.spi.BeanManager;
Expand All @@ -33,7 +32,7 @@ final class CdiInjector implements Injector {

@Override
public <T> T newInstance(Class<T> type) {
T bean = BeanManagerUtil.getContextualReference(manager, type, true);
T bean = BeanManagerHelper.getContextualReference(manager, type, true);
if (bean != null)
return type.cast(bean);

Expand All @@ -42,12 +41,6 @@ public <T> T newInstance(Class<T> 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);
return injector.newInstance(type, instance);
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/astefanutti/camel/cdi/DefaultLiteral.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2014 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed 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 io.astefanutti.camel.cdi;

import javax.enterprise.inject.Default;
import javax.enterprise.util.AnnotationLiteral;

final class DefaultLiteral extends AnnotationLiteral<Default> implements Default {

private static final long serialVersionUID = 1L;

static final Default INSTANCE = new DefaultLiteral();

private DefaultLiteral() {
}
}
Loading

0 comments on commit 120cd46

Please sign in to comment.