1 17 18 package org.objectweb.jac.aspects.integrity; 19 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Set ; 23 import java.util.Vector ; 24 import org.aopalliance.intercept.MethodInvocation; 25 import org.apache.log4j.Logger; 26 import org.objectweb.jac.core.AspectComponent; 27 import org.objectweb.jac.core.Collaboration; 28 import org.objectweb.jac.core.Wrapper; 29 import org.objectweb.jac.core.rtti.CollectionItem; 30 import org.objectweb.jac.core.rtti.FieldItem; 31 import org.objectweb.jac.core.rtti.MethodItem; 32 import org.objectweb.jac.core.rtti.RttiAC; 33 import org.objectweb.jac.util.Strings; 34 35 38 39 public class IntegrityAC extends AspectComponent implements IntegrityConf { 40 static final Logger loggerRep = Logger.getLogger("integrity.repository"); 41 42 HashMap preConditionsFields = new HashMap (); 43 HashMap postConditionsFields = new HashMap (); 44 45 RoleWrapper roleWrapper = new RoleWrapper(this); 46 47 public static final String CONSTRAINTS = "IntegrityAC.CONSTRAINTS"; 48 public static final String DISABLE_ROLE_UPDATES = 49 "IntegrityAC.DISABLE_ROLE_UPDATES"; 50 51 public static final String DISABLE_ADD_TO_REPOSITORY = 52 "IntegrityAC.DISABLE_ADD_TO_REPOSITORY"; 53 54 public void declareRepository( 55 String repositoryName, 56 CollectionItem collection, 57 FieldItem field) 58 { 59 RepositoryWrapper rw = 60 new RepositoryWrapper( 61 this, 62 repositoryName, 63 collection, 64 field, 65 RepositoryWrapper.ADDER); 66 if (field instanceof CollectionItem) { 67 pointcut( 68 "ALL", 69 field.getParent().getName(), 70 "ADDERS(" + field.getName() + ")", 71 rw, 72 null); 73 } else { 74 pointcut( 75 "ALL", 76 field.getParent().getName(), 77 "SETTERS(" + field.getName() + ")", 78 rw, 79 null); 80 } 81 82 pointcut(repositoryName,collection.getParent().getName(), 85 "ADDERS("+collection.getName()+")", 86 new AntiDupWrapper(this,collection),null); 87 } 88 89 class AntiDupWrapper extends Wrapper { 90 public AntiDupWrapper(AspectComponent ac, CollectionItem collection) { 91 super(ac); 92 this.collection = collection; 93 } 94 CollectionItem collection; 95 public Object invoke(MethodInvocation invocation) throws Throwable { 96 disableAddToRepository(collection); 97 loggerRep.debug("doAddToRepository " + collection); 98 try { 99 return proceed(invocation); 100 } finally { 101 enableAddToRepository(collection); 102 } 103 } 104 } 105 106 public void declareConstraint( 107 FieldItem relation, 108 FieldItem target, 109 String constraint) 110 { 111 List constraints = (List ) target.getAttributeAlways(CONSTRAINTS); 112 if (constraints == null) { 113 constraints = new Vector (); 114 target.setAttribute(CONSTRAINTS, constraints); 115 } 116 int c; 117 if (constraint.compareToIgnoreCase("DELETE_CASCADE") == 0) { 118 warning("DELETE_CASCADE is not implemented yet"); 119 c = Constraint.DELETE_CASCADE; 120 } else if (constraint.compareToIgnoreCase("SET_NULL") == 0) { 121 c = Constraint.SET_NULL; 122 } else if (constraint.compareToIgnoreCase("FORBIDDEN") == 0) { 123 c = Constraint.FORBIDDEN; 124 } else { 125 throw new RuntimeException ("Unknown constraint type " + constraint); 126 } 127 constraints.add(new Constraint(relation, c)); 128 if (target instanceof CollectionItem) { 129 pointcut( 130 "ALL", 131 target.getParent().getName(), 132 "REMOVERS(" + target.getName() + ")", 133 new RepositoryWrapper( 134 this, 135 "", 136 (CollectionItem) target, 137 null, 138 RepositoryWrapper.REMOVER), 139 null); 140 } else { 141 pointcut( 142 "ALL", 143 target.getParent().getName(), 144 "SETTERS(" + target.getName() + ")", 145 new RepositoryWrapper( 146 this, 147 "", 148 null, 149 target, 150 RepositoryWrapper.REMOVER), 151 null); 152 } 153 } 154 155 boolean hasConditions = false; 156 157 private void addCondition( 158 FieldItem field, 159 MethodItem constraint, 160 Object [] params, 161 String errorMsg, 162 HashMap conditionsList) 163 { 164 hasConditions = true; 165 field.setAttribute(RttiAC.CONSTRAINTS, Boolean.TRUE); 166 167 Vector vect = (Vector ) conditionsList.get(field); 168 if (vect == null) 169 vect = new Vector (); 170 171 ConstraintDescriptor obj = 172 new ConstraintDescriptor(constraint, params, errorMsg); 173 vect.add(obj); 174 conditionsList.put(field, vect); 175 } 176 177 public void addPostCondition( 178 FieldItem field, 179 MethodItem constraint, 180 Object [] params, 181 String errorMsg) 182 { 183 addCondition( 184 field, 185 constraint, 186 params, 187 errorMsg, 188 postConditionsFields); 189 } 190 191 public void addPreCondition( 192 FieldItem field, 193 MethodItem constraint, 194 Object [] params, 195 String errorMsg) 196 { 197 addCondition( 198 field, 199 constraint, 200 params, 201 errorMsg, 202 preConditionsFields); 203 } 204 205 215 protected void doCheck() { 216 ConstraintWrapper cw = new ConstraintWrapper(this); 217 pointcut( 218 "ALL", 219 "ALL", 220 "SETTERS(<" + RttiAC.CONSTRAINTS + ">)", 221 cw, 222 null); 223 } 224 225 233 void doPrimaryKeyCheck() { 234 PrimaryKeyWrapper pw = new PrimaryKeyWrapper(this); 235 pointcut( 236 "ALL", 237 "ALL", 238 "ADDERS(<" + RttiAC.PRIMARY_KEY + ">)", 239 pw, 240 null); 241 } 242 243 boolean updateAssociations = false; 244 public void updateAssociations() { 245 this.updateAssociations = true; 246 } 247 248 public void whenConfigured() { 249 if (!updateAssociations) 250 return; 251 if (hasConditions) { 252 doCheck(); 253 } 254 doPrimaryKeyCheck(); 255 Set classes = ((RttiAC)getAC("rtti")).getClassesWithAssociations(); 256 if (!classes.isEmpty()) { 257 String classExpr = Strings.join(classes, " || "); 258 pointcut( 259 "ALL", 260 classExpr, 261 "ADDERS(<" + RttiAC.OPPOSITE_ROLE + ">) " 262 + "|| REMOVERS(<" + RttiAC.OPPOSITE_ROLE + ">) " 263 + "|| SETTERS(<" + RttiAC.OPPOSITE_ROLE + ">) " 264 + "|| CONSTRUCTORS", 265 roleWrapper, 266 null); 267 } 270 } 271 272 276 public static boolean isAddToRepositoryEnabled(CollectionItem collection) { 277 List disabled = getDisabled(); 278 loggerRep.debug("isAddToRepositoryEnabled "+collection+" (disabled="+disabled+") ?"); 279 return !disabled.contains(collection); 280 } 281 282 286 public static void disableAddToRepository(CollectionItem collection) { 287 Collaboration collab = Collaboration.get(); 288 List disabled = getDisabled(); 289 loggerRep.debug("disableAddToRepository "+collection+ 290 " (disabled="+disabled+")"); 291 disabled.add(collection); 292 } 293 294 298 public static void enableAddToRepository(CollectionItem collection) { 299 loggerRep.debug("enableAddToRepository "+collection); 300 List disabled = getDisabled(); 301 disabled.remove(collection); 302 } 303 304 307 protected static List getDisabled() { 308 List disabled = 309 (List )Collaboration.get().getAttribute( 310 IntegrityAC.DISABLE_ADD_TO_REPOSITORY); 311 if (disabled==null) { 312 disabled = new Vector (); 313 Collaboration.get().addAttribute( 314 IntegrityAC.DISABLE_ADD_TO_REPOSITORY, 315 disabled); 316 } 317 return disabled; 318 } 319 320 public static void disableRoleUpdates() { 321 Collaboration.get().addAttribute(DISABLE_ROLE_UPDATES,Boolean.TRUE); 322 } 323 public static void enableRoleUpdates() { 324 Collaboration.get().removeAttribute(DISABLE_ROLE_UPDATES); 325 } 326 } 327 | Popular Tags |