KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
21 import java.util.Iterator 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.CollectionItem;
31 import org.objectweb.jac.core.rtti.MethodItem;
32 import org.objectweb.jac.core.rtti.RttiAC;
33
34 /**
35  * This wrapper provides a key scheme to limit fields
36  * doubles.
37  */

38
39 public class PrimaryKeyWrapper extends Wrapper {
40     static final Logger logger = Logger.getLogger("integrity.primary");
41
42     public PrimaryKeyWrapper(AspectComponent ac) {
43         super(ac);
44     }
45
46     /**
47      * This wrapping method checks if the added Object's fields match
48      * with an Object in the added collection, and throws an exception
49      * if there is one.
50      */

51     public Object JavaDoc checkDoubles(Interaction interaction) throws Exception JavaDoc {
52         logger.debug("entering doubles-check for " + interaction.method);
53
54         CollectionItem coll =
55             ((MethodItem) interaction.method).getAddedCollection();
56
57         String JavaDoc[] fieldsToComp =
58             (String JavaDoc[]) coll.getAttribute(RttiAC.PRIMARY_KEY);
59
60         Object JavaDoc addedObject = interaction.args[0];
61         logger.debug("added value: " + addedObject);
62
63         Collection JavaDoc collection = coll.getActualCollection(interaction.wrappee);
64
65         Object JavaDoc[] newFields = new Object JavaDoc[fieldsToComp.length];
66         ClassItem cli = ClassRepository.get().getClass(addedObject);
67         for (int i=0; i<fieldsToComp.length; i++) {
68             newFields[i] =
69                 cli.getField(fieldsToComp[i])
70                     .getThroughAccessor(addedObject);
71         }
72
73         Iterator JavaDoc it = collection.iterator();
74         while (it.hasNext()) {
75             Object JavaDoc object = it.next();
76             if (((fieldsToComp == null) || (fieldsToComp.length == 0))
77                 && (object.equals(addedObject)))
78                 throw new Exception JavaDoc(
79                     "Collection "
80                         + coll.getName()
81                         + " already contains an element equals to "
82                         + addedObject);
83
84             if ((fieldsToComp.length == 0)
85                 || (!addedObject.getClass().equals(object.getClass())))
86                 continue;
87
88             int counter = 0;
89             cli = ClassRepository.get().getClass(object);
90             for (int i=0; i<fieldsToComp.length; i++) {
91                 Object JavaDoc valueToTest =
92                     cli.getField(fieldsToComp[i]).getThroughAccessor(object);
93
94                 if (((valueToTest == null) && (newFields[i] == null))
95                     || ((valueToTest != null)
96                         && (valueToTest.equals(newFields[i]))))
97                     counter++;
98                 else
99                     break;
100             }
101             if (counter == fieldsToComp.length) {
102                 String JavaDoc fields = "";
103                 for (int j = 0; j < fieldsToComp.length; j++) {
104                     if (j > 0)
105                         fields += ", ";
106                     fields += fieldsToComp[j];
107                 }
108                 throw new Exception JavaDoc(
109                     "Collection "
110                         + coll.getName()
111                         + " already contains an element with fields { "
112                         + fields
113                         + " } equals to those in the added element");
114             }
115         }
116         return proceed(interaction);
117     }
118
119     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
120         return checkDoubles((Interaction) invocation);
121     }
122
123     public Object JavaDoc construct(ConstructorInvocation invocation)
124         throws Throwable JavaDoc {
125         throw new Exception JavaDoc("Wrapper "+this+" does not support construction interception.");
126     }
127 }
128
Popular Tags