1 16 17 package org.springframework.aop.framework.autoproxy.metadata; 18 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.springframework.aop.framework.AopConfigException; 26 import org.springframework.aop.support.DelegatingIntroductionInterceptor; 27 import org.springframework.aop.support.DefaultIntroductionAdvisor; 28 import org.springframework.beans.factory.InitializingBean; 29 import org.springframework.metadata.Attributes; 30 31 35 public class ModifiableAdvisor extends DefaultIntroductionAdvisor implements InitializingBean { 36 37 protected final Log log = LogFactory.getLog(getClass()); 38 39 private Attributes attributes; 40 41 public ModifiableAdvisor() { 42 super(new ModifiableIntroductionInterceptor(), Modifiable.class); 43 } 44 45 46 public void setAttributes(Attributes atts) { 47 this.attributes = atts; 48 } 49 50 private boolean matches(Collection c) { 51 for (Iterator itr = c.iterator(); itr.hasNext(); ) { 53 Object next = itr.next(); 54 if (next instanceof ModifiableAttribute) { 55 log.info("FOUND modifiable attribute " + next); 56 return true; 57 } 58 } 59 return false; 60 } 61 62 65 public int getOrder() { 66 throw new UnsupportedOperationException (); 67 } 68 69 70 73 public boolean matches(Class targetClass) { 74 log.info("Checking for mod attribute on " + targetClass + " attributes=" + attributes); 75 Collection atts = this.attributes.getAttributes(targetClass); 76 return matches(atts); 77 } 78 79 80 81 private static class ModifiableIntroductionInterceptor 82 extends DelegatingIntroductionInterceptor 83 implements Modifiable { 84 85 private boolean dirty = false; 86 89 public Object invoke(MethodInvocation mi) throws Throwable { 90 if (!isMethodOnIntroducedInterface(mi)) { 91 if (!mi.getMethod().getName().startsWith("get")) { 92 this.dirty = true; 93 } 94 } 95 return super.invoke(mi); 96 } 97 98 101 public boolean isModified() { 102 logger.info("isModified"); 103 return this.dirty; 104 } 105 106 public void acceptChanges() { 107 logger.info("Accepting changes"); 108 this.dirty = false; 109 } 110 } 111 112 115 public void afterPropertiesSet() throws Exception { 116 if (this.attributes == null) 117 throw new AopConfigException("Must set Attributes property on ModifiableAdvice"); 118 } 119 } 120 | Popular Tags |