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 java.io.IOException;
020import java.util.Set;
021
022import javax.management.ObjectName;
023
024import org.apache.activemq.broker.Connection;
025import org.apache.activemq.util.IOExceptionSupport;
026
027public class ConnectionView implements ConnectionViewMBean {
028
029    private final Connection connection;
030    private final ManagementContext managementContext;
031    private String userName;
032
033    public ConnectionView(Connection connection) {
034        this(connection, null);
035    }
036
037    public ConnectionView(Connection connection, ManagementContext managementContext) {
038        this.connection = connection;
039        this.managementContext = managementContext;
040    }
041
042    @Override
043    public void start() throws Exception {
044        connection.start();
045    }
046
047    @Override
048    public void stop() throws Exception {
049        connection.stop();
050    }
051
052    /**
053     * @return true if the Connection is slow
054     */
055    @Override
056    public boolean isSlow() {
057        return connection.isSlow();
058    }
059
060    /**
061     * @return if after being marked, the Connection is still writing
062     */
063    @Override
064    public boolean isBlocked() {
065        return connection.isBlocked();
066    }
067
068    /**
069     * @return true if the Connection is connected
070     */
071    @Override
072    public boolean isConnected() {
073        return connection.isConnected();
074    }
075
076    /**
077     * @return true if the Connection is active
078     */
079    @Override
080    public boolean isActive() {
081        return connection.isActive();
082    }
083
084    @Override
085    public int getDispatchQueueSize() {
086        return connection.getDispatchQueueSize();
087    }
088
089    /**
090     * Resets the statistics
091     */
092    @Override
093    public void resetStatistics() {
094        connection.getStatistics().reset();
095    }
096
097    @Override
098    public String getRemoteAddress() {
099        return connection.getRemoteAddress();
100    }
101
102    @Override
103    public String getClientId() {
104        return connection.getConnectionId();
105    }
106
107    public String getConnectionId() {
108        return connection.getConnectionId();
109    }
110
111    @Override
112    public String getUserName() {
113        return userName;
114    }
115
116    public void setUserName(String userName) {
117        this.userName = userName;
118    }
119
120    @Override
121    public ObjectName[] getConsumers() {
122        ObjectName[] result = null;
123
124        if (connection != null && managementContext != null) {
125
126            try {
127                ObjectName query = createConsumerQueury(connection.getConnectionId());
128                Set<ObjectName> names = managementContext.queryNames(query, null);
129                result = names.toArray(new ObjectName[0]);
130            } catch (Exception e) {
131            }
132        }
133
134        return result;
135    }
136
137    @Override
138    public ObjectName[] getProducers() {
139        ObjectName[] result = null;
140
141        if (connection != null && managementContext != null) {
142
143            try {
144                ObjectName query = createProducerQueury(connection.getConnectionId());
145                Set<ObjectName> names = managementContext.queryNames(query, null);
146                result = names.toArray(new ObjectName[0]);
147            } catch (Exception e) {
148            }
149        }
150
151        return result;
152    }
153
154    private ObjectName createConsumerQueury(String clientId) throws IOException {
155        try {
156            return BrokerMBeanSupport.createConsumerQueury(managementContext.getJmxDomainName(), clientId);
157        } catch (Throwable e) {
158            throw IOExceptionSupport.create(e);
159        }
160    }
161
162    private ObjectName createProducerQueury(String clientId) throws IOException {
163        try {
164            return BrokerMBeanSupport.createProducerQueury(managementContext.getJmxDomainName(), clientId);
165        } catch (Throwable e) {
166            throw IOExceptionSupport.create(e);
167        }
168    }
169
170    @Override
171    public int getActiveTransactionCount() {
172        return connection.getActiveTransactionCount();
173    }
174
175    @Override
176    public Long getOldestActiveTransactionDuration() {
177        return connection.getOldestActiveTransactionDuration();
178    }
179}