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.management;
018
019import java.util.List;
020import java.util.concurrent.CopyOnWriteArrayList;
021
022import org.apache.activemq.ActiveMQConnection;
023import org.apache.activemq.util.IndentPrinter;
024
025/**
026 * Statistics for a number of JMS connections
027 * 
028 * 
029 */
030public class JMSStatsImpl extends StatsImpl {
031    private List<ActiveMQConnection> connections = new CopyOnWriteArrayList<ActiveMQConnection>();
032
033    public JMSStatsImpl() {
034    }
035
036    public JMSConnectionStatsImpl[] getConnections() {
037        Object[] connectionArray = connections.toArray();
038        int size = connectionArray.length;
039        JMSConnectionStatsImpl[] answer = new JMSConnectionStatsImpl[size];
040        for (int i = 0; i < size; i++) {
041            ActiveMQConnection connection = (ActiveMQConnection)connectionArray[i];
042            answer[i] = connection.getConnectionStats();
043        }
044        return answer;
045    }
046
047    public void addConnection(ActiveMQConnection connection) {
048        connections.add(connection);
049    }
050
051    public void removeConnection(ActiveMQConnection connection) {
052        connections.remove(connection);
053    }
054
055    public void dump(IndentPrinter out) {
056        out.printIndent();
057        out.println("factory {");
058        out.incrementIndent();
059        JMSConnectionStatsImpl[] array = getConnections();
060        for (int i = 0; i < array.length; i++) {
061            JMSConnectionStatsImpl connectionStat = (JMSConnectionStatsImpl)array[i];
062            connectionStat.dump(out);
063        }
064        out.decrementIndent();
065        out.printIndent();
066        out.println("}");
067        out.flush();
068    }
069
070    /**
071     * @param enabled the enabled to set
072     */
073    public void setEnabled(boolean enabled) {
074        super.setEnabled(enabled);
075        JMSConnectionStatsImpl[] stats = getConnections();
076        int size = stats.length;
077        for (int i = 0; i < size; i++) {
078            stats[i].setEnabled(enabled);
079        }
080
081    }
082}