CollectionHelper.java revision 7197110fbdb6deb7868581c455516161cbcf7e0f
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.
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Joosteimport com.sun.identity.shared.configuration.SystemPropertiesManager;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This class contains various Collection manipulation methods.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static final String localDsameServer = SystemPropertiesManager.get(
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;
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));
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 public static int getIntMapAttr(
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 public static int getIntMapAttr(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return Integer.parseInt(getMapAttr(map, name));
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major debug.error("CollectionHelper.getIntMapAttr", nfe);
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);
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.
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * The priority order of the attributes as follows.
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * 1- LDAP Servers belong to current OpenAM Server, localDsameServer is prefixed with the attribute
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * 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.
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * @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);