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: ProxyPolicyEvaluatorFactory.java,v 1.2 2008/06/25 05:43:44 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.policy;
031
032import com.iplanet.sso.SSOException;
033import com.iplanet.sso.SSOToken;
034import com.iplanet.am.util.Cache;
035
036/**
037 * Factory class used to get ProxyPolicyEvaluator instances. 
038 * This is a singleton.
039 *
040 * @supported.all.api
041 */
042public class ProxyPolicyEvaluatorFactory {
043
044    private static final int CACHE_SIZE = 100;
045
046    private static ProxyPolicyEvaluatorFactory proxyPolicyEvaluatorFactory;
047
048    private Cache evaluatorCache;
049
050    /**
051     * Private constructor, disables instances being created from outside
052     * of this class 
053     */
054    private ProxyPolicyEvaluatorFactory() {
055        evaluatorCache = new Cache(CACHE_SIZE);
056    }
057
058    /**
059     * Gets an instance of ProxyPolicyEvaluatorFactory. 
060     *
061     * @return proxy policy evaluator factory
062     * @throws PolicyException any policy exception coming from policy frame 
063     *                         work
064     */
065    synchronized public static ProxyPolicyEvaluatorFactory getInstance() 
066        throws PolicyException
067    {
068        if (proxyPolicyEvaluatorFactory == null) {
069            proxyPolicyEvaluatorFactory = new ProxyPolicyEvaluatorFactory();
070        }
071        return proxyPolicyEvaluatorFactory;
072    }
073
074    /**
075     * Gets an instance of <code>ProxyPolicyEvaluator</code>.
076     * Only privileged users can get <code>ProxyPolicyEvaluator</code>. 
077     * Only top level admin, realm admin or policy admin can get
078     * <code>ProxyPolicyEvaluator</code>.
079     *
080     * @param token sso token used to get the proxy policy evaluator
081     * @param serviceType service type for which get the proxy policy 
082     *                    evaluator 
083     * @return proxy policy evaluator 
084     * @throws SSOException if the token is invalid
085     * @throws NoPermissionException if the token does not have privileges 
086     *                               to get proxy policy evaluator
087     * @throws NameNotFoundException if the serviceType is not found in
088     *         registered service types
089     *         
090     * @throws PolicyException any policy exception coming from policy frame 
091     *                         work
092     */
093    synchronized public ProxyPolicyEvaluator getProxyPolicyEvaluator(
094        SSOToken token, String serviceType) 
095        throws NoPermissionException, NameNotFoundException, 
096        PolicyException, SSOException 
097    {
098        String key = token.getTokenID().toString() + ":" + serviceType;
099        ProxyPolicyEvaluator ppe 
100                = (ProxyPolicyEvaluator)evaluatorCache.get(key);
101        if (ppe == null) {
102            if (PolicyManager.debug.messageEnabled()) {
103                PolicyManager.debug.message(
104                        " Admin: " + token.getPrincipal().getName()
105                        + " created proxy policy evaluator for "
106                        + " for serviceType: " + serviceType);
107            }
108            ppe = new ProxyPolicyEvaluator(token, serviceType);
109            evaluatorCache.put(key, ppe);
110        }
111        if (PolicyManager.debug.messageEnabled()) {
112            PolicyManager.debug.message(
113                    " Admin: " + token.getPrincipal().getName()
114                    + " gotproxy policy evaluator for "
115                    + " for serviceType: " + serviceType);
116        }
117        return ppe;
118    }
119}