001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JPolicyUserRoleMapping.java 5124 2004-07-13 15:20:41Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.jacc;
026:
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: /**
031: * Helper class to manage user to role mapping
032: * It uses the contextId as identifier.
033: * For clients, as there is no contextId, this is a per jvm property with contextId equals to "global"
034: * @author Florent Benoit
035: */
036: public class JPolicyUserRoleMapping {
037:
038: /**
039: * Global contextID (per JVM configuration)
040: */
041: public static final String GLOBAL_CTXID = "global";
042:
043: /**
044: * Internal list of Mapping for JACC context ID Map (ctxId) --> map
045: * (principal name --> roles)
046: */
047: private static Map jaccIdMappings = new HashMap();
048:
049: /**
050: * Utility class, no constructor
051: */
052: private JPolicyUserRoleMapping() {
053: }
054:
055: /**
056: * Add a mapping for the given principal name
057: * @param contextId Id of the application
058: * @param principalName mapping for this principal name
059: * @param roles roles for the principal
060: */
061: public static void addUserToRoleMapping(String contextId,
062: String principalName, String[] roles) {
063: Map mapping = (Map) jaccIdMappings.get(contextId);
064: if (mapping == null) {
065: mapping = new HashMap();
066: jaccIdMappings.put(contextId, mapping);
067: }
068: mapping.put(principalName, roles);
069: }
070:
071: /**
072: * Add a mapping for the given principal name (on all JVM)
073: * @param principalName mapping for this principal name
074: * @param roles roles for the principal
075: */
076: public static void addGlobalUserToRoleMapping(String principalName,
077: String[] roles) {
078: addUserToRoleMapping(GLOBAL_CTXID, principalName, roles);
079: }
080:
081: /**
082: * Gets the mapping for a principal name
083: * @param contextId Id of the application
084: * @param principalName name of the principal on which we want to retrieve
085: * roles
086: * @return array of roles
087: */
088: public static String[] getMappingForPrincipal(String contextId,
089: String principalName) {
090: Map mapping = (Map) jaccIdMappings.get(contextId);
091: if (mapping == null) {
092: mapping = new HashMap();
093: jaccIdMappings.put(contextId, mapping);
094: }
095: return (String[]) mapping.get(principalName);
096: }
097:
098: /**
099: * Gets the mapping for a principal name
100: * @param principalName name of the principal on which we want to retrieve
101: * roles
102: * @return array of roles
103: */
104: public static String[] getGlobalMappingForPrincipal(
105: String principalName) {
106: return getMappingForPrincipal(GLOBAL_CTXID, principalName);
107: }
108:
109: /**
110: * Remove a mapping for the given context Id
111: * @param contextId Id of the application
112: */
113: public static void removeUserToRoleMapping(String contextId) {
114: jaccIdMappings.remove(contextId);
115: }
116:
117: }
|