KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > user > Profile


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

17
18 package org.objectweb.jac.aspects.user;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.objectweb.jac.core.ACManager;
24 import org.objectweb.jac.core.rtti.MetaItem;
25 import org.objectweb.jac.util.Log;
26 import org.objectweb.jac.util.Stack;
27 import java.util.List JavaDoc;
28
29 public class Profile {
30
31     public Profile() {}
32
33     public Profile(String JavaDoc name) {
34         this.name = name;
35     }
36
37     public Profile(String JavaDoc name, Profile parent) {
38         this.name = name;
39         this.parent = parent;
40     }
41
42     String JavaDoc name;
43    
44     /**
45      * Get the value of name.
46      * @return value of name.
47      */

48     public String JavaDoc getName() {
49         return name;
50     }
51    
52     /**
53      * Set the value of name.
54      * @param v Value to assign to name.
55      */

56     public void setName(String JavaDoc v) {
57         this.name = v;
58     }
59    
60     /** inherited profile */
61     Profile parent = null;
62
63     public void setParent(Profile parent) {
64         this.parent = parent;
65         invalidateCache();
66     }
67     public Profile getParent() {
68         return parent;
69     }
70
71     transient boolean isNew = true;
72     public void setIsNew(boolean isNew) {
73         this.isNew = isNew;
74     }
75     public boolean isNew() {
76         return isNew;
77     }
78
79     /**
80      * Clear all rules
81      */

82     public void clear() {
83         readRules.clear();
84         writeRules.clear();
85         addRules.clear();
86         createRules.clear();
87         removeRules.clear();
88         invalidateCache();
89     }
90    
91     Vector JavaDoc readRules = new Vector JavaDoc();
92     Vector JavaDoc writeRules = new Vector JavaDoc();
93     Vector JavaDoc addRules = new Vector JavaDoc();
94     Vector JavaDoc removeRules = new Vector JavaDoc();
95     Vector JavaDoc createRules = new Vector JavaDoc();
96
97     public List JavaDoc getReadRules() {
98         return readRules;
99     }
100     public void addReadRule(Rule rule) {
101         readRules.add(rule);
102         invalidateCache();
103     }
104     public void removeReadRule(Rule rule) {
105         readRules.remove(rule);
106         invalidateCache();
107     }
108
109     public List JavaDoc getWriteRules() {
110         return writeRules;
111     }
112     public void addWriteRule(Rule rule) {
113         writeRules.add(rule);
114         invalidateCache();
115     }
116     public void removeWriteRule(Rule rule) {
117         writeRules.remove(rule);
118         invalidateCache();
119     }
120
121     public List JavaDoc getAddRules() {
122         return addRules;
123     }
124     public void addAddRule(Rule rule) {
125         addRules.add(rule);
126         invalidateCache();
127     }
128     public void removeAddRule(Rule rule) {
129         addRules.remove(rule);
130         invalidateCache();
131     }
132
133     public List JavaDoc getCreateRules() {
134         return createRules;
135     }
136     public void addCreateRule(Rule rule) {
137         createRules.add(rule);
138         invalidateCache();
139     }
140     public void removeCreateRule(Rule rule) {
141         createRules.remove(rule);
142         invalidateCache();
143     }
144
145     public List JavaDoc getRemoveRules() {
146         return removeRules;
147     }
148     public void addRemoveRule(Rule rule) {
149         removeRules.add(rule);
150         invalidateCache();
151     }
152     public void removeRemoveRule(Rule rule) {
153         removeRules.remove(rule);
154         invalidateCache();
155     }
156
157     public void addReadable(String JavaDoc resourceExpr) {
158         readRules.add(new Rule(Rule.ALLOW,resourceExpr));
159         invalidateCache();
160     }
161
162     public void addUnreadable(String JavaDoc resourceExpr) {
163         readRules.add(new Rule(Rule.DENY,resourceExpr));
164         invalidateCache();
165     }
166
167     public void addWritable(String JavaDoc resourceExpr) {
168         writeRules.add(new Rule(Rule.ALLOW,resourceExpr));
169         invalidateCache();
170     }
171
172     public void addUnwritable(String JavaDoc resourceExpr) {
173         writeRules.add(new Rule(Rule.DENY,resourceExpr));
174         invalidateCache();
175     }
176
177     public void addAddable(String JavaDoc resourceExpr) {
178         addRules.add(new Rule(Rule.ALLOW,resourceExpr));
179         invalidateCache();
180     }
181
182     public void addCreatable(String JavaDoc resourceExpr) {
183         createRules.add(new Rule(Rule.ALLOW,resourceExpr));
184         invalidateCache();
185     }
186
187     public void addUnaddable(String JavaDoc resourceExpr) {
188         addRules.add(new Rule(Rule.DENY,resourceExpr));
189         invalidateCache();
190     }
191
192     public void addRemovable(String JavaDoc resourceExpr) {
193         removeRules.add(new Rule(Rule.ALLOW,resourceExpr));
194         invalidateCache();
195     }
196
197     public void addUnremovable(String JavaDoc resourceExpr) {
198         removeRules.add(new Rule(Rule.DENY,resourceExpr));
199         invalidateCache();
200     }
201
202     public Stack getProfileStack() {
203         Stack profiles = new Stack();
204         Profile profile = this;
205         while (profile!=null) {
206             profiles.push(profile);
207             profile = profile.getParent();
208         }
209         return profiles;
210     }
211
212     public boolean isReadable(MetaItem item) {
213         Stack profiles = getProfileStack();
214         while(!profiles.empty()) {
215             Profile profile = (Profile)profiles.pop();
216             Collection JavaDoc rules = profile.getReadRules();
217             Iterator JavaDoc it = rules.iterator();
218             while (it.hasNext()) {
219                 Rule rule = (Rule)it.next();
220                 if (rule.match(item))
221                     return rule.getAllow();
222             }
223         }
224         return false;
225     }
226
227     public boolean isWritable(MetaItem item) {
228         Log.trace("profile",2,"isWritable "+item);
229         Stack profiles = getProfileStack();
230         while(!profiles.empty()) {
231             Profile profile = (Profile)profiles.pop();
232             Log.trace("profile",2,"Current profile "+profile);
233             Collection JavaDoc rules = profile.getWriteRules();
234             Iterator JavaDoc it = rules.iterator();
235             while (it.hasNext()) {
236                 Rule rule = (Rule)it.next();
237                 if (rule.match(item))
238                     return rule.getAllow();
239             }
240         }
241         return false;
242     }
243
244     public boolean isAddable(MetaItem item) {
245         Stack profiles = getProfileStack();
246         while(!profiles.empty()) {
247             Profile profile = (Profile)profiles.pop();
248             Collection JavaDoc rules = profile.getAddRules();
249             Iterator JavaDoc it = rules.iterator();
250             while (it.hasNext()) {
251                 Rule rule = (Rule)it.next();
252                 if (rule.match(item))
253                     return rule.getAllow();
254             }
255         }
256         return false;
257     }
258
259     public boolean isCreatable(MetaItem item) {
260         Stack profiles = getProfileStack();
261         while (!profiles.empty()) {
262             Profile profile = (Profile)profiles.pop();
263             Collection JavaDoc rules = profile.getCreateRules();
264             Iterator JavaDoc it = rules.iterator();
265             while (it.hasNext()) {
266                 Rule rule = (Rule)it.next();
267                 if (rule.match(item))
268                     return rule.getAllow();
269             }
270         }
271         return false;
272     }
273
274     public boolean isRemovable(MetaItem item) {
275         Stack profiles = getProfileStack();
276         while(!profiles.empty()) {
277             Profile profile = (Profile)profiles.pop();
278             Collection JavaDoc rules = profile.getRemoveRules();
279             Iterator JavaDoc it = rules.iterator();
280             while (it.hasNext()) {
281                 Rule rule = (Rule)it.next();
282                 if (rule.match(item))
283                     return rule.getAllow();
284             }
285         }
286         return false;
287     }
288
289     public static boolean isReadable(Collection JavaDoc profiles,MetaItem item) {
290         Iterator JavaDoc it = profiles.iterator();
291         while(it.hasNext()) {
292             Profile profile = (Profile)it.next();
293             if (profile.isReadable(item))
294                 return true;
295         }
296         return false;
297     }
298
299     public static boolean isWritable(Collection JavaDoc profiles,MetaItem item) {
300         Iterator JavaDoc it = profiles.iterator();
301         while(it.hasNext()) {
302             Profile profile = (Profile)it.next();
303             if (profile.isWritable(item))
304                 return true;
305         }
306         return false;
307     }
308
309     public static boolean isAddable(Collection JavaDoc profiles,MetaItem item) {
310         Iterator JavaDoc it = profiles.iterator();
311         while(it.hasNext()) {
312             Profile profile = (Profile)it.next();
313             if (profile.isAddable(item))
314                 return true;
315         }
316         return false;
317     }
318
319     public static boolean isCreatable(Collection JavaDoc profiles,MetaItem item) {
320         Iterator JavaDoc it = profiles.iterator();
321         while(it.hasNext()) {
322             Profile profile = (Profile)it.next();
323             if (profile.isCreatable(item))
324                 return true;
325         }
326         return false;
327     }
328
329     public static boolean isRemovable(Collection JavaDoc profiles,MetaItem item) {
330         Iterator JavaDoc it = profiles.iterator();
331         while(it.hasNext()) {
332             Profile profile = (Profile)it.next();
333             if (profile.isRemovable(item))
334                 return true;
335         }
336         return false;
337     }
338
339     protected void invalidateCache() {
340         UserAC ac = ((UserAC)ACManager.getACM().getAC("user"));
341         if (ac!=null)
342             ac.invalidateCache();
343     }
344
345     public String JavaDoc toString() {
346         return name;
347     }
348 }
349
Popular Tags