/** * Convenience method that simplifies removal of a thread-local Subject from the thread. * <p/> * The implementation just helps reduce casting and remembering of the ThreadContext key name, i.e it is * merely a conveient wrapper for the following: * <p/> * <code>return (Subject)remove( SUBJECT_KEY );</code> * <p/> * If you wish to just retrieve the object from the thread without removing it (so it can be retrieved later during * thread execution), you should use the {@link #getSubject() getSubject()} method for that purpose. * * @return the Subject object previously bound to the thread, or <tt>null</tt> if there was none bound. * @since 0.2 */ public static Subject unbindSubject() { return (Subject) remove(SUBJECT_KEY); }
/** * Convenience method that simplifies removal of the application's SecurityManager instance from the thread. * <p/> * The implementation just helps reduce casting and remembering of the ThreadContext key name, i.e it is * merely a conveient wrapper for the following: * <p/> * <code>return (SecurityManager)remove( SECURITY_MANAGER_KEY );</code> * <p/> * If you wish to just retrieve the object from the thread without removing it (so it can be retrieved later * during thread execution), use the {@link #getSecurityManager() getSecurityManager()} method instead. * * @return the application's SecurityManager instance previously bound to the thread, or <tt>null</tt> if there * was none bound. * @since 0.9 */ public static SecurityManager unbindSecurityManager() { return (SecurityManager) remove(SECURITY_MANAGER_KEY); }
/** * Binds <tt>value</tt> for the given <code>key</code> to the current thread. * <p/> * <p>A <tt>null</tt> <tt>value</tt> has the same effect as if <tt>remove</tt> was called for the given * <tt>key</tt>, i.e.: * <p/> * <pre> * if ( value == null ) { * remove( key ); * }</pre> * * @param key The key with which to identify the <code>value</code>. * @param value The value to bind to the thread. * @throws IllegalArgumentException if the <code>key</code> argument is <tt>null</tt>. */ public static void put(Object key, Object value) { if (key == null) { throw new IllegalArgumentException("key cannot be null"); } if (value == null) { remove(key); return; } resources.get().put(key, value); if (log.isTraceEnabled()) { String msg = "Bound value of type [" + value.getClass().getName() + "] for key [" + key + "] to thread " + "[" + Thread.currentThread().getName() + "]"; log.trace(msg); } }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpservletRequest = (HttpServletRequest) servletRequest; URL requestURL = URI.create(httpservletRequest.getRequestURL().toString()).toURL(); String currentResourceURI = ((Request) httpservletRequest).getHttpURI().getPathQuery(); ThreadContext.put("currentResourceURI", currentResourceURI); ThreadContext.put("currentURLProtocol", requestURL.getProtocol()); ThreadContext.put("currentURLHost", requestURL.getHost()); ThreadContext.put("currentURLPort", requestURL.getPort()); filterChain.doFilter(servletRequest, servletResponse); ThreadContext.remove(); }
@Override public <T> Future<T> submitCallable(final Callable<T> callable, final IFinishedExecution<T> callback, final Map<Object, Object> threadBindings) { return getExecutorService().submit(() -> { try { if (threadBindings != null) { ThreadContext.setResources(threadBindings); } final T result = callable.call(); callback.onComplete(result); return result; } catch (Throwable t) { log.error(t.getLocalizedMessage(), t); callback.onFailure(t); return null; } finally { ThreadContext.remove(); } }); }