001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2010-2013 ForgeRock, Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * http://forgerock.org/license/CDDLv1.0.html
013 * See the License for the specific language governing
014 * permission and limitations under the License.
015 *
016 * When distributing Covered Code, include this CDDL
017 * Header Notice in each file and include the License file
018 * at http://forgerock.org/license/CDDLv1.0.html
019 * If applicable, add the following below the CDDL Header,
020 * with the fields enclosed by brackets [] replaced by
021 * your own identifying information:
022 * "Portions Copyrighted [year] [name of copyright owner]"
023 *
024 */
025
026package org.forgerock.openam.entitlement;
027
028import com.sun.identity.entitlement.ConditionDecision;
029import java.io.Serializable;
030import java.util.HashMap;
031import java.util.Map;
032
033/**
034 * Holds the context of the policy evaluation making it available to policy
035 * conditions.
036 *
037 * @author Steve Ferris steve.ferris@forgerock.com
038 * @supported.all.api
039 */
040public class PrivilegeEvaluatorContext implements Serializable {
041    private String realm;
042    private String resourceName;
043    private String applicationName;
044    private Map<String, Object> attributes = new HashMap(2);
045    /**
046     * An entitlement condition decision cache, where the condition decisions are cached based on the condition's JSON
047     * representation.
048     */
049    private Map<String, ConditionDecision> conditionDecisionCache = new HashMap<String, ConditionDecision>();
050    private static ThreadLocal <PrivilegeEvaluatorContext> currentCtx = new ThreadLocal();
051
052    /**
053     * Creates a new Privilege Evaluator Context
054     *
055     * @param realm The realm of the policy evaluation
056     * @param resourceName The resource being evaluated
057     * @param applicationName The application being evaluated
058     */
059    public PrivilegeEvaluatorContext(String realm,
060                                     String resourceName,
061                                     String applicationName) {
062        this.realm = realm;
063        this.resourceName = resourceName;
064        this.applicationName = applicationName;
065    }
066
067   /**
068     * Returns the current context of the running thread
069     *
070     * @return object containing the current context
071     */
072    public static PrivilegeEvaluatorContext getCurrent() {
073        return currentCtx.get();
074    }
075
076    /**
077     * Set the current context of the running thread
078     */
079    public static void setCurrent(PrivilegeEvaluatorContext ctx) {
080        currentCtx.set(ctx);
081    }
082
083    /**
084     * Return the realm
085     *
086     * @return The realm
087     */
088    public String getRealm() {
089        return realm;
090    }
091
092    /**
093     * Return the resource name
094     *
095     * @return The resource name
096     */
097    public String getResourceName() {
098        return resourceName;
099    }
100
101    /**
102     * Return the application name
103     *
104     * @return The application name
105     */
106    public String getApplicationName() {
107        return applicationName;
108    }
109
110    /**
111     * Set an attribute on the context
112     *
113     * @param key The key of the attribute
114     * @param value The value
115     */
116    public void setAttribute(String key, Object value) {
117        attributes.put(key, value);
118    }
119
120    /**
121     * Fetch an attribute from the map
122     *
123     * @param key The key of the attribute
124     * @return The value
125     */
126    public Object getAttribute(String key) {
127        return attributes.get(key);
128    }
129
130    /**
131     * Return the condition decision cache.
132     *
133     * @return the condition decision cache.
134     */
135    public Map<String, ConditionDecision> getConditionDecisionCache() {
136        return conditionDecisionCache;
137    }
138}