/**
  * Only call this if you plan on managing the whole EntityManagerFactory lifecycle yourself.
  * Regular thundr apps should have no need to create these themselves and should simply declare an
  * injection dependency on EntityManagerHelper. If you do choose to manage this yourself be sure
  * to call <code>#destroy()</code> to ensure all resources are properly cleaned up.
  *
  * @param persistenceUnit the name of a persistence unit to initialize the EntityManagerFactory
  *     with
  */
 public PersistenceManagerImpl(String persistenceUnit) {
   String className = PersistenceManagerImpl.class.getName();
   try {
     entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
     threadLocal = new ThreadLocal<EntityManager>();
     Logger.debug("%s initialized.", className);
   } catch (Exception e) {
     Logger.error("Initialization of %s failed: %s", className, e.getMessage());
     throw new JpaException(e, "Initialization of %s failed.", className);
   }
 }
Exemplo n.º 2
0
 @Override
 public Object resolve(
     MethodAction action,
     RouteType routeType,
     HttpServletRequest req,
     HttpServletResponse resp,
     Map<String, String> pathVars)
     throws ActionException {
   Object controller = getOrCreateController(action);
   Map<Annotation, ActionInterceptor<Annotation>> interceptors = action.interceptors();
   Object result = null;
   Exception exception = null;
   try {
     result = beforeInterceptors(interceptors, req, resp);
     List<?> arguments = bindArguments(action, req, resp, pathVars);
     result = result != null ? result : action.invoke(controller, arguments);
     result = afterInterceptors(result, interceptors, req, resp);
   } catch (InvocationTargetException e) {
     // we need to unwrap InvocationTargetExceptions to get at the real exception
     exception = Cast.as(e.getTargetException(), Exception.class);
     if (exception == null) {
       throw new BaseException(e);
     }
   } catch (Exception e) {
     exception = e;
   }
   if (exception != null) {
     result = exceptionInterceptors(interceptors, req, resp, exception);
     if (result == null) {
       throw new ActionException(exception, "Failed in %s: %s", action, exception.getMessage());
     }
   }
   Logger.debug("%s -> %s resolved", req.getRequestURI(), action);
   return result;
 }
Exemplo n.º 3
0
 @Override
 public <A extends Annotation> void registerInterceptor(
     Class<A> annotation, ActionInterceptor<A> interceptor) {
   actionInterceptors.put(annotation, interceptor);
   Logger.info(
       "Added ActionInterceptor %s for methods annotated with %s", interceptor, annotation);
 }
 @Override
 public void destroy() {
   entityManagerFactory.close();
   entityManagerFactory = null;
   threadLocal = null;
   Logger.debug("%s destroyed.", PersistenceManagerImpl.class.getName());
 }
Exemplo n.º 5
0
  @Override
  public Result<T, K> createSearchResult(SearchImpl<T, K> searchRequest) {
    String queryString = buildQueryString(searchRequest.query());
    SortOptions.Builder sortOptions = buildSortOptions(searchRequest.order());

    QueryOptions.Builder queryOptions = QueryOptions.newBuilder();
    Integer limit = searchRequest.limit();
    int offset = 0;
    if (limit != null) {
      offset = searchRequest.offset() == null ? 0 : searchRequest.offset();
      int effectiveLimit = limit + offset;
      if (effectiveLimit > 1000) {
        Logger.warn(
            "Currently the Google Search API does not support queries with a limit over 1000. With an offset of %d and a limit of %d, you have an effective limit of %d",
            offset, limit, effectiveLimit);
      }
      limit = effectiveLimit;
      /* Note, this can't be more than 1000 (Crashes) */
      queryOptions = queryOptions.setLimit(limit);
    }
    if (searchRequest.accuracy() != null) {
      queryOptions.setNumberFoundAccuracy(searchRequest.accuracy());
    }
    queryOptions.setSortOptions(sortOptions);
    Query query = Query.newBuilder().setOptions(queryOptions).build(queryString);
    Future<Results<ScoredDocument>> searchAsync = getIndex().searchAsync(query);
    return new ResultImpl<T, K>(this, searchAsync, searchRequest.offset());
  }
  @Override
  public EntityManager getEntityManager() throws IllegalStateException {
    if (entityManagerFactory == null || !entityManagerFactory.isOpen()) {
      throw new IllegalStateException(
          "EntityManagerFactory is closed. You must now dispose of this instance.");
    }

    try {
      EntityManager em = threadLocal.get();
      if (em == null) {
        em = entityManagerFactory.createEntityManager();
        threadLocal.set(em);
      }
      return em;
    } catch (Exception e) {
      Logger.error("Error creating entity manager: %s", e.getMessage());
      throw new JpaException(e, "Error creating entity manager: %s", e.getMessage());
    }
  }