KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > interceptor > custom > MissingSnipAspect


1 package org.snipsnap.interceptor.custom;
2
3 /*
4  * This file is part of "SnipSnap Wiki/Weblog".
5  *
6  * Copyright (c) 2002-2003 Stephan J. Schmidt, Matthias L. Jugel
7  * All Rights Reserved.
8  *
9  * Please visit http://snipsnap.org/ for updates and contact.
10  *
11  * --LICENSE NOTICE--
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  * --LICENSE NOTICE--
26  */

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 JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * MissingLSnip Aspect caches method call result
43  * from SnipSpace (exists, create).
44  * When a missing snip is detected the
45  * snip name is stored. When the snip is created
46  * the snip is removed from the missing list.
47  *
48  * @author stephan
49  * @version $Id: MissingSnipAspect.java 1256 2003-12-11 13:24:57Z leo $
50  */

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 JavaDoc.class, HashMap JavaDoc.class);
62     this.existing = new ApplicationAwareMap(HashMap JavaDoc.class, HashMap JavaDoc.class);
63   }
64
65   public void advise(AspectInstance instance) {
66     Class JavaDoc klass = instance.getClassIdentifier();
67 // System.out.println("class="+klass);
68
// System.out.println("instance="+instance);
69
if (klass != null && klass.equals(SnipSpace.class)) {
70       existsPc.advise(instance, new MethodInterceptor() {
71         public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
72           String JavaDoc name = ((String JavaDoc)
73               invocation.getArgs()[0]).toUpperCase();
74           // Snip is in the missing list
75
if (missing.getMap().containsKey(name)) {
76             //System.out.println("Hit=" + name);
77
return new Boolean JavaDoc(false);
78           } else if (existing.getMap().containsKey(name)) {
79             return new Boolean JavaDoc(true);
80           }
81
82           //System.out.println("Miss=" + name);
83
Boolean JavaDoc result = (Boolean JavaDoc)
84               invocation.invokeNext();
85           // System.out.println("Result=" + name + " exists?=" + result);
86
// The snip does not exist so put it in the missing list
87
if (result.equals(Boolean.FALSE)) {
88             missing.getMap().put(name, new Integer JavaDoc(0));
89           } else {
90             existing.getMap().put(name, new Integer JavaDoc(0));
91           }
92           return result;
93         }
94       });
95
96
97       removePc.advise(instance, new MethodInterceptor() {
98         public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
99           Snip snip = (Snip) invocation.getArgs()[0];
100           String JavaDoc name = snip.getName().toUpperCase();
101
102           Object JavaDoc 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 JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
113           String JavaDoc name = ((String JavaDoc) invocation.getArgs()[0]).toUpperCase();
114
115           Object JavaDoc 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