KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > jacc > JPolicyUserRoleMapping


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JPolicyUserRoleMapping.java,v 1.1 2004/07/13 15:20:40 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas.security.jacc;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Helper class to manage user to role mapping
33  * It uses the contextId as identifier.
34  * For clients, as there is no contextId, this is a per jvm property with contextId equals to "global"
35  * @author Florent Benoit
36  */

37 public class JPolicyUserRoleMapping {
38
39     /**
40      * Global contextID (per JVM configuration)
41      */

42     public static final String JavaDoc GLOBAL_CTXID = "global";
43
44     /**
45      * Internal list of Mapping for JACC context ID Map (ctxId) --> map
46      * (principal name --> roles)
47      */

48     private static Map JavaDoc jaccIdMappings = new HashMap JavaDoc();
49
50     /**
51      * Utility class, no constructor
52      */

53     private JPolicyUserRoleMapping() {
54     }
55
56     /**
57      * Add a mapping for the given principal name
58      * @param contextId Id of the application
59      * @param principalName mapping for this principal name
60      * @param roles roles for the principal
61      */

62     public static void addUserToRoleMapping(String JavaDoc contextId, String JavaDoc principalName, String JavaDoc[] roles) {
63         Map JavaDoc mapping = (Map JavaDoc) jaccIdMappings.get(contextId);
64         if (mapping == null) {
65             mapping = new HashMap JavaDoc();
66             jaccIdMappings.put(contextId, mapping);
67         }
68         mapping.put(principalName, roles);
69     }
70
71     /**
72      * Add a mapping for the given principal name (on all JVM)
73      * @param principalName mapping for this principal name
74      * @param roles roles for the principal
75      */

76     public static void addGlobalUserToRoleMapping(String JavaDoc principalName, String JavaDoc[] roles) {
77         addUserToRoleMapping(GLOBAL_CTXID, principalName, roles);
78     }
79
80     /**
81      * Gets the mapping for a principal name
82      * @param contextId Id of the application
83      * @param principalName name of the principal on which we want to retrieve
84      * roles
85      * @return array of roles
86      */

87     public static String JavaDoc[] getMappingForPrincipal(String JavaDoc contextId, String JavaDoc principalName) {
88         Map JavaDoc mapping = (Map JavaDoc) jaccIdMappings.get(contextId);
89         if (mapping == null) {
90             mapping = new HashMap JavaDoc();
91             jaccIdMappings.put(contextId, mapping);
92         }
93         return (String JavaDoc[]) mapping.get(principalName);
94     }
95
96     /**
97      * Gets the mapping for a principal name
98      * @param principalName name of the principal on which we want to retrieve
99      * roles
100      * @return array of roles
101      */

102     public static String JavaDoc[] getGlobalMappingForPrincipal(String JavaDoc principalName) {
103         return getMappingForPrincipal(GLOBAL_CTXID, principalName);
104     }
105
106     /**
107      * Remove a mapping for the given context Id
108      * @param contextId Id of the application
109      */

110     public static void removeUserToRoleMapping(String JavaDoc contextId) {
111         jaccIdMappings.remove(contextId);
112     }
113
114 }
Popular Tags