1 6 7 package org.jfox.pool; 8 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 import org.jfox.ioc.ComponentContext; 16 import org.jfox.ioc.ext.ActiveComponent; 17 18 21 22 public class PoolManager implements ActiveComponent{ 23 24 private static Map pools = new HashMap (); 25 private static Map indexablePools = new HashMap (); 26 27 33 public ObjectPool obtainPool(ObjectFactory factory) { 34 synchronized(pools) { 35 if(existsPool(factory.getClass())) { 36 return (ObjectPool) pools.get(factory.getClass()); 37 } 38 else { 39 ObjectPool pool = new SimpleObjectPool(factory); 40 synchronized(pools) { 41 pools.put(factory.getClass(), pool); 42 } 43 return pool; 44 } 45 } 46 } 47 48 public boolean existsPool(Class factoryClass) { 49 synchronized(pools) { 50 return pools.containsKey(factoryClass); 51 } 52 } 53 54 60 public IndexableObjectPool obtainIndexablePool(ObjectFactory factory) { 61 synchronized(indexablePools) { 62 if(existsPool(factory.getClass())) { 63 return (IndexableObjectPool) indexablePools.get(factory.getClass()); 64 } 65 else { 66 IndexableObjectPool pool = new SimpleIndexableObjectPool(factory); 67 synchronized(indexablePools) { 68 indexablePools.put(factory.getClass(), pool); 69 } 70 return pool; 71 } 72 } 73 } 74 75 public boolean existsIndexablePool(Class factoryClass) { 76 synchronized(indexablePools) { 77 return indexablePools.containsKey(factoryClass); 78 } 79 } 80 81 public synchronized List pools() { 82 List allPools = new ArrayList (pools.size() + indexablePools.size()); 83 allPools.addAll(pools.values()); 84 allPools.addAll(indexablePools.values()); 85 return Collections.unmodifiableList(allPools); 86 } 87 88 public void setComponentContext(ComponentContext ctx) { 89 } 90 91 public static void main(String [] args) { 92 93 } 94 } 95 | Popular Tags |