public static boolean hasContext() { return get() != null; }
public static boolean hasContext() { return Ctxs.get() != null; }
public static boolean hasContext() { return Ctxs.get() != null; }
public static Ctx required() { Ctx ctx = get(); if (ctx == null) { throw new IllegalStateException("No context is available!"); } return ctx; }
@SuppressWarnings("unchecked") public static <T> T request() { Ctx ctx = Ctxs.get(); return ctx != null ? (T) ctx.exchange() : null; }
public static void close() { try { Ctx ctx = get(); if (ctx != null) { ctx.close(); } else { // don't throw error here, the context might be "double-closed" on shutdown Log.warn("The context was already closed!"); } } finally { CTXS.remove(); } }
public static ContextPreservingJobWrapper wrap(Runnable job) { Ctx ctx = Ctxs.get(); if (ctx != null) { // increment reference counter ctx = ctx.span(); // currently the same ctx is returned } return new ContextPreservingJobWrapper(job, ctx); }
public static UserInfo user() { Ctx ctx = Ctxs.get(); UserInfo user = ctx != null ? ctx.user() : null; return U.or(user, UserInfo.ANONYMOUS); }
public static UserInfo user() { Ctx ctx = Ctxs.get(); UserInfo user = ctx != null ? ctx.user() : null; return U.or(user, UserInfo.ANONYMOUS); }
public static EntityManager em() { Ctx ctx = Ctxs.get(); if (ctx != null) { return JPAInternals.wrapEM(ctx.persister()); } else { EntityManagerFactory emf = JPAUtil.emf; U.notNull(emf, "JPA.emf"); return JPAInternals.wrapEM(emf.createEntityManager()); } }
private static JPATool tool() { Ctx ctx = Ctxs.get(); if (ctx != null) { EntityManager em = ctx.persister(); return new JPATool(em, true); } else { EntityManagerFactory emf = JPAUtil.emf(); U.must(emf != null, "JPA hasn't been initialized! (JPA.emf is null)"); EntityManager em = emf.createEntityManager(); return new JPATool(em, false); } }
private static boolean inValidContext(Req req) { Ctx ctx = Ctxs.get(); Req ctxReq = ctx != null ? (Req) ctx.exchange() : null; U.must(ctx == null || !ctx.isClosed(), "The context for request (%s) is closed!"); U.must(req == ctxReq, "The customization request (%s) doesn't match the context request (%s)!", req, ctxReq); return true; }
final Ctx ctx = Ctxs.get();
public static void transaction(Runnable action, boolean readOnly) { Ctx ctx = Ctxs.get(); boolean newContext = ctx == null; if (newContext) { ctx = Ctxs.open("transaction"); } try { EntityManager em = ctx.persister(); JPA.with(em).transactional(action, readOnly); } finally { if (newContext) { Ctxs.close(); } } }
try { Ctx existingCtx = Ctxs.get(); if (existingCtx != null) { throw U.rte("Detected context leak! Found open context (%s) on thread: %s",
HttpStatus errorAndDone(final Req req, final Throwable error, final LogLevel logLevel) { req.revert(); req.async(); Runnable errorHandler = () -> { error(req, error, logLevel); // the Req object will do the rendering req.done(); }; Ctx ctx = Ctxs.get(); if (ctx == null) { With.exchange(req).run(errorHandler); } else { Jobs.execute(errorHandler); } return HttpStatus.ASYNC; }
public static void reset() { Env.reset(); Events.reset(); Log.reset(); Crypto.reset(); Res.reset(); Conf.reset(); Groups.reset(); Jobs.reset(); Env.reset(); Caching.reset(); Ctxs.reset(); U.must(Ctxs.get() == null); resetState(); }
public static void close() { try { Ctx ctx = get(); if (ctx != null) { ctx.close(); } else { throw new RuntimeException("There is no opened context!"); } } finally { CTXS.remove(); } }
public static UserInfo user() { Ctx ctx = Ctxs.get(); UserInfo user = ctx != null ? ctx.user() : null; return U.or(user, UserInfo.ANONYMOUS); }
public static Runnable wrap(Runnable job) { Ctx ctx = Ctxs.get(); if (ctx != null) { // U.notNull(ctx.app(), "Application wasn't attached to the context!"); Object x = ctx.exchange(); if (x instanceof JobStatusListener) { ((JobStatusListener) x).onAsync(); } // increment reference counter ctx = ctx.span(); // currently the same ctx is returned } return new ContextPreservingJobWrapper(job, ctx); }