1 18 package org.apache.activemq.xbean; 19 20 import java.util.HashMap ; 21 22 import org.apache.activemq.broker.BrokerService; 23 import org.springframework.beans.factory.DisposableBean; 24 import org.springframework.beans.factory.FactoryBean; 25 import org.springframework.beans.factory.InitializingBean; 26 import org.springframework.core.io.Resource; 27 28 36 public class PooledBrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean { 37 38 static final HashMap sharedBrokerMap = new HashMap (); 39 40 private boolean start; 41 private Resource config; 42 43 static class SharedBroker { 44 BrokerFactoryBean factory; 45 int refCount; 46 } 47 48 public void afterPropertiesSet() throws Exception { 49 synchronized( sharedBrokerMap ) { 50 SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config.getFilename()); 51 if( sharedBroker == null ) { 52 sharedBroker = new SharedBroker(); 53 sharedBroker.factory = new BrokerFactoryBean(); 54 sharedBroker.factory.setConfig(config); 55 sharedBroker.factory.setStart(start); 56 sharedBroker.factory.afterPropertiesSet(); 57 sharedBrokerMap.put(config.getFilename(), sharedBroker); 58 } 59 sharedBroker.refCount++; 60 } 61 } 62 63 public void destroy() throws Exception { 64 synchronized( sharedBrokerMap ) { 65 SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config.getFilename()); 66 if( sharedBroker != null ) { 67 sharedBroker.refCount--; 68 if( sharedBroker.refCount==0 ) { 69 sharedBroker.factory.destroy(); 70 sharedBrokerMap.remove(config.getFilename()); 71 } 72 } 73 } 74 } 75 76 public Resource getConfig() { 77 return config; 78 } 79 80 public Object getObject() throws Exception { 81 synchronized( sharedBrokerMap ) { 82 SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config.getFilename()); 83 if( sharedBroker != null ) { 84 return sharedBroker.factory.getObject(); 85 } 86 } 87 return null; 88 } 89 90 public Class getObjectType() { 91 return BrokerService.class; 92 } 93 94 public boolean isSingleton() { 95 return true; 96 } 97 98 public boolean isStart() { 99 return start; 100 } 101 102 public void setConfig(Resource config) { 103 this.config = config; 104 } 105 106 public void setStart(boolean start) { 107 this.start=start; 108 } 109 110 } 111 | Popular Tags |