01: package com.bostechcorp.cbesb.runtime.transformer.engine;
02:
03: //import java.util.HashMap;
04:
05: import org.apache.commons.pool.KeyedObjectPool;
06: import org.apache.commons.pool.KeyedPoolableObjectFactory;
07: import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory;
08:
09: import com.bostechcorp.cbesb.common.runtime.CbesbException;
10: import com.bostechcorp.cbesb.common.runtime.ConfigurationException;
11:
12: public class TransformerRuntimeFactory {
13: // private HashMap<String, TransformerRuntime> transformerCache = new HashMap<String, TransformerRuntime>();
14: private static final int MAX_IDLE_CACHE_SIZE = 50;
15: private KeyedObjectPool cache;
16:
17: public TransformerRuntimeFactory() {
18: StackKeyedObjectPoolFactory poolFactory = new StackKeyedObjectPoolFactory(
19: MAX_IDLE_CACHE_SIZE);
20: cache = poolFactory.createPool();
21: cache.setFactory(new PooledTransformerFactory());
22: }
23:
24: public TransformerRuntime newTransformerRuntime(String saName,
25: String translateControlClassName) throws CbesbException {
26: try {
27: String trnKey = saName + "::" + translateControlClassName;
28: //
29: // TransformerRuntime result = transformerCache.get(trnKey);
30: // if (result == null) {
31: // result = new TransformerRuntime(saName, translateControlClassName);
32: // transformerCache.put(trnKey, result);
33: // }
34: TransformerRuntime result = (TransformerRuntime) cache
35: .borrowObject(trnKey);
36: return result;
37: } catch (Exception e) {
38: throw new ConfigurationException(
39: "Unable to get TransformerRuntime instance - "
40: + e.getMessage(),
41: "Make sure the ESB project is built.", e);
42: }
43: }
44:
45: public void releaseTransformerRuntime(String saName,
46: String translateControlClassName, TransformerRuntime runtime)
47: throws CbesbException {
48: try {
49: String trnKey = saName + "::" + translateControlClassName;
50: cache.returnObject(trnKey, runtime);
51: } catch (Exception e) {
52: throw new CbesbException(
53: "Unable to release TransformerRuntime instance back to pool.",
54: "Please report this as a bug.", e);
55: }
56: }
57:
58: private class PooledTransformerFactory implements
59: KeyedPoolableObjectFactory {
60:
61: public void activateObject(Object key, Object obj)
62: throws Exception {
63:
64: }
65:
66: public void destroyObject(Object key, Object obj)
67: throws Exception {
68:
69: }
70:
71: public Object makeObject(Object key) throws Exception {
72: String keyStr = (String) key;
73: String[] keyArr = keyStr.split("::");
74: TransformerRuntime obj = new TransformerRuntime(keyArr[0],
75: keyArr[1]);
76: ;
77: return obj;
78: }
79:
80: public void passivateObject(Object key, Object obj)
81: throws Exception {
82:
83: }
84:
85: public boolean validateObject(Object key, Object obj) {
86:
87: return true;
88: }
89:
90: }
91: }
|