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.geronimo.monitoring.snapshot;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.Set;
023    
024    import javax.management.MBeanServer;
025    import javax.management.ObjectName;
026    
027    import org.apache.geronimo.monitoring.MasterRemoteControlJMX;
028    
029    /**
030     * Thread that is in charge of executing every x milliseconds. Upon each
031     * iteration, a snapshot of the server's information is recorded.
032     */
033    public class SnapshotThread extends Thread {
034    
035        private long SNAPSHOT_DURATION;
036        private MBeanServer mbServer = null;
037        int threadStatus = 1;
038        // list of mbean names that we will be taking snapshots of
039        private ArrayList<String> mbeanNames;
040    
041        public SnapshotThread(long snapshot_length, MBeanServer mbServer) {
042            SNAPSHOT_DURATION = snapshot_length;
043            this.mbServer = mbServer;
044            mbeanNames = new ArrayList<String>();
045        }
046    
047        /**
048         * Gets the elapsed time in milliseconds between each snapshot.
049         * 
050         * @return long
051         */
052        public long getSnapshotDuration() {
053            return SNAPSHOT_DURATION;
054        }
055    
056        public Integer SnapshotStatus() {
057            return threadStatus;
058    
059        }
060    
061        /**
062         * Adds the mbean name to list in memory. To update the snapshot-config.xml
063         * coder must use SnapshotConfigXMLBuilder class.
064         * 
065         * @param mbeanName
066         */
067        public void addMBeanForSnapshot(String mbeanName) {
068            mbeanNames.add(mbeanName);
069        }
070    
071        /**
072         * Removes the mbean name to list in memory. To update the
073         * snapshot-config.xml coder must use SnapshotConfigXMLBuilder class.
074         * 
075         * @param mbeanName
076         */
077        public void removeMBeanForSnapshot(String mbeanName) {
078            mbeanNames.remove(mbeanName);
079        }
080    
081        /**
082         * Sets the elapsed time in milliseconds between each snapshot.
083         * 
084         * @param snapshotDuration
085         */
086        public void setSnapshotDuration(long snapshot_length) {
087            SNAPSHOT_DURATION = snapshot_length;
088            if (snapshot_length == Long.MAX_VALUE)
089                threadStatus = -1;
090        }
091    
092        public void run() {
093            // get any saved mbean names from snapshot-config.xml
094            mbeanNames = SnapshotConfigXMLBuilder.getMBeanNames();
095            // in the case where nothing is present, grab a set of default mbeans
096            if (mbeanNames.size() <= 0) {
097                mbeanNames = getDefaultMBeanList();
098            }
099            // pause the thread from running every SNAPSHOT_DURATION seconds
100            while (true && SNAPSHOT_DURATION != Long.MAX_VALUE) {
101                try {
102                    // store the data
103                    SnapshotProcessor.takeSnapshot();
104                    // wait for next snapshot
105                    Thread.sleep(SNAPSHOT_DURATION);
106                } catch (Exception e) {
107                    e.printStackTrace();
108                }
109            }
110            // flag turned on to know when the thread stops
111            threadStatus = 0;
112        }
113    
114        /**
115         * @return A list of all default mbeans; namely, all connector or container
116         *         mbean names Prereq: in order to be a connector or container mbean
117         *         the name must contain "Connector"/"Container" and
118         *         "Tomcat"/"Jetty"
119         */
120        private ArrayList<String> getDefaultMBeanList() {
121            Set<String> mbeans = (new MasterRemoteControlJMX())
122                    .getStatisticsProviderMBeanNames();
123            ArrayList<String> retval = new ArrayList<String>();
124            for (Iterator it = mbeans.iterator(); it.hasNext();) {
125                String name = (String) it.next();
126                if ((name.contains("Connector") || name.contains("Container"))
127                        && (name.contains("Jetty") || name.contains("Tomcat"))) {
128                    // this is a connector, so add to the list
129                    retval.add(name);
130                    // update the snapshot-config.xml to include these
131                    SnapshotConfigXMLBuilder.addMBeanName(name);
132                }
133            }
134            return retval;
135        }
136    }