KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > interceptor > Aspects


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

25
26 package org.snipsnap.interceptor;
27
28 import org.snipsnap.interceptor.custom.*;
29 import org.snipsnap.snip.BlogImpl;
30 import org.snipsnap.snip.SnipImpl;
31 import org.snipsnap.snip.SnipSpaceImpl;
32
33 import java.lang.reflect.InvocationHandler JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.lang.reflect.Proxy JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38
39 public class Aspects implements InvocationHandler JavaDoc {
40   private Object JavaDoc target = null;
41   private List JavaDoc interceptors;
42
43   /** Neat trick, done with some thinking and help from Jon (Nanning) and
44    * Rickard. Thanks.
45    */

46   static ThreadLocal JavaDoc currentThis = new ThreadLocal JavaDoc();
47
48   public static boolean hasTarget(Object JavaDoc proxy) {
49     return ((Aspects) Proxy.getInvocationHandler(proxy)).hasTarget();
50   }
51
52   private boolean hasTarget() {
53     return null != target;
54   }
55
56   public static Object JavaDoc getThis() {
57     return currentThis.get();
58   }
59
60   public static Object JavaDoc newInstance(Object JavaDoc target) {
61     return newInstance(target, target.getClass().getInterfaces());
62   }
63
64   public static Object JavaDoc newInstance(Object JavaDoc target, Class JavaDoc interfaceTarget) {
65     return newInstance(target, new Class JavaDoc[]{interfaceTarget});
66   }
67
68   public static Object JavaDoc newInstance(Object JavaDoc target, Class JavaDoc[] interfaceTargets) {
69     Class JavaDoc targetClass = target.getClass();
70     Class JavaDoc interfaces[] = interfaceTargets;
71     Aspects aspects = new Aspects(target);
72     if (target.getClass().equals(SnipImpl.class)) {
73       aspects.addInterceptor(new ACLInterceptor());
74     } else if (target.getClass().equals(SnipSpaceImpl.class)) {
75       aspects.addInterceptor(new MissingInterceptor());
76       aspects.addInterceptor(new SnipSpaceACLInterceptor());
77       aspects.addInterceptor(new StoreInterceptor());
78     } else if (target.getClass().equals(BlogImpl.class)) {
79       aspects.addInterceptor(new BlogACLInterceptor());
80     }
81     return Proxy.newProxyInstance(targetClass.getClassLoader(),
82         interfaces, aspects);
83   }
84
85   public Aspects(Object JavaDoc target) {
86     this.target = target;
87     interceptors = new ArrayList JavaDoc();
88   }
89
90   public void addInterceptor(Interceptor interceptor) {
91      interceptors.add(interceptor);
92   }
93
94   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
95 // return method.invoke(target,args);
96
Object JavaDoc invocationResult = null;
97     Object JavaDoc previousThis = currentThis.get();
98     currentThis.set(proxy);
99     Invocation i = new Invocation(target, method, args, interceptors);
100     try {
101       invocationResult = i.next();
102     } catch (Throwable JavaDoc throwable) {
103       throwable.printStackTrace();
104     } finally {
105       currentThis.set(previousThis);
106     }
107
108     return invocationResult;
109   }
110 }
111
Popular Tags