KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > BrokerRegistry


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
4  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
5  * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  */

14
15 package org.apache.activemq.broker;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23  *
24  * @version $Revision: 1.3 $
25  */

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 JavaDoc mutex=new Object JavaDoc();
35     private final HashMap JavaDoc<String JavaDoc,BrokerService> brokers=new HashMap JavaDoc<String JavaDoc,BrokerService>();
36
37     /**
38      * @param brokerName
39      * @return the BrokerService
40      */

41     public BrokerService lookup(String JavaDoc 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     /**
56      * Returns the first registered broker found
57      * @return the first BrokerService
58      */

59     public BrokerService findFirst(){
60         synchronized(mutex){
61             Iterator JavaDoc<BrokerService> iter=brokers.values().iterator();
62             while(iter.hasNext()){
63                 return iter.next();
64             }
65             return null;
66         }
67     }
68
69     /**
70      * @param brokerName
71      * @param broker
72      */

73     public void bind(String JavaDoc brokerName,BrokerService broker){
74         synchronized(mutex){
75             brokers.put(brokerName,broker);
76         }
77     }
78
79     /**
80      * @param brokerName
81      */

82     public void unbind(String JavaDoc brokerName){
83         synchronized(mutex){
84             brokers.remove(brokerName);
85         }
86     }
87
88     /**
89      * @return the mutex used
90      */

91     public Object JavaDoc getRegistryMutext(){
92         return mutex;
93     }
94 }
95
Popular Tags