KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > rule > AccessRule


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002-2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.rule;
22
23 import net.innig.macker.structure.ClassManager;
24 import net.innig.macker.structure.ClassInfo;
25 import net.innig.macker.util.IncludeExcludeLogic;
26 import net.innig.macker.util.IncludeExcludeNode;
27 import net.innig.macker.event.AccessRuleViolation;
28 import net.innig.macker.event.MackerIsMadException;
29 import net.innig.macker.event.ListenerException;
30
31 import java.util.*;
32 import net.innig.collect.MultiMap;
33
34 public class AccessRule
35     extends Rule
36     {
37     //--------------------------------------------------------------------------
38
// Constructors
39
//--------------------------------------------------------------------------
40

41     public AccessRule(RuleSet parent)
42         {
43         super(parent);
44         type = AccessRuleType.DENY;
45         from = to = Pattern.ALL;
46         }
47     
48     //--------------------------------------------------------------------------
49
// Properties
50
//--------------------------------------------------------------------------
51

52     public AccessRuleType getType()
53         { return type; }
54     
55     public void setType(AccessRuleType type)
56         {
57         if(type == null)
58             throw new NullPointerException JavaDoc("type parameter cannot be null");
59         this.type = type;
60         }
61     
62     public Pattern getFrom()
63         { return from; }
64     
65     public void setFrom(Pattern from)
66         { this.from = from; }
67     
68     public String JavaDoc getMessage()
69         { return message; }
70     
71     public void setMessage(String JavaDoc message)
72         { this.message = message; }
73
74     public Pattern getTo()
75         { return to; }
76     
77     public void setTo(Pattern to)
78         { this.to = to; }
79     
80     public AccessRule getChild()
81         { return child; }
82     
83     public void setChild(AccessRule child)
84         { this.child = child; }
85     
86     public AccessRule getNext()
87         { return next; }
88     
89     public void setNext(AccessRule next)
90         { this.next = next; }
91     
92     private AccessRuleType type;
93     private Pattern from, to;
94     private String JavaDoc message;
95     private boolean bound;
96     private AccessRule child, next;
97
98     //--------------------------------------------------------------------------
99
// Evaluation
100
//--------------------------------------------------------------------------
101

102     public void check(EvaluationContext context, ClassManager classes)
103         throws RulesException, MackerIsMadException, ListenerException
104         {
105         EvaluationContext localContext = new EvaluationContext(context);
106         for(Iterator refIter = classes.getReferences().entrySet().iterator(); refIter.hasNext(); )
107             {
108             MultiMap.Entry entry = (MultiMap.Entry) refIter.next();
109             ClassInfo from = (ClassInfo) entry.getKey();
110             ClassInfo to = (ClassInfo) entry.getValue();
111             if(from.equals(to))
112                 continue;
113             if(!localContext.getRuleSet().isInSubset(localContext, from))
114                 continue;
115
116             localContext.setVariableValue("from", from.getClassName());
117             localContext.setVariableValue("to", to.getClassName());
118             localContext.setVariableValue("from-package", from.getPackageName());
119             localContext.setVariableValue("to-package", to.getPackageName());
120             localContext.setVariableValue("from-full", from.getFullName());
121             localContext.setVariableValue("to-full", to.getFullName());
122
123             if(!checkAccess(localContext, from, to))
124                 {
125                 List messages;
126                 if(getMessage() == null)
127                     messages = Collections.EMPTY_LIST;
128                 else
129                     messages = Collections.singletonList(
130                         VariableParser.parse(localContext, getMessage()));
131                 
132                 context.broadcastEvent(
133                     new AccessRuleViolation(this, from, to, messages));
134                 }
135             }
136         }
137     
138     public boolean checkAccess(EvaluationContext context, ClassInfo fromClass, ClassInfo toClass)
139         throws RulesException
140         { return IncludeExcludeLogic.apply(makeIncludeExcludeNode(this, context, fromClass, toClass)); }
141     
142     private static IncludeExcludeNode makeIncludeExcludeNode(
143             final AccessRule rule,
144             final EvaluationContext context,
145             final ClassInfo fromClass,
146             final ClassInfo toClass)
147         {
148         return (rule == null)
149             ? null
150             : new IncludeExcludeNode()
151                 {
152                 public boolean isInclude()
153                     { return rule.getType() == AccessRuleType.ALLOW; }
154         
155                 public boolean matches()
156                     throws RulesException
157                     {
158                     return rule.getFrom().matches(context, fromClass)
159                         && rule. getTo().matches(context, toClass);
160                     }
161                 
162                 public IncludeExcludeNode getChild()
163                     { return makeIncludeExcludeNode(rule.getChild(), context, fromClass, toClass); }
164                 
165                 public IncludeExcludeNode getNext()
166                     { return makeIncludeExcludeNode(rule.getNext(), context, fromClass, toClass); }
167                 };
168         }
169     }
170
171
172
173
Popular Tags