KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

16 package com.jdon.aop.interceptor;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.aopalliance.intercept.MethodInterceptor;
24 import org.aopalliance.intercept.MethodInvocation;
25
26 import com.jdon.aop.reflection.ProxyMethodInvocation;
27 import com.jdon.bussinessproxy.TargetMetaDef;
28 import com.jdon.bussinessproxy.target.TargetServiceFactory;
29 import com.jdon.container.ContainerWrapper;
30 import com.jdon.container.finder.ComponentKeys;
31 import com.jdon.container.finder.ContainerCallback;
32 import com.jdon.controller.cache.InstanceCache;
33 import com.jdon.controller.pool.CommonsPoolFactory;
34 import com.jdon.controller.pool.Pool;
35 import com.jdon.controller.pool.PoolConfigure;
36 import com.jdon.controller.pool.Poolable;
37 import com.jdon.util.Debug;
38
39 /**
40  * PoolInterceptor must be the last in Interceptors.
41  * this class is active for the pojoServices that
42  * implements com.jdon.controller.pool.Poolable.
43  *
44  * @author <a HREF="mailto:banqiao@jdon.com">banq </a>
45  *
46  */

47 public class PoolInterceptor implements MethodInterceptor {
48     private final static String JavaDoc module = PoolInterceptor.class.getName();
49
50     /**
51      * one target object, one pool
52      */

53     private Map JavaDoc poolFactorys;
54
55     private TargetServiceFactory targetServiceFactory;
56
57     private ContainerCallback containerCallback;
58
59     private PoolConfigure poolConfigure;
60
61     private List JavaDoc isPoolableCache = new ArrayList JavaDoc();
62     
63     private List JavaDoc unPoolableCache = new ArrayList JavaDoc();
64
65     /**
66      * @param containerCallback
67      */

68     public PoolInterceptor(TargetServiceFactory targetServiceFactory,
69             ContainerCallback containerCallback, PoolConfigure poolConfigure) {
70         super();
71         this.targetServiceFactory = targetServiceFactory;
72         this.containerCallback = containerCallback;
73         this.poolConfigure = poolConfigure;
74         this.poolFactorys = new HashMap JavaDoc();
75     }
76
77     /*
78      * (non-Javadoc)
79      *
80      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
81      */

82     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
83         ProxyMethodInvocation proxyMethodInvocation = (ProxyMethodInvocation) invocation;
84         TargetMetaDef targetMetaDef = proxyMethodInvocation.getTargetMetaDef();
85         if (targetMetaDef.isEJB())
86             return invocation.proceed();
87
88         if (!isPoolabe(targetMetaDef)) {
89             //Debug.logVerbose("[JdonFramework] target service is not Poolable: "
90
// + targetMetaDef.getClassName() + " pool unactiive", module);
91
return invocation.proceed(); //下一个interceptor
92
}
93         Debug.logVerbose("[JdonFramework] enter PoolInterceptor", module);
94         CommonsPoolFactory commonsPoolFactory = (CommonsPoolFactory)poolFactorys.get(targetMetaDef.getCacheKey());
95         if (commonsPoolFactory == null) {
96             commonsPoolFactory = getCommonsPoolFactory(targetMetaDef);
97             poolFactorys.put(targetMetaDef.getCacheKey(), commonsPoolFactory);
98         }
99         
100         Pool pool = commonsPoolFactory.getPool();
101         Object JavaDoc poa = null;
102         Object JavaDoc result = null;
103         try {
104             poa = pool.acquirePoolable();
105             Debug.logVerbose("[JdonFramework] borrow a object:" + targetMetaDef.getClassName()
106                     + " id:" + poa.hashCode() + " from pool", module);
107             Debug.logVerbose("[JdonFramework]pool state: active=" + pool.getNumActive() + " free=" + pool.getNumIdle(), module);
108                         
109             //set the object that borrowed from pool to MethodInvocation
110
//so later other Interceptors or MethodInvocation can use it!
111
proxyMethodInvocation.setThis(poa);
112             result = invocation.proceed();
113         } catch (Exception JavaDoc ex) {
114             Debug.logError(ex, module);
115         } finally {
116             if (poa != null) {
117                 pool.releasePoolable(poa);
118                 Debug.logVerbose("[JdonFramework] realease a object:" + targetMetaDef.getClassName()
119                         + " to pool", module);
120             }
121         }
122         return result;
123     }
124
125     /**
126      * every target service has its CommonsPoolFactory, we cache it in
127      * container, next time, we can get the cached Object Pool for the target
128      * service;
129      *
130      * @param targetMetaDef
131      * @return
132      */

133     private CommonsPoolFactory getCommonsPoolFactory(TargetMetaDef targetMetaDef) {
134         CommonsPoolFactory commonsPoolFactory = null;
135         try {
136             ContainerWrapper containerWrapper = containerCallback.getContainerWrapper();
137             InstanceCache instanceCache = (InstanceCache)containerWrapper.lookup(ComponentKeys.INSTANCE_CACHE);
138             
139             String JavaDoc key = targetMetaDef.getCacheKey() + " CommonsPoolFactory";
140
141             commonsPoolFactory = (CommonsPoolFactory) instanceCache.get(key);
142             if (commonsPoolFactory == null) {
143                 Debug.logVerbose("[JdonFramework] first time call commonsPoolFactory, create it:"
144                                         + key, module);
145                 commonsPoolFactory = new CommonsPoolFactory(
146                         targetServiceFactory, targetMetaDef, poolConfigure
147                                 .getMaxPoolSize());
148                 instanceCache.put(key, commonsPoolFactory);
149             }
150
151         } catch (Exception JavaDoc ex) {
152             Debug.logError(ex, module);
153         }
154         return commonsPoolFactory;
155     }
156
157     public boolean isPoolabe(TargetMetaDef targetMetaDef) {
158         boolean found = false;
159         if (isPoolableCache.contains(targetMetaDef.getName())) {
160             found = true;
161         }else if(!unPoolableCache.contains(targetMetaDef.getName())){
162             Debug.logVerbose("[JdonFramework] check if it is a Poolable", module);
163             ContainerWrapper containerWrapper = containerCallback.getContainerWrapper();
164             Class JavaDoc thisCLass = containerWrapper.getComponentClass(targetMetaDef.getName());
165             if (Poolable.class.isAssignableFrom(thisCLass)) {
166                 found = true;
167                 isPoolableCache.add(targetMetaDef.getName());
168             }else{
169                 unPoolableCache.add(targetMetaDef.getName());
170             }
171         }
172         return found;
173     }
174
175 }
176
Popular Tags