KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.diagnostics.collect;
24
25 import com.sun.enterprise.diagnostics.*;
26 import com.sun.enterprise.diagnostics.util.DiagnosticServiceHelper;
27 import com.sun.logging.LogDomains;
28
29 import java.util.List JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FilenameFilter JavaDoc;
36 import java.util.Arrays JavaDoc;
37
38 /**
39  * Responsible for configuring various collectors based on diagnostic service
40  * configuration and colleting diagnostic information for the entire report.
41  *
42  * @author Manisha Umbarje
43  */

44 public abstract class Harvester implements Collector {
45
46     protected ReportConfig config;
47     protected ReportTarget target;
48     private List JavaDoc<Collector> collectors;
49     private ChecksumCollector checksumCollector;
50    
51     private SystemInfoCollector systemInfoCollector ;
52     private FilesCollector installationLogCollector;
53     protected static Logger JavaDoc logger =
54             LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
55
56     /**
57      * Creates instance of Harvester
58      * @param config combined representation of CLIOptions and ReportTarget
59      */

60     public Harvester(ReportConfig config) {
61         if (config != null) {
62             this.config = config;
63             this.target = config.getTarget();
64         }
65     }
66
67     /**
68      * Invoke various collectors to capture diagnostic data
69      */

70     public Data capture() {
71         WritableDataImpl dataImpl = new WritableDataImpl();
72         Iterator JavaDoc<Collector> list = collectors.iterator();
73         while(list.hasNext()) {
74             Data dataObj = capture(list.next());
75             if(dataObj != null)
76                 dataImpl.addChild(dataObj);
77         }
78         return dataImpl;
79     }
80
81     public void addCollector(Collector obj) {
82         if(obj != null)
83             collectors.add(obj);
84     }
85
86     public abstract void addRemoteCollectors() ;
87
88     /**
89      * Initialize collectors
90      */

91     public void initialize() {
92         collectors = new ArrayList JavaDoc(8);
93         addCustomerInputCollector();
94         //addInstallationCheckSumCollector();
95
initializeInstanceCollectors(config.getInstanceConfigurations());
96         addRemoteCollectors();
97         //addSystemInfoCollector();
98
}
99
100     /**
101      * Initialize instance collectors
102      */

103     protected void initializeInstanceCollectors(
104             Iterator JavaDoc<ServiceConfig> iterator) {
105         while(iterator.hasNext()) {
106             ServiceConfig serviceConfig = iterator.next();
107             String JavaDoc repositoryDir = serviceConfig.getRepositoryDir() ;
108             String JavaDoc reportDir = target.getIntermediateReportDir();
109
110             if(!(serviceConfig.getInstanceName().equals(Constants.SERVER)))
111                 reportDir = reportDir+ File.separator +
112                     serviceConfig.getInstanceName();
113
114             if(serviceConfig.isCaptureChecksumEnabled()) {
115                 addInstallationCheckSumCollector();
116                 ChecksumCollector collector = new ChecksumCollector(
117                         repositoryDir,
118                         reportDir);
119                 addCollector(collector);
120             }
121
122             if(serviceConfig.isVerifyDomainEnabled()) {
123                 DomainXMLVerificationCollector domainXMLCollector
124                          = new DomainXMLVerificationCollector(repositoryDir,
125                          reportDir);
126                  addCollector(domainXMLCollector);
127             }
128
129             addCollector(new ConfigCollector(repositoryDir, reportDir));
130             //If minLogLevel != OFF, add collector
131
if(!(serviceConfig.getMinLogLevel() == Level.OFF.intValue())) {
132                 Collector collectorObj = new LogCollector(
133                         config.getCLIOptions().getStartDate(),
134                         config.getCLIOptions().getEndDate(),
135                         reportDir, serviceConfig );
136                 if (collectorObj != null)
137                     addCollector(collectorObj);
138             }
139
140             if(serviceConfig.isCaptureAppDDEnabled()) {
141                 addCollector(new AppInfoCollector(repositoryDir, reportDir));
142             }
143
144             addFilesCollector(serviceConfig.isCaptureInstallLogEnabled(),
145                     DataType.INSTALLATION_LOG);
146             addSystemInfoCollector(serviceConfig.isCaptureSystemInfoEnabled());
147         }
148     }
149
150     protected void addFilesCollector(boolean captureInstallationLog, String JavaDoc dataType) {
151         if(captureInstallationLog) {
152             if(installationLogCollector == null) {
153                 String JavaDoc installationRoot = DiagnosticServiceHelper.getInstallationRoot();
154                 List JavaDoc<String JavaDoc> files = new ArrayList JavaDoc(2);
155                 String JavaDoc[] logFiles =
156                         new File JavaDoc(installationRoot).list(new FilenameFilter JavaDoc() {
157                    public boolean accept(File JavaDoc folder, String JavaDoc name) {
158                        if( (name.contains(Constants.INSTALLATION_LOG_PREFIX)) ||
159                                (name.contains(Constants.SJSAS_INSTALLATION_LOG_PREFIX)))
160                            return true;
161                        return false;
162                    }
163                 });
164                 files.addAll(Arrays.asList(logFiles));
165
166                 installationLogCollector = new FilesCollector(installationRoot,
167                     target.getIntermediateReportDir(),files, dataType);
168                 addCollector(installationLogCollector);
169             }
170         }
171     }
172
173     protected void addCustomerInputCollector() {
174         if (config.getCLIOptions().getCustomerInput() != null ||
175                 config.getCLIOptions().getCustomerInputFile() != null) {
176             addCollector(new CustomerInputCollector(
177                     config.getCLIOptions().getCustomerInputFile(),
178                     config.getCLIOptions().getCustomerInput(),
179                     target.getIntermediateReportDir(),
180                     config.getExecutionContext().isLocal()));
181         }
182     }
183
184     protected void addInstallationCheckSumCollector() {
185         if(checksumCollector == null) {
186             checksumCollector = new ChecksumCollector(
187                             DiagnosticServiceHelper.getInstallationRoot(),
188                             target.getIntermediateReportDir());
189             addCollector(checksumCollector);
190         }
191     }
192
193     protected void addMonitoringInfoCollectors(String JavaDoc path, List JavaDoc<String JavaDoc> instances) {
194         String JavaDoc reportDir = target.getIntermediateReportDir();
195         Iterator JavaDoc<String JavaDoc> iterator = instances.iterator();
196
197         while (iterator.hasNext()) {
198             String JavaDoc instanceName = iterator.next();
199             addMonitoringInfoCollector(reportDir, path,instanceName);
200         }
201     }
202
203     protected void addMonitoringInfoCollector(String JavaDoc reportDir,
204             String JavaDoc path, String JavaDoc instanceName) {
205         if(path == null)
206             path = "";
207         reportDir= reportDir + File.separator + path + File.separator +
208                 instanceName;
209         addCollector(new MonitoringInfoCollector(path, instanceName, reportDir));
210     }
211
212     protected void addSystemInfoCollector(boolean captureSystemInfoEnabled){
213         if(captureSystemInfoEnabled) {
214             String JavaDoc reportDir = config.getTarget().getIntermediateReportDir();
215             if(systemInfoCollector == null) {
216                 systemInfoCollector = new SystemInfoCollector(reportDir);
217                 //Add the collector only once.
218
addCollector(systemInfoCollector);
219             }
220         }
221     }
222
223     /**
224      * Invoke collector to capture information
225      * @param obj collector
226      */

227     private Data capture(Collector obj) {
228         try {
229              return obj.capture();
230         } catch (Throwable JavaDoc de) {
231             logger.log(Level.WARNING,
232                     "diagnostic-service.error_collecting_data" ,
233                     new Object JavaDoc[]{de.getMessage()});
234         }
235         return null;
236     }
237 }
238
Popular Tags