KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > RoleManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 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  * RoleManager.java
20  *
21  * The role manager, responsible for managing all the role information used
22  * in coadunation.
23  */

24
25 // the package name
26
package com.rift.coad.lib.security;
27
28 // java import
29
import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 // log 4 j imports
39
import org.apache.log4j.Logger;
40
41 // coadunation imports
42
import com.rift.coad.lib.configuration.ConfigurationFactory;
43 import com.rift.coad.lib.configuration.Configuration;
44 import com.rift.coad.lib.deployment.DeploymentMonitor;
45 import com.rift.coad.lib.thread.ThreadStateMonitor;
46
47
48 /**
49  * The role manager, responsible for managing all the role information used
50  * in coadunation.
51  *
52  * @author Brett Chaldecott
53  */

54 public class RoleManager {
55     
56     /**
57      * This thread is responsible for running in the back ground and managing
58      * the loading of roles. It is self terminating based on the status of the
59      * deployment loader.
60      */

61     public class RoleLoaderThread extends Thread JavaDoc {
62         
63         // the thread state monitor
64
private ThreadStateMonitor stateMonitor = new ThreadStateMonitor(60000);
65         
66         /**
67          * The constructor of the roler loader thread.
68          */

69         public RoleLoaderThread() {
70             
71         }
72         
73         
74         /**
75          * The run method for the role loader thread.
76          */

77         public void run() {
78             while (!stateMonitor.isTerminated()) {
79                 stateMonitor.monitor();
80                 if (!stateMonitor.isTerminated()) {
81                     try {
82                         loadRoles();
83                     } catch (Throwable JavaDoc ex) {
84                         log.error("Failed to load the roles : " +
85                                 ex.getMessage(),ex);
86                     }
87                 }
88             }
89         }
90         
91         
92         /**
93          * This method is called to terminate the role loader thread
94          */

95         public void terminate() {
96             if (DeploymentMonitor.getInstance().isTerminated()) {
97                 stateMonitor.terminate(true);
98             }
99         }
100     }
101     
102     
103     // the classes constant static variables
104
private static final String JavaDoc ROLE_HANDLERS = "role_handlers";
105     
106     // The role manager logger
107
protected static Logger log =
108             Logger.getLogger(RoleManager.class.getName());
109     
110     
111     // the class imports
112
private static RoleManager singleton = null;
113     private List JavaDoc roleHandlers = null;
114     private Map JavaDoc roles = null;
115     private RoleLoaderThread roleLoaderThread = null;
116     
117     /**
118      * Creates a new instance of the RoleManager.
119      *
120      * @exception SecurityException
121      */

122     private RoleManager() throws SecurityException JavaDoc {
123         roleHandlers = new ArrayList JavaDoc();
124         roles = new HashMap JavaDoc();
125         
126         try {
127             Configuration config = ConfigurationFactory.getInstance().getConfig(
128                         RoleManager.class);
129             StringTokenizer JavaDoc roleHandlerList = new StringTokenizer JavaDoc(
130                     config.getString(ROLE_HANDLERS),",");
131             while(roleHandlerList.hasMoreTokens()) {
132                 roleHandlers.add(
133                         Class.forName(roleHandlerList.nextToken().trim()).
134                         newInstance());
135             }
136         } catch (Exception JavaDoc ex) {
137             throw new SecurityException JavaDoc(
138                     "Failed to load the configuration role handler list : " +
139                     ex.getMessage(),ex);
140         }
141         loadRoles();
142     }
143     
144     
145     /**
146      * Load the roles into memory.
147      *
148      * @exception SecurityException
149      */

150     private void loadRoles() throws SecurityException JavaDoc {
151         try {
152             Map JavaDoc roles = new HashMap JavaDoc();
153             for (Iterator JavaDoc iter = roleHandlers.iterator(); iter.hasNext();) {
154                 RoleHandler handler = (RoleHandler)iter.next();
155                 Map JavaDoc updatedRoles = handler.getRoles();
156                 for (Iterator JavaDoc roleIter = updatedRoles.keySet().iterator();
157                 roleIter.hasNext();) {
158                     String JavaDoc role = (String JavaDoc)roleIter.next();
159                     if (roles.containsKey(role)) {
160                         log.info("Duplicate role [" + role + "] from source : "
161                                 + handler.getClass().getName());
162                     } else {
163                         roles.put(role,updatedRoles.get(role));
164                     }
165                 }
166             }
167             synchronized (this) {
168                 this.roles = roles;
169             }
170         } catch (Exception JavaDoc ex) {
171             throw new SecurityException JavaDoc("Failed to load the role information : "
172                     + ex.getMessage(),ex);
173         }
174     }
175     
176     
177     /**
178      * The method that retrieves the reference to the role manager singleton.
179      *
180      * @return The reference to the role manager singleton.
181      * @exception SecurityException
182      */

183     public static synchronized RoleManager getInstance()
184         throws SecurityException JavaDoc {
185         if (singleton != null) {
186             return singleton;
187         }
188         singleton = new RoleManager();
189         return singleton;
190     }
191     
192     
193     /**
194      * This method is called to start the role managers background thread.
195      */

196     public void startBackgroundThread() {
197         if (roleLoaderThread == null) {
198             roleLoaderThread = new RoleLoaderThread();
199             roleLoaderThread.start();
200         }
201     }
202     
203     
204     /**
205      * This method is called to terminate the background threads processing. It
206      * will only succedded if the coadunation instance has been terminated.
207      */

208     public void terminateBackgroundThread() {
209         roleLoaderThread.terminate();
210         try {
211             roleLoaderThread.join();
212         } catch (Exception JavaDoc ex) {
213             log.error("Failed to terminate the background thread : " +
214                     ex.getMessage(),ex);
215         }
216     }
217     
218     
219     /**
220      * Retrieve the list of roles.
221      *
222      * @return The list of roles.
223      */

224     public synchronized Set JavaDoc getRoles() {
225         return roles.keySet();
226     }
227     
228     
229     /**
230      * Retrieve the role with the matching name.
231      *
232      * @return The role object.
233      * @exception SecurityException
234      */

235     public synchronized Role getRole(String JavaDoc role) throws SecurityException JavaDoc {
236         Role roleRef = (Role)roles.get(role);
237         if (roleRef == null) {
238             throw new SecurityException JavaDoc("The role [" + role
239                     + "] could not be found");
240         }
241         return roleRef;
242     }
243 }
244
Popular Tags