View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.broker;
19  
20  import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
21  
22  import javax.jms.JMSException;
23  import java.util.Map;
24  
25  /***
26   * A cache of all the brokers and broker connectors in use which is usually used
27   * in a singleton way but could be used in an IoC style manner.
28   *
29   * @version $Revision: 1.4 $
30   */
31  public class BrokerContext {
32  
33      private static final BrokerContext singleton = new BrokerContext();
34  
35      private Map brokersByName = new ConcurrentHashMap();
36      private Map connectorsByURL = new ConcurrentHashMap();
37  
38      public static BrokerContext getInstance() {
39          return singleton;
40      }
41  
42  
43      public synchronized BrokerContainer getBrokerContainerByName(String name, BrokerContainerFactory factory) throws JMSException {
44          BrokerContainer container = (BrokerContainer) brokersByName.get(name);
45          if (container == null) {
46              // this should register the container
47              container = factory.createBrokerContainer(name, this);
48  
49              assert brokersByName.get(name) == container : "Should have registered the container by now";
50  
51              container.start();
52          }
53          return container;
54      }
55  
56      public void registerContainer(String name, BrokerContainer container) {
57          if (name == null) {
58              throw new IllegalArgumentException("Name must not be null");
59          }
60          brokersByName.put(name, container);
61      }
62  
63      public void deregisterContainer(String name, BrokerContainer container) {
64          brokersByName.remove(name);
65      }
66  
67      public void registerConnector(String url, BrokerConnector connector) {
68          connectorsByURL.put(url, connector);
69      }
70  
71      public void deregisterConnector(String urlString) {
72          connectorsByURL.remove(urlString);
73      }
74  
75      public BrokerConnector getConnectorByURL(String url) {
76          BrokerConnector brokerConnector = (BrokerConnector) connectorsByURL.get(url);
77          if (brokerConnector == null) {
78              if (url.startsWith("reliable:")) {
79                  return getConnectorByURL(url.substring("reliable:".length()));
80              }
81              else if (url.startsWith("list:")) {
82                  return getConnectorByURL(url.substring("list:".length()));
83              }
84          }
85          return brokerConnector;
86      }
87  }