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 * Portions Copyrighted 2011-2015 ForgeRock AS.
028 */
029
030package com.sun.identity.policy.client;
031
032import com.sun.identity.shared.debug.Debug;
033import java.util.HashMap;
034import java.util.Map;
035import com.sun.identity.policy.PolicyException;
036import com.sun.identity.security.AppSSOTokenProvider;
037import com.iplanet.sso.SSOException;
038
039/**
040 * This class acts as a factory to get an instance of 
041 * <code>com.sun.idenity.policy.client.PolicyEvaluator</code>
042 *
043 * @supported.all.api
044 */
045public class PolicyEvaluatorFactory {
046
047    static Debug debug = Debug.getInstance("amRemotePolicy");
048    private static PolicyEvaluatorFactory factory; //singleton instance
049    private Map evaluatorsCache;
050
051    /**
052     * Constructs a policy evaluator factory
053     */
054    private PolicyEvaluatorFactory() {
055        evaluatorsCache = new HashMap(10);
056        if (debug.messageEnabled()) {
057            debug.message("PolicyEvaluatorFactory():"
058                    + "created singleton instance");
059        }
060    }
061
062    /**
063     * Returns an instance of 
064     * <code>com.sun.identity.policy.client.PolicyEvaluatorFactory</code>
065     *
066     * @return an instance of 
067     * <code>com.sun.identity.policy.client.PolicyEvaluatorFactory</code> 
068     */
069    synchronized public static PolicyEvaluatorFactory getInstance() {
070        if (factory == null) {
071            factory = new PolicyEvaluatorFactory();
072        }
073        return factory;
074    }
075
076    /**
077     * Returns an instance of 
078     * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
079     *
080     * @param serviceName name of the service for which to get the
081     *            <code>PolicyEvaluator</code>.
082     * @return an instance of <code>PolicyEvaluator</code>.
083     * @throws PolicyException if creation of evaluator fails.
084     * @throws SSOException if application single sign on token is invalid
085     */
086    public PolicyEvaluator getPolicyEvaluator(String serviceName)
087            throws PolicyException, SSOException
088    {
089        return getPolicyEvaluator(serviceName, 
090                null); //null appSSOTokenProvider
091    }
092
093    /**
094     * Returns an instance of 
095     * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
096     *
097     * @param serviceName name of the service for which to get the
098     *        <code>com.sun.identity.policy.client.PolicyEvaluator</code> 
099     * @param appSSOTokenProvider application single sign on token Provider
100     * @return an instance of 
101     *         <code>com.sun.identity.policy.client.PolicyEvaluator</code> 
102     * @throws PolicyException if creation of evaluator fails.
103     * @throws SSOException if application single sign on token is invalid.
104     */
105    synchronized public PolicyEvaluator getPolicyEvaluator(
106        String serviceName,
107        AppSSOTokenProvider appSSOTokenProvider)
108        throws PolicyException, SSOException
109    {
110        PolicyEvaluator pe = null;
111        if (serviceName == null) {
112            if (debug.warningEnabled()) {
113                debug.warning("PolicyEvaluatorFactory.getPolicyEvaluator():"
114                        + "serviceName is null");
115            }
116            return null;
117        } //else do the following
118
119        Map appTokenEvaluatorsMap = (Map)evaluatorsCache.get(serviceName);
120        if (appTokenEvaluatorsMap == null) {
121            appTokenEvaluatorsMap = new HashMap(5);
122            evaluatorsCache.put(serviceName, appTokenEvaluatorsMap);
123        }
124        pe = (PolicyEvaluator)appTokenEvaluatorsMap.get(appSSOTokenProvider);
125        if ( pe == null) {
126            if (debug.messageEnabled()) {
127                debug.message("PolicyEvaluatorFactory.getPolicyEvaluator():"
128                        + "serviceName=" + serviceName
129                        + ":appSSOTokenProvider=" +appSSOTokenProvider
130                        + ":creating new PolicyEvaluator");
131            }
132            pe = PolicyEvaluator.getInstance(serviceName, appSSOTokenProvider);
133            appTokenEvaluatorsMap.put(appSSOTokenProvider, pe);
134        } else {
135            if (debug.messageEnabled()) {
136                debug.message("PolicyEvaluatorFactory.getPolicyEvaluator():"
137                        + "serviceName=" + serviceName
138                        + ":appSSOTokenProvider=" +appSSOTokenProvider
139                        + ":returning PolicyEvaluator from cache");
140            }
141        }
142        return pe;
143    }
144}