KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > integrity > ConstraintWrapper


1 /*
2   Copyright (C) 2002 Julien van Malderen, 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.integrity;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.Vector JavaDoc;
22 import org.aopalliance.intercept.ConstructorInvocation;
23 import org.aopalliance.intercept.MethodInvocation;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.AspectComponent;
26 import org.objectweb.jac.core.Interaction;
27 import org.objectweb.jac.core.Wrapper;
28 import org.objectweb.jac.core.rtti.ClassItem;
29 import org.objectweb.jac.core.rtti.ClassRepository;
30 import org.objectweb.jac.core.rtti.FieldItem;
31 import org.objectweb.jac.core.rtti.MethodItem;
32
33 /**
34  * This wrapper provides a constraints scheme to limit fields
35  * modifications.
36  */

37
38 public class ConstraintWrapper extends Wrapper {
39     static final Logger logger = Logger.getLogger("integrity.conditions");
40
41     public ConstraintWrapper(AspectComponent ac) {
42         super(ac);
43     }
44
45     private void invokeTest(
46         MethodItem method,
47         FieldItem field,
48         Object JavaDoc[] args,
49         String JavaDoc errorMsg)
50         throws Exception JavaDoc
51     {
52         Boolean JavaDoc result = (Boolean JavaDoc) method.invoke(null, args);
53
54         if (result == null)
55             throw new Exception JavaDoc("The constraint " + method + " does not exist");
56
57         if (!result.booleanValue()) {
58             if ((errorMsg == null) || errorMsg.length() == 0)
59                 throw new Exception JavaDoc(
60                     "The constraint "
61                         + method
62                         + " has not been validated for "
63                         + field);
64             throw new Exception JavaDoc(field + ": " + errorMsg);
65         }
66     }
67
68     /**
69      * This wrapping method checks if pre and post conditions are
70      * validated for modified fields.
71      */

72
73     public Object JavaDoc testConditions(Interaction interaction) throws Exception JavaDoc {
74         logger.debug(
75             "entering test conditions for " + interaction.method);
76
77         IntegrityAC ac = (IntegrityAC) getAspectComponent();
78         String JavaDoc fieldName =
79             ((MethodItem) interaction.method).getWrittenFields()[0].getName();
80         ClassItem curClass =
81             ClassRepository.get().getClass(interaction.wrappee);
82         FieldItem field = null;
83         Vector JavaDoc preList = null;
84
85         // get the pre list from the superclasses if not defined here
86
while (preList == null && curClass != null) {
87             field = curClass.getField(fieldName);
88             preList = (Vector JavaDoc) ac.preConditionsFields.get(field);
89             logger.debug(
90                 field.getClassItem() + "." + field + " => " + preList);
91             curClass = curClass.getSuperclass();
92         }
93
94         Vector JavaDoc postList = null;
95         curClass = ClassRepository.get().getClass(interaction.wrappee);
96
97         // get the post list from the superclasses if not defined here
98
while (postList == null && curClass != null) {
99             field = curClass.getField(fieldName);
100             postList = (Vector JavaDoc) ac.postConditionsFields.get(field);
101             logger.debug(
102                 field.getClassItem() + "." + field + " => " + preList);
103             curClass = curClass.getSuperclass();
104         }
105
106         if (preList != null) {
107             Iterator JavaDoc i = preList.iterator();
108             Object JavaDoc currentFieldValue =
109                 field.getThroughAccessor(interaction.wrappee);
110             while (i.hasNext()) {
111                 ConstraintDescriptor conditionToTest =
112                     (ConstraintDescriptor) i.next();
113                 logger.debug(
114                     "testing pre " + conditionToTest.getConstraint());
115                 invokeTest(
116                     conditionToTest.getConstraint(),
117                     field,
118                     new Object JavaDoc[] {
119                         interaction.wrappee,
120                         field,
121                         currentFieldValue,
122                         conditionToTest.getParams()},
123                     conditionToTest.getErrorMsg());
124             }
125         }
126
127         if (postList != null) {
128             Iterator JavaDoc i = postList.iterator();
129             while (i.hasNext()) {
130                 ConstraintDescriptor conditionToTest =
131                     (ConstraintDescriptor) i.next();
132                 logger.debug(
133                     "testing post " + conditionToTest.getConstraint());
134                 invokeTest(
135                     conditionToTest.getConstraint(),
136                     field,
137                     new Object JavaDoc[] {
138                         interaction.wrappee,
139                         field,
140                         interaction.args[0],
141                         conditionToTest.getParams()},
142                     conditionToTest.getErrorMsg());
143             }
144         }
145
146         return proceed(interaction);
147     }
148
149     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
150         return testConditions((Interaction) invocation);
151     }
152 }
153
Popular Tags