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