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     */
017    package org.apache.activemq.web;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Set;
025    import javax.management.*;
026    import javax.management.openmbean.CompositeData;
027    import javax.management.openmbean.TabularData;
028    
029    import org.apache.activemq.broker.jmx.*;
030    import org.springframework.util.StringUtils;
031    
032    /**
033     * A useful base class for an implementation of {@link BrokerFacade}
034     * 
035     * 
036     */
037    public abstract class BrokerFacadeSupport implements BrokerFacade {
038        public abstract ManagementContext getManagementContext();
039        public abstract Set queryNames(ObjectName name, QueryExp query) throws Exception;
040        public abstract Object newProxyInstance( ObjectName objectName, Class interfaceClass, boolean notificationBroadcaster) throws Exception;
041    
042        public Collection<QueueViewMBean> getQueues() throws Exception {
043            BrokerViewMBean broker = getBrokerAdmin();
044            if (broker == null) {
045                return Collections.EMPTY_LIST;
046            }
047            ObjectName[] queues = broker.getQueues();
048            return getManagedObjects(queues, QueueViewMBean.class);
049        }
050    
051        public Collection<TopicViewMBean> getTopics() throws Exception {
052            BrokerViewMBean broker = getBrokerAdmin();
053            if (broker == null) {
054                return Collections.EMPTY_LIST;
055            }
056            ObjectName[] queues = broker.getTopics();
057            return getManagedObjects(queues, TopicViewMBean.class);
058        }
059    
060        public Collection<DurableSubscriptionViewMBean> getDurableTopicSubscribers() throws Exception {
061            BrokerViewMBean broker = getBrokerAdmin();
062            if (broker == null) {
063                return Collections.EMPTY_LIST;
064            }
065            ObjectName[] queues = broker.getDurableTopicSubscribers();
066            return getManagedObjects(queues, DurableSubscriptionViewMBean.class);
067        }
068    
069        public Collection<DurableSubscriptionViewMBean> getInactiveDurableTopicSubscribers() throws Exception {
070            BrokerViewMBean broker = getBrokerAdmin();
071            if (broker == null) {
072                return Collections.EMPTY_LIST;
073            }
074            ObjectName[] queues = broker.getInactiveDurableTopicSubscribers();
075            return getManagedObjects(queues, DurableSubscriptionViewMBean.class);
076        }
077    
078        public QueueViewMBean getQueue(String name) throws Exception {
079            return (QueueViewMBean) getDestinationByName(getQueues(), name);
080        }
081    
082        public TopicViewMBean getTopic(String name) throws Exception {
083            return (TopicViewMBean) getDestinationByName(getTopics(), name);
084        }
085    
086        protected DestinationViewMBean getDestinationByName(Collection<? extends DestinationViewMBean> collection,
087                String name) {
088            Iterator<? extends DestinationViewMBean> iter = collection.iterator();
089            while (iter.hasNext()) {
090                DestinationViewMBean destinationViewMBean = iter.next();
091                if (name.equals(destinationViewMBean.getName())) {
092                    return destinationViewMBean;
093                }
094            }
095            return null;
096        }
097    
098        @SuppressWarnings("unchecked")
099        protected <T> Collection<T> getManagedObjects(ObjectName[] names, Class<T> type) throws Exception {
100            List<T> answer = new ArrayList<T>();
101            for (int i = 0; i < names.length; i++) {
102                ObjectName name = names[i];
103                T value = (T) newProxyInstance(name, type, true);
104                if (value != null) {
105                    answer.add(value);
106                }
107            }
108            return answer;
109        }
110    
111        @SuppressWarnings("unchecked")
112        public Collection<ConnectionViewMBean> getConnections() throws Exception {
113            String brokerName = getBrokerName();
114            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName + ",connector=clientConnectors,connectorName=*,connectionName=*");
115    
116            Set<ObjectName> queryResult = queryNames(query, null);
117            return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]), ConnectionViewMBean.class);
118        }
119    
120        @SuppressWarnings("unchecked")
121        public Collection<String> getConnections(String connectorName) throws Exception {
122            String brokerName = getBrokerName();
123            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName
124                    + ",connector=clientConnectors,connectorName=" + connectorName + ",connectionName=*");
125            Set<ObjectName> queryResult = queryNames(query, null);
126            Collection<String> result = new ArrayList<String>(queryResult.size());
127            for (ObjectName on : queryResult) {
128                String name = StringUtils.replace(on.getKeyProperty("connectionName"), "_", ":");
129                result.add(name);
130            }
131            return result;
132        }
133    
134        @SuppressWarnings("unchecked")
135        public ConnectionViewMBean getConnection(String connectionName) throws Exception {
136            connectionName = StringUtils.replace(connectionName, ":", "_");
137            String brokerName = getBrokerName();
138            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName
139                    + ",connector=clientConnectors,*,connectionName=" + connectionName);
140            Set<ObjectName> queryResult = queryNames(query, null);
141            if (queryResult.size() == 0)
142                return null;
143            ObjectName objectName = queryResult.iterator().next();
144            return (ConnectionViewMBean) newProxyInstance(objectName, ConnectionViewMBean.class,
145                    true);
146        }
147    
148        @SuppressWarnings("unchecked")
149        public Collection<String> getConnectors() throws Exception {
150            String brokerName = getBrokerName();
151            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName + ",connector=clientConnectors,connectorName=*");
152            Set<ObjectName> queryResult = queryNames(query, null);
153            Collection<String> result = new ArrayList<String>(queryResult.size());
154            for (ObjectName on : queryResult)
155                result.add(on.getKeyProperty("connectorName"));
156            return result;
157        }
158    
159        public ConnectorViewMBean getConnector(String name) throws Exception {
160            String brokerName = getBrokerName();
161            ObjectName objectName = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName
162                    + ",connector=clientConnectors,connectorName=" + name);
163            return (ConnectorViewMBean) newProxyInstance(objectName, ConnectorViewMBean.class, true);
164        }
165    
166        @SuppressWarnings("unchecked")
167        public Collection<NetworkConnectorViewMBean> getNetworkConnectors() throws Exception {
168            String brokerName = getBrokerName();
169            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName + ",connector=networkConnectors,networkConnectorName=*");
170            Set<ObjectName> queryResult = queryNames(query, null);
171            return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]),
172                    NetworkConnectorViewMBean.class);
173        }
174    
175        public Collection<NetworkBridgeViewMBean> getNetworkBridges() throws Exception {
176            String brokerName = getBrokerName();
177            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName + ",connector=networkConnectors,networkConnectorName=*,networkBridge=*");
178            Set<ObjectName> queryResult = queryNames(query, null);
179            return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]),
180                    NetworkBridgeViewMBean.class);
181        }
182    
183        @SuppressWarnings("unchecked")
184        public Collection<SubscriptionViewMBean> getQueueConsumers(String queueName) throws Exception {
185            String brokerName = getBrokerName();
186            queueName = StringUtils.replace(queueName, "\"", "_");
187            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName
188                    + ",destinationType=Queue,destinationName=" + queueName + ",endpoint=Consumer,*");
189            Set<ObjectName> queryResult = queryNames(query, null);
190            return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]), SubscriptionViewMBean.class);
191        }
192    
193        @SuppressWarnings("unchecked")
194        public Collection<SubscriptionViewMBean> getConsumersOnConnection(String connectionName) throws Exception {
195            connectionName = StringUtils.replace(connectionName, ":", "_");
196            String brokerName = getBrokerName();
197            ObjectName query = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerName
198                    + ",*,endpoint=Consumer,clientId=" + connectionName);
199            Set<ObjectName> queryResult = queryNames(query, null);
200            return getManagedObjects(queryResult.toArray(new ObjectName[queryResult.size()]), SubscriptionViewMBean.class);
201        }
202    
203        public JobSchedulerViewMBean getJobScheduler() throws Exception {
204            ObjectName name = getBrokerAdmin().getJMSJobScheduler();
205            return (JobSchedulerViewMBean) newProxyInstance(name, JobSchedulerViewMBean.class, true);
206        }
207    
208        public Collection<JobFacade> getScheduledJobs() throws Exception {
209            JobSchedulerViewMBean jobScheduler = getJobScheduler();
210            List<JobFacade> result = new ArrayList<JobFacade>();
211            TabularData table = jobScheduler.getAllJobs();
212            for (Object object : table.values()) {
213                CompositeData cd = (CompositeData) object;
214                JobFacade jf = new JobFacade(cd);
215                result.add(jf);
216            }
217            return result;
218        }
219    
220    
221        public boolean isJobSchedulerStarted() {
222            try {
223                JobSchedulerViewMBean jobScheduler = getJobScheduler();
224                return true;
225            } catch (Exception e) {
226                return false;
227            }
228        }
229    }