KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > MonitorTask


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * This task is put in a Timer thread and is executed every N seconds. This
26  * task prints in the output the monitorabled of the ORB.
27  * @author Darpan Dinker, $Author: tcfujii $
28  * @version $Revision: 1.3 $ on $Date: 2005/12/25 04:12:02 $
29  * @since jdk1.4
30  * Note: None of the methods in this class are synchronized, it is assumed
31  * that code which is using this class would take care of synchronization
32  * if there is a need. Most cases where initialization of these monitorables
33  * is done during server startup should not require any kind of
34  * synchronization
35  */

36
37 // START IASRI 4682740 - support standalone monitoring
38

39 package com.sun.enterprise.util;
40
41 import java.util.logging.Level JavaDoc;
42 import com.sun.enterprise.util.logging.Debug;
43 import com.sun.enterprise.corba.ee.internal.util.LogWrap;
44 import java.util.ArrayList JavaDoc;
45 import java.util.TimerTask JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 public class MonitorTask extends java.util.TimerTask JavaDoc {
49
50     private static boolean initialized = false;
51     private static boolean needORBMonitoring = false;
52     private static boolean needEJBMonitoring = false;
53     private static boolean needJDBCMonitoring = false;
54     private static ArrayList JavaDoc monitorableList = null;
55     private static long schedPeriod = 180000; // 3 minutes default
56
private static java.util.Timer JavaDoc timer;
57
58     /**
59      * This method will read the System properties and look for
60      * "MONITOR_ORB".
61      * "MONITOR_EJB".
62      * "MONITOR_JDBC".
63      * If the string exists, the
64      * monitoring task shall be created at every
65      * MONITOR_TIME_PERIOD_SECONDS
66      * if provided too, otherwise defaults will be taken for scheduling period.
67      */

68     private synchronized static boolean isMonitoring () {
69         if (!initialized) {
70             try {
71                 String JavaDoc str1=System.getProperties().getProperty("MONITOR_ORB");
72                 String JavaDoc str2=System.getProperties().getProperty("MONITOR_EJB");
73                 String JavaDoc str3=System.getProperties().getProperty("MONITOR_JDBC");
74                 String JavaDoc strm=System.getProperties().getProperty("MONITOR_TIME_PERIOD_SECONDS");
75                 if( null!=str1 ) {
76                     if ( str1.startsWith("true") || str1.startsWith("TRUE") ) {
77                         needORBMonitoring = true;
78                     }
79                 }
80                 if ( null!=str2 ) {
81                     if ( str2.startsWith("true") || str2.startsWith("TRUE") ) {
82                         needEJBMonitoring = true;
83                     }
84                 }
85                 if ( null!=str3 ) {
86                     if ( str3.startsWith("true") || str3.startsWith("TRUE") ) {
87                         needJDBCMonitoring = true;
88                     }
89                 }
90                 if (needORBMonitoring || needEJBMonitoring || needJDBCMonitoring) {
91                     if(null!=strm) {
92                         schedPeriod = 1000 * Long.parseLong(strm);
93                     }
94                 }
95             } catch(Exception JavaDoc e) {
96                 LogWrap.logger.log(Level.FINE,
97                                    "MINOR: Unable to start a performance monitoring task > " + e);
98             }
99             if (needORBMonitoring || needEJBMonitoring || needJDBCMonitoring) {
100                 monitorableList = new ArrayList JavaDoc();
101                 timer = new java.util.Timer JavaDoc();
102                 timer.schedule(new MonitorTask(), schedPeriod, schedPeriod);
103                 LogWrap.logger.log(Level.SEVERE,
104                                    "Starting the MonitorTask every "+schedPeriod+" milliseconds.");
105             }
106             initialized = true;
107         }
108         return (needORBMonitoring || needEJBMonitoring || needJDBCMonitoring);
109     }
110
111     /**
112      * Other subsystems in the ORB can use this method to add monitorable
113      * entities which are then dumped at the preset intervals.
114      * The only requirement for adding an object to the list is that it should
115      * implement the toString interface so that details about the object are
116      * dumped
117      */

118     public static void addORBMonitorable(Object JavaDoc monitorable) {
119         if (isMonitoring() && needORBMonitoring) {
120             monitorableList.add(monitorable);
121         }
122     }
123
124     public static void addEJBMonitorable(Object JavaDoc monitorable) {
125         if (isMonitoring() && needEJBMonitoring) {
126             monitorableList.add(monitorable);
127         }
128     }
129
130     public static void addJDBCMonitorable(Object JavaDoc monitorable) {
131         if (isMonitoring() && needJDBCMonitoring) {
132             monitorableList.add(monitorable);
133         }
134     }
135
136     public static ArrayList JavaDoc getMonitorableList() {
137         return monitorableList;
138     }
139
140     /**
141      * When an object implementing interface <code>Runnable</code> is used
142      * to create a thread, starting the thread causes the object's
143      * <code>run</code> method to be called in that separately executing
144      * thread.
145      * <p>
146      * The general contract of the method <code>run</code> is that it may
147      * take any action whatsoever.
148      *
149      * @see java.lang.Thread#run()
150      */

151     public void run() {
152         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
153         try {
154             boolean first = true;
155
156             sb.append("MONITORING : ");
157             Iterator JavaDoc iter = MonitorTask.getMonitorableList().iterator();
158             while (iter.hasNext()) {
159                 if (first == false) {
160                     sb.append(", ");
161                 } else {
162                     first = false;
163                 }
164                 sb.append(iter.next().toString());
165             }
166             LogWrap.logger.log(Level.SEVERE, sb.toString() );
167         } catch(Exception JavaDoc e) {
168             LogWrap.logger.log(Level.FINE, "MonitorTask received an exception > " + e);
169         }
170     }
171
172     protected java.util.Timer JavaDoc getTimer() {
173         return timer;
174     }
175
176 }
177
178 // End IASRI 4682740 - ORB to support standalone monitoring
179
Popular Tags