KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > definition > InterceptorDefinition


1 /*
2  * Nanning Aspects
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package com.tirsen.nanning.definition;
8
9 import java.lang.reflect.Method JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import com.tirsen.nanning.*;
16
17 /**
18  * TODO document InterceptorDefinition
19  *
20  * <!-- $Id: InterceptorDefinition.java,v 1.7 2003/05/22 20:18:32 tirsen Exp $ -->
21  *
22  * @author $Author: tirsen $
23  * @version $Revision: 1.7 $
24  *
25  * @deprecated please use the new {@link com.tirsen.nanning.config.AspectSystem} framework instead.
26  */

27 public class InterceptorDefinition {
28     private final Class JavaDoc interceptorClass;
29     private Interceptor singletonInterceptor;
30     private Map JavaDoc mapAttributes;
31     private Set JavaDoc negativeCache = new HashSet JavaDoc();
32     private Set JavaDoc positiveCache = new HashSet JavaDoc();
33
34     public InterceptorDefinition(Class JavaDoc interceptorClass) {
35         if (!Interceptor.class.isAssignableFrom(interceptorClass)) {
36             throw new IllegalArgumentException JavaDoc(interceptorClass + " is not an interceptor.");
37         }
38         this.interceptorClass = interceptorClass;
39     }
40
41     public Interceptor newInstance() {
42         if (singletonInterceptor != null) {
43             return singletonInterceptor;
44         }
45         try {
46             Interceptor instance = (Interceptor) interceptorClass.newInstance();
47             if (SingletonInterceptor.class.isAssignableFrom(interceptorClass)) {
48                 singletonInterceptor = instance;
49             }
50
51             if (instance instanceof DefinitionAwareInterceptor) {
52                 ((DefinitionAwareInterceptor) instance).setInterceptorDefinition(this);
53             }
54             return instance;
55         } catch (Exception JavaDoc e) {
56             throw new AspectException(e);
57         }
58     }
59
60     public Class JavaDoc getInterceptorClass() {
61         return interceptorClass;
62     }
63
64     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
65         if (mapAttributes == null) mapAttributes = new HashMap JavaDoc();
66         mapAttributes.put(name, value);
67     }
68
69     public Object JavaDoc getAttribute(String JavaDoc name) {
70         if (mapAttributes == null) {
71             return null;
72         } else {
73             return mapAttributes.get(name);
74         }
75     }
76
77     public boolean interceptsMethod(Method JavaDoc method) {
78         if (negativeCache.contains(method)) {
79             return false;
80         }
81         if (positiveCache.contains(method)) {
82             return true;
83         }
84
85         Interceptor interceptor = newInstance();
86         if (interceptor instanceof FilterMethodsInterceptor) {
87             if (((FilterMethodsInterceptor) interceptor).interceptsMethod(method)) {
88                 positiveCache.add(method);
89                 return true;
90             }
91         } else if (interceptor instanceof MethodInterceptor) {
92             positiveCache.add(method);
93             return true;
94         }
95         negativeCache.add(interceptor);
96         return false;
97     }
98
99     public boolean interceptsConstructor(Class JavaDoc interfaceClass) {
100         Interceptor interceptor = newInstance();
101         if (interceptor instanceof ConstructionInterceptor) {
102             return ((ConstructionInterceptor) interceptor).interceptsConstructor(interfaceClass);
103         }
104         return false;
105     }
106
107     public Interceptor getSingleton() {
108         if (singletonInterceptor == null) {
109             newInstance();
110         }
111         if (singletonInterceptor == null) {
112             throw new IllegalStateException JavaDoc("This is not a singleton-interceptor: " + interceptorClass);
113         }
114
115         return singletonInterceptor;
116     }
117 }
118
Popular Tags