org.apache.shiro.util.ThreadState Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.util;
/**
* A {@code ThreadState} instance manages any state that might need to be bound and/or restored during a thread's
* execution.
* Usage
* Calling {@link #bind bind()} will place state on the currently executing thread to be accessed later during
* the thread's execution.
* WARNING
* After the thread is finished executing, or if an exception occurs, any previous state MUST be
* {@link #restore restored} to guarantee all threads stay clean in any thread-pooled environment. This should always
* be done in a {@code try/finally} block:
*
* ThreadState state = //acquire or instantiate as necessary
* try {
* state.bind();
* doSomething(); //execute any logic downstream logic that might need to access the state
* } finally {
* state.restore();
* }
*
*
* @since 1.0
*/
public interface ThreadState {
/**
* Binds any state that should be made accessible during a thread's execution. This should typically always
* be called in a {@code try/finally} block paired with the {@link #restore} call to guarantee that the thread
* is cleanly restored back to its original state. For example:
*
* ThreadState state = //acquire or instantiate as necessary
* try {
* state.bind();
* doSomething(); //execute any logic downstream logic that might need to access the state
* } finally {
* state.restore();
* }
*
*/
void bind();
/**
* Restores a thread to its state before bind {@link #bind bind} was invoked. This should typically always be
* called in a {@code finally} block to guarantee that the thread is cleanly restored back to its original state
* before {@link #bind bind}'s bind was called. For example:
*
* ThreadState state = //acquire or instantiate as necessary
* try {
* state.bind();
* doSomething(); //execute any logic downstream logic that might need to access the state
* } finally {
* state.restore();
* }
*
*/
void restore();
/**
* Completely clears/removes the {@code ThreadContext} state. Typically this method should
* only be called in special cases - it is more 'correct' to {@link #restore restore} a thread to its previous
* state than to clear it entirely.
*/
void clear();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy