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    
025    import javax.management.MBeanServer;
026    import javax.management.MBeanServerInvocationHandler;
027    import javax.management.ObjectName;
028    
029    import org.apache.activemq.broker.jmx.BrokerViewMBean;
030    import org.apache.activemq.broker.jmx.DestinationViewMBean;
031    import org.apache.activemq.broker.jmx.DurableSubscriptionViewMBean;
032    import org.apache.activemq.broker.jmx.ManagementContext;
033    import org.apache.activemq.broker.jmx.QueueViewMBean;
034    import org.apache.activemq.broker.jmx.TopicViewMBean;
035    
036    /**
037     * A useful base class for an implementation of {@link BrokerFacade}
038     *
039     * @version $Revision: 569067 $
040     */
041    public abstract class BrokerFacadeSupport implements BrokerFacade {
042        public abstract ManagementContext getManagementContext();
043    
044        public Collection<Object> getQueues() throws Exception {
045            BrokerViewMBean broker = getBrokerAdmin();
046            if (broker == null) {
047                return Collections.EMPTY_LIST;
048            }
049            ObjectName[] queues = broker.getQueues();
050            return getManagedObjects(queues, QueueViewMBean.class);
051        }
052    
053        public Collection<Object> getTopics() throws Exception {
054            BrokerViewMBean broker = getBrokerAdmin();
055            if (broker == null) {
056                return Collections.EMPTY_LIST;
057            }
058            ObjectName[] queues = broker.getTopics();
059            return getManagedObjects(queues, TopicViewMBean.class);
060        }
061    
062        public Collection<Object> getDurableTopicSubscribers() throws Exception {
063            BrokerViewMBean broker = getBrokerAdmin();
064            if (broker == null) {
065                return Collections.EMPTY_LIST;
066            }
067            ObjectName[] queues = broker.getDurableTopicSubscribers();
068            return getManagedObjects(queues, DurableSubscriptionViewMBean.class);
069        }
070    
071        public QueueViewMBean getQueue(String name) throws Exception {
072            return (QueueViewMBean) getDestinationByName(getQueues(), name);
073        }
074    
075        public TopicViewMBean getTopic(String name) throws Exception {
076            return (TopicViewMBean) getDestinationByName(getTopics(), name);
077        }
078    
079        protected DestinationViewMBean getDestinationByName(Collection<Object> collection, String name) {
080            Iterator<Object> iter = collection.iterator();
081            while (iter.hasNext()) {
082                DestinationViewMBean destinationViewMBean = (DestinationViewMBean) iter.next();
083                if (name.equals(destinationViewMBean.getName())) {
084                    return destinationViewMBean;
085                }
086            }
087            return null;
088        }
089    
090        protected Collection<Object> getManagedObjects(ObjectName[] names, Class type) {
091            List<Object> answer = new ArrayList<Object>();
092            MBeanServer mbeanServer = getManagementContext().getMBeanServer();
093            if (mbeanServer != null) {
094                for (int i = 0; i < names.length; i++) {
095                    ObjectName name = names[i];
096                    Object value = MBeanServerInvocationHandler.newProxyInstance(mbeanServer, name, type, true);
097                    if (value != null) {
098                        answer.add(value);
099                    }
100                }
101            }
102            return answer;
103        }
104    }