1 17 18 package org.objectweb.jac.aspects.integrity; 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import org.aopalliance.intercept.ConstructorInvocation; 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.Interaction; 29 import org.objectweb.jac.core.NameRepository; 30 import org.objectweb.jac.core.ObjectRepository; 31 import org.objectweb.jac.core.Wrapper; 32 import org.objectweb.jac.core.rtti.ClassItem; 33 import org.objectweb.jac.core.rtti.CollectionItem; 34 import org.objectweb.jac.core.rtti.FieldItem; 35 36 39 40 public class RepositoryWrapper extends Wrapper { 41 static final Logger logger = Logger.getLogger("integrity.repository"); 42 43 public static final int ADDER = 0; 44 public static final int REMOVER = 1; 45 46 Object repository; 47 String repositoryName; 48 CollectionItem collection; 49 FieldItem field; 50 int type = ADDER; 51 52 public RepositoryWrapper( 53 AspectComponent ac, 54 String repositoryName, 55 CollectionItem collection, 56 FieldItem field, 57 int type) 58 { 59 super(ac); 60 this.repositoryName = repositoryName; 61 this.collection = collection; 62 this.field = field; 63 this.type=type; 64 } 65 66 69 public Object addToRepository(Interaction interaction) { 70 Object ret = doAddToRepository(interaction); 71 if (IntegrityAC.isAddToRepositoryEnabled(collection)) { 72 logger.debug("checking for repository on " + interaction.method); 73 if (repository == null) 74 repository = NameRepository.get().getObject(repositoryName); 75 Object toAdd = interaction.args[0]; 76 77 if (toAdd != null 78 && repository != null 79 && !collection.getActualCollection(repository).contains(toAdd)) { 80 logger.debug("addToRepository(" + repositoryName + ")"); 81 collection.addThroughAdder(repository, toAdd); 82 } 83 } else { 84 logger.debug(collection + " disabled"); 85 } 86 return ret; 87 } 88 89 92 public Object doAddToRepository(Interaction interaction) { 93 IntegrityAC.disableAddToRepository(collection); 94 logger.debug("doAddToRepository " + collection); 95 try { 96 return proceed(interaction); 97 } finally { 98 IntegrityAC.enableAddToRepository(collection); 99 } 100 } 101 102 public Object removeFromRepository(Interaction interaction) { 103 logger.debug("removeFromRepository(" + repositoryName + ")"); 104 Object toRemove = interaction.args[0]; 105 FieldItem target = collection != null ? collection : field; 106 List constraints = (List ) target.getAttribute(IntegrityAC.CONSTRAINTS); 107 if (constraints == null) 108 return proceed(interaction); 109 Iterator it = constraints.iterator(); 110 while (it.hasNext()) { 111 Constraint constraint = (Constraint) it.next(); 112 Collection objects = 113 ObjectRepository.getObjectsWhere( 114 constraint.relation.getClassItem(), 115 constraint.relation, 116 toRemove); 117 logger.debug(" " + constraint + " => " + objects); 118 switch (constraint.constraint) { 119 case Constraint.DELETE_CASCADE : 120 break; 121 case Constraint.SET_NULL : 122 Iterator it2 = objects.iterator(); 123 while (it2.hasNext()) { 124 Object substance = it2.next(); 125 logger.debug( 126 " SET_NULL " 127 + substance + "." + constraint.relation); 128 try { 129 if (constraint.relation 130 instanceof CollectionItem) { 131 ((CollectionItem) constraint.relation) 132 .removeThroughRemover( 133 substance, 134 toRemove); 135 } else { 136 constraint.relation.setThroughWriter( 137 substance, 138 null); 139 } 140 } catch (Exception e) { 141 logger.error( 142 "Failed to enforce SET_NULL constraint for " 143 + constraint.relation + " on " + substance,e); 144 } 145 } 146 break; 147 case Constraint.FORBIDDEN : 148 if (objects.size() > 0) 149 throw new RuntimeException ( 150 "Cannot delete " 151 + toRemove + " from " + target.getLongName() 152 + " because of constraint " + constraint); 153 break; 154 default : 155 logger.warn( 156 "Unknown constraint type " + constraint.constraint); 157 } 158 } 159 160 return proceed(interaction); 161 } 162 163 public Object invoke(MethodInvocation invocation) throws Throwable { 164 Interaction interaction = (Interaction) invocation; 165 switch (type) { 166 case ADDER: 167 return addToRepository(interaction); 168 case REMOVER: 169 return removeFromRepository(interaction); 170 default: 171 throw new Exception ("Unknown RepositoryWrapper type: "+type); 172 } 173 } 174 } 175 176 | Popular Tags |