5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * The contents of this file are subject to the terms
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * of the Common Development and Distribution License
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * (the License). You may not use this file except in
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * compliance with the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * You can obtain a copy of the License at
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * See the License for the specific language governing
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * permission and limitations under the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * When distributing Covered Code, include this CDDL
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Header Notice in each file and include the License file
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If applicable, add the following below the CDDL Header,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * with the fields enclosed by brackets [] replaced by
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * your own identifying information:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * $Id: CollectionHelper.java,v 1.6 2010/01/06 22:31:55 veiming Exp $
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * Portions Copyrighted 2010-2015 ForgeRock AS.
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrestimport com.sun.identity.shared.configuration.SystemPropertiesManager;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This class contains various Collection manipulation methods.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford private static final String localDsameServer = SystemPropertiesManager.get(Constants.AM_SERVER_HOST);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford private static final Logger logger = LoggerFactory.getLogger(CollectionHelper.class);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns String from a map of string of set of string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param map Map of string of set of string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param name Key of the map entry.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return String from a map of string of set of string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getMapAttr(Map map, String name) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String retVal = ((s == null) || s.isEmpty()) ? null :
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (retVal != null) ? retVal.trim() : null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns String from a map of string of set of string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param map Map of string of set of string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param name Key of the map entry.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param defaultValue Default value if the string is not found.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return String from a map of string of set of string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getMapAttr(Map map, String name, String defaultValue) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return ((str != null) && (str.length() > 0)) ? str : defaultValue;
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * Return String from a map of strings to set of strings.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @param map
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @param key
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @return the String value from a map of strings to set of strings.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @throws ValueNotFoundException if no value is found for the key.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford public static String getMapAttrThrows(Map map, String key) throws ValueNotFoundException {
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen throw new ValueNotFoundException("No value found for key '" + key + "'.");
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * Gets the set based on the passed key.
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * key to lookup
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * @return associated set
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * @throws ValueNotFoundException
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * should the key not exist
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest public static Set<String> getMapSetThrows(Map<String, Set<String>> map, String key) throws ValueNotFoundException {
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest throw new ValueNotFoundException("No value found for key " + key);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * The key we are given must refer to an entry in the Map which is a set of lines of the form:<br>
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * en_GB|Here is some text in English<br>
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * fr_FR|Voici un texte en français<br>
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * All the text must fit onto one line. If it does not, we are unlikely to be able to cope since lines will be
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * retrieved from the {@link Set} in an almost random order. Also if you specify the same locale on two or more
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * lines, only one can be added to the map (a random one due to the random order - although you will get a warning.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * Caveat administrator.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * @param map The map of strings (keys) to sets of strings
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * @param key The key to use to access the map
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * @return A map of locales to localized text.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * @throws ValueNotFoundException if the set of values we need are not present
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford public static Map<Locale, String> getLocaleMapAttrThrows(Map<String, Set<String>> map, String key)
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford throw new ValueNotFoundException("No value found for key '" + key + "'.");
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford // ignore blank or empty lines
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford if (org.forgerock.openam.utils.StringUtils.isBlank(s)) {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford String[] parts = s.split(Pattern.quote(SEPARATOR));
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + " has value in invalid format: "
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford Locale locale = com.sun.identity.shared.locale.Locale.getLocale(parts[0]);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + " has multiple entries for locale "
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * Gets a boolean attribute from a {@code Map<String, Set<String>>}, defaulting to the given default value if
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * the attribute is not present.
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * @param map the attribute map.
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * @param name the name of the attribute to retrieve.
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * @param defaultValue the value to use if the attribute is not present.
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott * @return the boolean value using {@link Boolean#parseBoolean(String)}.
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott public static boolean getBooleanMapAttr(Map map, String name, boolean defaultValue) {
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott String value = getMapAttr(map, name, Boolean.toString(defaultValue));
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * Gets a boolean attribute from a {@code Map<String, Set<String>>}, throwing an exception if no boolean value (case
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * insensitive comparisons against "true" or "false") is found for the given key.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @param map
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * the attribute map.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @param name
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * the name of the attribute to retrieve.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @return the boolean value using {@link Boolean#parseBoolean(String)}.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @throws ValueNotFoundException
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * if no boolean value is found for the given key.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen public static boolean getBooleanMapAttrThrows(Map map, String name)
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen Boolean boolValue = Boolean.parseBoolean(value);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns integer value from a Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param map Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param name Kye of the map entry.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param defaultValue Default value if the integer value is not found.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param debug Debug object.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return integer value from a Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return Integer.parseInt(getMapAttr(map, name, defaultValue));
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major debug.error("CollectionHelper.getIntMapAttr", nfe);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns integer value from a Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param map Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param name Key of the map entry.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param defaultValue Default value if the integer value is not found.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param debug Debug object.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return integer value from a Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return Integer.parseInt(getMapAttr(map, name));
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major debug.error("CollectionHelper.getIntMapAttr", nfe);
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * Returns integer value from a Map of String of Set of String.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @param map
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * Map of String of Set of String.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @param name
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * Key of the map entry.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @return integer value from a Map of String of Set of String.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * @throws ValueNotFoundException
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * if there is no value for the key provided.
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen public static int getIntMapAttrThrows(Map map, String name)
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen throw new ValueNotFoundException("No value found for key '" + name + "'.");
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden * Returns a long value from the given configuration map.
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden * @param config the map of attribute values.
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden * @param name the attribute name to get.
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden * @param defaultValue the default value to use if the attribute is not set or is not a long.
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden * @param debug the debug object to report format errors to.
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden * @return the long value of the attribute or the defaultValue if not set/not a long.
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden public static long getLongMapAttr(Map<String, Set<String>> config, String name, long defaultValue, Debug debug) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott debug.error("Unable to parse " + name + "=" + valueString, nfe);
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * Given the map attempts to return the named value as a long.
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * @param name
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * the named value
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * @return the corresponding long value
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * @throws ValueNotFoundException
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * should the value fail to parse
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest public static long getLongMapAttrThrows(Map<String, Set<String>> map, String name) throws ValueNotFoundException {
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest return Long.parseLong(getMapAttr(map, name));
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest throw new ValueNotFoundException("No value found for key '" + name + "'.");
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * Returns the first attribute value for the corresponding name in the config map and parses it to a long.
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * @param config The map where the attribute should be retrieved from.
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * @param name The name of the attribute that should be retrieved from the map.
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * @return The attribute from the map corresponding to the provided attribute name, parsed to a long.
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * If the attribute does not exist the current date time will be returned.
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste public static long getMapAttrAsDateLong(Map<String, Set<String>> config, String name, Logger logger) {
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste logger.error("Unable to parse " + name + "=" + valueString, nfe);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This convenience method is for getting server specific attributes from a
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * list attribute. Server specific is determined by prefixing a list
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * attribute value with DSAME local server name followed by the | character.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If the list has more than one entry but no matching local server prefixes
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * than null is returned as this is an invalid configuration for these type
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * of attributes. This allows services like authentication to support a
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * geographic directory configuration.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param map Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param attrName Key of the map entry of interest.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the server name.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getServerMapAttr(Map map, String attrName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((attrValues != null) && !attrValues.isEmpty()) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This convenience method is for getting server specific attributes from a
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * list attribute. Server specific is determined by prefixing a list
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * attribute value with DSAME local server name followed by the | character.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * The priority order of the attributes as follows.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * 1- LDAP Servers belong to current OpenAM Server, localDsameServer is prefixed with the attribute
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * 2- LDAP Servers belong to no OpenAM Server, no server prefix
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * 3- All other servers - LDAP Servers prefixed with other OpenAM Servers
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * This allows services like authentication to support a geographic directory configuration.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param map Map of String of Set of String.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param attrName Key of the map entry of interest.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford * @return attributes based on the prioritization.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set<String> getServerMapAttrs(Map<String, Set<?>> map, String attrName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set<String> attrValues = (Set<String>) map.get(attrName);
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam Set<String> currentServerDefined = new LinkedHashSet<String>();
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam Set<String> otherServerDefined = new LinkedHashSet<String>();
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam Set<String> nonMatchingServers = new LinkedHashSet<String>();
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam String currentPrefix = attr.substring(0, index);