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     */
017    package org.apache.activemq.management;
018    
019    import java.util.List;
020    
021    import org.apache.activemq.ActiveMQSession;
022    import org.apache.activemq.util.IndentPrinter;
023    
024    /**
025     * Statistics for a JMS connection
026     * 
027     * 
028     */
029    public class JMSConnectionStatsImpl extends StatsImpl {
030        private List sessions;
031        private boolean transactional;
032    
033        public JMSConnectionStatsImpl(List sessions, boolean transactional) {
034            this.sessions = sessions;
035            this.transactional = transactional;
036        }
037    
038        public JMSSessionStatsImpl[] getSessions() {
039            // lets make a snapshot before we process them
040            Object[] sessionArray = sessions.toArray();
041            int size = sessionArray.length;
042            JMSSessionStatsImpl[] answer = new JMSSessionStatsImpl[size];
043            for (int i = 0; i < size; i++) {
044                ActiveMQSession session = (ActiveMQSession)sessionArray[i];
045                answer[i] = session.getSessionStats();
046            }
047            return answer;
048        }
049    
050        public void reset() {
051            super.reset();
052            JMSSessionStatsImpl[] stats = getSessions();
053            int size = stats.length;
054            for (int i = 0; i < size; i++) {
055                stats[i].reset();
056            }
057        }
058    
059        /**
060         * @param enabled the enabled to set
061         */
062        public void setEnabled(boolean enabled) {
063            super.setEnabled(enabled);
064            JMSSessionStatsImpl[] stats = getSessions();
065            int size = stats.length;
066            for (int i = 0; i < size; i++) {
067                stats[i].setEnabled(enabled);
068            }
069    
070        }
071    
072        public boolean isTransactional() {
073            return transactional;
074        }
075    
076        public String toString() {
077            StringBuffer buffer = new StringBuffer("connection{ ");
078            JMSSessionStatsImpl[] array = getSessions();
079            for (int i = 0; i < array.length; i++) {
080                if (i > 0) {
081                    buffer.append(", ");
082                }
083                buffer.append(Integer.toString(i));
084                buffer.append(" = ");
085                buffer.append(array[i]);
086            }
087            buffer.append(" }");
088            return buffer.toString();
089        }
090    
091        public void dump(IndentPrinter out) {
092            out.printIndent();
093            out.println("connection {");
094            out.incrementIndent();
095            JMSSessionStatsImpl[] array = getSessions();
096            for (int i = 0; i < array.length; i++) {
097                JMSSessionStatsImpl sessionStat = (JMSSessionStatsImpl)array[i];
098                out.printIndent();
099                out.println("session {");
100                out.incrementIndent();
101                sessionStat.dump(out);
102                out.decrementIndent();
103                out.printIndent();
104                out.println("}");
105            }
106            out.decrementIndent();
107            out.printIndent();
108            out.println("}");
109            out.flush();
110        }
111    }