All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.util.security.propagation.context.SecurityCurrent Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2006-2012 Bull S.A.S.
 *
 * Licensed 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.ow2.util.security.propagation.context;

import org.ow2.util.security.api.ISecurityContext;
import org.ow2.util.security.api.ISecurityCurrent;

/**
 * Manages the current security context associated to the current thread.
 * @author Florent Benoit
 */
public class SecurityCurrent implements ISecurityCurrent {

    /**
     * Inherited Local thread used to keep the security context.
     */
    private static InheritableThreadLocal threadLocal;

    /**
     * Static Security Context that is applied on all threads (used for heavy
     * client).
     */
    private static ISecurityContext globalContext = null;

    /**
     * Default security context.
     */
    private static final ISecurityContext DEFAULT_CTX = new SecurityContext();

    /**
     * Init the thread
     */
    static {
        threadLocal = new InheritableThreadLocal();
        threadLocal.set(new SecurityContext());
    }

    /**
     * Unique instance of this current object.
     */
    private static ISecurityCurrent unique = null;

    /**
     * Return the unique instance of this object.
     * @return SecurityCurrent return the current
     */
    public static ISecurityCurrent getCurrent() {
        if (unique == null) {
            // Build a default implementation
            unique = new SecurityCurrent();
        }
        return unique;
    }

    /**
     * Associates the given security context to the current thread.
     * @param securityContext Security context to associate to the current
     *        thread.
     */
    public void setSecurityContext(final ISecurityContext securityContext) {
        threadLocal.set(securityContext);
    }

    /**
     * Associates the given security context to all threads (JVM).
     * @param securityContext Security context to associate to the JVM
     */
    public static void setGlobalSecurityContext(final ISecurityContext securityContext) {
        globalContext = securityContext;
    }

    /**
     * Gets the current context.
     * @return SecurityContext return the Security context associated to the
     *         current thread or the JVM
     */
    public ISecurityContext getSecurityContext() {
        if (globalContext != null) {
            return globalContext;
        }
        if (threadLocal.get() != null) {
            return threadLocal.get();
        }

        // else, never null context.
        return DEFAULT_CTX;
    }


    /**
     * Sets the security current instance to use.
     * @param current the given instance.
     */
    public static void setSecurityCurrent(final ISecurityCurrent current) {
        if (unique != null) {
            throw new IllegalStateException("Unable to set the unique instance. It is already set");
        }
        unique = current;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy