Skip to content
Permalink
Browse files
BATCHEE-65 added support for CDI-1.1
extensions/cdi currently failes with cdi-1.1
  • Loading branch information
rsandtner committed Nov 9, 2015
1 parent a212f4f commit b0bf6a1003f87f1a0a046f940d0a0d16c4dd9482
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 32 deletions.
@@ -29,28 +29,78 @@
<description>This module implements some CDI scopes batch oriented.</description>

<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.1_spec</artifactId>
<version>1.0</version>
<groupId>org.apache.deltaspike.cdictrl</groupId>
<artifactId>deltaspike-cdictrl-owb</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.openwebbeans.test</groupId>
<artifactId>cditest-owb</artifactId>
<version>1.2.1</version>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>${owb.version}</version>
<scope>test</scope>
</dependency>

</dependencies>


<profiles>
<profile>
<id>cdi-1.0</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>

<properties>
<owb.version>1.2.1</owb.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.1_spec</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

</dependencies>
</profile>

<profile>
<id>cdi-1.1</id>

<properties>
<owb.version>1.6.0-SNAPSHOT</owb.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.1_spec</artifactId>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.2_spec</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

</dependencies>
</profile>
</profiles>
</project>
@@ -16,14 +16,14 @@
*/
package org.apache.batchee.cdi.testng;

import org.apache.webbeans.cditest.CdiTestContainer;
import org.apache.webbeans.cditest.CdiTestContainerLoader;
import org.apache.deltaspike.cdise.api.CdiContainer;
import org.apache.deltaspike.cdise.api.CdiContainerLoader;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class CdiContainerLifecycle implements ITestListener {
private CdiTestContainer container;
private CdiContainer container;

@Override
public void onTestStart(final ITestResult iTestResult) {
@@ -52,9 +52,9 @@ public void onTestFailedButWithinSuccessPercentage(final ITestResult iTestResult

@Override
public void onStart(final ITestContext iTestContext) {
container = CdiTestContainerLoader.getCdiContainer();
container = CdiContainerLoader.getCdiContainer();
try {
container.bootContainer();
container.boot();
} catch (final Exception e) {
throw new RuntimeException(e);
}
@@ -63,7 +63,7 @@ public void onStart(final ITestContext iTestContext) {
@Override
public void onFinish(final ITestContext iTestContext) {
try {
container.shutdownContainer();
container.shutdown();
} catch (final Exception e) {
// no-op
}
@@ -66,4 +66,32 @@
<artifactId>derby</artifactId>
</dependency>
</dependencies>

<profiles>
<profile>
<id>cdi-1.0</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>

<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
</dependency>
</dependencies>
</profile>

<profile>
<id>cdi-1.1</id>

<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.1_spec</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
@@ -44,10 +44,6 @@
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ejb_3.1_spec</artifactId>
@@ -85,12 +81,6 @@
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-core</artifactId>
<version>4.6.0</version>
<scope>test</scope>
</dependency>

</dependencies>

@@ -434,6 +424,53 @@
</plugins>
</build>
</profile>

<profile>
<id>cdi-1.0</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>

<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
</dependency>

<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-core</artifactId>
<version>4.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>

<profile>
<id>cdi-1.1</id>

<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.1_spec</artifactId>
</dependency>

<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>openejb-core</artifactId>
<version>7.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

<!-- dont know why we need this -->
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.8</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
@@ -17,6 +17,7 @@

package org.apache.batchee.container.cdi;

import javax.batch.operations.BatchRuntimeException;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
@@ -26,14 +27,44 @@
import javax.enterprise.inject.spi.Extension;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// excepted beforeBeanDiscovery() all is forked from DeltaSpike - we don't want to depend from it here
public class BatchCDIInjectionExtension implements Extension {

private static final boolean CDI_1_1_AVAILABLE;
private static final Method CDI_CURRENT_METHOD;
private static final Method CDI_GET_BEAN_MANAGER_METHOD;


private static BatchCDIInjectionExtension bmpSingleton = null;

private volatile Map<ClassLoader, BeanManagerInfo> bmInfos = new ConcurrentHashMap<ClassLoader, BeanManagerInfo>();


static {
boolean cdi11Available;
Method currentMethod;
Method getBmMethod;

try {
Class<?> cdi = Class.forName("javax.enterprise.inject.spi.CDI", false, loader());
currentMethod = cdi.getDeclaredMethod("current");
getBmMethod = cdi.getDeclaredMethod("getBeanManager");
cdi11Available = true;
} catch (Exception e) {
currentMethod = null;
getBmMethod = null;
cdi11Available = false;
}

CDI_CURRENT_METHOD = currentMethod;
CDI_GET_BEAN_MANAGER_METHOD = getBmMethod;
CDI_1_1_AVAILABLE = cdi11Available;
}

void beforeBeanDiscovery(final @Observes BeforeBeanDiscovery bbd, BeanManager bm) {
bbd.addAnnotatedType(bm.createAnnotatedType(BatchProducerBean.class));
}
@@ -43,20 +74,26 @@ public void setBeanManager(final @Observes AfterBeanDiscovery afterBeanDiscovery
if (bmpSingleton == null) {
bmpSingleton = this;
}
final BeanManagerInfo bmi = getBeanManagerInfo(loader());
bmi.loadTimeBm = beanManager;

if (!CDI_1_1_AVAILABLE) {
final BeanManagerInfo bmi = getBeanManagerInfo(loader());
bmi.loadTimeBm = beanManager;
}
}

public void cleanupFinalBeanManagers(final @Observes AfterDeploymentValidation adv) {
for (final BeanManagerInfo bmi : bmpSingleton.bmInfos.values()) {
bmi.finalBm = null;
if (!CDI_1_1_AVAILABLE) {
for (final BeanManagerInfo bmi : bmpSingleton.bmInfos.values()) {
bmi.finalBm = null;
}
}
}

public void cleanupStoredBeanManagerOnShutdown(final @Observes BeforeShutdown beforeShutdown) {
if (bmpSingleton == null) {
if (CDI_1_1_AVAILABLE || bmpSingleton == null) {
return;
}

bmpSingleton.bmInfos.remove(loader());
}

@@ -69,6 +106,15 @@ public static BatchCDIInjectionExtension getInstance() {
}

public BeanManager getBeanManager() {
if (CDI_1_1_AVAILABLE) {
try {
return (BeanManager) CDI_GET_BEAN_MANAGER_METHOD.invoke(CDI_CURRENT_METHOD.invoke(null));
} catch (Exception e) {
throw new BatchRuntimeException("unable to resolve BeanManager");
}
}

// fallback if CDI isn't available
final BeanManagerInfo bmi = getBeanManagerInfo(loader());

BeanManager result = bmi.finalBm;
@@ -132,6 +132,12 @@
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.1_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ejb_3.1_spec</artifactId>

0 comments on commit b0bf6a1

Please sign in to comment.