1 17 18 package org.objectweb.jac.aspects.idGen; 19 20 import org.aopalliance.intercept.ConstructorInvocation; 21 import org.aopalliance.intercept.MethodInvocation; 22 import org.apache.log4j.Logger; 23 import org.objectweb.jac.core.AspectComponent; 24 import org.objectweb.jac.core.Collaboration; 25 import org.objectweb.jac.core.Interaction; 26 import org.objectweb.jac.core.NameRepository; 27 import org.objectweb.jac.core.Wrapper; 28 import org.objectweb.jac.core.rtti.ClassItem; 29 import org.objectweb.jac.core.rtti.FieldItem; 30 import org.objectweb.jac.util.Log; 31 32 public class IdGenAC extends AspectComponent { 33 static Logger logger = Logger.getLogger("idgen"); 34 35 public static final String COUNTER = "IdGenAC.COUNTER"; 36 public static final String ID_FIELD = "IdGenAC.ID_FIELD"; 37 38 public void genId(ClassItem cl, String counter, String fieldName) { 39 cl.setAttribute(COUNTER, counter); 40 cl.setAttribute(ID_FIELD, cl.getField(fieldName)); 41 pointcut( 42 "ALL", 43 cl.getName(), 44 "CONSTRUCTORS", 45 IdGenWrapper.class.getName(), 46 null, 47 false); 48 } 49 50 Counters counters; 51 protected Counters getCounters() { 52 if (counters == null) { 53 if (countersName != null) { 54 counters = 55 (Counters) NameRepository.get().getObject(countersName); 56 if (counters == null) { 57 logger.error("IdGenAC: No object named " + countersName); 58 } 59 } 60 } 61 return counters; 62 } 63 64 String countersName = "counters#0"; 65 public void setCountersName(String countersName) { 66 this.countersName = countersName; 67 } 68 69 public class IdGenWrapper extends Wrapper { 70 public IdGenWrapper(AspectComponent ac) { 71 super(ac); 72 } 73 74 public Object genId(Interaction interaction) { 75 Object result = proceed(interaction); 76 logger.debug("generating id for " + interaction.wrappee); 77 ClassItem cl = interaction.getClassItem(); 78 FieldItem field = (FieldItem) cl.getAttribute(ID_FIELD); 79 if (Collaboration.get().getAttribute("PersistenceAC.RESTORE") 80 == null) { 81 String counterName = (String ) cl.getAttribute(COUNTER); 82 Counters counters = getCounters(); 83 if (counters != null) { 84 long id = counters.genId(counterName); 85 logger.debug(" -> " + id); 86 try { 87 if (field.getType() == String .class) 88 field.setThroughWriter( 89 interaction.wrappee, 90 Long.toString(id)); 91 else 92 field.setThroughWriter( 93 interaction.wrappee, 94 new Long (id)); 95 } catch (IllegalAccessException e) { 96 logger.error( 97 "Failed to to set field " + field 98 + " for " + interaction.wrappee); 99 } 100 } else { 101 logger.debug(" No counters object"); 102 } 103 } else { 104 logger.debug(" skipping id generation for " + interaction.wrappee); 105 } 106 return result; 107 } 108 109 public Object invoke(MethodInvocation invocation) throws Throwable { 110 throw new Exception ("This wrapper does not support invocation interception."); 111 } 112 113 public Object construct(ConstructorInvocation invocation) 114 throws Throwable { 115 return genId((Interaction) invocation); 116 } 117 } 118 } 119 | Popular Tags |