5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster/**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
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 *
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 * opensso/legal/CDDLv1.0.txt
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * See the License for the specific language governing
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * permission and limitations under the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * When distributing Covered Code, include this CDDL
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Header Notice in each file and include the License file
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * at opensso/legal/CDDLv1.0.txt.
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 *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * $Id: CollectionHelper.java,v 1.6 2010/01/06 22:31:55 veiming Exp $
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam * Portions Copyrighted 2010-2015 ForgeRock AS.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterpackage com.sun.identity.shared.datastruct;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrestimport com.sun.identity.shared.Constants;
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrestimport com.sun.identity.shared.configuration.SystemPropertiesManager;
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrestimport com.sun.identity.shared.debug.Debug;
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrestimport org.apache.commons.lang.StringUtils;
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrestimport org.slf4j.Logger;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamfordimport org.slf4j.LoggerFactory;
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Joosteimport java.util.Date;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamfordimport java.util.HashMap;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.Iterator;
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandamimport java.util.LinkedHashSet;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamfordimport java.util.Locale;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.Map;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.Set;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamfordimport java.util.regex.Pattern;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster/**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This class contains various Collection manipulation methods.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterpublic class CollectionHelper {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford private static final String localDsameServer = SystemPropertiesManager.get(Constants.AM_SERVER_HOST);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford private static final Logger logger = LoggerFactory.getLogger(CollectionHelper.class);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford private static final String SEPARATOR = "|";
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns String from a map of string of set of string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
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 */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getMapAttr(Map map, String name) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set s = (Set)map.get(name);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String retVal = ((s == null) || s.isEmpty()) ? null :
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ((String)s.iterator().next());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (retVal != null) ? retVal.trim() : null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns String from a map of string of set of string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
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 */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getMapAttr(Map map, String name, String defaultValue) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String str = getMapAttr(map, name);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return ((str != null) && (str.length() > 0)) ? str : defaultValue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen /**
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 *
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen */
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford public static String getMapAttrThrows(Map map, String key) throws ValueNotFoundException {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford String str = getMapAttr(map, key);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford if (StringUtils.isBlank(str)){
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen throw new ValueNotFoundException("No value found for key '" + key + "'.");
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford return str;
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest /**
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * Gets the set based on the passed key.
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest *
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * @param map
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * the map
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * @param key
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * key to lookup
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest *
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * @return associated set
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest *
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * @throws ValueNotFoundException
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest * should the key not exist
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest */
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest public static Set<String> getMapSetThrows(Map<String, Set<String>> map, String key) throws ValueNotFoundException {
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest if (!map.containsKey(key)) {
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest throw new ValueNotFoundException("No value found for key " + key);
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest }
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest return map.get(key);
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest }
cf6215c51096624df2e7776c8b8e6319715d4202Andrew Forrest
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford /*
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 *
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 */
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford public static Map<Locale, String> getLocaleMapAttrThrows(Map<String, Set<String>> map, String key)
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford throws ValueNotFoundException {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford Set<String> values = map.get(key);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford if (values == null || values.isEmpty()) {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford throw new ValueNotFoundException("No value found for key '" + key + "'.");
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford Map<Locale, String> result = new HashMap<>();
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford for (String s : values) {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford // ignore blank or empty lines
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford if (org.forgerock.openam.utils.StringUtils.isBlank(s)) {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford continue;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford String[] parts = s.split(Pattern.quote(SEPARATOR));
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford if (parts.length != 2) {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford logger.warn("Config key "
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + key
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + " has value in invalid format: "
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + s);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford continue;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford Locale locale = com.sun.identity.shared.locale.Locale.getLocale(parts[0]);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford if (result.containsKey(locale)) {
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford logger.warn("Config key "
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + key
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + " has multiple entries for locale "
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford + locale.toString());
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford result.put(locale, parts[1]);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford return result;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott /**
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 *
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 */
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott public static boolean getBooleanMapAttr(Map map, String name, boolean defaultValue) {
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott String value = getMapAttr(map, name, Boolean.toString(defaultValue));
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott return Boolean.parseBoolean(value);
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott }
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen /**
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 *
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 */
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen public static boolean getBooleanMapAttrThrows(Map map, String name)
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen throws ValueNotFoundException {
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen String value = getMapAttrThrows(map, name);
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen Boolean boolValue = Boolean.parseBoolean(value);
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen return boolValue;
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford }
2210fc30709625f254e6a6d5dc89c3bfefa48816Robert Wapshott
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns integer value from a Map of String of Set of String.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford *
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 */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static int getIntMapAttr(
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford Map map,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String name,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String defaultValue,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Debug debug
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major ) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return Integer.parseInt(getMapAttr(map, name, defaultValue));
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major } catch (NumberFormatException nfe) {
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major debug.error("CollectionHelper.getIntMapAttr", nfe);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return Integer.parseInt(defaultValue);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns integer value from a Map of String of Set of String.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford *
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 */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static int getIntMapAttr(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Map map,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String name,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int defaultValue,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Debug debug
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return Integer.parseInt(getMapAttr(map, name));
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major } catch (NumberFormatException nfe) {
f5d3a8b100ee0ed5cb341715566899484736d71cPeter Major debug.error("CollectionHelper.getIntMapAttr", nfe);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return defaultValue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen /**
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen * Returns integer value from a Map of String of Set of String.
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford *
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 */
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen public static int getIntMapAttrThrows(Map map, String name)
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen throws ValueNotFoundException {
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen try {
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen return Integer.parseInt(getMapAttr(map, name));
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen } catch (NumberFormatException nfe) {
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen throw new ValueNotFoundException("No value found for key '" + name + "'.");
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen }
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen }
5c124de5c36bfc236d55578429df5f048f0d0a07Jamie Bowen
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden /**
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden * Returns a long value from the given configuration map.
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden *
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 */
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden public static long getLongMapAttr(Map<String, Set<String>> config, String name, long defaultValue, Debug debug) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott String valueString = null;
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden try {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott valueString = getMapAttr(config, name);
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott return Long.parseLong(valueString);
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden } catch (NumberFormatException nfe) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott debug.error("Unable to parse " + name + "=" + valueString, nfe);
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden return defaultValue;
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden }
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden }
c44bbf5d2025d672efe11a37ea6e9c867e78b691Neil Madden
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest /**
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * Given the map attempts to return the named value as a long.
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest *
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * @param map
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * the map
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * @param name
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * the named value
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest *
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * @return the corresponding long value
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest *
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * @throws ValueNotFoundException
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest * should the value fail to parse
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest */
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest public static long getLongMapAttrThrows(Map<String, Set<String>> map, String name) throws ValueNotFoundException {
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest try {
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest return Long.parseLong(getMapAttr(map, name));
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest } catch (NumberFormatException nfe) {
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest throw new ValueNotFoundException("No value found for key '" + name + "'.");
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest }
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest }
89fd04babb277ae294172d6fa5c28c1b775cfa9fAndrew Forrest
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste /**
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste * Returns the first attribute value for the corresponding name in the config map and parses it to a long.
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste *
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 */
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste public static long getMapAttrAsDateLong(Map<String, Set<String>> config, String name, Logger logger) {
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste String valueString = null;
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste try {
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste valueString = getMapAttr(config, name);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste return Long.parseLong(valueString);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste } catch (NumberFormatException nfe) {
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste logger.error("Unable to parse " + name + "=" + valueString, nfe);
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste return new Date().getTime();
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste }
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste }
7197110fbdb6deb7868581c455516161cbcf7e0fJaco Jooste
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
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 *
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 */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getServerMapAttr(Map map, String attrName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String result = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set attrValues = (Set)map.get(attrName);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (attrValues.size() == 1) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Iterator iter = attrValues.iterator();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String strServer = (String)iter.next();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (strServer != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster strServer = strServer.trim();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return strServer;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((attrValues != null) && !attrValues.isEmpty()) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (Iterator i = attrValues.iterator();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster i.hasNext() && (result == null);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster result = (String)i.next();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (result != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster result = result.trim();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (result.startsWith(localDsameServer)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int index = result.indexOf("|");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (index != -1) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster result = result.substring(index + 1);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } else {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster result = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } else {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster result = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return result;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
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 *
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 */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set<String> getServerMapAttrs(Map<String, Set<?>> map, String attrName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set<String> ret = new LinkedHashSet<String>();
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>();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (String attr : attrValues) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (attr != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster attr = attr.trim();
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam int index = attr.indexOf("|");
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam if (index == -1) {
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam nonMatchingServers.add(attr);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford } else {
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam String currentPrefix = attr.substring(0, index);
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam if (currentPrefix.equalsIgnoreCase(localDsameServer)) {
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam attr = attr.substring(index + 1);
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam currentServerDefined.add(attr);
268080d6459b85a79fd59c4246b84e0f7beeeb17Tony Bamford } else {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster attr = attr.substring(index + 1);
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam otherServerDefined.add(attr);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam ret.addAll(currentServerDefined);
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam ret.addAll(nonMatchingServers);
745f1659d4d3978dd0de6cb3ee017b91610fc12bKamal Sivanandam ret.addAll(otherServerDefined);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return ret;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster}