001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems 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 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: PolicyEvaluatorFactory.java,v 1.3 2008/06/25 05:43:46 qcheng Exp $
026 *
027 */
028
029/*
030 * Portions Copyrighted [2011] [ForgeRock AS]
031 */
032package com.sun.identity.policy.client;
033
034import com.sun.identity.shared.debug.Debug;
035import java.util.HashMap;
036import java.util.Map;
037import com.sun.identity.policy.PolicyException;
038import com.sun.identity.security.AppSSOTokenProvider;
039import com.iplanet.sso.SSOException;
040
041/**
042 * This class acts as a factory to get an instance of 
043 * <code>com.sun.idenity.policy.client.PolicyEvaluator</code>
044 *
045 * @supported.all.api
046 */
047public class PolicyEvaluatorFactory {
048
049    static Debug debug = Debug.getInstance("amRemotePolicy");
050    private static PolicyEvaluatorFactory factory; //singleton instance
051    private Map evaluatorsCache;
052
053    /**
054     * Constructs a policy evaluator factory
055     */
056    private PolicyEvaluatorFactory() {
057        evaluatorsCache = new HashMap(10);
058        if (debug.messageEnabled()) {
059            debug.message("PolicyEvaluatorFactory():"
060                    + "created singleton instance");
061        }
062    }
063
064    /**
065     * Returns an instance of 
066     * <code>com.sun.identity.policy.client.PolicyEvaluatorFactory</code>
067     *
068     * @return an instance of 
069     * <code>com.sun.identity.policy.client.PolicyEvaluatorFactory</code> 
070     */
071    synchronized public static PolicyEvaluatorFactory getInstance() {
072        if (factory == null) {
073            factory = new PolicyEvaluatorFactory();
074        }
075        return factory;
076    }
077
078    /**
079     * Returns an instance of 
080     * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
081     *
082     * @param serviceName name of the service for which to get the
083     *            <code>PolicyEvaluator</code>.
084     * @return an instance of <code>PolicyEvaluator</code>.
085     * @throws PolicyException if creation of evaluator fails.
086     * @throws SSOException if application single sign on token is invalid
087     */
088    public PolicyEvaluator getPolicyEvaluator(String serviceName)
089            throws PolicyException, SSOException
090    {
091        return getPolicyEvaluator(serviceName, 
092                null); //null appSSOTokenProvider
093    }
094
095    /**
096     * Returns an instance of 
097     * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
098     *
099     * @param serviceName name of the service for which to get the
100     *        <code>com.sun.identity.policy.client.PolicyEvaluator</code> 
101     * @param appSSOTokenProvider application single sign on token Provider
102     * @return an instance of 
103     *         <code>com.sun.identity.policy.client.PolicyEvaluator</code> 
104     * @throws PolicyException if creation of evaluator fails.
105     * @throws SSOException if application single sign on token is invalid.
106     */
107    synchronized public PolicyEvaluator getPolicyEvaluator(
108        String serviceName,
109        AppSSOTokenProvider appSSOTokenProvider)
110        throws PolicyException, SSOException
111    {
112        PolicyEvaluator pe = null;
113        if (serviceName == null) {
114            if (debug.warningEnabled()) {
115                debug.warning("PolicyEvaluatorFactory.getPolicyEvaluator():"
116                        + "serviceName is null");
117            }
118            return null;
119        } //else do the following
120
121        Map appTokenEvaluatorsMap = (Map)evaluatorsCache.get(serviceName);
122        if (appTokenEvaluatorsMap == null) {
123            appTokenEvaluatorsMap = new HashMap(5);
124            evaluatorsCache.put(serviceName, appTokenEvaluatorsMap);
125        }
126        pe = (PolicyEvaluator)appTokenEvaluatorsMap.get(appSSOTokenProvider);
127        if ( pe == null) {
128            if (debug.messageEnabled()) {
129                debug.message("PolicyEvaluatorFactory.getPolicyEvaluator():"
130                        + "serviceName=" + serviceName
131                        + ":appSSOTokenProvider=" +appSSOTokenProvider
132                        + ":creating new PolicyEvaluator");
133            }
134            pe = new PolicyEvaluator(serviceName, appSSOTokenProvider);
135            appTokenEvaluatorsMap.put(appSSOTokenProvider, pe);
136        } else {
137            if (debug.messageEnabled()) {
138                debug.message("PolicyEvaluatorFactory.getPolicyEvaluator():"
139                        + "serviceName=" + serviceName
140                        + ":appSSOTokenProvider=" +appSSOTokenProvider
141                        + ":returning PolicyEvaluator from cache");
142            }
143        }
144        return pe;
145    }
146}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.