KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > aop > interceptor > CacheInterceptor


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7  http://www.apache.org/licenses/LICENSE-2.0
8
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package com.jdon.aop.interceptor;
16
17 import java.lang.reflect.*;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.aopalliance.intercept.*;
22 import com.jdon.controller.model.*;
23 import com.jdon.model.*;
24 import com.jdon.util.*;
25
26 /**
27  *
28  * Cache Interceptor all Interceptors are added in picoContainer
29  *
30  * method match can be done by this class, CacheInterceptor only interceptor the
31  * method getXXXXX.
32  *
33  * @see AopInterceptorRegistry.java
34  *
35  * <p>
36  * </p>
37  * @author banq
38  */

39 public class CacheInterceptor implements MethodInterceptor {
40     private final static String JavaDoc module = CacheInterceptor.class.getName();
41
42     private ModelManager modelManager;
43
44     public String JavaDoc match_MethodName = "get";
45
46     private List JavaDoc isModelCache = new ArrayList JavaDoc();
47
48     public CacheInterceptor(ModelManager modelManager) {
49         this.modelManager = modelManager;
50     }
51
52     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
53         Method method = invocation.getMethod();
54       
55         if (!methodMatchsModelGET(method)) {
56             //Debug.logVerbose("[JdonFramework] cacheInteceptor don't action, enter next invocation.proceed()",
57
// module);
58
return invocation.proceed(); //下一个interceptor
59
}
60         Debug.logVerbose("[JdonFramework] enter cacheInteceptor method:" + method.getName(),
61                 module);
62         try {
63             String JavaDoc dataKey = getArguments(invocation);
64             if (dataKey == null) return invocation.proceed();
65             Class JavaDoc modelClass = method.getReturnType();
66             ModelKey modelKey = new ModelKey(dataKey, modelClass);
67             ModelIF model = modelManager.getCache(modelKey);
68             if (model == null){
69                 model = (ModelIF) invocation.proceed(); //下一个interceptor
70
Debug.logVerbose("[JdonFramework] save to cache", module);
71                 modelManager.addCache(modelKey, model);
72             }
73             return model;
74         } catch (Exception JavaDoc e) {
75             Debug.logError("[JdonFramework]CacheInterceptor Exception error:" + e, module);
76         }
77         return invocation.proceed();
78     }
79            
80
81     /**
82      * 1.check return type if is Model 2.check method name if include "get" 3.
83      * if found them, cache this method
84      *
85      *
86      * @param method
87      * Method
88      * @return boolean
89      */

90     private boolean methodMatchsModelGET(Method method) {
91         boolean condition = false;
92         try {
93             if (isModelCache.contains(method)) {
94                 condition = true;
95                 return condition;
96             }
97
98             String JavaDoc mehtodName = method.getName();
99             if (method.getReturnType() == null)
100                 return condition; //无返回值,ä¸?å?šç¼“å­˜
101
Class JavaDoc returnClass = method.getReturnType();
102             if (returnClass.getSuperclass() == null)
103                 return condition; //无返回值,ä¸?å?šç¼“å­˜
104

105             Debug.logVerbose("[JdonFramework]methodMatchsModelGET: returnClassName = "
106                     + returnClass.getName(), module);
107             if (ModelIF.class.isAssignableFrom(returnClass)) {
108                 if (mehtodName.indexOf(match_MethodName) != -1) {
109                     condition = true;
110                     //method name include "getXXXX" and return Type is subClass
111
// of Model
112
isModelCache.add(method);
113                 }
114             }
115         } catch (Exception JavaDoc ex) {
116             Debug.logError("[JdonFramework]Exception error:" + ex, module);
117         } catch (Throwable JavaDoc the) {
118             Debug.logError("[JdonFramework]Throwable error:" + the, module);
119         }
120         return condition;
121
122     }
123
124     /**
125      * 组å?ˆå?‚数数值为一个字符串 这些å?‚数必须实现toString();
126      *
127      * @param invocation
128      * MethodInvocation
129      * @return String
130      */

131      public String JavaDoc getArguments(MethodInvocation invocation) {
132           
133           try {
134               Object JavaDoc[] args = invocation.getArguments();
135               if (args == null) return null;
136               if (args.length > 1) return null;//å?‚数是一个主键
137

138               return args[0].toString();
139           } catch (Exception JavaDoc ex) {
140               Debug.logError("[JdonFramework] method:"+ invocation.getMethod().getName()
141                              +" args is null or has not implements method: toString() ", module);
142               return null;
143           }
144         
145       }
146   
147
148     public String JavaDoc getMatch_MethodName() {
149         return match_MethodName;
150     }
151
152     public void setMatch_MethodName(String JavaDoc match_MethodName) {
153         this.match_MethodName = match_MethodName;
154     }
155
156 }
157
Popular Tags