Skip to content

Commit

Permalink
ISPN-5181 The CDIMapReduceTaskLifecycle should not do anything when C…
Browse files Browse the repository at this point in the history
…DI is not available
  • Loading branch information
tristantarrant committed Jan 28, 2015
1 parent aa7905f commit 2840e57
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 100 deletions.
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans" xmlns:weld="http://jboss.org/schema/weld/beans"
xsi:schemaLocation=" xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd
http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd"> http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">


<weld:scan> <weld:scan>
Expand Down

This file was deleted.

Expand Up @@ -8,48 +8,62 @@
import javax.enterprise.inject.spi.InjectionTarget; import javax.enterprise.inject.spi.InjectionTarget;


import org.infinispan.Cache; import org.infinispan.Cache;
import org.infinispan.cdi.util.BeanManagerProvider; import org.infinispan.cdi.util.CDIHelper;
import org.infinispan.distexec.spi.DistributedTaskLifecycle; import org.infinispan.distexec.spi.DistributedTaskLifecycle;
import org.kohsuke.MetaInfServices; import org.kohsuke.MetaInfServices;


@MetaInfServices @MetaInfServices
public class CDIDistributedTaskLifecycle implements public class DelegatingDistributedTaskLifecycle implements
DistributedTaskLifecycle { DistributedTaskLifecycle {
private final boolean HAVE_CDI; private final DistributedTaskLifecycle delegate;


public CDIDistributedTaskLifecycle() { public DelegatingDistributedTaskLifecycle() {
boolean success; delegate = CDIHelper.isCDIAvailable() ? new CDIDistributedTaskLifecycle() : new NoCDIDistributedTaskLifecycle();
try {
this.getClass().getClassLoader().loadClass("javax.enterprise.inject.spi.BeanManager");
success = true;
} catch(ClassNotFoundException e) {
success = false;
}
HAVE_CDI = success;
} }


@Override
public <T, K, V> void onPreExecute(Callable<T> task, Cache<K, V> inputDataCache) {
delegate.onPreExecute(task, inputDataCache);
}


@Override @Override
@SuppressWarnings({ "unchecked" }) public <T> void onPostExecute(Callable<T> task) {
public <T, K, V> void onPreExecute(Callable<T> task, Cache<K, V> inputCache) { delegate.onPostExecute(task);
if (HAVE_CDI) { }
BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
ContextInputCache.set(inputCache); static class NoCDIDistributedTaskLifecycle implements DistributedTaskLifecycle {

@Override
public <T, K, V> void onPreExecute(Callable<T> task, Cache<K, V> inputDataCache) {
}

@Override
public <T> void onPostExecute(Callable<T> task) {
}
}

static class CDIDistributedTaskLifecycle implements DistributedTaskLifecycle {

@Override
public <T, K, V> void onPreExecute(Callable<T> task, Cache<K, V> inputDataCache) {
BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
ContextInputCache.set(inputDataCache);
Class<Callable<T>> clazz = (Class<Callable<T>>) task.getClass(); Class<Callable<T>> clazz = (Class<Callable<T>>) task.getClass();
AnnotatedType<Callable<T>> type = bm.createAnnotatedType(clazz); AnnotatedType<Callable<T>> type = bm.createAnnotatedType(clazz);
InjectionTarget<Callable<T>> it = bm.createInjectionTarget(type); InjectionTarget<Callable<T>> it = bm.createInjectionTarget(type);
CreationalContext<Callable<T>> ctx = bm.createCreationalContext(null); CreationalContext<Callable<T>> ctx = bm.createCreationalContext(null);
it.inject(task, ctx); it.inject(task, ctx);
it.postConstruct(task); it.postConstruct(task);
} }
}


@Override @Override
@SuppressWarnings({ "unchecked" }) public <T> void onPostExecute(Callable<T> task) {
public <T> void onPostExecute(Callable<T> task) {
if (HAVE_CDI) {
try { try {
BeanManager bm = BeanManagerProvider.getInstance().getBeanManager(); BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
Class<Callable<T>> clazz = (Class<Callable<T>>) task.getClass(); Class<Callable<T>> clazz = (Class<Callable<T>>) task.getClass();
AnnotatedType<Callable<T>> type = bm.createAnnotatedType(clazz); AnnotatedType<Callable<T>> type = bm.createAnnotatedType(clazz);
InjectionTarget<Callable<T>> it = bm.createInjectionTarget(type); InjectionTarget<Callable<T>> it = bm.createInjectionTarget(type);
Expand Down
@@ -0,0 +1,127 @@
package org.infinispan.cdi;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;

import org.infinispan.Cache;
import org.infinispan.cdi.util.CDIHelper;
import org.infinispan.distexec.mapreduce.Mapper;
import org.infinispan.distexec.mapreduce.Reducer;
import org.infinispan.distexec.mapreduce.spi.MapReduceTaskLifecycle;
import org.kohsuke.MetaInfServices;

@MetaInfServices
public class DelegatingMapReduceTaskLifecycle implements MapReduceTaskLifecycle {

private final MapReduceTaskLifecycle delegate;

public DelegatingMapReduceTaskLifecycle() {
delegate = CDIHelper.isCDIAvailable() ? new CDIMapReduceTaskLifecycle() : new NoCDIMapReduceTaskLifecycle();
}

@Override
public <KIn, VIn, KOut, VOut> void onPreExecute(Mapper<KIn, VIn, KOut, VOut> mapper, Cache<KIn, VIn> inputCache) {
delegate.onPreExecute(mapper, inputCache);
}

@Override
public <KIn, VIn, KOut, VOut> void onPostExecute(Mapper<KIn, VIn, KOut, VOut> mapper) {
delegate.onPostExecute(mapper);
}

@Override
public <KOut, VOut> void onPreExecute(Reducer<KOut, VOut> reducer, Cache<?, ?> cache) {
delegate.onPreExecute(reducer, cache);
}

@Override
public <KOut, VOut> void onPostExecute(Reducer<KOut, VOut> reducer) {
delegate.onPostExecute(reducer);
}

static class NoCDIMapReduceTaskLifecycle implements MapReduceTaskLifecycle {

@Override
public <KIn, VIn, KOut, VOut> void onPreExecute(Mapper<KIn, VIn, KOut, VOut> mapper, Cache<KIn, VIn> inputCache) {
}

@Override
public <KIn, VIn, KOut, VOut> void onPostExecute(Mapper<KIn, VIn, KOut, VOut> mapper) {
}

@Override
public <KOut, VOut> void onPreExecute(Reducer<KOut, VOut> reducer, Cache<?, ?> inputCache) {
}

@Override
public <KOut, VOut> void onPostExecute(Reducer<KOut, VOut> reducer) {
}

}

static class CDIMapReduceTaskLifecycle implements MapReduceTaskLifecycle {

@Override
@SuppressWarnings("unchecked")
public <KIn, VIn, KOut, VOut> void onPreExecute(Mapper<KIn, VIn, KOut, VOut> mapper, Cache<KIn, VIn> inputCache) {
BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
ContextInputCache.set(inputCache);
Class<Mapper<KIn, VIn, KOut, VOut>> clazz = (Class<Mapper<KIn, VIn, KOut, VOut>>) mapper.getClass();
AnnotatedType<Mapper<KIn, VIn, KOut, VOut>> type = bm.createAnnotatedType(clazz);
InjectionTarget<Mapper<KIn, VIn, KOut, VOut>> it = bm.createInjectionTarget(type);
CreationalContext<Mapper<KIn, VIn, KOut, VOut>> ctx = bm.createCreationalContext(null);
it.inject(mapper, ctx);
it.postConstruct(mapper);
}

@Override
@SuppressWarnings("unchecked")
public <KIn, VIn, KOut, VOut> void onPostExecute(Mapper<KIn, VIn, KOut, VOut> mapper) {
try {
BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
Class<Mapper<KIn, VIn, KOut, VOut>> clazz = (Class<Mapper<KIn, VIn, KOut, VOut>>) mapper.getClass();
AnnotatedType<Mapper<KIn, VIn, KOut, VOut>> type = bm.createAnnotatedType(clazz);
InjectionTarget<Mapper<KIn, VIn, KOut, VOut>> it = bm.createInjectionTarget(type);
it.preDestroy(mapper);
it.dispose(mapper);
} finally {
ContextInputCache.clean();
}
}

@Override
@SuppressWarnings("unchecked")
public <KOut, VOut> void onPreExecute(Reducer<KOut, VOut> reducer, Cache<?, ?> inputCache) {
BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
Class<Reducer<KOut, VOut>> clazz = (Class<Reducer<KOut, VOut>>) reducer.getClass();
AnnotatedType<Reducer<KOut, VOut>> type = bm.createAnnotatedType(clazz);
InjectionTarget<Reducer<KOut, VOut>> it = bm.createInjectionTarget(type);
CreationalContext<Reducer<KOut, VOut>> ctx = bm.createCreationalContext(null);
it.inject(reducer, ctx);
it.postConstruct(reducer);
}

@Override
@SuppressWarnings("unchecked")
public <KOut, VOut> void onPostExecute(Reducer<KOut, VOut> reducer) {
BeanManager bm = CDIHelper.getBeanManager();
if (bm == null)
return;
Class<Reducer<KOut, VOut>> clazz = (Class<Reducer<KOut, VOut>>) reducer.getClass();
AnnotatedType<Reducer<KOut, VOut>> type = bm.createAnnotatedType(clazz);
InjectionTarget<Reducer<KOut, VOut>> it = bm.createInjectionTarget(type);
it.preDestroy(reducer);
it.dispose(reducer);
}

}

}
23 changes: 23 additions & 0 deletions cdi/src/main/java/org/infinispan/cdi/util/CDIHelper.java
@@ -0,0 +1,23 @@
package org.infinispan.cdi.util;

import javax.enterprise.inject.spi.BeanManager;

public class CDIHelper {

public static final boolean isCDIAvailable() {
try {
CDIHelper.class.getClassLoader().loadClass("javax.enterprise.inject.spi.BeanManager");
return true;
} catch(ClassNotFoundException e) {
return false;
}
}

public static final BeanManager getBeanManager() {
try {
return BeanManagerProvider.getInstance().getBeanManager();
} catch (IllegalStateException ise) {
return null;
}
}
}

0 comments on commit 2840e57

Please sign in to comment.