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