public static void reset() { validateInContext(null); MANAGED_THREAD_LOCAL.remove(); }
@Override public T get() { validateInContext(this); final Map<Object, Object> map = MANAGED_THREAD_LOCAL.get(); Object result = map.get(this); if (result == null) { result = initialValue(); map.put(this, result); } return (T) result; }
public static CallContext unregister() { final CallContext context = s_currentContext.get(); if (context == null) { return null; } s_currentContext.remove(); if (s_logger.isTraceEnabled()) { s_logger.trace("Unregistered: " + context); } MDC.remove("ctx"); final Stack<CallContext> stack = s_currentContextStack.get(); stack.pop(); if (!stack.isEmpty()) { s_currentContext.set(stack.peek()); } else { s_currentContext.set(null); } return context; }
public static LogContext current() { LogContext context = s_currentContext.get(); if (context == null) { context = registerSystemLogContextOnceOnly(); } return context; }
public static LogContext registerPlaceHolderContext() { final LogContext context = new LogContext(0, 0, UUID.randomUUID().toString()); s_currentContext.set(context); return context; }
@Before public void init() { ManagedThreadLocal.setValidateInContext(false); this.context = new DefaultManagedContext(); }
ManagedThreadLocal.reset();
protected static CallContext register(final User callingUser, final Account callingAccount, final Long userId, final Long accountId, final String contextId) { /* Unit tests will have multiple times of setup/tear-down call to this, remove assertions to all unit test to run assert s_currentContext.get() == null : "There's a context already so what does this new register context mean? " + s_currentContext.get().toString(); if (s_currentContext.get() != null) { // FIXME: This should be removed soon. I added this check only to surface all the places that have this problem. throw new CloudRuntimeException("There's a context already so what does this new register context mean? " + s_currentContext.get().toString()); } */ CallContext callingContext = null; if (userId == null || accountId == null) { callingContext = new CallContext(callingUser, callingAccount, contextId); } else { callingContext = new CallContext(userId, accountId, contextId); } s_currentContext.set(callingContext); MDC.put("ctx", " (ctx: " + UuidUtils.first(contextId) + ")"); if (s_logger.isTraceEnabled()) { s_logger.trace("Registered: " + callingContext); } s_currentContextStack.get().push(callingContext); return callingContext; }
public static CallContext current() { CallContext context = s_currentContext.get(); // TODO other than async job and api dispatches, there are many system background running threads // that do not setup CallContext at all, however, many places in code that are touched by these background tasks // assume not-null CallContext. Following is a fix to address therefore caused NPE problems // // There are security implications with this. It assumes that all system background running threads are // indeed have no problem in running under system context. // if (context == null) { context = registerSystemCallContextOnceOnly(); } return context; }
@Override public void set(final T value) { validateInContext(this); final Map<Object, Object> map = MANAGED_THREAD_LOCAL.get(); map.put(this, value); }
protected static LogContext register(final User callingUser, final Account callingAccount, final Long userId, final Long accountId, final String contextId) { LogContext callingContext = null; if (userId == null || accountId == null) { callingContext = new LogContext(callingUser, callingAccount, contextId); } else { callingContext = new LogContext(userId, accountId, contextId); } s_currentContext.set(callingContext); MDC.put("logcontextid", " (logid: " + UuidUtils.first(contextId) + ")"); if (s_logger.isTraceEnabled()) { s_logger.trace("Registered for log: " + callingContext); } return callingContext; }
public static LogContext registerSystemLogContextOnceOnly() { try { final LogContext context = s_currentContext.get(); if (context == null) { return register(null, null, User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, UUID.randomUUID().toString()); } assert context.getCallingUserId() == User.UID_SYSTEM : "You are calling a very specific method that registers a one time system context. This method is meant for " + "background threads that does processing."; return context; } catch (final Exception e) { s_logger.error("Failed to register the system log context.", e); throw new CloudRuntimeException("Failed to register system log context", e); } }
public static CallContext registerSystemCallContextOnceOnly() { try { final CallContext context = s_currentContext.get(); if (context == null) { return register(null, null, User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, UUID.randomUUID().toString()); } assert context.getCallingUserId() == User.UID_SYSTEM : "You are calling a very specific method that registers a one time system context. This method is meant for " + "background threads that does processing."; return context; } catch (final Exception e) { s_logger.error("Failed to register the system call context.", e); throw new CloudRuntimeException("Failed to register system call context", e); } }