-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISPN-1636 Have remote tasks inject services via CDI before execution
- Loading branch information
Showing
23 changed files
with
923 additions
and
69 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
cdi/extension/src/main/java/org/infinispan/cdi/BeanManagerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors | ||
* as indicated by the @author tag. All rights reserved. | ||
* See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, | ||
* modify, copy, or redistribute it subject to the terms and conditions | ||
* of the GNU Lesser General Public License, v. 2.1. | ||
* This program is distributed in the hope that it will be useful, but WITHOUT A | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | ||
* You should have received a copy of the GNU Lesser General Public License, | ||
* v.2.1 along with this distribution; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
package org.infinispan.cdi; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.enterprise.inject.spi.BeanManager; | ||
|
||
import org.jboss.solder.beanManager.BeanManagerAware; | ||
|
||
public class BeanManagerController extends BeanManagerAware { | ||
|
||
private Map<ClassLoader, BeanManager> beanManagers = new ConcurrentHashMap<ClassLoader, BeanManager>(); | ||
|
||
public BeanManager getRegisteredBeanManager() { | ||
ClassLoader classLoader = getClassLoader(null); | ||
|
||
BeanManager bm = resolveBeanManager(classLoader); | ||
|
||
if (bm == null) { | ||
bm = getBeanManager(); | ||
|
||
if (bm == null) { | ||
throw new IllegalStateException("Can not find BeanManager. Check CDI setup in your execution environment!"); | ||
} | ||
|
||
registerBeanManager(classLoader, bm); | ||
|
||
} | ||
return bm; | ||
} | ||
|
||
public void registerBeanManager(ClassLoader cl, BeanManager bm){ | ||
beanManagers.put(cl, bm); | ||
} | ||
|
||
public void registerBeanManager(BeanManager bm){ | ||
beanManagers.put(getClassLoader(null), bm); | ||
} | ||
|
||
public void deregisterBeanManager(ClassLoader cl){ | ||
beanManagers.remove(cl); | ||
} | ||
|
||
public void deregisterBeanManager(){ | ||
beanManagers.remove(getClassLoader(null)); | ||
} | ||
|
||
public BeanManager resolveBeanManager(ClassLoader cl){ | ||
return beanManagers.get(cl); | ||
} | ||
|
||
public ClassLoader getClassLoader(Object o) { | ||
ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { | ||
|
||
@Override | ||
public ClassLoader run() { | ||
try { | ||
return Thread.currentThread().getContextClassLoader(); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
}); | ||
|
||
if (loader == null && o != null) { | ||
loader = o.getClass().getClassLoader(); | ||
} | ||
|
||
if (loader == null) { | ||
loader = BeanManagerController.class.getClassLoader(); | ||
} | ||
|
||
return loader; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
cdi/extension/src/main/java/org/infinispan/cdi/CDIDistributedTaskLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.infinispan.cdi; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
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.distexec.spi.DistributedTaskLifecycle; | ||
import org.jboss.solder.beanManager.BeanManagerAware; | ||
|
||
public class CDIDistributedTaskLifecycle extends BeanManagerAware implements DistributedTaskLifecycle { | ||
|
||
@Override | ||
@SuppressWarnings({ "unchecked" }) | ||
public <T> void onPreExecute(Callable<T> task) { | ||
BeanManager bm = InfinispanExtension.getBeanManagerController().getRegisteredBeanManager(); | ||
Class<Callable<T>> clazz = (Class<Callable<T>>) task.getClass(); | ||
AnnotatedType<Callable<T>> type = bm.createAnnotatedType(clazz); | ||
InjectionTarget<Callable<T>> it = bm.createInjectionTarget(type); | ||
CreationalContext<Callable<T>> ctx = bm.createCreationalContext(null); | ||
it.inject(task, ctx); | ||
it.postConstruct(task); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({ "unchecked" }) | ||
public <T> void onPostExecute(Callable<T> task) { | ||
BeanManager bm = InfinispanExtension.getBeanManagerController().getRegisteredBeanManager(); | ||
Class<Callable<T>> clazz = (Class<Callable<T>>) task.getClass(); | ||
AnnotatedType<Callable<T>> type = bm.createAnnotatedType(clazz); | ||
InjectionTarget<Callable<T>> it = bm.createInjectionTarget(type); | ||
it.preDestroy(task); | ||
it.dispose(task); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
cdi/extension/src/main/java/org/infinispan/cdi/CDIMapReduceTaskLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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.distexec.mapreduce.Mapper; | ||
import org.infinispan.distexec.mapreduce.Reducer; | ||
import org.infinispan.distexec.mapreduce.spi.MapReduceTaskLifecycle; | ||
import org.jboss.solder.beanManager.BeanManagerAware; | ||
|
||
public class CDIMapReduceTaskLifecycle extends BeanManagerAware implements MapReduceTaskLifecycle { | ||
|
||
@Override | ||
@SuppressWarnings({ "unchecked" }) | ||
public <KIn, VIn, KOut, VOut> void onPreExecute(Mapper<KIn, VIn, KOut, VOut> mapper) { | ||
BeanManager bm = InfinispanExtension.getBeanManagerController().getRegisteredBeanManager(); | ||
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) { | ||
BeanManager bm = InfinispanExtension.getBeanManagerController().getRegisteredBeanManager(); | ||
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); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({ "unchecked" }) | ||
public <KOut, VOut> void onPreExecute(Reducer<KOut, VOut> reducer) { | ||
BeanManager bm = InfinispanExtension.getBeanManagerController().getRegisteredBeanManager(); | ||
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 = InfinispanExtension.getBeanManagerController().getRegisteredBeanManager(); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/services/org.infinispan.distexec.mapreduce.spi.MapReduceTaskLifecycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.infinispan.cdi.CDIMapReduceTaskLifecycle |
1 change: 1 addition & 0 deletions
1
...src/main/resources/META-INF/services/org.infinispan.distexec.spi.DistributedTaskLifecycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.infinispan.cdi.CDIDistributedTaskLifecycle |
83 changes: 83 additions & 0 deletions
83
cdi/extension/src/test/java/org/infinispan/cdi/test/distexec/DistributedExecutorCDITest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2011 Red Hat Inc. and/or its affiliates and other | ||
* contributors as indicated by the @author tags. All rights reserved. | ||
* See the copyright.txt in the distribution for a full listing of | ||
* individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.infinispan.cdi.test.distexec; | ||
|
||
import static org.infinispan.cdi.test.testutil.Deployments.baseDeployment; | ||
|
||
import java.io.Serializable; | ||
import java.util.concurrent.Callable; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.infinispan.Cache; | ||
import org.infinispan.distexec.DistributedExecutorTest; | ||
import org.infinispan.test.MultipleCacheManagersTest; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Tests CDI integration with org.infinispan.distexec.DistributedExecutorService | ||
* | ||
* @author Vladimir Blagojevic | ||
*/ | ||
@Test(enabled = true, groups = "functional", testName = "distexec.DistributedExecutorTest") | ||
public class DistributedExecutorCDITest extends MultipleCacheManagersArquillianTest { | ||
|
||
DistributedExecutorTest delegate; | ||
|
||
public DistributedExecutorCDITest() { | ||
delegate = new DistributedExecutorTest(); | ||
} | ||
|
||
@Override | ||
MultipleCacheManagersTest getDelegate() { | ||
return delegate; | ||
} | ||
|
||
@Deployment | ||
public static Archive<?> deployment() { | ||
return baseDeployment().addClass(DistributedExecutorCDITest.class); | ||
} | ||
|
||
public void testBasicInvocation() throws Exception { | ||
delegate.basicInvocation(new SimpleCallable()); | ||
} | ||
|
||
|
||
static class SimpleCallable implements Callable<Integer>, Serializable { | ||
|
||
/** The serialVersionUID */ | ||
private static final long serialVersionUID = -8589149500259272402L; | ||
|
||
@Inject | ||
private Cache<String, String> cache; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
Assert.assertNotNull(cache, "Cache not injected into " + this); | ||
return 1; | ||
} | ||
} | ||
} |
Oops, something went wrong.