KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > ConfigurationStoreUtil


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.configuration;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.LineNumberReader JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.security.MessageDigest JavaDoc;
28 import java.security.NoSuchAlgorithmException JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Utility methods for dealing with checksums (hashes) of files in the
35  * configuration store.
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class ConfigurationStoreUtil {
40     private static final Log log = LogFactory.getLog(ConfigurationStoreUtil.class);
41
42     public static void writeChecksumFor(File JavaDoc file) throws IOException JavaDoc {
43         // check if the sum already exists
44
File JavaDoc sumFile = new File JavaDoc(file.getParentFile(), file.getName() + ".sha1");
45         if (sumFile.exists()) {
46             throw new IOException JavaDoc("Sum file already exists");
47         }
48
49         // calculate the checksum
50
String JavaDoc actualChecksum;
51         try {
52             actualChecksum = calculateChecksum(file, "SHA-1");
53         } catch (NoSuchAlgorithmException JavaDoc e) {
54             throw new IOException JavaDoc("SHA-1 algorithm not available");
55         }
56
57         // write it
58
FileWriter JavaDoc writer = new FileWriter JavaDoc(sumFile);
59         try {
60             writer.write(actualChecksum);
61         } finally {
62             try {
63                 writer.close();
64             } catch (IOException JavaDoc ignored) {
65             }
66         }
67     }
68
69     public static boolean verifyChecksum(File JavaDoc file) {
70         String JavaDoc expectedChecksum = getExpectedChecksum(file);
71         if (expectedChecksum == null) {
72             // log message already printed
73
return false;
74         }
75
76         String JavaDoc actualChecksum = getActualChecksum(file);
77         if (actualChecksum == null) {
78             // log message already printed
79
return false;
80         }
81
82
83         if (!actualChecksum.equals(expectedChecksum)) {
84             log.warn("Configuration file was modified: " + file.getAbsolutePath());
85             return false;
86         }
87
88         return true;
89     }
90
91     public static String JavaDoc getExpectedChecksum(File JavaDoc file) {
92         File JavaDoc sumFile = new File JavaDoc(file.getParentFile(), file.getName() + ".sha1");
93         if (!sumFile.exists()) {
94             log.warn("Checksum file not found: " + sumFile.getAbsolutePath());
95             return null;
96         }
97         if (!sumFile.canRead()) {
98             log.warn("Checksum file is not readable: " + sumFile.getAbsolutePath());
99             return null;
100         }
101         LineNumberReader JavaDoc lineNumberReader = null;
102         try {
103             lineNumberReader = new LineNumberReader JavaDoc(new FileReader JavaDoc(sumFile));
104             String JavaDoc expectedChecksum = lineNumberReader.readLine();
105             if (expectedChecksum == null) {
106                 log.error("Checksum file was empty: " + sumFile.getAbsolutePath());
107                 return null;
108             }
109             return expectedChecksum.trim();
110         } catch (IOException JavaDoc e) {
111             log.error("Unable to read checksum file: " + sumFile.getAbsolutePath(), e);
112         } finally {
113             if (lineNumberReader != null) {
114                 try {
115                     lineNumberReader.close();
116                 } catch (IOException JavaDoc ignored) {
117                 }
118             }
119
120         }
121         return null;
122     }
123
124     public static String JavaDoc getActualChecksum(File JavaDoc file) {
125         return getActualChecksum(file, "SHA-1");
126     }
127     public static String JavaDoc getActualChecksum(File JavaDoc file, String JavaDoc algorithm) {
128         try {
129             return calculateChecksum(file, algorithm);
130         } catch (Exception JavaDoc e) {
131             log.error("Unable to calculate checksum for configuration file: " + file.getAbsolutePath(), e);
132         }
133         return null;
134     }
135
136     private static String JavaDoc calculateChecksum(File JavaDoc file, String JavaDoc algorithm) throws NoSuchAlgorithmException JavaDoc, IOException JavaDoc {
137
138         InputStream JavaDoc stream = null;
139         try {
140             stream = new FileInputStream JavaDoc(file);
141             
142             MessageDigest JavaDoc digester = MessageDigest.getInstance(algorithm);
143             digester.reset();
144
145             byte buf[] = new byte[4096];
146             int len = 0;
147
148             while ((len = stream.read(buf, 0, 1024)) != -1) {
149                 digester.update(buf, 0, len);
150             }
151
152             String JavaDoc actualChecksum = encode(digester.digest());
153             return actualChecksum;
154         } finally {
155             try {
156                 if (stream != null)
157                     stream.close();
158             } catch (IOException JavaDoc ignored) {
159             }
160         }
161     }
162
163     private static String JavaDoc encode(byte[] binaryData) {
164         if (binaryData.length != 16 && binaryData.length != 20) {
165             int bitLength = binaryData.length * 8;
166             throw new IllegalArgumentException JavaDoc("Unrecognised length for binary data: " + bitLength + " bits");
167         }
168
169         String JavaDoc retValue = "";
170
171         for (int i = 0; i < binaryData.length; i++) {
172             String JavaDoc t = Integer.toHexString(binaryData[i] & 0xff);
173
174             if (t.length() == 1) {
175                 retValue += ("0" + t);
176             } else {
177                 retValue += t;
178             }
179         }
180
181         return retValue.trim();
182     }
183
184     public static class ChecksumOutputStream extends OutputStream JavaDoc {
185         private final OutputStream JavaDoc out;
186         private MessageDigest JavaDoc digester;
187
188         public ChecksumOutputStream(OutputStream JavaDoc out) throws IOException JavaDoc {
189             this.out = out;
190             try {
191                 digester = MessageDigest.getInstance("SHA-1");
192                 digester.reset();
193             } catch (NoSuchAlgorithmException JavaDoc e) {
194                 throw new IOException JavaDoc("SHA-1 algorithm not available");
195             }
196         }
197
198         public String JavaDoc getChecksum() {
199             String JavaDoc actualChecksum = encode(digester.digest());
200             return actualChecksum;
201         }
202
203         public void write(int b) throws IOException JavaDoc {
204             digester.update((byte) b);
205             out.write(b);
206         }
207
208         public void write(byte[] b) throws IOException JavaDoc {
209             digester.update(b);
210             out.write(b);
211         }
212
213         public void write(byte[] b, int off, int len) throws IOException JavaDoc {
214             digester.update(b, off, len);
215             out.write(b, off, len);
216         }
217
218         public void flush() throws IOException JavaDoc {
219             out.flush();
220         }
221
222         public void close() throws IOException JavaDoc {
223             out.close();
224         }
225     }
226 }
227
Popular Tags