KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > security > SecurityConfig


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.security;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.apache.commons.digester.Digester;
24 import org.apache.commons.digester.ExtendedBaseRules;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import web.security.Operation;
28 import web.security.Privilege;
29 import web.security.Range;
30 import web.security.Resource;
31 import web.security.Role;
32 import web.security.impl.OperationImpl;
33 import web.security.impl.PrivilegeImpl;
34 import web.security.impl.RangeImpl;
35 import web.security.impl.ResourceImpl;
36
37 /**
38  * DLOG4j的安全配置信息
39  * 该类对应security.xml中的信息
40  * @author Winter Lau
41  */

42 public class SecurityConfig {
43     
44     public final static String JavaDoc root = "web-security";
45     //单件模式-Singleton
46
private static SecurityConfig config;
47     
48     static{
49         Digester dig = new Digester();
50         dig.setValidating(false);
51         dig.setRules(new ExtendedBaseRules());
52
53         dig.addObjectCreate(root, SecurityConfig.class);
54         dig.addSetProperties(root);
55         
56         //Ranges
57
String JavaDoc key_range = root+"/ranges/range";
58         dig.addObjectCreate(key_range, "type", RangeImpl.class);
59         dig.addSetProperties(key_range);
60         dig.addBeanPropertySetter(key_range + "/?");
61         dig.addSetNext(key_range, "addRange");
62
63         //Operations
64
String JavaDoc key_opt = root+"/operations/operation";
65         dig.addObjectCreate(key_opt, "type", OperationImpl.class);
66         dig.addSetProperties(key_opt);
67         dig.addBeanPropertySetter(key_opt + "/?");
68         dig.addSetNext(key_opt, "addOperation");
69
70         //Resources
71
String JavaDoc key_res = root+"/resources/resource";
72         dig.addObjectCreate(key_res, "type", ResourceImpl.class);
73         dig.addSetProperties(key_res);
74         dig.addBeanPropertySetter(key_res + "/?");
75         dig.addSetNext(key_res, "addResource");
76         
77         InputStream JavaDoc in = SecurityConfig.class.getResourceAsStream("/security.xml");
78         try{
79             config = (SecurityConfig) dig.parse(in);
80         }catch(Exception JavaDoc e){
81             System.err.println("Initialized security.xml failed.");
82             e.printStackTrace(System.err);
83         }finally{
84             try{
85                 in.close();
86             }catch(Exception JavaDoc e){}
87         }
88         
89     }
90     
91     private List JavaDoc resources;
92     private List JavaDoc operations;
93     private List JavaDoc ranges;
94     private List JavaDoc roles;
95     
96     /**
97      * 由于该方法是提供给Digester调用,因此必须是public
98      */

99     public SecurityConfig() {
100         resources = new Vector JavaDoc();
101         operations = new Vector JavaDoc();
102         ranges = new Vector JavaDoc();
103         roles = new Vector JavaDoc();
104     }
105     
106     public Range getRangeByName(String JavaDoc name){
107         for(int i=0;i<ranges.size();i++){
108             Range range = (Range)ranges.get(i);
109             if(range.getName().equals(name))
110                 return range;
111         }
112         return null;
113     }
114     
115     public Operation getOperationByName(String JavaDoc name){
116         for(int i=0;i<operations.size();i++){
117             Operation opt = (Operation)operations.get(i);
118             if(opt.getName().equals(name))
119                 return opt;
120         }
121         return null;
122     }
123     
124     public Resource getResourceByName(String JavaDoc name){
125         for(int i=0;i<resources.size();i++){
126             Resource res = (Resource)resources.get(i);
127             if(res.getName().equals(name))
128                 return res;
129         }
130         return null;
131     }
132     
133     /**
134      * 根据编号获取对应的角色信息
135      * @param id
136      * @return
137      */

138     public DlogRole getRoleById(int id){
139         for(int i=0;i<getRoles().size();i++){
140             DlogRole role = (DlogRole)roles.get(i);
141             if(role.getId()==id)
142                 return role;
143         }
144         return null;
145     }
146     
147     /**
148      * 该方法是提供给Digester使用,请不要使用该方法
149      * @param range
150      */

151     public void addRange(Range range){
152         ranges.add(range);
153     }
154     
155     /**
156      * 该方法是提供给Digester使用,请不要使用该方法
157      * @param opt
158      */

159     public void addOperation(Operation opt){
160         operations.add(opt);
161     }
162     
163     /**
164      * 该方法是提供给Digester使用,请不要使用该方法
165      * @param res
166      */

167     public void addResource(Resource res){
168         resources.add(res);
169     }
170     
171     /**
172      * 该方法是提供给Digester使用,请不要使用该方法
173      * @param role
174      */

175     public void addRole(Role role){
176         roles.add(role);
177     }
178
179     /**
180      * 用于获取安全配置信息的实例
181      * SecurityConfig config = SecurityConfig.getConfig();
182      * @return
183      * @throws IOException
184      * @throws SAXException
185      */

186     public static SecurityConfig getConfig() throws IOException JavaDoc, SAXException JavaDoc{
187         return config;
188     }
189     
190     public List JavaDoc getOperations() {
191         return operations;
192     }
193     public List JavaDoc getRanges() {
194         return ranges;
195     }
196     public List JavaDoc getResources() {
197         return resources;
198     }
199     public List JavaDoc getRoles() {
200         if(roles.size()==0){
201             Digester dig = new Digester();
202             dig.setValidating(false);
203             dig.setRules(new ExtendedBaseRules());
204
205             dig.addObjectCreate(root, SecurityConfig.class);
206             dig.addSetProperties(root);
207             
208             //Roles
209
String JavaDoc key_role = root+"/roles/role";
210             dig.addObjectCreate(key_role, "type", DlogRole.class);
211             dig.addSetProperties(key_role);
212             //privilege
213
String JavaDoc key_pvg = key_role + "/privileges/privilege";
214             dig.addCallMethod(key_pvg, "addPrivilege", 3);
215             dig.addCallParam(key_pvg+"/resource",0);
216             dig.addCallParam(key_pvg+"/operation",1);
217             dig.addCallParam(key_pvg+"/range",2);
218             dig.addSetNext(key_role, "addRole");
219
220             InputStream JavaDoc in = SecurityConfig.class.getResourceAsStream("/security.xml");
221             try{
222                 SecurityConfig config2 = (SecurityConfig) dig.parse(in);
223                 config.roles.addAll(config2.getRoles());
224             }catch(Exception JavaDoc e){
225                 System.err.println("Initialized security.xml failed.");
226                 e.printStackTrace(System.err);
227             }finally{
228                 try{
229                     in.close();
230                 }catch(Exception JavaDoc e){}
231             }
232         }
233         return roles;
234     }
235     
236     public static void main(String JavaDoc[] args) throws IOException JavaDoc, SAXException JavaDoc{
237         SecurityConfig config = SecurityConfig.getConfig();
238         Role guest = config.getRoleById(DlogRole.ROLE_GUEST);
239         Role user = config.getRoleById(DlogRole.ROLE_COMMON);
240         Role friend = config.getRoleById(DlogRole.ROLE_FRIEND);
241         Role buddy = config.getRoleById(DlogRole.ROLE_BUDDY);
242         Role manager = config.getRoleById(DlogRole.ROLE_MANAGER);
243         /*
244         System.out.println("manager.pvg.count="+manager.privileges().length);
245         for(int i=0;i<manager.privileges().length;i++){
246             System.out.println("manager.pvg["+i+"].resource="+manager.privileges()[i].getResource());
247             System.out.println("manager.pvg["+i+"].operation="+manager.privileges()[i].getOperation());
248             System.out.println("manager.pvg["+i+"].range="+manager.privileges()[i].getRange());
249         }*/

250         
251         Resource res = config.getResourceByName("log");
252         Operation opt = config.getOperationByName("maintain");
253         Range range = config.getRangeByName("all");
254         
255         Privilege pvg = new PrivilegeImpl(res,opt,range);
256         
257         System.out.println("guest cando: "+guest.canDo(pvg));
258         System.out.println("user cando: "+user.canDo(pvg));
259         System.out.println("friend cando: "+friend.canDo(pvg));
260         System.out.println("buddy cando: "+buddy.canDo(pvg));
261         System.out.println("manager cando: "+manager.canDo(pvg));
262         
263         
264         /*
265         if(config==null)
266             return;
267         for(int i=0;i<config.getRanges().size();i++)
268             System.out.println(config.getRanges().get(i)+" ===== "+config.getRanges().get(i).getClass().getName());
269         for(int i=0;i<config.getOperations().size();i++)
270             System.out.println(config.getOperations().get(i)+" ===== "+config.getOperations().get(i).getClass().getName());
271         for(int i=0;i<config.getResources().size();i++)
272             System.out.println(config.getResources().get(i)+" ===== "+config.getResources().get(i).getClass().getName());
273         for(int i=0;i<config.getRoles().size();i++)
274             System.out.println(config.getRoles().get(i)+" ===== "+config.getRoles().get(i).getClass().getName());
275         */

276     }
277     
278 }
279
Popular Tags