1 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 ; 36 import java.util.logging.Logger ; 37 import java.util.zip.CheckedInputStream ; 38 import java.util.zip.CRC32 ; 39 40 44 public class ChecksumCollector implements Collector { 45 46 private String source; 47 private String destFolder; 48 private String destFile; 49 private BufferedWriter writer; 50 private WritableData dataObj; 51 private static final String BIN_FOLDER = File.separator + "bin"; 52 private static final String LIB_FOLDER = File.separator + "lib"; 53 private static final String JAR_EXT = ".jar"; 54 private static final String DLL_EXT = "*.dll"; 55 private static final String SO_EXT = "*.so"; 56 private static Logger logger = 57 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 58 63 public ChecksumCollector(String source, String destFolder) { 64 this.destFolder = destFolder; 65 this.source = source; 66 dataObj = new WritableDataImpl(source, DataType.CHECKSUM); 67 } 68 69 73 public Data capture() throws DiagnosticException { 74 dataObj.addRow(Arrays.asList(new String []{"Name", "Length", "Checksum"})); 75 captureChecksum(source + LIB_FOLDER, getFilter()); 76 captureChecksum(source + BIN_FOLDER, null); 77 return dataObj; 78 } 79 80 86 private void captureChecksum(String sourceName, FilenameFilter filter) 87 throws DiagnosticException { 88 89 File fileObj = new File(sourceName); 90 String [] 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 105 private List<String > generateCRC32Checksum(String parent, String fileName) 106 throws DiagnosticException { 107 try { 108 String file = parent + File.separator + fileName; 110 List<String > checksumInfo = new ArrayList(3); 111 CRC32 crc32 = new CRC32 (); 112 int length = 0; 113 BufferedInputStream fileinputstream = new BufferedInputStream( 114 new FileInputStream(new File(file))); 115 for( CheckedInputStream checkedinputstream = new CheckedInputStream ( 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 e) { 127 logger.log(Level.FINE, "diagnostic-service.compute_checksum_failed" 129 ,new Object []{fileName, e.getMessage()}); 130 return null; 131 } 132 } 133 134 138 private FilenameFilter getFilter() { 139 final String [] exts ; 140 if(isSolaris()) 141 exts = new String [] {JAR_EXT, SO_EXT}; 142 else 143 exts = new String [] {JAR_EXT, DLL_EXT}; 144 return new FilenameFilter(){ 145 public boolean accept(File dir, String 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 > checksumInfo) throws 157 DiagnosticException{ 158 if (checksumInfo != null) { 159 try { 160 writer.write("\n"); 161 for(String entry : checksumInfo) { 162 writer.write(entry + "\t"); 163 } }catch(IOException ioe) { 165 } 167 168 } }} 171 | Popular Tags |