1 17 18 package org.objectweb.jac.aspects.integrity; 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 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 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 51 public Object checkDoubles(Interaction interaction) throws Exception { 52 logger.debug("entering doubles-check for " + interaction.method); 53 54 CollectionItem coll = 55 ((MethodItem) interaction.method).getAddedCollection(); 56 57 String [] fieldsToComp = 58 (String []) coll.getAttribute(RttiAC.PRIMARY_KEY); 59 60 Object addedObject = interaction.args[0]; 61 logger.debug("added value: " + addedObject); 62 63 Collection collection = coll.getActualCollection(interaction.wrappee); 64 65 Object [] newFields = new Object [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 it = collection.iterator(); 74 while (it.hasNext()) { 75 Object object = it.next(); 76 if (((fieldsToComp == null) || (fieldsToComp.length == 0)) 77 && (object.equals(addedObject))) 78 throw new Exception ( 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 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 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 ( 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 invoke(MethodInvocation invocation) throws Throwable { 120 return checkDoubles((Interaction) invocation); 121 } 122 123 public Object construct(ConstructorInvocation invocation) 124 throws Throwable { 125 throw new Exception ("Wrapper "+this+" does not support construction interception."); 126 } 127 } 128 | Popular Tags |