1 package org.snipsnap.interceptor.custom; 2 3 27 28 import org.codehaus.nanning.AspectInstance; 29 import org.codehaus.nanning.Invocation; 30 import org.codehaus.nanning.MethodInterceptor; 31 import org.codehaus.nanning.config.Aspect; 32 import org.codehaus.nanning.config.P; 33 import org.codehaus.nanning.config.Pointcut; 34 import org.snipsnap.snip.SnipSpace; 35 import org.snipsnap.snip.Snip; 36 import org.snipsnap.util.ApplicationAwareMap; 37 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 51 52 public class MissingSnipAspect implements Aspect { 53 Pointcut existsPc = P.methodName("exists.*"); 54 Pointcut createPc = P.methodName("create.*"); 55 Pointcut removePc = P.methodName("remove.*"); 56 57 private ApplicationAwareMap missing; 58 private ApplicationAwareMap existing; 59 60 public MissingSnipAspect() { 61 this.missing = new ApplicationAwareMap(HashMap .class, HashMap .class); 62 this.existing = new ApplicationAwareMap(HashMap .class, HashMap .class); 63 } 64 65 public void advise(AspectInstance instance) { 66 Class klass = instance.getClassIdentifier(); 67 if (klass != null && klass.equals(SnipSpace.class)) { 70 existsPc.advise(instance, new MethodInterceptor() { 71 public Object invoke(Invocation invocation) throws Throwable { 72 String name = ((String ) 73 invocation.getArgs()[0]).toUpperCase(); 74 if (missing.getMap().containsKey(name)) { 76 return new Boolean (false); 78 } else if (existing.getMap().containsKey(name)) { 79 return new Boolean (true); 80 } 81 82 Boolean result = (Boolean ) 84 invocation.invokeNext(); 85 if (result.equals(Boolean.FALSE)) { 88 missing.getMap().put(name, new Integer (0)); 89 } else { 90 existing.getMap().put(name, new Integer (0)); 91 } 92 return result; 93 } 94 }); 95 96 97 removePc.advise(instance, new MethodInterceptor() { 98 public Object invoke(Invocation invocation) throws Throwable { 99 Snip snip = (Snip) invocation.getArgs()[0]; 100 String name = snip.getName().toUpperCase(); 101 102 Object result = invocation.invokeNext(); 103 104 if (existing.getMap().containsKey(name)) { 105 existing.getMap().remove(name); 106 } 107 return result; 108 } 109 }); 110 111 createPc.advise(instance, new MethodInterceptor() { 112 public Object invoke(Invocation invocation) throws Throwable { 113 String name = ((String ) invocation.getArgs()[0]).toUpperCase(); 114 115 Object result = invocation.invokeNext(); 116 117 if (missing.getMap().containsKey(name)) { 118 missing.getMap().remove(name); 119 } 120 return result; 121 } 122 }); 123 } 124 } 125 126 public void introduce(AspectInstance instance) { 127 } 128 } 129 | Popular Tags |