KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > collect > MonitoringInfoCollector


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 package com.sun.enterprise.diagnostics.collect;
25
26 import com.sun.enterprise.diagnostics.Data;
27 import com.sun.enterprise.diagnostics.DiagnosticException;
28 import com.sun.enterprise.diagnostics.Defaults;
29 import com.sun.enterprise.diagnostics.Constants;
30 import com.sun.enterprise.admin.server.core.jmx.AppServerMBeanServerFactory;
31 import com.sun.logging.LogDomains;
32
33
34 import javax.management.*;
35 import java.util.*;
36 import java.util.logging.Logger JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.io.*;
39
40 /**
41  * To collect monitoring information of application server components,
42  * applications deployed, jvm etc.,
43  *
44  * @author Jagadish Ramu
45  */

46 public class MonitoringInfoCollector extends InterruptableCollector {
47
48     Set<String JavaDoc> restrictedProperties = null; // Properties that need
49
// to be discarded
50

51     public static final String JavaDoc DOTTED_NAME_REGISTRY_OPERATION_NAME =
52             "dottedNameToObjectName";
53     public static final String JavaDoc KEY_NOT_FOUND = "key not found";
54     public static final String JavaDoc LIST_COMMAND = "list";
55     public static final String JavaDoc GET_COMMAND = "get";
56     public static final String JavaDoc MONITOR_OPTION = "monitor";
57
58     private String JavaDoc instanceName;
59     private String JavaDoc nodeAgentName;
60
61     private PrintStream out = System.out;
62
63     private String JavaDoc fileName;
64     private String JavaDoc destFolder;
65
66     private static Logger JavaDoc logger =
67             LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
68
69     /**
70      * Creates a new instance of MonitoringInfoCollector
71      *
72      * @param instanceName - name of the instacne
73      * @param destFolder - Destination folder in which generated
74      * report is stored
75      */

76     public MonitoringInfoCollector(String JavaDoc nodeAgentName, String JavaDoc instanceName, String JavaDoc destFolder) {
77         if (instanceName != null) {
78             this.instanceName = instanceName;
79         }
80         if(nodeAgentName !=null){
81             this.nodeAgentName = nodeAgentName;
82         }
83         this.destFolder = destFolder;
84
85         initializeRestrictedPropertiesLookUp();
86     }
87
88     /**
89      * Initialize the list of properties that need to be ignored.
90      */

91     private void initializeRestrictedPropertiesLookUp() {
92         restrictedProperties = new HashSet<String JavaDoc>();
93         restrictedProperties.add("-description");
94         restrictedProperties.add("-name");
95         restrictedProperties.add("-lowerbound");
96         restrictedProperties.add("-starttime");
97         restrictedProperties.add("-upperbound");
98     }
99
100     /**
101      * To check whether a particular property is in restricted list
102      *
103      * @param dottedName - property name
104      * @return boolean
105      */

106     private boolean isRestircted(String JavaDoc dottedName) {
107         boolean restricted = false;
108
109         if (dottedName != null) {
110             int index = dottedName.lastIndexOf("-");
111             if (index >= 0) {
112                 String JavaDoc property = dottedName.substring(index);
113                 if (restrictedProperties != null) {
114                     restricted = restrictedProperties.contains(property);
115                 }
116             }
117         }
118         return restricted;
119     }
120
121     /**
122      * Capture information
123      *
124      * @ throw DiagnosticException
125      */

126     public Data capture() throws DiagnosticException {
127         FileData data = null;
128
129         if (destFolder != null) {
130             File destFolderObj = new File(destFolder);
131             fileName = destFolder + File.separator +
132                     Defaults.MONITORING_INFO_FILE;
133
134             if (!destFolderObj.exists()) {
135                 destFolderObj.mkdirs();
136             }
137
138             try {
139                 out = new PrintStream(
140                         new BufferedOutputStream(
141                                 new FileOutputStream(fileName)), true);
142
143
144                     File reportFile = new File(fileName);
145                     if(this.instanceName.equalsIgnoreCase(Constants.SERVER)) {
146                         data = new FileData( reportFile.getName(), DataType.MONITORING_INFO);
147                     }else{
148                         data = new FileData(nodeAgentName + File.separator + instanceName + File.separator + reportFile.getName(), DataType.MONITORING_INFO);
149                     }
150
151                 ArrayList<String JavaDoc> cmdOutput = new ArrayList<String JavaDoc>();
152                 MonitoringInfoHelper cmd = new MonitoringInfoHelper();
153                 cmd.setName(LIST_COMMAND);
154
155                 ArrayList<String JavaDoc> dottedNames = new ArrayList<String JavaDoc>();
156                 if (instanceName != null) {
157                     dottedNames.add(instanceName.trim() + ".*");
158
159                     cmd.setOperands(dottedNames);
160
161                     cmd.setOption(MonitoringInfoHelper.SECURE, "true");
162                     cmd.setOption(MONITOR_OPTION, "true");
163
164                     cmd.runCommand(cmdOutput);
165
166                     if (checkInterrupted()) {
167                         logger.log(Level.WARNING, "diagnostic-service." +
168                                 "monitoring_info_collector_timeout",
169                                 new Object JavaDoc[]{Thread.currentThread().getName(),
170                                         this.getClass().getName()});
171
172                         if(out!=null){
173                             out.print("Monitoring Info Collector Timeout");
174                             out.close();
175                         }
176                         return data;
177                     }
178
179                     ArrayList<String JavaDoc> list = getIndividualProperties(cmdOutput);
180
181                     MBeanServer mbs = AppServerMBeanServerFactory.
182                             getMBeanServerInstance();
183
184                     final String JavaDoc[] types = new String JavaDoc[]{String JavaDoc.class.getName()};
185
186                     for (String JavaDoc value : list) {
187
188                         if (checkInterrupted()) {
189                             logger.log(Level.WARNING, "diagnostic-service." +
190                                     "monitoring_info_collector_timeout",
191                                     new Object JavaDoc[]{Thread.currentThread().
192                                             getName(), this.getClass().
193                                             getName()});
194
195                             if(out!=null){
196                                 out.print("Monitoring Info Collector Timeout");
197                                 out.close();
198                             }
199                             return data;
200                         }
201
202                         Object JavaDoc[] params = new Object JavaDoc[]{value};
203                         ObjectName dottedNameRegistry = (ObjectName) mbs.invoke(
204                                 new ObjectName("com.sun.appserv:name=" +
205                                         "dotted-name-monitoring-registry," +
206                                         "type=dotted-name-support"),
207                                 DOTTED_NAME_REGISTRY_OPERATION_NAME, params,
208                                 types);
209
210                         Set set = getAllAttributeNames(mbs, dottedNameRegistry);
211
212                         Iterator attributesIterator = set.iterator();
213
214                         ArrayList<String JavaDoc> properties = new ArrayList<String JavaDoc>();
215                         while (attributesIterator.hasNext()) {
216                             String JavaDoc attr = (String JavaDoc) attributesIterator.next();
217                             if (!isRestircted(attr)) {
218                                 properties.add(value + "." + attr);
219                             }
220                         }
221
222                         if (properties.size() > 0) {
223                             cmd.setName(GET_COMMAND);
224                             cmd.setOperands(properties);
225
226                             ArrayList<String JavaDoc> result = new ArrayList<String JavaDoc>();
227                             cmd.runCommand(result);
228
229                             for (String JavaDoc attributeValue : result) {
230                                 if (!(attributeValue.toLowerCase().
231                                         indexOf(KEY_NOT_FOUND) >= 0)) {
232
233                                     out.println(attributeValue);
234
235                                 }
236                             }
237                         }
238                         if (checkInterrupted()) {
239                             logger.log(Level.WARNING, "diagnostic-service." +
240                                     "monitoring_info_collector_timeout",
241                                     new Object JavaDoc[]{Thread.currentThread().
242                                             getName(),
243                                             this.getClass().getName()});
244                             if(out!=null){
245                                 out.print("Monitoring Info Collector Timeout");
246                                 out.close();
247                             }
248                             return data;
249                         }
250                     }
251                     out.close();
252                 }
253                 return data;
254             }
255             catch (FileNotFoundException fnfe) {
256                 logger.log(Level.WARNING, "File Not Found exception occurred " +
257                         "while collecting Monitoring information", fnfe);
258             }
259             catch (IOException ioe) {
260                 logger.log(Level.WARNING, "IO Exception occurred while " +
261                         "collecting Monitoring information", ioe);
262             }
263             catch (Exception JavaDoc e) {
264                 logger.log(Level.WARNING, "Exception occurred while collecting"+
265                         " Monitoring information", e);
266             }
267         }
268         return data;
269     }
270
271
272     /*
273         Return a Set of String of the names of all attributes within the MBean
274      */

275     public static Set<String JavaDoc>
276             getAllAttributeNames(final MBeanServer server,
277                                  final ObjectName objectName)
278             throws ReflectionException, InstanceNotFoundException,
279             IntrospectionException {
280         final Set<String JavaDoc> allNames = new HashSet<String JavaDoc>();
281
282         // add the Attribute names
283
final MBeanInfo info = server.getMBeanInfo(objectName);
284         final MBeanAttributeInfo[] attrsInfo = info.getAttributes();
285         if (attrsInfo != null) {
286             for (MBeanAttributeInfo aAttrsInfo : attrsInfo) {
287                 allNames.add(aAttrsInfo.getName());
288             }
289         }
290         return (allNames);
291     }
292
293     /**
294      * To remove the non-properties.<br>
295      * eg: Ignores, Server.transaction-service and accepts server.
296      * transaction-service.commitedcount
297      *
298      * @param list representing the monitorable properties
299      * @return List representing the unique properties
300      * @throws IOException
301      */

302     public ArrayList<String JavaDoc> getIndividualProperties(ArrayList<String JavaDoc> list)
303             throws IOException {
304
305         ArrayList<String JavaDoc> modifiedList = new ArrayList<String JavaDoc>();
306
307         String JavaDoc current = null;
308
309         Collections.reverse(list);
310         for (String JavaDoc next : list) {
311             if (current != null) {
312                 if (current.indexOf(next) != 0) {
313                     modifiedList.add(next);
314                     current = next;
315                 }
316             } else {
317                 modifiedList.add(next);
318                 current = next;
319             }
320         }
321         return modifiedList;
322     }
323     /**
324      * To cleanup the resources before exiting
325      */

326     public void cleanUp() {
327         out.close();
328     }
329 }
330
Popular Tags