KickJava   Java API By Example, From Geeks To Geeks.

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


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.jmanage.core.util.CoreUtils;
20 import org.jmanage.core.util.Loggers;
21 import org.jmanage.util.StringUtils;
22
23 import java.util.*;
24 import java.util.logging.Logger JavaDoc;
25 import java.io.*;
26
27 /**
28  * Date: Mar 8, 2005 8:00:47 AM
29  * @author Shashank Bellary
30  */

31 public class ACLStore {
32
33     private static final String JavaDoc ACL_CONFIG_FILE = "acl-config.properties";
34     private static final Logger JavaDoc logger = Loggers.getLogger(ACLStore.class);
35     private static final ACLStore instance = new ACLStore();
36
37     private Map aclNameToACLMap = new HashMap();
38
39     /**
40      *
41      */

42     private ACLStore() {
43
44         final String JavaDoc configFile = CoreUtils.getConfigDir() + File.separator +
45                 ACL_CONFIG_FILE;
46
47         try {
48             BufferedReader reader = new BufferedReader(new FileReader(configFile));
49             String JavaDoc line = reader.readLine();
50             while(line != null){
51                 parse(line);
52                 line = reader.readLine();
53             }
54         } catch (IOException e) {
55             throw new RuntimeException JavaDoc("Error reading: " + configFile, e);
56         }
57         logger.info("Loaded ACLs");
58     }
59
60
61     /**
62      * The only access to this instance.
63      *
64      * @return
65      */

66     public static ACLStore getInstance() {
67         return instance;
68     }
69
70     public ACL getACL(String JavaDoc aclName){
71         return (ACL)aclNameToACLMap.get(aclName);
72     }
73
74     private void parse(String JavaDoc line){
75         line = line.trim();
76         if(line.length() == 0 || line.startsWith("#")){
77             return;
78         }
79
80         int index = line.lastIndexOf('=');
81         if(index == -1){
82             throw new RuntimeException JavaDoc("Invalid line format: " + line);
83         }
84         String JavaDoc acl = line.substring(0, index);
85         String JavaDoc authorizedList = line.substring(index + 1);
86         /* now seperate acl name from the context */
87         index = acl.indexOf('@');
88         // todo: ACLContext should be made abstract and should be constructed
89
// todo: based on the application that is using it.
90
String JavaDoc aclName = null;
91         String JavaDoc aclContext = null;
92         if(index != -1){
93             aclName = acl.substring(0, index);
94             aclContext = acl.substring(index+1);
95         }else{
96             aclName = acl;
97         }
98
99         storeACL(aclName, aclContext, authorizedList);
100     }
101
102     private void storeACL(String JavaDoc aclName,
103                           String JavaDoc aclContext,
104                           String JavaDoc authorizedList){
105         ACL acl = (ACL)aclNameToACLMap.get(aclName);
106         if(acl == null){
107             acl = new ACL(aclName);
108             aclNameToACLMap.put(aclName, acl);
109         }
110         List authorizedListObj = StringUtils.csvToList(authorizedList);
111         if(aclContext == null){
112             acl.setAuthorizedList(authorizedListObj);
113         }else{
114             acl.add(new ACLContext(aclContext), authorizedListObj);
115         }
116         logger.fine("Added ACL: " + aclName + " - " +
117                 aclContext + " - " + authorizedList);
118     }
119 }
Popular Tags