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.broker.jmx;
018
019import javax.management.openmbean.CompositeData;
020import javax.management.openmbean.OpenDataException;
021import javax.management.openmbean.TabularData;
022
023import org.apache.activemq.broker.BrokerService;
024import org.apache.activemq.broker.ConnectionContext;
025import org.apache.activemq.broker.region.Subscription;
026import org.apache.activemq.command.ConsumerInfo;
027import org.apache.activemq.command.RemoveSubscriptionInfo;
028import org.apache.activemq.command.SubscriptionInfo;
029
030/**
031 *
032 *
033 */
034public class InactiveDurableSubscriptionView extends DurableSubscriptionView implements DurableSubscriptionViewMBean {
035    protected SubscriptionInfo subscriptionInfo;
036
037    /**
038     * Constructor
039     *
040     * @param broker
041     * @param brokerService
042     * @param clientId
043     * @param subInfo
044     * @param subscription
045     */
046    public InactiveDurableSubscriptionView(ManagedRegionBroker broker, BrokerService brokerService, String clientId, SubscriptionInfo subInfo, Subscription subscription) {
047        super(broker, brokerService, clientId, null, subscription);
048        this.broker = broker;
049        this.subscriptionInfo = subInfo;
050    }
051
052    /**
053     * @return the id of the Subscription
054     */
055    @Override
056    public long getSubscriptionId() {
057        return -1;
058    }
059
060    /**
061     * @return the destination name
062     */
063    @Override
064    public String getDestinationName() {
065        return subscriptionInfo.getDestination().getPhysicalName();
066    }
067
068    /**
069     * @return true if the destination is a Queue
070     */
071    @Override
072    public boolean isDestinationQueue() {
073        return false;
074    }
075
076    /**
077     * @return true of the destination is a Topic
078     */
079    @Override
080    public boolean isDestinationTopic() {
081        return true;
082    }
083
084    /**
085     * @return true if the destination is temporary
086     */
087    @Override
088    public boolean isDestinationTemporary() {
089        return false;
090    }
091
092    /**
093     * @return name of the durable consumer
094     */
095    @Override
096    public String getSubscriptionName() {
097        return subscriptionInfo.getSubscriptionName();
098    }
099
100    /**
101     * @return true if the subscriber is active
102     */
103    @Override
104    public boolean isActive() {
105        return false;
106    }
107
108    @Override
109    protected ConsumerInfo getConsumerInfo() {
110        // when inactive, consumer info is stale
111        return null;
112    }
113
114    /**
115     * Browse messages for this durable subscriber
116     *
117     * @return messages
118     * @throws OpenDataException
119     */
120    @Override
121    public CompositeData[] browse() throws OpenDataException {
122        return broker.browse(this);
123    }
124
125    /**
126     * Browse messages for this durable subscriber
127     *
128     * @return messages
129     * @throws OpenDataException
130     */
131    @Override
132    public TabularData browseAsTable() throws OpenDataException {
133        return broker.browseAsTable(this);
134    }
135
136    /**
137     * Destroys the durable subscription so that messages will no longer be
138     * stored for this subscription
139     */
140    @Override
141    public void destroy() throws Exception {
142        RemoveSubscriptionInfo info = new RemoveSubscriptionInfo();
143        info.setClientId(clientId);
144        info.setSubscriptionName(subscriptionInfo.getSubscriptionName());
145        ConnectionContext context = new ConnectionContext();
146        context.setBroker(broker);
147        context.setClientId(clientId);
148        brokerService.getBroker().removeSubscription(context, info);
149    }
150
151    @Override
152    public String toString() {
153        return "InactiveDurableSubscriptionView: " + getClientId() + ":" + getSubscriptionName();
154    }
155
156    @Override
157    public String getSelector() {
158        return subscriptionInfo.getSelector();
159    }
160
161    @Override
162    public void removeMessage(@MBeanInfo("messageId") String messageId) throws Exception {
163        broker.remove(this, messageId);
164    }
165
166}