1 17 18 package org.objectweb.jac.aspects.timestamp; 19 20 import java.util.Iterator ; 21 import org.aopalliance.intercept.ConstructorInvocation; 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.apache.log4j.Logger; 24 import org.objectweb.*; 25 import org.objectweb.jac.core.AspectComponent; 26 import org.objectweb.jac.core.Interaction; 27 import org.objectweb.jac.core.NameRepository; 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.FieldItem; 32 import org.objectweb.jac.core.rtti.RttiAC; 33 import org.objectweb.jac.util.ExtBoolean; 34 35 public class TimestampAC extends AspectComponent implements TimestampConf { 36 static final Logger logger = Logger.getLogger("timestamp"); 37 38 public static final String FOLLOW = "TimestampAC.FOLLOW"; 39 40 public void declareTimestampedClasses(String classExpr, 41 String wrappeeExpr, 42 String repositoryName) 43 { 44 pointcut("ALL",classExpr,"MODIFIERS", 45 new Wrapper(this,repositoryName),null); 46 } 47 48 public void followLink(FieldItem link, boolean follow) { 49 logger.info("Setting follow on "+link); 50 link.setAttribute(FOLLOW,ExtBoolean.valueOf(follow)); 51 } 52 53 public static class Wrapper extends org.objectweb.jac.core.Wrapper { 54 public Wrapper(AspectComponent ac, String stampsName) { 55 super(ac); 56 this.stampsName = stampsName; 57 } 58 59 String stampsName; 60 Timestamps stamps; 61 62 void setStamps() { 63 stamps = (Timestamps)NameRepository.get().getObject(stampsName); 64 } 65 66 public Object invoke(MethodInvocation invocation) throws Throwable { 67 Object res = invocation.proceed(); 68 setStamps(); 69 if (stamps!=null) { 70 Interaction interaction = (Interaction)invocation; 71 touch(interaction.wrappee,interaction.getClassItem()); 72 } 73 return res; 74 } 75 76 void touch(Object object, ClassItem cl) { 77 logger.info("touch "+object); 78 stamps.touch(object); 79 if (cl==null) { 80 cl = ClassRepository.get().getClass(object); 81 } 82 FieldItem[] fields = cl.getFields(); 83 for (int i=0; i<fields.length; i++) { 84 FieldItem field= fields[i]; 85 logger.debug("Testing field "+field); 86 boolean followed = false; 87 if (field.isReference()) { 88 FieldItem opposite = (FieldItem)field.getOppositeRole(); 89 if (opposite!=null && opposite.isAggregation()) { 90 logger.debug(" opposite is aggregation"); 91 followed = true; 92 Object touched = fields[i].getThroughAccessor(object); 93 if (touched!=null) 94 touch(touched,null); 95 } 96 } 97 98 if (!followed && field.getBoolean(FOLLOW, false)) { 99 logger.debug(" must be followed"); 100 if (field instanceof CollectionItem) { 101 Iterator it = ((CollectionItem)field).getActualCollectionThroughAccessor(object).iterator(); 102 while (it.hasNext()) { 103 Object touched = it.next(); 104 if (touched!=null) 105 touch(touched,null); 106 else 107 logger.debug(" Not touching null value in "+field.getLongName()); 108 } 109 } else { 110 Object touched = field.getThroughAccessor(object); 111 if (touched!=null) 112 touch(touched,null); 113 else 114 logger.info(" Not touching null value "+field.getLongName()); 115 } 116 } 117 } 118 } 119 120 public Object construct(ConstructorInvocation invocation) throws Throwable { 121 return invocation.proceed(); 122 } 123 } 124 } 125 | Popular Tags |