a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*
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.3 2008/06/25 05:43:46 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington * Portions Copyrighted 2011-2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.policy.client;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.policy.PolicyException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.security.AppSSOTokenProvider;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This class acts as a factory to get an instance of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.idenity.policy.client.PolicyEvaluator</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.all.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class PolicyEvaluatorFactory {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static Debug debug = Debug.getInstance("amRemotePolicy");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static PolicyEvaluatorFactory factory; //singleton instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map evaluatorsCache;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a policy evaluator factory
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private PolicyEvaluatorFactory() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster evaluatorsCache = new HashMap(10);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("PolicyEvaluatorFactory():"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "created singleton instance");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns an instance of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.policy.client.PolicyEvaluatorFactory</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return an instance of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.policy.client.PolicyEvaluatorFactory</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized public static PolicyEvaluatorFactory getInstance() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (factory == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster factory = new PolicyEvaluatorFactory();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return factory;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns an instance of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceName name of the service for which to get the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>PolicyEvaluator</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return an instance of <code>PolicyEvaluator</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws PolicyException if creation of evaluator fails.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SSOException if application single sign on token is invalid
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public PolicyEvaluator getPolicyEvaluator(String serviceName)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return getPolicyEvaluator(serviceName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster null); //null appSSOTokenProvider
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns an instance of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceName name of the service for which to get the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param appSSOTokenProvider application single sign on token Provider
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return an instance of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>com.sun.identity.policy.client.PolicyEvaluator</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws PolicyException if creation of evaluator fails.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SSOException if application single sign on token is invalid.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized public PolicyEvaluator getPolicyEvaluator(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AppSSOTokenProvider appSSOTokenProvider)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyEvaluator pe = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (serviceName == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.warningEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.warning("PolicyEvaluatorFactory.getPolicyEvaluator():"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "serviceName is null");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } //else do the following
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map appTokenEvaluatorsMap = (Map)evaluatorsCache.get(serviceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (appTokenEvaluatorsMap == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster appTokenEvaluatorsMap = new HashMap(5);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster evaluatorsCache.put(serviceName, appTokenEvaluatorsMap);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster pe = (PolicyEvaluator)appTokenEvaluatorsMap.get(appSSOTokenProvider);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( pe == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("PolicyEvaluatorFactory.getPolicyEvaluator():"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "serviceName=" + serviceName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + ":appSSOTokenProvider=" +appSSOTokenProvider
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + ":creating new PolicyEvaluator");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
6033fe2cfc1c391360277704d2c66456a33e9446sachiko pe = PolicyEvaluator.getInstance(serviceName, appSSOTokenProvider);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster appTokenEvaluatorsMap.put(appSSOTokenProvider, pe);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("PolicyEvaluatorFactory.getPolicyEvaluator():"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "serviceName=" + serviceName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + ":appSSOTokenProvider=" +appSSOTokenProvider
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + ":returning PolicyEvaluator from cache");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return pe;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}