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