#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package};
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import org.jbpm.services.task.identity.JBossUserGroupCallbackImpl;
import org.kie.internal.task.api.UserGroupCallback;
/**
* CDI producer that provides all required beans for the execution.
*
* IMPORTANT: this is for JavaSE environment and not for JavaEE.
* JavaEE environment should rely on RequestScoped EntityManager and some TransactionInterceptor
* to manage transactions.
*
*/
@ApplicationScoped
public class EnvironmentProducer {
private EntityManagerFactory emf;
@Produces
public UserGroupCallback produceSelectedUserGroupCalback() {
return new JBossUserGroupCallbackImpl("classpath:/usergroup.properties");
}
@PersistenceUnit(unitName = "org.jbpm.sample")
@ApplicationScoped
@Produces
public EntityManagerFactory getEntityManagerFactory() {
if (this.emf == null) {
this.emf = Persistence.createEntityManagerFactory("org.jbpm.sample");
}
return this.emf;
}
@Produces
@ApplicationScoped
public EntityManager getEntityManager() {
final EntityManager em = getEntityManagerFactory().createEntityManager();
EntityManager emProxy = (EntityManager)
Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{EntityManager.class}, new EmInvocationHandler(em));
return emProxy;
}
@ApplicationScoped
public void commitAndClose(@Disposes EntityManager em) {
try {
em.close();
} catch (Exception e) {
}
}
private class EmInvocationHandler implements InvocationHandler {
private EntityManager delegate;
EmInvocationHandler(EntityManager em) {
this.delegate = em;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
joinTransactionIfNeeded();
return method.invoke(delegate, args);
}
private void joinTransactionIfNeeded() {
try {
UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
if (ut.getStatus() == Status.STATUS_ACTIVE) {
delegate.joinTransaction();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}