WaitingBlock(Runnable cmd) { this.cmd = cmd; this.waitline = System.currentTimeMillis() + 1000; this.context = CallContext.getCurrent(); }
/** * Returns the currently active language as two-letter code. * * @return a two-letter code of the currently active language, as defined in * {@link sirius.kernel.async.CallContext#getLang()} */ @Nonnull public static String getCurrentLang() { return CallContext.getCurrent().getLang(); }
/** * Prepares the execution of this task while checking all preconditions. */ void prepare() { if (fork) { ctx = CallContext.getCurrent().fork(); } if (runnable == null) { throw new IllegalArgumentException("Please provide a runnable for me to execute!"); } }
/** * Generates a new TaskContext. * <p> * Normally this is should only be invoked by {@link CallContext}. Use {@link CallContext#get(Class)} to obtain an * instance. */ public TaskContext() { this.adapter = new BasicTaskContextAdapter(this); this.parent = CallContext.getCurrent(); }
/** * Provides access to the <tt>TaskContext</tt> for the current thread. * <p> * This is boilerplate for {@code CallContext.getCurrent().get(TaskContext.class)} * * @return the task context for the current thread */ public static TaskContext get() { return CallContext.getCurrent().get(TaskContext.class); }
private static ExecutionPoint snapshot(boolean fast) { ExecutionPoint result = new ExecutionPoint(); result.threadName = Thread.currentThread().getName(); result.stacktraceHolder = new Exception(); if (!fast) { result.mdc = CallContext.getCurrent().getMDC(); } return result; }
protected <E extends Entity> void traceChange(E entity) { if (!traceOptimisticLockErrors) { return; } IndexTrace trace = new IndexTrace(); trace.threadName = Thread.currentThread().getName(); trace.id = entity.getId(); trace.type = entity.getClass().getName(); trace.timestamp = System.currentTimeMillis(); trace.mdc = CallContext.getCurrent().getMDC(); trace.stackTrace = Thread.currentThread().getStackTrace(); traces.put(trace.type + "-" + trace.id, trace); }
/** * Creates a new XMLReader. * <p> * Use {@link #addHandler(String, NodeHandler)} tobind handlers to tags and then call one of the <tt>parse</tt> * methods to process the XML file. * <p> * To interrupt processing use {@link TaskContext#cancel()}. */ public XMLReader() { try { documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); taskContext = CallContext.getCurrent().get(TaskContext.class); } catch (ParserConfigurationException e) { throw Exceptions.handle(e); } }
/** * Queues the given task to be called after roughly the number of seconds given here. * <p> * In contrast to {@link #callDelayed(String, long, Runnable)}, this will preserve the {@link CallContext} when * invoking the <tt>task</tt>. * </p> * * @param executor the executor to execute the task in. Use {@link Tasks#DEFAULT} is no other appropriate * pool * is available. * @param delayInSeconds the number to wait in seconds. Note that the delay can be a bit longer, depending on the * system load. * @param task the task to execute */ public void forkDelayed(@Nonnull String executor, long delayInSeconds, @Nonnull Runnable task) { CallContext currentContext = CallContext.getCurrent(); callDelayed(executor, delayInSeconds, () -> { CallContext backup = CallContext.getCurrent(); try { CallContext.setCurrent(currentContext); task.run(); } finally { CallContext.setCurrent(backup); } }); }
/** * Returns the two-letter code of the fall back language. Provided via the {@link CallContext}. If the value is * empty, {@link NLS#getDefaultLanguage} is returned. * * @return the language code of the fallback language */ @Nonnull public static String getFallbackLanguage() { String fallback = CallContext.getCurrent().getFallbackLang(); if (Strings.isEmpty(fallback)) { return getDefaultLanguage(); } return fallback; }
for (Tuple<String, String> t : CallContext.getCurrent().getMDC()) { msg.append(t.getFirst()).append(": ").append(t.getSecond()).append("\n");
private void fixMDC() { if (logger.isDebugEnabled() || Sirius.isDev() || Sirius.isStartedAsTest()) { CallContext callContext = CallContext.getCurrent(); MDC.put("flow", "|" + callContext.getWatch().elapsedMillis() + "ms"); } }
private void notifyHandlers(HandledException result) { // Injector might not have run yet if (handlers == null || Boolean.TRUE.equals(frozen.get())) { return; } try { frozen.set(Boolean.TRUE); String location = computeLocation(result); for (ExceptionHandler handler : handlers) { try { handler.handle(new Incident(log.getName(), location, CallContext.getCurrent().getMDC(), result)); } catch (Exception e) { // Just log the exception - anything else might call a rather long infinite loop LOG.SEVERE(new Exception(Strings.apply( "An error occurred while calling the ExceptionHandler: %s - %s (%s)", handler, e.getMessage(), e.getClass().getName()), e)); } } } finally { frozen.set(Boolean.FALSE); } }
/** * Writes an API success entry to the log */ private void signalObjectSuccess(WebContext ctx) { log.log(ctx.getRequest().method().name(), ctx.getRequestedURI(), APILog.Result.OK, CallContext.getCurrent().getWatch()); }
dropped.inc(); } else if (wrapper.synchronizer == null) { CallContext current = CallContext.getCurrent(); try { wrapper.run();
private boolean objectCheckAuth(WebContext ctx, Bucket bucket) { String hash = getAuthHash(ctx); if (hash != null) { String expectedHash = hashCalculator.computeHash(ctx, ""); String alternativeHash = hashCalculator.computeHash(ctx, "/s3"); if (!expectedHash.equals(hash) && !alternativeHash.equals(hash)) { ctx.respondWith() .error(HttpResponseStatus.UNAUTHORIZED, Strings.apply("Invalid Hash (Expected: %s, Found: %s)", expectedHash, hash)); log.log(ctx.getRequest().method().name(), ctx.getRequestedURI(), APILog.Result.REJECTED, CallContext.getCurrent().getWatch()); return false; } } if (bucket.isPrivate() && !ctx.get("noAuth").isFilled() && hash == null) { ctx.respondWith().error(HttpResponseStatus.UNAUTHORIZED, "Authentication required"); log.log(ctx.getRequest().method().name(), ctx.getRequestedURI(), APILog.Result.REJECTED, CallContext.getCurrent().getWatch()); return false; } return true; }
/** * Writes an API error to the log */ private void signalObjectError(WebContext ctx, HttpResponseStatus status, String message) { if (ctx.getRequest().method() == HEAD) { ctx.respondWith().status(status); } else { ctx.respondWith().error(status, message); } log.log(ctx.getRequest().method().name(), message + " - " + ctx.getRequestedURI(), APILog.Result.ERROR, CallContext.getCurrent().getWatch()); }