Example #1
0
 /**
  * Executes the desired method call with a new instance of the {@link ResourceServlet}
  * implementation. Transaction management is done here.
  *
  * @param call
  * @param uow
  * @param req
  * @param resp
  */
 private final void dispatch(
     Method call, UnitOfWork uow, HttpServletRequest req, HttpServletResponse resp) {
   if (uow == null) {
     try {
       ResourceServlet servlet = GuiceBundle.getInjector().getInstance(servletClass);
       call.invoke(servlet, req, resp);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   } else {
     SessionFactory sessionFactory = GuiceBundle.getInjector().getInstance(SessionFactory.class);
     Session session = sessionFactory.openSession();
     try {
       configureSession(session, uow);
       ManagedSessionContext.bind(session);
       beginTransaction(session, uow);
       try {
         ResourceServlet servlet =
             (singleton == null) ? singleton : GuiceBundle.getInjector().getInstance(servletClass);
         call.invoke(servlet, req, resp);
         commitTransaction(session, uow);
       } catch (Exception e) {
         rollbackTransaction(session, uow);
         throw new RuntimeException(e);
       }
     } finally {
       session.close();
       ManagedSessionContext.unbind(sessionFactory);
     }
   }
 }
Example #2
0
  /**
   * Contstructor to get
   *
   * @param servletClass {@link ResourceServlet} implementation class and cache all methods and
   *     {@link io.dropwizard.hibernate.UnitOfWork} annotations of methods.
   * @throws NoSuchMethodException
   */
  public WrapperServlet(Class<ResourceServlet> servletClass, boolean singleInstance)
      throws NoSuchMethodException {
    this.servletClass = servletClass;
    this.singleton = null;
    try {
      if (singleInstance) singleton = GuiceBundle.getInjector().getInstance(servletClass);
      doGet = servletClass.getMethod("doGet", HttpServletRequest.class, HttpServletResponse.class);
      doPut = servletClass.getMethod("doPut", HttpServletRequest.class, HttpServletResponse.class);
      doPost =
          servletClass.getMethod("doPost", HttpServletRequest.class, HttpServletResponse.class);
      doDelete =
          servletClass.getMethod("doDelete", HttpServletRequest.class, HttpServletResponse.class);
      doOptions =
          servletClass.getMethod("doOptions", HttpServletRequest.class, HttpServletResponse.class);
      doHead =
          servletClass.getMethod("doHead", HttpServletRequest.class, HttpServletResponse.class);
      doTrace =
          servletClass.getMethod("doTrace", HttpServletRequest.class, HttpServletResponse.class);

      uowDoGet = doGet.getAnnotation(UnitOfWork.class);
      uowDoPut = doPut.getAnnotation(UnitOfWork.class);
      uowDoPost = doPost.getAnnotation(UnitOfWork.class);
      uowDoDelete = doDelete.getAnnotation(UnitOfWork.class);
      uowDoOptions = doOptions.getAnnotation(UnitOfWork.class);
      uowDoHead = doHead.getAnnotation(UnitOfWork.class);
      uowDoTrace = doTrace.getAnnotation(UnitOfWork.class);
    } catch (NoSuchMethodException e) {
      throw e; // Never
    }
  }