KickJava   Java API By Example, From Geeks To Geeks.

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


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.regex.Matcher JavaDoc;
11 import java.util.regex.Pattern JavaDoc;
12
13 import com.tirsen.nanning.attribute.Attributes;
14 import com.tirsen.nanning.FilterMethodsInterceptor;
15
16 /**
17  * basic interceptor with methodNameFilter support.
18  * TODO document BasicInterceptor
19  *
20  * <!-- $Id: BasicInterceptor.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 abstract class BasicInterceptor implements DefinitionAwareInterceptor, FilterMethodsInterceptor {
28     public static String JavaDoc METHOD_NAME_FILTER_ATTRIBUTE_NAME = "methodNameFilter";
29
30     private InterceptorDefinition interceptorDefinition = null;
31
32     /**
33      * This method reads the methodNameFilter attribute from interceptor definition
34      * compiles it as a pattern and matches with the passed method's name.
35      * If the pattern matches it returns true.
36      * If there is not methodNameFilter attribute specified then returns true.
37      */

38     public boolean interceptsMethod(Method JavaDoc method) {
39         InterceptorDefinition interceptorDefinition = getInterceptorDefinition();
40         String JavaDoc methodNameFilterPattern = (String JavaDoc) interceptorDefinition.getAttribute(METHOD_NAME_FILTER_ATTRIBUTE_NAME);
41
42         if (methodNameFilterPattern != null) {
43             methodNameFilterPattern = (String JavaDoc) interceptorDefinition.getAttribute(METHOD_NAME_FILTER_ATTRIBUTE_NAME);
44         } else {
45             // try the runtime-attributes
46
try {
47                 methodNameFilterPattern = Attributes.getAttribute(this.getClass(), METHOD_NAME_FILTER_ATTRIBUTE_NAME);
48                 // qdox returns [.*] as [. ] so replace [. ] with [.*]; qdox should fix this or we have got to come up with a better work-around
49
methodNameFilterPattern = Pattern.compile(". ").matcher(methodNameFilterPattern).replaceAll(".*");
50
51             } catch (Exception JavaDoc e) {
52                 // bad stuff happend so let it intercept
53
return true;
54             }
55
56         }
57         if (methodNameFilterPattern != null) {
58             Matcher JavaDoc m = Pattern.compile(methodNameFilterPattern).matcher(method.getName());
59             return m.matches();
60         }
61         // no attribute specified return true.
62
return true;
63     }
64
65     public InterceptorDefinition getInterceptorDefinition() {
66         return interceptorDefinition;
67     }
68
69     public void setInterceptorDefinition(InterceptorDefinition interceptorDefinition) {
70         this.interceptorDefinition = interceptorDefinition;
71     }
72 }
73
Popular Tags