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.Iterator;
020    import java.util.Set;
021    
022    import javax.management.ObjectName;
023    import javax.management.QueryExp;
024    
025    import org.apache.activemq.broker.Broker;
026    import org.apache.activemq.broker.BrokerService;
027    import org.apache.activemq.broker.jmx.BrokerView;
028    import org.apache.activemq.broker.jmx.BrokerViewMBean;
029    import org.apache.activemq.broker.jmx.ManagedRegionBroker;
030    import org.apache.activemq.broker.jmx.ManagementContext;
031    import org.apache.activemq.broker.region.Destination;
032    import org.apache.activemq.broker.region.Queue;
033    import org.apache.activemq.command.ActiveMQDestination;
034    
035    /**
036     * An implementation of {@link BrokerFacade} which uses a local in JVM broker
037     * 
038     * 
039     */
040    public class LocalBrokerFacade extends BrokerFacadeSupport {
041            private BrokerService brokerService;
042    
043            public LocalBrokerFacade(BrokerService brokerService) {
044                    this.brokerService = brokerService;
045            }
046    
047            public BrokerService getBrokerService() {
048                    return brokerService;
049            }
050            public String getBrokerName() throws Exception {
051                    return brokerService.getBrokerName();
052            }
053            public Broker getBroker() throws Exception {
054                    return brokerService.getBroker();
055            }
056            public ManagementContext getManagementContext() {
057                    return brokerService.getManagementContext();
058            }
059            public BrokerViewMBean getBrokerAdmin() throws Exception {
060                    return brokerService.getAdminView();
061            }
062            public ManagedRegionBroker getManagedBroker() throws Exception {
063                    BrokerView adminView = brokerService.getAdminView();
064                    if (adminView == null) {
065                            return null;
066                    }
067                    return adminView.getBroker();
068            }
069    
070        public void purgeQueue(ActiveMQDestination destination) throws Exception {
071            Set destinations = getManagedBroker().getQueueRegion().getDestinations(destination);
072            for (Iterator i = destinations.iterator(); i.hasNext();) {
073                Destination dest = (Destination) i.next();
074                if (dest instanceof Queue) {
075                    Queue regionQueue = (Queue) dest;
076                    regionQueue.purge();
077                }
078            }
079        }
080    
081        @Override
082        public Set queryNames(ObjectName name, QueryExp query) throws Exception {
083            return getManagementContext().queryNames(name, query);
084        }
085    
086        @Override
087        public Object newProxyInstance(ObjectName objectName, Class interfaceClass, boolean notificationBroadcaster) {
088            return getManagementContext().newProxyInstance(objectName, interfaceClass, notificationBroadcaster);
089        }
090        
091    }