Skip to content

Commit

Permalink
Inital weld JPA integration
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Mar 15, 2011
1 parent fd56965 commit d586f7d
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<module name="javax.enterprise.api"/>
<module name="javax.faces.api"/>
<module name="javax.interceptor.api"/>
<module name="javax.persistence.api"/>
<module name="javax.servlet.api"/>
<module name="javax.servlet.jsp.api"/>
<module name="javax.transaction.api"/>
Expand All @@ -46,6 +47,7 @@
<module name="org.jboss.as.controller"/>
<module name="org.jboss.as.ee"/>
<module name="org.jboss.as.ejb3"/>
<module name="org.jboss.as.jpa"/>
<module name="org.jboss.as.naming"/>
<module name="org.jboss.as.server"/>
<module name="org.jboss.as.transactions"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module name="javax.api"/>
<module name="javax.enterprise.api"/>
<module name="javax.inject.api"/>
<module name="javax.persistence.api"/>
<module name="javax.interceptor.api"/>
<module name="javax.servlet.api"/>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<module name="javax.faces.api"/>
<module name="javax.inject.api"/>
<module name="javax.interceptor.api"/>
<module name="javax.persistence.api"/>
<module name="javax.servlet.api"/>
<module name="javax.transaction.api"/>
<module name="javax.validation.api"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<module name="javax.api"/>
<module name="javax.enterprise.api"/>
<module name="javax.inject.api"/>
<module name="javax.persistence.api"/>
<module name="javax.servlet.api"/>
<module name="javax.validation.api"/>
<module name="org.jboss.weld.api"/>
Expand Down
5 changes: 5 additions & 0 deletions weld/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-ejb3</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
import org.jboss.as.weld.deployment.WeldDeploymentMetadata;
import org.jboss.as.weld.injection.WeldInjectionFactory;
import org.jboss.as.weld.services.bootstrap.WeldEjbInjectionServices;
import org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.logging.Logger;
import org.jboss.modules.Module;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.injection.spi.EjbInjectionServices;
import org.jboss.weld.injection.spi.JpaInjectionServices;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -123,9 +125,11 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
processEjbComponents(deploymentUnit,bdaMap,rootBda,indexes);

final EjbInjectionServices ejbInjectionServices = new WeldEjbInjectionServices(deploymentUnit.getServiceRegistry(),eeModuleDescription);
final JpaInjectionServices jpaInjectionServices = new WeldJpaInjectionServices(deploymentUnit,deploymentUnit.getServiceRegistry());

final BeanDeploymentModule bdm = new BeanDeploymentModule(beanDeploymentArchives);
bdm.addService(EjbInjectionServices.class,ejbInjectionServices);
bdm.addService(JpaInjectionServices.class,jpaInjectionServices);
deploymentUnit.putAttachment(WeldAttachments.BEAN_DEPLOYMENT_MODULE,bdm);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.ejb.spi.EjbServices;
import org.jboss.weld.injection.spi.EjbInjectionServices;
import org.jboss.weld.injection.spi.JpaInjectionServices;
import org.jboss.weld.validation.spi.ValidationServices;

import javax.enterprise.inject.spi.Extension;
Expand Down Expand Up @@ -155,14 +156,17 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro

weldContainer.addWeldService(EjbServices.class,new WeldEjbServices(deploymentUnit.getServiceRegistry()));


final JpaInjectionServices rootJpaInjectionServices = new WeldJpaInjectionServices(deploymentUnit,deploymentUnit.getServiceRegistry());
weldContainer.addWeldService(JpaInjectionServices.class,rootJpaInjectionServices);

final WeldService weldService = new WeldService(weldContainer);
final ServiceName weldServiceName = deploymentUnit.getServiceName().append(WeldService.SERVICE_NAME);
// add the weld service
final ServiceBuilder<WeldContainer> weldServiceBuilder = serviceTarget.addService(weldServiceName, weldService);

weldServiceBuilder.addDependencies(TCCLSingletonService.SERVICE_NAME);

installJpaInjectionService(serviceTarget, deploymentUnit, weldService, weldServiceBuilder);
installResourceInjectionService(serviceTarget, deploymentUnit, weldService, weldServiceBuilder);
installSecurityService(serviceTarget, deploymentUnit, weldService, weldServiceBuilder);
installTransactionService(serviceTarget, deploymentUnit, weldService, weldServiceBuilder);
Expand All @@ -171,18 +175,6 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro

}

private ServiceName installJpaInjectionService(ServiceTarget serviceTarget, DeploymentUnit deploymentUnit,
WeldService weldService, ServiceBuilder<WeldContainer> weldServiceBuilder) {
final WeldJpaInjectionServices service = new WeldJpaInjectionServices();

final ServiceName serviceName = deploymentUnit.getServiceName().append(WeldJpaInjectionServices.SERVICE_NAME);

serviceTarget.addService(serviceName, service).install();

weldServiceBuilder.addDependency(serviceName, WeldJpaInjectionServices.class, weldService.getJpaInjectionServices());

return serviceName;
}

private ServiceName installSecurityService(ServiceTarget serviceTarget, DeploymentUnit deploymentUnit,
WeldService weldService, ServiceBuilder<WeldContainer> weldServiceBuilder) {
Expand Down
16 changes: 10 additions & 6 deletions weld/src/main/java/org/jboss/as/weld/services/WeldService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
package org.jboss.as.weld.services;

import org.jboss.as.weld.WeldContainer;

import org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices;

import org.jboss.as.weld.services.bootstrap.WeldEjbInjectionServices;
import org.jboss.as.weld.services.bootstrap.WeldEjbServices;

import org.jboss.as.weld.services.bootstrap.WeldResourceInjectionServices;
import org.jboss.as.weld.services.bootstrap.WeldSecurityServices;
import org.jboss.as.weld.services.bootstrap.WeldTransactionServices;
Expand All @@ -34,7 +39,12 @@
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;

import org.jboss.weld.injection.spi.JpaInjectionServices;

import org.jboss.weld.ejb.spi.EjbServices;
import org.jboss.weld.injection.spi.EjbInjectionServices;

import org.jboss.weld.injection.spi.ResourceInjectionServices;
import org.jboss.weld.security.spi.SecurityServices;
import org.jboss.weld.transaction.spi.TransactionServices;
Expand All @@ -55,7 +65,6 @@ public class WeldService implements Service<WeldContainer> {

private final WeldContainer weldContainer;

private final InjectedValue<WeldJpaInjectionServices> jpaInjectionServices = new InjectedValue<WeldJpaInjectionServices>();
private final InjectedValue<WeldResourceInjectionServices> resourceInjectionServices = new InjectedValue<WeldResourceInjectionServices>();
private final InjectedValue<WeldSecurityServices> securityServices = new InjectedValue<WeldSecurityServices>();
private final InjectedValue<WeldTransactionServices> weldTransactionServices = new InjectedValue<WeldTransactionServices>();
Expand All @@ -74,7 +83,6 @@ public void start(StartContext context) throws StartException {

// TODO: this is a complete hack. We will need one instance of each service per bean deployment archive
for (BeanDeploymentArchive bda : weldContainer.getBeanDeploymentArchives()) {
bda.getServices().add(JpaInjectionServices.class, jpaInjectionServices.getValue());
bda.getServices().add(ResourceInjectionServices.class, resourceInjectionServices.getValue());
}
// start weld
Expand Down Expand Up @@ -104,10 +112,6 @@ public InjectedValue<WeldTransactionServices> getWeldTransactionServices() {
return weldTransactionServices;
}

public InjectedValue<WeldJpaInjectionServices> getJpaInjectionServices() {
return jpaInjectionServices;
}

public InjectedValue<WeldResourceInjectionServices> getResourceInjectionServices() {
return resourceInjectionServices;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,76 @@
*/
package org.jboss.as.weld.services.bootstrap;

import javax.enterprise.inject.spi.InjectionPoint;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.jboss.msc.service.Service;
import org.jboss.as.jpa.container.PersistenceUnitSearch;
import org.jboss.as.jpa.container.TransactionScopedEntityManager;
import org.jboss.as.jpa.service.PersistenceUnitService;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.service.ServiceRegistry;
import org.jboss.weld.injection.spi.JpaInjectionServices;

public class WeldJpaInjectionServices implements Service<WeldJpaInjectionServices>, JpaInjectionServices {

public static final ServiceName SERVICE_NAME = ServiceName.of("WeldJpaInjectionServices");

@Override
public void start(StartContext context) throws StartException {
import javax.enterprise.inject.spi.InjectionPoint;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import java.util.HashMap;

}
public class WeldJpaInjectionServices implements JpaInjectionServices {

@Override
public void stop(StopContext context) {
}
private final DeploymentUnit deploymentUnit;
private final ServiceRegistry serviceRegistry;

@Override
public WeldJpaInjectionServices getValue() throws IllegalStateException, IllegalArgumentException {
return this;
public WeldJpaInjectionServices(DeploymentUnit deploymentUnit, ServiceRegistry serviceRegistry) {
this.deploymentUnit = deploymentUnit;
this.serviceRegistry = serviceRegistry;
}

@Override
public EntityManager resolvePersistenceContext(InjectionPoint injectionPoint) {
throw new RuntimeException("not implemented");
//TODO: cache this stuff
final PersistenceContext context = injectionPoint.getAnnotated().getAnnotation(PersistenceContext.class);
if(context == null) {
throw new RuntimeException("Could not find @PersistenceContext annotation on " + injectionPoint.getMember());
}
final String scopedPuName = getScopedPUName(deploymentUnit, context.unitName());
final ServiceName persistenceUnitServiceName = PersistenceUnitService.getPUServiceName(scopedPuName);

final ServiceController<?> serviceController = serviceRegistry.getRequiredService(persistenceUnitServiceName);
//now we have the service controller, as this method is only called at runtime the service should
//always be up
PersistenceUnitService persistenceUnitService = (PersistenceUnitService)serviceController.getValue();
return new TransactionScopedEntityManager(scopedPuName,new HashMap<Object,Object>(), persistenceUnitService.getEntityManagerFactory());
}

@Override
public EntityManagerFactory resolvePersistenceUnit(InjectionPoint injectionPoint) {
throw new RuntimeException("not implemented");
//TODO: cache this stuff
final PersistenceUnit context = injectionPoint.getAnnotated().getAnnotation(PersistenceUnit.class);
if(context == null) {
throw new RuntimeException("Could not find @PersistenceUnit annotation on " + injectionPoint.getMember());
}
final String scopedPuName = getScopedPUName(deploymentUnit, context.unitName());
final ServiceName persistenceUnitServiceName = PersistenceUnitService.getPUServiceName(scopedPuName);

final ServiceController<?> serviceController = serviceRegistry.getRequiredService(persistenceUnitServiceName);
//now we have the service controller, as this method is only called at runtime the service should
//always be up
PersistenceUnitService persistenceUnitService = (PersistenceUnitService)serviceController.getValue();
return persistenceUnitService.getEntityManagerFactory();
}

@Override
public void cleanup() {
}

private String getScopedPUName(final DeploymentUnit deploymentUnit, String persistenceUnitName) {
String scopedPuName;
scopedPuName = PersistenceUnitSearch.resolvePersistenceUnitSupplier(deploymentUnit, persistenceUnitName);
if (null == scopedPuName) {
throw new RuntimeException("Can't find a deployment unit named " +persistenceUnitName+ " at " + deploymentUnit);
}
return scopedPuName;
}
}

0 comments on commit d586f7d

Please sign in to comment.