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 " + result.getBrokerName() + " instead");
053                }
054            }
055        }
056        return result;
057    }
058
059    /**
060     * Returns the first registered broker found
061     * 
062     * @return the first BrokerService
063     */
064    public BrokerService findFirst() {
065        synchronized (mutex) {
066            Iterator<BrokerService> iter = brokers.values().iterator();
067            while (iter.hasNext()) {
068                return iter.next();
069            }
070            return null;
071        }
072    }
073
074    /**
075     * @param brokerName
076     * @param broker
077     */
078    public void bind(String brokerName, BrokerService broker) {
079        synchronized (mutex) {
080            brokers.put(brokerName, broker);
081            mutex.notifyAll();
082        }
083    }
084
085    /**
086     * @param brokerName
087     */
088    public void unbind(String brokerName) {
089        synchronized (mutex) {
090            brokers.remove(brokerName);
091        }
092    }
093
094    /**
095     * @return the mutex used
096     */
097    public Object getRegistryMutext() {
098        return mutex;
099    }
100    
101    public Map<String, BrokerService> getBrokers() {
102        return Collections.unmodifiableMap(this.brokers);
103    }
104}