KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > auth > RoleManager


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.jmanage.core.auth;
18
19 import org.jdom.Document;
20 import org.jdom.JDOMException;
21 import org.jdom.Element;
22 import org.jdom.input.SAXBuilder;
23 import org.jmanage.core.util.Loggers;
24
25 import java.io.File JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 /**
32  * Curently loading roles from configuration files.
33  *
34  * Date: Mar 20, 2005 12:58:25 PM
35  * @author Shashank Bellary
36  */

37 public class RoleManager implements AuthConstants{
38     private static long lastModified = -1;
39     private static List JavaDoc roles = null;
40     private static final Logger JavaDoc logger = Loggers.getLogger(RoleManager.class);
41
42     static{
43         init(new File JavaDoc(ROLE_CONFIG_FILE_NAME));
44     }
45
46     private RoleManager(){}
47
48     /**
49      * Initializer
50      *
51      * @param roleConfigFile
52      */

53     private static void init(File JavaDoc roleConfigFile) {
54         try{
55             lastModified = roleConfigFile.lastModified();
56             Document roleConfig = new SAXBuilder().build(roleConfigFile);
57             roles = loadUserRoles(roleConfig);
58         }catch(JDOMException jdEx){
59             logger.info("Error reading roles "+ROLE_CONFIG_FILE_NAME);
60             jdEx.printStackTrace();
61         }
62     }
63
64     /**
65      * The only accessor method to the configured roles.
66      *
67      * @return
68      */

69     public static List JavaDoc getAll(){
70         File JavaDoc roleConfigFile = new File JavaDoc(ROLE_CONFIG_FILE_NAME);
71         if(lastModified < roleConfigFile.lastModified()){
72             /* Refresh the cache */
73             init(roleConfigFile);
74         }
75         return roles;
76     }
77
78     /**
79      * Read the configuration file and load all configured roles
80      *
81      * @param roleConfig
82      * @return
83      */

84     private static List JavaDoc loadUserRoles(Document roleConfig){
85         List JavaDoc userRoles = new ArrayList JavaDoc();
86         List JavaDoc configuredRoles = roleConfig.getRootElement().getChildren();
87         Iterator JavaDoc roleIterator = configuredRoles.iterator();
88         while(roleIterator.hasNext()){
89             Element role = (Element)roleIterator.next();
90             userRoles.add(new Role(role.getTextTrim()));
91         }
92         return userRoles;
93     }
94 }
Popular Tags