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: ResponseProviderTypeManager.java,v 1.3 2008/06/25 05:43:45 qcheng Exp $
026 *
027 */
028
029
030
031package com.sun.identity.policy;
032
033import java.util.*;
034
035import com.iplanet.sso.SSOToken;
036import com.iplanet.sso.SSOException;
037import com.sun.identity.sm.*;
038import com.sun.identity.policy.interfaces.ResponseProvider;
039import com.sun.identity.shared.locale.AMResourceBundleCache;
040import com.sun.identity.shared.debug.Debug;
041import com.sun.identity.shared.locale.Locale;
042
043/**
044 * The class <code>ResponseProviderTypeManager</code> provides
045 * methods to get a list of configured <code>ResponseProvider
046 * </code> objects, and to obtain a factory object for it.
047 *
048 * @supported.all.api
049 */
050public class ResponseProviderTypeManager {
051
052    private static String RESPONSE_PROVIDER = "ResponseProvider";
053
054    private SSOToken token;
055    private PolicyManager pm;
056
057    private ResourceBundle rb;
058    private static AMResourceBundleCache amCache = 
059            AMResourceBundleCache.getInstance();
060
061    private static Debug debug = PolicyManager.debug;
062
063    /**
064     * Constructs a <code>ResponseProviderTypeManager</code> object
065     */
066    ResponseProviderTypeManager() throws SSOException {
067        token = ServiceTypeManager.getSSOToken();
068        String lstr;
069        lstr = token.getProperty("Locale");
070        java.util.Locale loc = 
071            com.sun.identity.shared.locale.Locale.getLocale(lstr);
072        rb = amCache.getResBundle(ResBundleUtils.rbName, loc);
073    }
074
075    /**
076     * Constructs a <code>ResponseProviderTypeManager</code> object
077     * @param pm <code>PolicyManager</code> to initialize the 
078     * <code>ResponseProviderTypeManager</code> with
079     */
080    public ResponseProviderTypeManager(PolicyManager pm) {
081        this.pm = pm;
082        token = pm.token;
083        java.util.Locale loc ;
084        try {
085            String lstr = token.getProperty("Locale");
086            loc = com.sun.identity.shared.locale.Locale.getLocale(lstr);
087        } catch (SSOException ex) {
088            debug.error ("ResponseProviderTypeManager:Unable to retreive "
089                +"locale from SSOToken", ex);
090            loc = Locale.getDefaultLocale();
091        }
092
093         if (debug.messageEnabled()) {
094            debug.message("ResponseProviderTypeManager locale="+loc+
095                "\tI18nFileName = "+ ResBundleUtils.rbName);
096        }
097        rb = amCache.getResBundle(ResBundleUtils.rbName, loc);
098    }
099
100    /**
101     * Returns a set of all valid <code>ResponseProvider</code> type names 
102     * defined in the  <code>PolicyConfig</code> service. 
103     * Out of the box will have only
104     * <code>IDRepoResponseProvider</code>
105     *
106     * @return a set of all valid <code>ResponseProvider</code> type 
107     * names defined in the  <code>PolicyConfig</code> service.
108     * @throws SSOException if the <code>SSOToken</code> used to create 
109     *                      the <code>PolicyManager</code> has become invalid
110     * @throws PolicyException for any other abnormal condition.
111     */
112    public Set getResponseProviderTypeNames() throws SSOException, 
113            PolicyException  {
114        return (PolicyManager.getPluginSchemaNames(RESPONSE_PROVIDER));
115    }
116
117    /**
118     * Returns a set of valid <code>ResponseProvider</code> type names 
119     * configured.
120     * Examples are <code>IDRepoResponseProvider</code> and any other
121     * configured providers.
122     *
123     * @return a set of all valid <code>ResponseProvider</code> type names
124     * defined in the  <code>PolicyConfig</code>  service.
125     * @throws SSOException if the <code>SSOToken</code> used to create 
126     *                      the <code>PolicyManager</code> has become invalid
127     * @throws PolicyException for any other abnormal condition
128     */
129    public Set getSelectedResponseProviderTypeNames() throws SSOException,
130            PolicyException  {
131        String org = pm.getOrganizationDN();
132        Map map = PolicyConfig.getPolicyConfig(org);
133        if (map != null) {
134            Object answer = map.get(PolicyConfig.SELECTED_RESPONSE_PROVIDERS);
135            if (answer != null) {
136                return (Set) answer; 
137            }
138        }
139        return Collections.EMPTY_SET;
140    }
141
142    /**
143     * Returns the type of the <code>ResponseProvider</code> object.
144     * For example <code>IDRepoResponseProvider</code> or any other
145     * configured providers.
146     *
147     * @param respProvider <code>ResponseProvider</code> object for which this
148     *        method will return its associated type
149     * @return type of the responseprovider, e.g. <code>IDRepoResponseProvider
150     *         </code> . Returns <code>null</code> if not present.
151     */
152    public String getResponseProviderTypeName(ResponseProvider respProvider) {
153        return responseProviderTypeName(respProvider);
154    }
155
156    /**
157     * Returns <code>ResponseProvider</code> type name
158     * @param respProvider <code>ResponseProvider</code> for which 
159     * to get the type name
160     * @return <code>ResponseProvider</code> type name for the given
161     * <code>ResponseProvider</code>
162     */
163    public static String responseProviderTypeName(
164        ResponseProvider respProvider)
165    {
166        if (respProvider == null) {
167            return (null);
168        }
169        String name = null;
170        String className = respProvider.getClass().getName();
171        Iterator items = PolicyManager.getPluginSchemaNames(RESPONSE_PROVIDER).
172            iterator();
173        while (items.hasNext()) {
174            String pluginName = (String) items.next();
175            PluginSchema ps = PolicyManager.getPluginSchema(RESPONSE_PROVIDER, 
176                pluginName);
177            if (className.equals(ps.getClassName())) {
178                name = pluginName;
179                break;
180            }
181        }
182        return (name);
183    }
184
185    /**
186     * Returns the I18N properties file name that should be
187     * used to localize display names for the given
188     * responseprovider name.
189     *
190     * @param responseProviderTypeName response provider type name
191     *
192     * @return i18n properties file name
193     */
194    protected String getI18NPropertiesFileName(
195        String responseProviderTypeName) 
196    {
197        PluginSchema ps = PolicyManager.getPluginSchema(RESPONSE_PROVIDER, 
198            responseProviderTypeName);
199        if (ps != null) {
200            return ps.getI18NFileName();
201        }
202        return null;
203    }
204
205    /**
206     * Returns the I18N key to be used to localize the
207     * display name for the responseprovider name.
208     *
209     * @param responseProviderName Response provider type name.
210     *
211     * @return i18n key to obtain the display name.
212     */
213    public String getI18NKey(String responseProviderName) {
214        PluginSchema ps = PolicyManager.getPluginSchema(RESPONSE_PROVIDER, 
215            responseProviderName);
216        if (ps != null) {
217            return (ps.getI18NKey());
218        }
219        return null;
220    }
221
222    /**
223     * Returns the display name for the response provider
224     * @param responseProviderTypeName responseprovider type name
225     * @return display name for the response provider
226     */
227    public String getDisplayName(String responseProviderTypeName) {
228        String displayName = null;
229        String i18nKey = getI18NKey(responseProviderTypeName);
230        if (i18nKey == null || i18nKey.length()==0 ) {
231            displayName = responseProviderTypeName;
232        } else {
233            displayName = Locale.getString(rb,i18nKey,debug);
234        }
235        return displayName;
236    }
237
238    /**
239     * Returns an instance of the <code>ResponseProvider</code>
240     * given the response provider type name.
241     *
242     * @param responseProviderTypeName response provider type name.
243     * @return an instance of the <code>ResponseProvider</code>
244     * given the response provider type name.
245     * @throws NameNotFoundException if the <code>ResponseProvider</code> 
246     * not found
247     * @throws PolicyException for any other abnormal condition
248     */
249    public ResponseProvider getResponseProvider(String responseProviderTypeName)
250        throws NameNotFoundException, PolicyException 
251    {
252        PluginSchema ps = PolicyManager.getPluginSchema(RESPONSE_PROVIDER, 
253            responseProviderTypeName);
254        if (ps == null) {
255            throw (new NameNotFoundException(ResBundleUtils.rbName, 
256                "invalid_response_provider", null, responseProviderTypeName, 
257                PolicyException.RESPONSE_PROVIDER_COLLECTION));
258        }
259
260        // Construct the object
261        ResponseProvider respProvider = null;
262        try {
263            String className = ps.getClassName();
264            respProvider = (ResponseProvider) Class.forName(className).
265                newInstance();
266        } catch (Exception e) {
267            throw (new PolicyException(e));
268        }
269        respProvider.initialize(pm.getPolicyConfig());
270        return respProvider;
271    }
272
273    /**
274     * Returns the view bean URL given the <code>ResponseProvider</code>
275     *
276     * @param respProvider <code>ResponseProvider</code> for which 
277     * to get the  view bean URL
278     *
279     * @return view bean URL defined for the <code>ResponseProvider</code> 
280     * plugin in the  policy  service <code>PluginSchema</code>.
281     */
282    public String getViewBeanURL(ResponseProvider respProvider)  {
283        return PolicyManager.getViewBeanURL(RESPONSE_PROVIDER, 
284            respProvider.getClass().getName());
285    }
286}