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