KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@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 gnu.regexp.RE;
21 import gnu.regexp.REException;
22 import org.objectweb.jac.core.ACManager;
23 import org.objectweb.jac.core.rtti.MemberItem;
24 import org.objectweb.jac.core.rtti.MetaItem;
25 import org.objectweb.jac.util.Log;
26 import org.objectweb.jac.core.rtti.AbstractMethodItem;
27
28 /**
29  * Rules for profiles : allow/deny reading, allow/deny writing, etc ...
30  *
31  * @see Profile
32  * @see UserAC
33  */

34
35 public class Rule {
36     public static final boolean ALLOW = true;
37     public static final boolean DENY = false;
38
39     boolean allow;
40     String JavaDoc resourceExpression;
41     transient RE regexp;
42
43     /**
44      * @param allow if <code>true</code> : allowing rule, if
45      * <code>false</code> : denying rule
46      * @param resourceExpression regular expression indicating which
47      * fields or methods this rule aplies to.
48      */

49     public Rule(boolean allow, String JavaDoc resourceExpression) {
50         this.allow = allow;
51         this.resourceExpression = resourceExpression;
52         try {
53             this.regexp = new RE(resourceExpression);
54         } catch (REException e) {
55             throw new RuntimeException JavaDoc("Caught regular expression exception: "+e);
56         }
57     }
58
59     /**
60      * Get rule mode (allow or deny).
61      *
62      * @return the rule's mode.
63      */

64     public boolean getAllow() {
65         return allow;
66     }
67
68     /**
69      * Set rule mode (allow or deny).
70      *
71      * @param allow mode (<code>true</code> for allow,
72      * <code>false</code> for deny).
73      */

74     public void setAllow(boolean allow) {
75         this.allow = allow;
76         invalidateCache();
77     }
78
79     /**
80      * Set regular expression indicating which fields to apply rule for.
81      *
82      * @param resourceExpression the regular expression.
83      */

84     public void setResourceExpression(String JavaDoc resourceExpression) {
85         this.resourceExpression = resourceExpression;
86         invalidateCache();
87     }
88
89     public String JavaDoc getResourceExpression() {
90         return resourceExpression;
91     }
92
93     /**
94      * Returns a regular expression object for the resource expression.
95      */

96     protected RE getRegexp() {
97         if (regexp==null)
98             try {
99                 regexp = new RE(resourceExpression==null?"":resourceExpression);
100             } catch (REException e) {
101             }
102         return regexp;
103     }
104
105     /**
106      * Tells if a <code>MetaItem</code> matches the rule.
107      *
108      * @param item the MetaItem
109      */

110     public boolean match(MetaItem item) {
111         String JavaDoc resourceDescr;
112         if (item instanceof AbstractMethodItem) {
113             AbstractMethodItem method = (AbstractMethodItem)item;
114             resourceDescr = method.getParent().getName()+
115                 "."+method.getFullName();
116         } else if (item instanceof MemberItem) {
117             resourceDescr = ((MemberItem)item).getParent().getName()+
118                 "."+item.getName();
119         } else {
120             resourceDescr = item.toString();
121         }
122         boolean result = getRegexp().isMatch(resourceDescr);
123         if (!result && (item instanceof AbstractMethodItem)) {
124             resourceDescr = ((MemberItem)item).getParent().getName()+
125                 "."+item.getName();
126             result = getRegexp().isMatch(resourceDescr);
127         }
128         Log.trace("profile.rule",resourceExpression+
129                   (result?" matches ":" does not match ")+resourceDescr);
130         return result;
131     }
132
133     /**
134      * Invalidates the UserAC.controlAttribute's cache.
135      *
136      * <p>It merely calls invalidateCache() on UserAC.</p>
137      *
138      * @see UserAC#controlAttribute(Object,MetaItem,String,Object)
139      * @see UserAC#invalidateCache()
140      */

141     protected void invalidateCache() {
142         UserAC ac = (UserAC)ACManager.getACM().getAC("user");
143         if (ac!=null)
144             ac.invalidateCache();
145     }
146 }
147
Popular Tags