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    public boolean isDispatchAsync() {
128        if (info != null) {
129            return info.isDispatchAsync();
130        }
131        return false;
132    }
133
134    /**
135     * @return pretty print
136     */
137    public String toString() {
138        return "ProducerView: " + getClientId() + ":" + getConnectionId();
139    }
140
141    /**
142     * Set the last used Destination name for a Dynamic Destination Producer.
143     */
144    void setLastUsedDestinationName(ActiveMQDestination destinationName) {
145        this.lastUsedDestination = destinationName;
146    }
147
148    @Override
149    public String getUserName() {
150        return userName;
151    }
152
153    @Override
154    public boolean isProducerBlocked() {
155        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
156        if (producerBrokerExchange != null){
157            return producerBrokerExchange.isBlockedForFlowControl();
158        }
159        return false;
160    }
161
162    @Override
163    public long getTotalTimeBlocked() {
164        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
165        if (producerBrokerExchange != null){
166            return producerBrokerExchange.getTotalTimeBlocked();
167        }
168        return 0;
169    }
170
171    @Override
172    public int getPercentageBlocked() {
173        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
174        if (producerBrokerExchange != null){
175            return producerBrokerExchange.getPercentageBlocked();
176        }
177        return 0;
178    }
179
180    @Override
181    public void resetFlowControlStats() {
182        ProducerBrokerExchange producerBrokerExchange = broker.getBrokerService().getProducerBrokerExchange(info);
183        if (producerBrokerExchange != null){
184            producerBrokerExchange.resetFlowControl();
185        }
186    }
187
188    @Override
189    public void resetStatistics() {
190       if (info != null){
191           info.resetSentCount();
192       }
193    }
194
195    @Override
196    public long getSentCount() {
197        return info != null ? info.getSentCount() :0;
198    }
199}