Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.dropwizard.hibernate;
  
  import java.util.HashMap;
  import java.util.Map;
  
  
 
An application event listener that listens for Jersey application initialization to be finished, then creates a map of resource method that have metrics annotations. Finally, it listens for method start events, and returns a org.glassfish.jersey.server.monitoring.RequestEventListener that updates the relevant metric for suitably annotated methods when it gets the request events indicating that the method is about to be invoked, or just got done being invoked.
 
 
     private final SessionFactory sessionFactory;

    
Construct an application event listener using the given session factory.

When using this constructor, the UnitOfWorkApplicationListener should be added to a Jersey ResourceConfig as a singleton.

Parameters:
sessionFactory a org.hibernate.SessionFactory
 
     public UnitOfWorkApplicationListener(SessionFactory sessionFactory) {
         this. = sessionFactory;
     }
 
     private static class UnitOfWorkEventListener implements RequestEventListener {
         private final Map<MethodUnitOfWorkmethodMap;
         private final SessionFactory sessionFactory;
         private UnitOfWork unitOfWork;
         private Session session;
 
 
         public UnitOfWorkEventListener(Map<MethodUnitOfWorkmethodMap,
                                        SessionFactory sessionFactory) {
             this. = methodMap;
             this. = sessionFactory;
         }
 
         @Override
         public void onEvent(RequestEvent event) {
             if (event.getType() == ..) {
                 this. = this..get(event.getUriInfo()
                         .getMatchedResourceMethod().getInvocable().getDefinitionMethod());
                 if ( != null) {
                     this. = this..openSession();
                     try {
                         configureSession();
                         ManagedSessionContext.bind(this.);
                         beginTransaction();
                     } catch (Throwable th) {
                         this..close();
                         this. = null;
                         ManagedSessionContext.unbind(this.);
                         throw th;
                     }
                 }
             } else if (event.getType() == ..) {
                 if (this. != null) {
                     try {
                         commitTransaction();
                     } catch (Exception e) {
                         rollbackTransaction();
                         throw new MappableException(e);
                     } finally {
                         this..close();
                         this. = null;
                         ManagedSessionContext.unbind(this.);
                     }
                 }
             } else if (event.getType() == ..) {
                 if (this. != null) {
                     try {
                         rollbackTransaction();
                     } finally {
                         this..close();
                        this. = null;
                        ManagedSessionContext.unbind(this.);
                    }
                }
            }
        }
        private void beginTransaction() {
            if (this..transactional()) {
                this..beginTransaction();
            }
        }
        private void configureSession() {
            this..setDefaultReadOnly(this..readOnly());
            this..setCacheMode(this..cacheMode());
            this..setFlushMode(this..flushMode());
        }
        private void rollbackTransaction() {
            if (this..transactional()) {
                final Transaction txn = this..getTransaction();
                if (txn != null && txn.isActive()) {
                    txn.rollback();
                }
            }
        }
        private void commitTransaction() {
            if (this..transactional()) {
                final Transaction txn = this..getTransaction();
                if (txn != null && txn.isActive()) {
                    txn.commit();
                }
            }
        }
    }
    private Map<MethodUnitOfWorkmethodMap = new HashMap<>();
    @Override
    public void onEvent(ApplicationEvent event) {
        if (event.getType() == ..) {
            for (Resource resource : event.getResourceModel().getResources()) {
                for (ResourceMethod method : resource.getAllMethods()) {
                    registerUnitOfWorkAnnotations(method);
                }
                for (Resource childResource : resource.getChildResources()) {
                    for (ResourceMethod method : childResource.getAllMethods()) {
                        registerUnitOfWorkAnnotations(method);
                    }
                }
            }
        }
    }
    @Override
    public RequestEventListener onRequest(RequestEvent event) {
        return new UnitOfWorkEventListener();
    }
    private void registerUnitOfWorkAnnotations(ResourceMethod method) {
        UnitOfWork annotation = method.getInvocable().getDefinitionMethod().getAnnotation(UnitOfWork.class);
        if (annotation == null) {
            annotation = method.getInvocable().getHandlingMethod().getAnnotation(UnitOfWork.class);
        }
        if (annotation != null) {
            this..put(method.getInvocable().getDefinitionMethod(), annotation);
        }
    }
New to GrepCode? Check out our FAQ X