KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > asset > ResourceDigestSourceImpl


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

15 package org.apache.tapestry.asset;
16
17 import java.io.BufferedInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.security.MessageDigest JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.commons.codec.binary.Hex;
26 import org.apache.hivemind.ApplicationRuntimeException;
27 import org.apache.hivemind.ClassResolver;
28 import org.apache.hivemind.util.IOUtils;
29 import org.apache.tapestry.event.ResetEventListener;
30
31 /**
32  * Implementation of {@link org.apache.tapestry.asset.ResourceDigestSource} that calculates an
33  * DIGEST checksum digest and converts it to a string of hex digits.
34  *
35  * @author Howard M. Lewis Ship
36  * @since 4.0
37  */

38 public class ResourceDigestSourceImpl implements ResourceDigestSource, ResetEventListener
39 {
40     private ClassResolver _classResolver;
41
42     private static final int BUFFER_SIZE = 5000;
43
44     /**
45      * Map keyed on resource path of DIGEST checksum (as a string).
46      */

47
48     private final Map JavaDoc _cache = new HashMap JavaDoc();
49
50     public synchronized String JavaDoc getDigestForResource(String JavaDoc resourcePath)
51     {
52         String JavaDoc result = (String JavaDoc) _cache.get(resourcePath);
53
54         if (result == null)
55         {
56             result = computeMD5(resourcePath);
57             _cache.put(resourcePath, result);
58         }
59
60         return result;
61     }
62
63     public synchronized void resetEventDidOccur()
64     {
65         _cache.clear();
66     }
67
68     private String JavaDoc computeMD5(String JavaDoc resourcePath)
69     {
70         URL JavaDoc url = _classResolver.getResource(resourcePath);
71
72         if (url == null)
73             throw new ApplicationRuntimeException(AssetMessages.noSuchResource(resourcePath));
74
75         InputStream JavaDoc stream = null;
76
77         try
78         {
79             MessageDigest JavaDoc digest = MessageDigest.getInstance("MD5");
80
81             stream = new BufferedInputStream JavaDoc(url.openStream());
82
83             digestStream(digest, stream);
84
85             stream.close();
86             stream = null;
87
88             byte[] bytes = digest.digest();
89             char[] encoded = Hex.encodeHex(bytes);
90
91             return new String JavaDoc(encoded);
92         }
93         catch (IOException JavaDoc ex)
94         {
95             throw new ApplicationRuntimeException(AssetMessages.unableToReadResource(
96                     resourcePath,
97                     ex));
98         }
99         catch (Exception JavaDoc ex)
100         {
101             throw new ApplicationRuntimeException(ex);
102         }
103         finally
104         {
105             IOUtils.close(stream);
106         }
107     }
108
109     private void digestStream(MessageDigest JavaDoc digest, InputStream JavaDoc stream) throws IOException JavaDoc
110     {
111         byte[] buffer = new byte[BUFFER_SIZE];
112
113         while (true)
114         {
115             int length = stream.read(buffer);
116
117             if (length < 0)
118                 return;
119
120             digest.update(buffer, 0, length);
121         }
122     }
123
124     public void setClassResolver(ClassResolver classResolver)
125     {
126         _classResolver = classResolver;
127     }
128 }
Popular Tags