KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > controller > pool > CommonsPoolFactory


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.controller.pool;
17 import org.apache.commons.pool.PoolableObjectFactory;
18 import org.apache.commons.pool.impl.GenericObjectPool;
19
20 import com.jdon.bussinessproxy.TargetMetaDef;
21 import com.jdon.bussinessproxy.target.TargetServiceFactory;
22 import com.jdon.util.Debug;
23
24 public class CommonsPoolFactory implements PoolableObjectFactory {
25     private final static String JavaDoc module = CommonsPoolFactory.class.getName();
26     
27     private TargetServiceFactory targetServiceFactory;
28     private TargetMetaDef targetMetaDef;
29     private CommonsPoolAdapter pool;
30     
31     /**
32      * @param targetServiceFactory
33      */

34     public CommonsPoolFactory(TargetServiceFactory targetServiceFactory,
35                               TargetMetaDef targetMetaDef,
36                               String JavaDoc maxSize) {
37         super();
38         this.targetServiceFactory = targetServiceFactory;
39         this.targetMetaDef = targetMetaDef;
40         
41             GenericObjectPool apachePool = new GenericObjectPool(this);
42             pool = new CommonsPoolAdapter(apachePool);
43             if (maxSize == null){
44                 Debug.logError("[JdonFramework] not set pool's max size", module);
45             }else{
46                 int maxInt = Integer.parseInt(maxSize);
47                 pool.setMaxPoolSize(maxInt);
48             }
49     }
50     
51     
52     /**
53      * @return Returns the pool.
54      */

55     public CommonsPoolAdapter getPool() {
56         return pool;
57     }
58             
59
60     public Object JavaDoc makeObject() {
61         Object JavaDoc o = null;
62         try {
63             o = targetServiceFactory.create(targetMetaDef);
64         } catch (Exception JavaDoc ex) {
65             Debug.logError("[JdonFramework] Pool can not make object, error: " + ex , module);
66         }
67         return o;
68     }
69
70     public void destroyObject(Object JavaDoc o) throws Exception JavaDoc {
71         targetServiceFactory.destroy(targetMetaDef);
72     }
73
74     public void activateObject(Object JavaDoc o) throws Exception JavaDoc {
75     }
76
77     public void passivateObject(Object JavaDoc o) throws Exception JavaDoc {
78     }
79
80     public boolean validateObject(Object JavaDoc o) {
81         return true;
82     }
83
84 }
85
Popular Tags