KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > role > ConfigurationRoleHandler


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ConfigurationRoleHandler.java
20  */

21
22 // package path
23
package com.rift.coad.lib.security.role;
24
25 // java import
26
import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 // coadunation imports
33
import com.rift.coad.lib.configuration.ConfigurationFactory;
34 import com.rift.coad.lib.configuration.Configuration;
35 import com.rift.coad.lib.security.RoleManager;
36 import com.rift.coad.lib.security.Role;
37 import com.rift.coad.lib.security.RoleHandler;
38 import com.rift.coad.lib.security.SecurityException;
39
40
41 /**
42  * This class is responsible for implementing the traditional configuration
43  * based role handler.
44  *
45  * @author brett
46  */

47 public class ConfigurationRoleHandler implements RoleHandler {
48     
49     // the classes constant static variables
50
private static final String JavaDoc PRINCIPALS = "principals";
51     private static final String JavaDoc ROLES = "roles";
52     
53     // private member variables
54
private Set JavaDoc principals = new HashSet JavaDoc();
55             
56     /**
57      * Creates a new instance of ConfigurationRoleHandler
58      */

59     public ConfigurationRoleHandler() {
60     }
61     
62     
63     /**
64      * This method returns the roles managed by this class.
65      *
66      * @return The map containing the roles
67      *
68      * @exception SecurityException
69      */

70     public Map JavaDoc getRoles() throws SecurityException JavaDoc {
71         try {
72             Map JavaDoc roles = new HashMap JavaDoc();
73             Configuration config = ConfigurationFactory.getInstance().getConfig(
74                     RoleManager.class);
75             StringTokenizer JavaDoc principalList = new StringTokenizer JavaDoc(
76                     config.getString(PRINCIPALS),",");
77             while(principalList.hasMoreTokens()) {
78                 principals.add(principalList.nextToken().trim());
79             }
80             StringTokenizer JavaDoc roleList = new StringTokenizer JavaDoc(
81                     config.getString(ROLES),",");
82             while(roleList.hasMoreTokens()) {
83                 String JavaDoc role = (String JavaDoc)roleList.nextToken().trim();
84                 roles.put(role,loadRole(config,role));
85             }
86             return roles;
87         } catch (Exception JavaDoc ex) {
88             throw new SecurityException JavaDoc("Failed to load the role information : "
89                     + ex.getMessage(),ex);
90         }
91     }
92     
93     
94     /**
95      * This method will load the required role into memory.
96      *
97      * @return The role to load into memory.
98      * @param config The configuration class.
99      * @param role The string identifying the role.
100      * @exception SecurityException
101      */

102     private Role loadRole(Configuration config,String JavaDoc role)
103             throws SecurityException JavaDoc {
104         try {
105             StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(
106                     config.getString(role),",");
107             Set JavaDoc roleSet = new HashSet JavaDoc();
108             while(stringTokenizer.hasMoreTokens()) {
109                 String JavaDoc principal = (String JavaDoc)stringTokenizer.nextToken();
110                 if (principals.contains(principal) == false) {
111                     throw new SecurityException JavaDoc("Invalid principal : " +
112                             principal);
113                 }
114                 roleSet.add(principal);
115             }
116             return new Role(role,roleSet);
117         } catch (Exception JavaDoc ex) {
118             throw new SecurityException JavaDoc("The list of roles : " + ex.getMessage(),
119                     ex);
120         }
121     }
122 }
123
Popular Tags