8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: PolicyEvaluatorFactory.java,v 1.2 2008/06/25 05:43:44 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.policy;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.Cache;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/** Factory class used to get PolicyEvaluator instances.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This is a singleton class.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterclass PolicyEvaluatorFactory {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final int CACHE_SIZE = 100;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static PolicyEvaluatorFactory policyEvaluatorFactory;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Cache evaluatorCache;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Private constructor, disables instances being created from outside
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of this class
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private PolicyEvaluatorFactory() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster evaluatorCache = new Cache(CACHE_SIZE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets an instance of PolicyEvaluatorFactory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>PolicyEvaluatorFactory</code> object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException any policy exception coming from policy frame
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * work
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized public static PolicyEvaluatorFactory getInstance()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (policyEvaluatorFactory == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyEvaluatorFactory = new PolicyEvaluatorFactory();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return policyEvaluatorFactory;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets an instance of PolicyEValuator
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token sso token used to get the policy evaluator
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceType service type for which get the proxy policy
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * evaluator
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>PolicyEvaluator</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SSOException if the token is invalid
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NameNotFoundException if the <code>serviceType</code> is not
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * found in registered service types
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException any policy exception coming from policy frame
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * work
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized public PolicyEvaluator getPolicyEvaluator(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SSOToken token, String serviceType)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws NameNotFoundException,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyException, SSOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyEvaluator pe
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = (PolicyEvaluator)evaluatorCache.get(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pe == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.message(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " User: " + token.getPrincipal().getName()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + " created policy evaluator (using "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + " PolicyEvaluatorFactory) for "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + " for serviceType: " + serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster pe = new PolicyEvaluator(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster evaluatorCache.put(serviceType, pe);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.message(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " User: " + token.getPrincipal().getName()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + " got policy evaluator "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + " (using PolicyEvaluatorFactory) for "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + " for serviceType: " + serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return pe;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}