1 14 15 package org.apache.activemq.broker; 16 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 26 public class BrokerRegistry{ 27 28 private static final Log log=LogFactory.getLog(BrokerRegistry.class); 29 static final private BrokerRegistry instance=new BrokerRegistry(); 30 31 public static BrokerRegistry getInstance(){ 32 return instance; 33 } 34 private final Object mutex=new Object (); 35 private final HashMap <String ,BrokerService> brokers=new HashMap <String ,BrokerService>(); 36 37 41 public BrokerService lookup(String brokerName){ 42 BrokerService result=null; 43 synchronized(mutex){ 44 result=brokers.get(brokerName); 45 if(result==null&&brokerName!=null&&brokerName.equals(BrokerService.DEFAULT_BROKER_NAME)){ 46 result=findFirst(); 47 if(result!=null){ 48 log.warn("Broker localhost not started so using "+result.getBrokerName()+" instead"); 49 } 50 } 51 } 52 return result; 53 } 54 55 59 public BrokerService findFirst(){ 60 synchronized(mutex){ 61 Iterator <BrokerService> iter=brokers.values().iterator(); 62 while(iter.hasNext()){ 63 return iter.next(); 64 } 65 return null; 66 } 67 } 68 69 73 public void bind(String brokerName,BrokerService broker){ 74 synchronized(mutex){ 75 brokers.put(brokerName,broker); 76 } 77 } 78 79 82 public void unbind(String brokerName){ 83 synchronized(mutex){ 84 brokers.remove(brokerName); 85 } 86 } 87 88 91 public Object getRegistryMutext(){ 92 return mutex; 93 } 94 } 95 | Popular Tags |