001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Iterator;
022import java.util.Map;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * 
028 */
029public class BrokerRegistry {
030
031    private static final Logger LOG = LoggerFactory.getLogger(BrokerRegistry.class);
032    private static final BrokerRegistry INSTANCE = new BrokerRegistry();
033
034    private final Object mutex = new Object();
035    private final Map<String, BrokerService> brokers = new HashMap<String, BrokerService>();
036
037    public static BrokerRegistry getInstance() {
038        return INSTANCE;
039    }
040
041    /**
042     * @param brokerName
043     * @return the BrokerService
044     */
045    public BrokerService lookup(String brokerName) {
046        BrokerService result = null;
047        synchronized (mutex) {
048            result = brokers.get(brokerName);
049            if (result == null && brokerName != null && brokerName.equals(BrokerService.DEFAULT_BROKER_NAME)) {
050                result = findFirst();
051                if (result != null) {
052                    LOG.warn("Broker localhost not started so using {} instead", result.getBrokerName());
053                }
054            }
055            if (result == null && (brokerName==null || brokerName.isEmpty() || brokerName.equals("null"))){
056                result = findFirst();
057            }
058        }
059        return result;
060    }
061
062    /**
063     * Returns the first registered broker found
064     * 
065     * @return the first BrokerService
066     */
067    public BrokerService findFirst() {
068        synchronized (mutex) {
069            Iterator<BrokerService> iter = brokers.values().iterator();
070            while (iter.hasNext()) {
071                return iter.next();
072            }
073            return null;
074        }
075    }
076
077    /**
078     * @param brokerName
079     * @param broker
080     */
081    public void bind(String brokerName, BrokerService broker) {
082        synchronized (mutex) {
083            brokers.put(brokerName, broker);
084            mutex.notifyAll();
085        }
086    }
087
088    /**
089     * @param brokerName
090     */
091    public void unbind(String brokerName) {
092        synchronized (mutex) {
093            brokers.remove(brokerName);
094        }
095    }
096
097    /**
098     * @return the mutex used
099     */
100    public Object getRegistryMutext() {
101        return mutex;
102    }
103    
104    public Map<String, BrokerService> getBrokers() {
105        return Collections.unmodifiableMap(this.brokers);
106    }
107}