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 org.apache.activemq.broker.ProducerBrokerExchange;
020import org.apache.activemq.command.ActiveMQDestination;
021import org.apache.activemq.command.ProducerInfo;
022
023public class ProducerView implements ProducerViewMBean {
024
025    protected final ProducerInfo info;
026    protected final String clientId;
027    protected final String userName;
028    protected final ManagedRegionBroker broker;
029
030    protected ActiveMQDestination lastUsedDestination;
031
032    public ProducerView(ProducerInfo info, String clientId, String userName, ManagedRegionBroker broker) {
033        this.info = info;
034        this.clientId = clientId;
035        this.userName = userName;
036        this.broker = broker;
037    }
038
039    @Override
040    public String getClientId() {
041        return this.clientId;
042    }
043
044    @Override
045    public String getConnectionId() {
046        if (info != null) {
047            return info.getProducerId().getConnectionId();
048        }
049        return "NOTSET";
050    }
051
052    @Override
053    public long getSessionId() {
054        if (info != null) {
055            return info.getProducerId().getSessionId();
056        }
057        return 0;
058    }
059
060    @Override
061    public String getProducerId() {
062        if (info != null) {
063            return info.getProducerId().toString();
064        }
065        return "NOTSET";
066    }
067
068    @Override
069    public String getDestinationName() {
070        if (info != null && info.getDestination() != null) {
071            ActiveMQDestination dest = info.getDestination();
072            return dest.getPhysicalName();
073        } else if (this.lastUsedDestination != null) {
074            return this.lastUsedDestination.getPhysicalName();
075        }
076        return "NOTSET";
077    }
078
079    @Override
080    public boolean isDestinationQueue() {
081        if (info != null) {
082            if (info.getDestination() != null) {
083                ActiveMQDestination dest = info.getDestination();
084                return dest.isQueue();
085            } else if(lastUsedDestination != null) {
086                return lastUsedDestination.isQueue();
087            }
088        }
089        return false;
090    }
091
092    @Override
093    public boolean isDestinationTopic() {
094        if (info != null) {
095            if (info.getDestination() != null) {
096                ActiveMQDestination dest = info.getDestination();
097                return dest.isTopic();
098            } else if(lastUsedDestination != null) {
099                return lastUsedDestination.isTopic();
100            }
101        }
102        return false;
103    }
104
105    @Override
106    public boolean isDestinationTemporary() {
107        if (info != null) {
108            if (info.getDestination() != null) {
109                ActiveMQDestination dest = info.getDestination();
110                return dest.isTemporary();
111            } else if(lastUsedDestination != null) {
112                return lastUsedDestination.isTemporary();
113            }
114        }
115        return false;
116    }
117
118    @Override
119    public int getProducerWindowSize() {
120        if (info != null) {
121            return info.getWindowSize();
122        }
123        return 0;
124    }
125
126    @Override
127    @Deprecated
128    public boolean isDispatchAsync() {
129        return false;
130    }
131
132    /**
133     * @return pretty print
134     */
135    @Override
136    public String toString() {
137        return "ProducerView: " + getClientId() + ":" + getConnectionId();
138    }
139
140    /**
141     * Set the last used Destination name for a Dynamic Destination Producer.
142     */
143    void setLastUsedDestinationName(ActiveMQDestination destinationName) {
144        this.lastUsedDestination = destinationName;
145    }
146
147    @Override
148    public String getUserName() {
149        return userName;
150    }
151
152    @Override
153    public boolean isProducerBlocked() {
154        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
155        if (producerBrokerExchange != null){
156            return producerBrokerExchange.isBlockedForFlowControl();
157        }
158        return false;
159    }
160
161    @Override
162    public long getTotalTimeBlocked() {
163        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
164        if (producerBrokerExchange != null){
165            return producerBrokerExchange.getTotalTimeBlocked();
166        }
167        return 0;
168    }
169
170    @Override
171    public int getPercentageBlocked() {
172        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
173        if (producerBrokerExchange != null){
174            return producerBrokerExchange.getPercentageBlocked();
175        }
176        return 0;
177    }
178
179    @Override
180    public void resetFlowControlStats() {
181        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
182        if (producerBrokerExchange != null){
183            producerBrokerExchange.resetFlowControl();
184        }
185    }
186
187    @Override
188    public void resetStatistics() {
189       if (info != null){
190           info.resetSentCount();
191       }
192    }
193
194    @Override
195    public long getSentCount() {
196        return info != null ? info.getSentCount() :0;
197    }
198}