KickJava   Java API By Example, From Geeks To Geeks.

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


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
26
27 import com.sun.logging.LogDomains;
28 import com.sun.enterprise.diagnostics.DiagnosticException;
29 import com.sun.enterprise.diagnostics.Data;
30 import com.sun.enterprise.diagnostics.Defaults;
31 import com.sun.enterprise.diagnostics.util.DiagnosticServiceHelper;
32
33 import java.io.*;
34 import java.util.*;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import java.util.zip.CheckedInputStream JavaDoc;
38 import java.util.zip.CRC32 JavaDoc;
39
40 /**
41  * Collects check sum information for binaries
42  * @author Manisha Umbarje
43  */

44 public class ChecksumCollector implements Collector {
45     
46     private String JavaDoc source;
47     private String JavaDoc destFolder;
48     private String JavaDoc destFile;
49     private BufferedWriter writer;
50     private WritableData dataObj;
51     private static final String JavaDoc BIN_FOLDER = File.separator + "bin";
52     private static final String JavaDoc LIB_FOLDER = File.separator + "lib";
53     private static final String JavaDoc JAR_EXT = ".jar";
54     private static final String JavaDoc DLL_EXT = "*.dll";
55     private static final String JavaDoc SO_EXT = "*.so";
56     private static Logger JavaDoc logger =
57     LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
58     /**
59      * Creates a new instance of ChecksumCollector
60      * @param destFolder destination folder in which checksum information
61      * is copied.
62      */

63     public ChecksumCollector(String JavaDoc source, String JavaDoc destFolder) {
64         this.destFolder = destFolder;
65         this.source = source;
66         dataObj = new WritableDataImpl(source, DataType.CHECKSUM);
67     }
68     
69     /**
70      * Captures check sum information
71      * @throw DiagnosticException
72      */

73     public Data capture() throws DiagnosticException {
74         dataObj.addRow(Arrays.asList(new String JavaDoc[]{"Name", "Length", "Checksum"}));
75         captureChecksum(source + LIB_FOLDER, getFilter());
76         captureChecksum(source + BIN_FOLDER, null);
77         return dataObj;
78     }
79     
80     /**
81      * Captures checksum of files specified by the filter
82      * @param sourceName source folder from where files are copied
83      * @param filter file name filter
84      * @throw DiagnosticException
85      */

86     private void captureChecksum(String JavaDoc sourceName, FilenameFilter filter)
87         throws DiagnosticException {
88         
89         File fileObj = new File(sourceName);
90         String JavaDoc[] fileNames = fileObj.list(filter);
91         if(fileNames != null) {
92             int length = fileNames.length;
93             for (int i = 0 ; i < length; i++) {
94                 dataObj.addRow(generateCRC32Checksum(sourceName, fileNames[i]));
95             }
96         }
97
98     }
99     
100     /**
101      * Generates check sum
102      * @param file name of the file for which checkum is calculated
103      * @return list containing file name, length and checksum
104      */

105     private List<String JavaDoc> generateCRC32Checksum(String JavaDoc parent, String JavaDoc fileName)
106     throws DiagnosticException {
107         try {
108         // First item being name of file, second - length; third being checksum
109
String JavaDoc file = parent + File.separator + fileName;
110         List<String JavaDoc> checksumInfo = new ArrayList(3);
111         CRC32 JavaDoc crc32 = new CRC32 JavaDoc();
112         int length = 0;
113         BufferedInputStream fileinputstream = new BufferedInputStream(
114                 new FileInputStream(new File(file)));
115         for( CheckedInputStream JavaDoc checkedinputstream = new CheckedInputStream JavaDoc(
116         fileinputstream, crc32); checkedinputstream.read() != -1;)
117                 length++;
118         long cksum = crc32.getValue();
119         fileinputstream.close();
120         fileinputstream = null;
121         crc32 = null;
122         checksumInfo.add(fileName);
123         checksumInfo.add(Integer.toString(length));
124         checksumInfo.add(Long.toString(cksum));
125         return checksumInfo;
126         } catch(Exception JavaDoc e) {
127            //ignore
128
logger.log(Level.FINE, "diagnostic-service.compute_checksum_failed"
129                     ,new Object JavaDoc[]{fileName, e.getMessage()});
130             return null;
131         }
132     }
133     
134     /**
135      * Gets filter which recognizes .jar and .so/.dll files
136      * @return FilenameFilter
137      */

138     private FilenameFilter getFilter() {
139         final String JavaDoc[] exts ;
140         if(isSolaris())
141             exts = new String JavaDoc[] {JAR_EXT, SO_EXT};
142         else
143             exts = new String JavaDoc[] {JAR_EXT, DLL_EXT};
144         return new FilenameFilter(){
145             public boolean accept(File dir, String JavaDoc name) {
146                 return name.endsWith(exts[0]) ||
147                         name.endsWith(exts[1]);
148             }
149         };
150     }
151     
152     private boolean isSolaris() {
153         return DiagnosticServiceHelper.isSolaris();
154     }
155     
156     private void writeToFile(List<String JavaDoc> checksumInfo) throws
157             DiagnosticException{
158         if (checksumInfo != null) {
159             try {
160                 writer.write("\n");
161                 for(String JavaDoc entry : checksumInfo) {
162                     writer.write(entry + "\t");
163                 } //for
164
}catch(IOException ioe) {
165                //ignore
166
}
167             
168        }//if
169
}//writeToFile
170
}
171
Popular Tags