| .. | .. |
|---|
| 3 | 3 | import java.io.IOException; |
|---|
| 4 | 4 | import java.lang.reflect.Method; |
|---|
| 5 | 5 | |
|---|
| 6 | | -import javax.inject.Inject; |
|---|
| 7 | | -import javax.interceptor.AroundInvoke; |
|---|
| 8 | | -import javax.interceptor.Interceptor; |
|---|
| 6 | +import javax.annotation.Priority; |
|---|
| 9 | 7 | import javax.interceptor.InvocationContext; |
|---|
| 10 | 8 | import javax.persistence.EntityManager; |
|---|
| 9 | +import javax.persistence.EntityManagerFactory; |
|---|
| 10 | +import javax.ws.rs.Priorities; |
|---|
| 11 | 11 | import javax.ws.rs.container.ContainerRequestContext; |
|---|
| 12 | +import javax.ws.rs.container.ContainerRequestFilter; |
|---|
| 12 | 13 | import javax.ws.rs.container.ContainerResponseContext; |
|---|
| 14 | +import javax.ws.rs.container.ContainerResponseFilter; |
|---|
| 13 | 15 | import javax.ws.rs.core.Response.Status; |
|---|
| 16 | +import javax.ws.rs.ext.Provider; |
|---|
| 14 | 17 | |
|---|
| 15 | 18 | import org.apache.logging.log4j.LogManager; |
|---|
| 16 | 19 | import org.apache.logging.log4j.Logger; |
|---|
| 17 | 20 | import org.jboss.resteasy.core.ResourceMethodInvoker; |
|---|
| 18 | 21 | import org.jboss.resteasy.spi.ResteasyProviderFactory; |
|---|
| 19 | 22 | |
|---|
| 20 | | -import com.google.inject.persist.Transactional; |
|---|
| 21 | | - |
|---|
| 22 | | -@Interceptor |
|---|
| 23 | | -@MyTrans |
|---|
| 24 | | -public class TransactionsManager { |
|---|
| 23 | +@Provider |
|---|
| 24 | +@Priority(Priorities.AUTHENTICATION) |
|---|
| 25 | +public class TransactionsManager implements ContainerRequestFilter, ContainerResponseFilter { |
|---|
| 25 | 26 | |
|---|
| 26 | 27 | private static final Logger LOG = LogManager.getLogger(TransactionsManager.class); |
|---|
| 27 | 28 | |
|---|
| 28 | | - @Inject |
|---|
| 29 | | - private EntityManager em; |
|---|
| 29 | + private EntityManagerFactory entityManagerFactory = javax.persistence.Persistence.createEntityManagerFactory("localdb"); |
|---|
| 30 | + |
|---|
| 31 | + private EntityManager getEM() { |
|---|
| 32 | + return entityManagerFactory.createEntityManager(); |
|---|
| 33 | + } |
|---|
| 30 | 34 | |
|---|
| 31 | 35 | // @Inject |
|---|
| 32 | 36 | // private ThreadLocal<EntityManager> threadLocal; |
|---|
| 33 | 37 | |
|---|
| 34 | | - @AroundInvoke |
|---|
| 38 | + // @AroundInvoke |
|---|
| 35 | 39 | public Object invoke(InvocationContext context) throws Exception { |
|---|
| 36 | | - // EntityTransaction trx = manager.getTransaction(); |
|---|
| 40 | + EntityManager em = getEM(); |
|---|
| 37 | 41 | if (!em.getTransaction().isActive()) { |
|---|
| 38 | 42 | em.getTransaction().begin(); |
|---|
| 39 | 43 | LOG.info("INIT trans"); |
|---|
| .. | .. |
|---|
| 49 | 53 | return result; |
|---|
| 50 | 54 | } |
|---|
| 51 | 55 | |
|---|
| 56 | + @Override |
|---|
| 52 | 57 | public void filter(ContainerRequestContext requestContext) throws IOException { |
|---|
| 53 | 58 | ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker"); |
|---|
| 54 | 59 | Method method = methodInvoker.getMethod(); |
|---|
| 60 | + EntityManager em = getEM(); |
|---|
| 55 | 61 | |
|---|
| 56 | | - if (method.isAnnotationPresent(Transactional.class)) { |
|---|
| 62 | + ResteasyProviderFactory.pushContext(EntityManager.class, em); |
|---|
| 63 | + |
|---|
| 64 | + if (method.isAnnotationPresent(EnsureTransaction.class)) { |
|---|
| 57 | 65 | LOG.info("WE need transaction!!!"); |
|---|
| 66 | + |
|---|
| 58 | 67 | em.getTransaction().begin(); |
|---|
| 59 | 68 | } |
|---|
| 69 | + LOG.info("Sent to context, em: {}, {}?", em, method.toGenericString()); |
|---|
| 60 | 70 | } |
|---|
| 61 | 71 | |
|---|
| 72 | + @Override |
|---|
| 62 | 73 | public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { |
|---|
| 63 | 74 | // TODO Auto-generated method stub |
|---|
| 75 | + // ResteasyProviderFactory.pushContext(EntityManager.class, em); |
|---|
| 76 | + EntityManager em = (EntityManager) ResteasyProviderFactory.getContextData(EntityManager.class); |
|---|
| 77 | + LOG.info("Operating on em: {}", em); |
|---|
| 64 | 78 | |
|---|
| 65 | | - if (em.getTransaction().isActive()) { |
|---|
| 79 | + if (em != null && em.getTransaction().isActive()) { |
|---|
| 66 | 80 | LOG.info("There is transaction go ahead..."); |
|---|
| 67 | 81 | if (responseContext.getStatus() == Status.OK.getStatusCode()) { |
|---|
| 68 | 82 | em.getTransaction().commit(); |
|---|