001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     * 
017     **/
018    package org.activemq.broker;
019    
020    import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
021    
022    import javax.jms.JMSException;
023    import java.util.Map;
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Iterator;
027    
028    /**
029     * A cache of all the brokers and broker connectors in use which is usually used
030     * in a singleton way but could be used in an IoC style manner.
031     *
032     * @version $Revision: 1.1.1.1 $
033     */
034    public class BrokerContext {
035    
036        private static final BrokerContext singleton = new BrokerContext();
037    
038        private Map brokersByName = new ConcurrentHashMap();
039        private Map connectorsByURL = new ConcurrentHashMap();
040    
041        public static BrokerContext getInstance() {
042            return singleton;
043        }
044    
045    
046        public synchronized BrokerContainer getBrokerContainerByName(String url, String name, BrokerContainerFactory factory) throws JMSException {
047            BrokerContainer container = (BrokerContainer) brokersByName.get(url);
048            if (container == null) {
049                // this should register the container
050                container = factory.createBrokerContainer(name, this);
051    
052                // note that we will register the broker by name and by URL
053                brokersByName.put(url, container);
054    
055                assert brokersByName.get(url) == container : "Should have registered the container by now";
056    
057                container.start();
058            }
059            return container;
060        }
061    
062        public void registerContainer(String url, BrokerContainer container) {
063            if (url == null) {
064                throw new IllegalArgumentException("Name must not be null");
065            }
066            brokersByName.put(url, container);
067        }
068    
069        public void deregisterContainer(String url, BrokerContainer container) {
070            brokersByName.remove(url);
071    
072            // we may have registered the container under an alias, so lets remove it as any value
073            List list = new ArrayList();
074            for (Iterator iter = brokersByName.entrySet().iterator(); iter.hasNext(); ) {
075                Map.Entry entry = (Map.Entry) iter.next();
076                Object key = entry.getKey();
077                Object value = entry.getValue();
078                if (container.equals(value)) {
079                    list.add(key);
080                }
081            }
082    
083            for (Iterator iter = list.iterator(); iter.hasNext();) {
084                Object key = iter.next();
085                brokersByName.remove(key);
086            }
087        }
088    
089        public void registerConnector(String url, BrokerConnector connector) {
090            connectorsByURL.put(url, connector);
091        }
092    
093        public void deregisterConnector(String urlString) {
094            connectorsByURL.remove(urlString);
095        }
096    
097        public BrokerConnector getConnectorByURL(String url) {
098            BrokerConnector brokerConnector = (BrokerConnector) connectorsByURL.get(url);
099            if (brokerConnector == null) {
100                if (url.startsWith("reliable:")) {
101                    return getConnectorByURL(url.substring("reliable:".length()));
102                }
103                else if (url.startsWith("list:")) {
104                    return getConnectorByURL(url.substring("list:".length()));
105                }else if (url.startsWith("peer:")){
106                    return getConnectorByURL(url.substring("peer:".length()));
107                }
108            }
109            return brokerConnector;
110        }
111    }