KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > BrokerFacadeSupport


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

18 package org.apache.activemq.web;
19
20 import org.apache.activemq.broker.jmx.BrokerViewMBean;
21 import org.apache.activemq.broker.jmx.DurableSubscriptionViewMBean;
22 import org.apache.activemq.broker.jmx.ManagementContext;
23 import org.apache.activemq.broker.jmx.TopicViewMBean;
24 import org.apache.activemq.broker.jmx.QueueViewMBean;
25 import org.apache.activemq.broker.jmx.DestinationViewMBean;
26
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerInvocationHandler JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * A useful base class for an implementation of {@link BrokerFacade}
38  *
39  * @version $Revision: 516117 $
40  */

41 public abstract class BrokerFacadeSupport implements BrokerFacade {
42     public abstract ManagementContext getManagementContext();
43
44     public Collection JavaDoc getQueues() throws Exception JavaDoc {
45         BrokerViewMBean broker = getBrokerAdmin();
46         if (broker == null) {
47             return Collections.EMPTY_LIST;
48         }
49         ObjectName JavaDoc[] queues = broker.getQueues();
50         return getManagedObjects(queues, QueueViewMBean.class);
51     }
52
53     public Collection JavaDoc getTopics() throws Exception JavaDoc {
54         BrokerViewMBean broker = getBrokerAdmin();
55         if (broker == null) {
56             return Collections.EMPTY_LIST;
57         }
58         ObjectName JavaDoc[] queues = broker.getTopics();
59         return getManagedObjects(queues, TopicViewMBean.class);
60     }
61
62     public Collection JavaDoc getDurableTopicSubscribers() throws Exception JavaDoc {
63         BrokerViewMBean broker = getBrokerAdmin();
64         if (broker == null) {
65             return Collections.EMPTY_LIST;
66         }
67         ObjectName JavaDoc[] queues = broker.getDurableTopicSubscribers();
68         return getManagedObjects(queues, DurableSubscriptionViewMBean.class);
69     }
70
71     public QueueViewMBean getQueue(String JavaDoc name) throws Exception JavaDoc {
72         return (QueueViewMBean) getDestinationByName(getQueues(), name);
73     }
74
75     public TopicViewMBean getTopic(String JavaDoc name) throws Exception JavaDoc {
76         return (TopicViewMBean) getDestinationByName(getTopics(), name);
77     }
78
79     protected DestinationViewMBean getDestinationByName(Collection JavaDoc collection, String JavaDoc name) {
80         Iterator JavaDoc iter = collection.iterator();
81         while (iter.hasNext()) {
82             DestinationViewMBean destinationViewMBean = (DestinationViewMBean) iter.next();
83             if (name.equals(destinationViewMBean.getName())) {
84                 return destinationViewMBean;
85             }
86         }
87         return null;
88     }
89
90     protected Collection JavaDoc getManagedObjects(ObjectName JavaDoc[] names, Class JavaDoc type) {
91         List JavaDoc answer = new ArrayList JavaDoc();
92         MBeanServer JavaDoc mbeanServer = getManagementContext().getMBeanServer();
93         if (mbeanServer != null) {
94             for (int i = 0; i < names.length; i++) {
95                 ObjectName JavaDoc name = names[i];
96                 Object JavaDoc value = MBeanServerInvocationHandler.newProxyInstance(mbeanServer, name, type, true);
97                 if (value != null) {
98                     answer.add(value);
99                 }
100             }
101         }
102         return answer;
103     }
104 }
105
Popular Tags