1 package org.apache.ojb.broker.core; 2 3 17 18 import java.util.Properties ; 19 20 import org.apache.commons.lang.builder.ToStringBuilder; 21 import org.apache.commons.lang.builder.ToStringStyle; 22 import org.apache.commons.pool.KeyedObjectPool; 23 import org.apache.commons.pool.KeyedPoolableObjectFactory; 24 import org.apache.commons.pool.impl.GenericKeyedObjectPool; 25 import org.apache.ojb.broker.PBFactoryException; 26 import org.apache.ojb.broker.PBKey; 27 import org.apache.ojb.broker.PBState; 28 import org.apache.ojb.broker.PersistenceBroker; 29 import org.apache.ojb.broker.PersistenceBrokerInternal; 30 import org.apache.ojb.broker.util.BrokerHelper; 31 import org.apache.ojb.broker.util.logging.Logger; 32 import org.apache.ojb.broker.util.logging.LoggerFactory; 33 34 62 public class PersistenceBrokerFactoryDefaultImpl extends PersistenceBrokerFactoryBaseImpl 63 { 64 private static Logger log = LoggerFactory.getLogger(PersistenceBrokerFactoryDefaultImpl.class); 65 private GenericKeyedObjectPool brokerPool; 66 private PBPoolInfo poolConfig; 67 68 public PersistenceBrokerFactoryDefaultImpl() 69 { 70 super(); 71 poolConfig = new PBPoolInfo(); 73 brokerPool = this.createPool(); 75 log.info("Create PersistenceBroker instance pool, pool configuration was " + getPoolConfiguration()); 76 } 77 78 85 public PersistenceBrokerInternal createPersistenceBroker(PBKey pbKey) throws PBFactoryException 86 { 87 if (log.isDebugEnabled()) log.debug("Obtain broker from pool, used PBKey is " + pbKey); 88 PersistenceBrokerInternal broker = null; 89 90 93 pbKey = BrokerHelper.crossCheckPBKey(pbKey); 94 95 try 96 { 97 101 broker = ((PersistenceBrokerInternal) brokerPool.borrowObject(pbKey)); 102 106 broker = wrapRequestedBrokerInstance(broker); 107 108 } 109 catch (Exception e) 110 { 111 try 112 { 113 if(broker != null) broker.close(); 115 } 116 catch (Exception ignore) 117 { 118 } 120 throw new PBFactoryException("Borrow broker from pool failed, using PBKey " + pbKey, e); 121 } 122 return broker; 123 } 124 125 134 protected PersistenceBrokerInternal wrapBrokerWithPoolingHandle(PersistenceBrokerInternal broker, KeyedObjectPool pool) 135 { 136 return new PoolablePersistenceBroker(broker, pool); 137 } 138 139 148 protected PersistenceBrokerInternal wrapRequestedBrokerInstance(PersistenceBrokerInternal broker) 149 { 150 return new PersistenceBrokerHandle(broker); 151 } 152 153 156 public synchronized void releaseAllInstances() 157 { 158 log.warn("Release all instances referenced by this object"); 159 super.releaseAllInstances(); 160 try 161 { 162 brokerPool.close(); 163 brokerPool = this.createPool(); 164 } 165 catch (Exception e) 166 { 167 log.error("Error while release all pooled broker instances and refresh pool", e); 168 } 169 } 170 171 public void shutdown() 172 { 173 try 174 { 175 brokerPool.close(); 176 brokerPool = null; 177 } 178 catch(Exception e) 179 { 180 log.error("Error while shutdown of broker pool", e); 181 } 182 super.shutdown(); 183 } 184 185 public int activePersistenceBroker() 186 { 187 return brokerPool.getNumActive(); 188 } 189 190 194 public Properties getPoolConfiguration() 195 { 196 return poolConfig; 197 } 198 199 203 public void setPoolConfiguration(Properties prop) 204 { 205 poolConfig = new PBPoolInfo(prop); 206 log.info("Change pooling configuration properties: " + poolConfig.getKeyedObjectPoolConfig()); 207 brokerPool.setConfig(poolConfig.getKeyedObjectPoolConfig()); 208 } 209 210 211 216 private GenericKeyedObjectPool createPool() 217 { 218 GenericKeyedObjectPool.Config conf = poolConfig.getKeyedObjectPoolConfig(); 219 if (log.isDebugEnabled()) 220 log.debug("PersistenceBroker pool will be setup with the following configuration " + 221 ToStringBuilder.reflectionToString(conf, ToStringStyle.MULTI_LINE_STYLE)); 222 GenericKeyedObjectPool pool = new GenericKeyedObjectPool(null, conf); 223 pool.setFactory(new PersistenceBrokerFactoryDefaultImpl.PBKeyedPoolableObjectFactory(this, pool)); 224 return pool; 225 } 226 227 232 239 class PBKeyedPoolableObjectFactory implements KeyedPoolableObjectFactory 240 { 241 private PersistenceBrokerFactoryDefaultImpl pbf; 242 private KeyedObjectPool pool; 243 244 public PBKeyedPoolableObjectFactory(PersistenceBrokerFactoryDefaultImpl pbf, KeyedObjectPool pool) 245 { 246 this.pbf = pbf; 247 this.pool = pool; 248 } 249 250 public Object makeObject(Object key) throws Exception 251 { 252 return wrapBrokerWithPoolingHandle(pbf.createNewBrokerInstance((PBKey) key), pool); 253 } 254 255 258 public void destroyObject(Object key, Object obj) throws Exception 259 { 260 PoolablePersistenceBroker pb = (PoolablePersistenceBroker) obj; 261 PersistenceBroker broker = pb.getInnermostDelegate(); 262 if (broker instanceof PersistenceBrokerImpl) 263 { 264 log.info("Destroy PersistenceBroker instance " + obj); 265 ((PersistenceBrokerImpl) broker).destroy(); 266 } 267 pb.destroy(); 268 } 269 270 279 public boolean validateObject(Object key, Object obj) 280 { 281 if (((PersistenceBroker) obj).isInTransaction()) 284 { 285 log.error("Illegal broker state! This broker instance was already in transaction."); 286 return false; 287 } 288 return true; 289 } 290 291 294 public void activateObject(Object key, Object obj) throws Exception 295 { 296 ((PBState) obj).setClosed(false); 297 } 298 299 302 public void passivateObject(Object key, Object obj) throws Exception 303 { 304 ((PBState) obj).setClosed(true); 305 } 306 } 307 } 308 | Popular Tags |