SubConfigMeta.java revision 28de0461d82a4e6471c71d793af7a9492ca98dd3
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste/**
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste *
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste *
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * The contents of this file are subject to the terms
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * of the Common Development and Distribution License
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * (the License). You may not use this file except in
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * compliance with the License.
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste *
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * You can obtain a copy of the License at
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * https://opensso.dev.java.net/public/CDDLv1.0.html or
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * opensso/legal/CDDLv1.0.txt
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * See the License for the specific language governing
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * permission and limitations under the License.
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste *
7bf512aea50c834dc9c3ef5a0a228059fcc753a5jenkins * When distributing Covered Code, include this CDDL
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * Header Notice in each file and include the License file
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * at opensso/legal/CDDLv1.0.txt.
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * If applicable, add the following below the CDDL Header,
7bf512aea50c834dc9c3ef5a0a228059fcc753a5jenkins * with the fields enclosed by brackets [] replaced by
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * your own identifying information:
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * "Portions Copyrighted [year] [name of copyright owner]"
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste *
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * $Id: SubConfigMeta.java,v 1.2 2008/06/25 05:42:50 qcheng Exp $
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste *
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste * Portions Copyrighted 2015 ForgeRock AS.
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste */
15f60c5ba586de69e94c6045e9bf91103e924020James Phillpotts
15f60c5ba586de69e94c6045e9bf91103e924020James Phillpottspackage com.sun.identity.console.base.model;
15f60c5ba586de69e94c6045e9bf91103e924020James Phillpotts
15f60c5ba586de69e94c6045e9bf91103e924020James Phillpottsimport com.sun.identity.shared.datastruct.CollectionHelper;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.sun.identity.shared.locale.Locale;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.iplanet.sso.SSOException;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.iplanet.sso.SSOToken;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.sun.identity.sm.AttributeSchema;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.sun.identity.sm.SMSUtils;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.sun.identity.sm.ServiceConfig;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.sun.identity.sm.ServiceConfigManager;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.sun.identity.sm.ServiceSchema;
7cbae22b7560eefcf086863923459880aa87f014Jaco Joosteimport com.sun.identity.sm.ServiceSchemaManager;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport com.sun.identity.sm.SMSException;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport org.forgerock.openam.utils.StringUtils;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.text.Collator;
95ac3084c619fd042f83410e5436ec3f1c3da190Brian Baileyimport java.util.ArrayList;
95ac3084c619fd042f83410e5436ec3f1c3da190Brian Baileyimport java.util.HashMap;
95ac3084c619fd042f83410e5436ec3f1c3da190Brian Baileyimport java.util.HashSet;
95ac3084c619fd042f83410e5436ec3f1c3da190Brian Baileyimport java.util.List;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.util.Iterator;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.util.Map;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.util.ResourceBundle;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.util.Set;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.util.SortedSet;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.util.StringTokenizer;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joosteimport java.util.TreeSet;
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste/* - NEED NOT LOG - */
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Joostepublic class SubConfigMeta {
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste public static final String SUBCONFIG_ID_DELIMITER = "/";
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste private static SSOToken adminSSOToken =
c423fd5d4f121413c5e72c134412b6ce5daa0dd9Jaco Jooste AMAdminUtils.getSuperAdminSSOToken();
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste private String serviceName;
7cbae22b7560eefcf086863923459880aa87f014Jaco Jooste private AMModel amModel;
ServiceConfig globalConfig;
ServiceSchema globalSchema;
private ResourceBundle serviceResourceBundle;
private Set globalSubSchemaNames;
private Map mapServiceSchemaNameToL10NName;
private Set singleInstanceGlobalSubSchemas;
private Set creatableGlobalSubSchemas;
private ServiceSchema corrSchema;
private ServiceConfig parentConfig;
private List globalSubConfigurations;
public SubConfigMeta(String serviceName, AMModel model) {
this.serviceName = serviceName;
amModel = model;
initialize();
}
private void resetMeta() {
globalSubSchemaNames = null;
mapServiceSchemaNameToL10NName = null;
singleInstanceGlobalSubSchemas = null;
creatableGlobalSubSchemas = null;
corrSchema = null;
parentConfig = null;
globalSubConfigurations = null;
}
/**
* Returns true if there are global sub schema.
*
* @return true if there are global sub schema.
*/
public boolean hasGlobalSubSchema() {
return (globalSubSchemaNames != null) &&
!globalSubSchemaNames.isEmpty();
}
/**
* Returns list of sub configuration objects.
*
* @return list of sub configuration objects.
* @see SMSubConfig
*/
public List getSubConfigurations() {
return (globalSubConfigurations != null) ?
globalSubConfigurations : new ArrayList();
}
/**
* Returns a map of sub schema name to its localized name. We should
* be able to create sub configuration with these names.
*
* @return Map of sub schema name to its localized name.
*/
public Map getCreateableSubSchemaNames() {
Map map = new HashMap(creatableGlobalSubSchemas.size() *2);
for (Iterator i = creatableGlobalSubSchemas.iterator(); i.hasNext(); ) {
String s = (String)i.next();
map.put(s, mapServiceSchemaNameToL10NName.get(s));
}
return map;
}
/**
* Returns sub service schema.
*
* @throws SMSException if sub schema cannot be determined.
*/
public ServiceSchema getServiceSchema() {
return corrSchema;
}
/**
* Returns sub service schema of current sub schema.
*
* @param name Name of sub schema.
* @throws SMSException if sub schema cannot be determined.
*/
public ServiceSchema getServiceSchema(String name)
throws SMSException {
return corrSchema.getSubSchema(name);
}
/**
* Returns default values of sub service schema of current sub schema. Map
* of attribute name (String) to attribute values (Set).
*
* @param name Name of sub schema.
* @throws SMSException if default values be determined.
*/
public Map getServiceSchemaDefaultValues(String name)
throws SMSException {
Map defaultValues = new HashMap();
ServiceSchema ss = corrSchema.getSubSchema(name);
Set attributeSchemas = ss.getAttributeSchemas();
for (Iterator i = attributeSchemas.iterator(); i.hasNext(); ) {
AttributeSchema as = (AttributeSchema)i.next();
Set values = as.getDefaultValues();
if (values != null) {
defaultValues.put(as.getName(), values);
}
}
return defaultValues;
}
private void initialize() {
try {
ServiceConfigManager mgr = new ServiceConfigManager(
serviceName, adminSSOToken);
globalConfig = mgr.getGlobalConfig(null);
if (globalConfig != null) {
ServiceSchemaManager schemaMgr = new ServiceSchemaManager(
serviceName, adminSSOToken);
globalSchema = schemaMgr.getGlobalSchema();
String rbName = schemaMgr.getI18NFileName();
if ((rbName != null) && (rbName.trim().length() > 0)) {
serviceResourceBundle = AMResBundleCacher.getBundle(rbName,
amModel.getUserLocale());
}
}
} catch (SSOException e) {
AMModelBase.debug.error("SubConfigMeta.getGlobalSchema" ,e);
} catch (SMSException e) {
AMModelBase.debug.error("SubConfigMeta.getGlobalSchema" ,e);
}
}
private boolean validInstance() {
return (globalSchema != null) &&
(globalConfig != null) &&
(serviceResourceBundle != null);
}
public void setParentId(String parentId) {
if (validInstance()) {
resetMeta();
try {
getCorrespondingSchema(parentId);
getSupportedGlobalSubSchema();
if ((globalSubSchemaNames != null) &&
!globalSubSchemaNames.isEmpty()
) {
creatableGlobalSubSchemas = new HashSet();
creatableGlobalSubSchemas.addAll(globalSubSchemaNames);
getSubConfigurationsFromConfig();
}
} catch (SSOException e) {
AMModelBase.debug.error("SubConfigMeta.getParentId", e);
} catch (SMSException e) {
AMModelBase.debug.error("SubConfigMeta.getParentId", e);
}
}
}
/**
* Get the parent configuration's display name. It will check if the service configuration
* has an internationalization key and if it does, the localised name will be returned.
* Otherwise the config name itself will be returned.
* @return The parent configuration's display name.
*/
public String getParentDisplayName() {
String i18nKey = CollectionHelper.getMapAttr(parentConfig.getAttributes(), SMSUtils.I18N_KEY);
return StringUtils.isEmpty(i18nKey) ? parentConfig.getName() : getLocalizedString(i18nKey);
}
private void getSupportedGlobalSubSchema() {
try {
globalSubSchemaNames = corrSchema.getSubSchemaNames();
if ((globalSubSchemaNames != null) &&
!globalSubSchemaNames.isEmpty()
) {
mapServiceSchemaNameToL10NName = new HashMap();
singleInstanceGlobalSubSchemas = new HashSet();
for (Iterator i =globalSubSchemaNames.iterator(); i.hasNext();){
String name = (String)i.next();
ServiceSchema ss = corrSchema.getSubSchema(name);
String i18nKey = ss.getI18NKey();
if ((i18nKey == null) || (i18nKey.trim().length() == 0)) {
i.remove();
} else {
mapServiceSchemaNameToL10NName.put(name, getLocalizedString(i18nKey));
if (!ss.supportsMultipleConfigurations()) {
singleInstanceGlobalSubSchemas.add(name);
}
}
}
}
} catch (SMSException e) {
AMModelBase.debug.error("SubConfigMeta.getSupportedGlobalSubSchema" ,e);
}
}
private String getLocalizedString(String key) {
return StringUtils.isEmpty(key) ? null : Locale.getString(serviceResourceBundle, key, AMModelBase.debug);
}
private void getCorrespondingSchema(String parentId)
throws SSOException, SMSException {
corrSchema = globalSchema;
parentConfig = globalConfig;
StringTokenizer st = new StringTokenizer(parentId,
SUBCONFIG_ID_DELIMITER);
/*
* discard the first one because it is always /
*/
if (st.hasMoreTokens()) {
st.nextToken();
}
while (st.hasMoreTokens()) {
String configId = st.nextToken();
configId = AMAdminUtils.replaceString(configId, "%2F", "/");
configId = AMAdminUtils.replaceString(configId, "%25", "%");
parentConfig = parentConfig.getSubConfig(configId);
corrSchema = corrSchema.getSubSchema(parentConfig.getSchemaID());
}
}
private void getSubConfigurationsFromConfig() {
try {
String[] params = {serviceName, parentConfig.getComponentName()};
amModel.logEvent("ATTEMPT_READ_GLOBAL_SUB_CONFIGURATION_NAMES",
params);
Set names = parentConfig.getSubConfigNames();
if ((names != null) && !names.isEmpty()) {
Collator collator = Collator.getInstance(
amModel.getUserLocale());
SortedSet set = new TreeSet(
new SMSubConfigComparator(collator));
for (Iterator iter = names.iterator(); iter.hasNext(); ) {
String name = (String)iter.next();
ServiceConfig conf = parentConfig.getSubConfig(name);
String schemaID = conf.getSchemaID();
if (globalSubSchemaNames.contains(schemaID)) {
String displayType = (String)
mapServiceSchemaNameToL10NName.get(schemaID);
String localizedName = getLocalizedString(
CollectionHelper.getMapAttr(conf.getAttributes(), SMSUtils.I18N_KEY));
set.add(new SMSubConfig(conf.getComponentName(), name, displayType, localizedName));
if (singleInstanceGlobalSubSchemas.contains(schemaID)) {
creatableGlobalSubSchemas.remove(schemaID);
}
}
}
if (!set.isEmpty()) {
globalSubConfigurations = new ArrayList(set.size());
globalSubConfigurations.addAll(set);
}
}
amModel.logEvent("SUCCEED_READ_GLOBAL_SUB_CONFIGURATION_NAMES",
params);
} catch (SSOException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
amModel.getErrorString(e)};
amModel.logEvent(
"SSO_EXCEPTION_READ_GLOBAL_SUB_CONFIGURATION_NAMES",
paramsEx);
AMModelBase.debug.error("SubConfigMeta.getSubConfigurations", e);
} catch (SMSException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
amModel.getErrorString(e)};
amModel.logEvent(
"SMS_EXCEPTION_READ_GLOBAL_SUB_CONFIGURATION_NAMES",
paramsEx);
AMModelBase.debug.error("SubConfigMeta.getSubConfigurations", e);
}
}
/**
* Deletes sub configurations.
*
* @param names Set of sub configuration names that are to be deleted.
* @throws AMConsoleException if sub configurations cannot be deleted.
*/
public void deleteSubConfigurations(Set names)
throws AMConsoleException {
String curName = null;
try {
if (parentConfig != null) {
String[] params = new String[3];
params[0] = serviceName;
params[1] = parentConfig.getComponentName();
for (Iterator iter = names.iterator(); iter.hasNext(); ) {
curName = (String)iter.next();
params[2] = curName;
amModel.logEvent(
"ATTEMPT_DELETE_GLOBAL_SUB_CONFIGURATION", params);
parentConfig.removeSubConfig(curName);
removeFromSubConfigList(curName);
amModel.logEvent(
"SUCCEED_DELETE_GLOBAL_SUB_CONFIGURATION", params);
}
}
} catch (SSOException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
curName, amModel.getErrorString(e)};
amModel.logEvent("SSO_EXCEPTION_DELETE_GLOBAL_SUB_CONFIGURATION",
paramsEx);
throw new AMConsoleException(amModel.getErrorString(e));
} catch (SMSException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
curName, amModel.getErrorString(e)};
amModel.logEvent("SMS_EXCEPTION_DELETE_GLOBAL_SUB_CONFIGURATION",
paramsEx);
throw new AMConsoleException(amModel.getErrorString(e));
}
}
private void removeFromSubConfigList(String name) {
boolean removed = false;
for (Iterator i = globalSubConfigurations.iterator();
i.hasNext() && !removed;
) {
SMSubConfig sc = (SMSubConfig)i.next();
if (sc.getName().equals(name)) {
i.remove();
removed = true;
}
}
}
/**
* Creates a new sub configuration.
*
* @param name Name of sub configuration.
* @param schemaName Name of schema name.
* @param values Map of attribute name to its values.
* @throws AMConsoleException if sub configuration cannot be created.
*/
public void createSubConfig(String name, String schemaName, Map values)
throws AMConsoleException {
String[] params = {serviceName, parentConfig.getComponentName(),
name, schemaName};
try {
amModel.logEvent(
"ATTEMPT_CREATE_GLOBAL_SUB_CONFIGURATION", params);
parentConfig.addSubConfig(name, schemaName, 0, values);
amModel.logEvent(
"SUCCEED_CREATE_GLOBAL_SUB_CONFIGURATION", params);
} catch (SSOException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
name, schemaName, amModel.getErrorString(e)};
amModel.logEvent("SSO_EXCEPTION_CREATE_GLOBAL_SUB_CONFIGURATION",
paramsEx);
throw new AMConsoleException(amModel.getErrorString(e));
} catch (SMSException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
name, schemaName, amModel.getErrorString(e)};
amModel.logEvent("SMS_EXCEPTION_CREATE_GLOBAL_SUB_CONFIGURATION",
paramsEx);
throw new AMConsoleException(amModel.getErrorString(e));
}
}
/**
* Returns attribute values.
*
* @return attribute values.
* @throws AMConsoleException if attribute values cannot be determined.
*/
public Map getSubConfigAttributeValues()
throws AMConsoleException {
String[] params = {serviceName, parentConfig.getComponentName()};
amModel.logEvent(
"SUCCEED_READ_GLOBAL_SUB_CONFIGURATION_ATTRIBUTE_VALUES",
params);
return parentConfig.getAttributes();
}
/**
* Set attribute values.
*
* @param values Attribute values.
* @throws AMConsoleException if attribute values cannot be set.
*/
public void setSubConfigAttributeValues(Map values)
throws AMConsoleException {
try {
String[] params = {serviceName, parentConfig.getComponentName()};
amModel.logEvent(
"ATTEMPT_WRITE_GLOBAL_SUB_CONFIGURATION_ATTRIBUTE_VALUES",
params);
parentConfig.setAttributes(values);
amModel.logEvent(
"SUCCEED_WRITE_GLOBAL_SUB_CONFIGURATION_ATTRIBUTE_VALUES",
params);
} catch (SSOException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
amModel.getErrorString(e)};
amModel.logEvent(
"SSO_EXCEPTION_WRITE_GLOBAL_SUB_CONFIGURATION_ATTRIBUTE_VALU",
paramsEx);
throw new AMConsoleException(amModel.getErrorString(e));
} catch (SMSException e) {
String[] paramsEx = {serviceName, parentConfig.getComponentName(),
amModel.getErrorString(e)};
amModel.logEvent(
"SMS_EXCEPTION_WRITE_GLOBAL_SUB_CONFIGURATION_ATTRIBUTE_VALU",
paramsEx);
throw new AMConsoleException(amModel.getErrorString(e));
}
}
}