1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.asset; |
16 |
| |
17 |
| import java.io.BufferedInputStream; |
18 |
| import java.io.IOException; |
19 |
| import java.io.InputStream; |
20 |
| import java.net.URL; |
21 |
| import java.security.MessageDigest; |
22 |
| import java.util.HashMap; |
23 |
| import java.util.Map; |
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 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
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 |
| |
46 |
| |
47 |
| |
48 |
| private final Map _cache = new HashMap(); |
49 |
| |
50 |
81
| public synchronized String getDigestForResource(String resourcePath)
|
51 |
| { |
52 |
81
| String result = (String) _cache.get(resourcePath);
|
53 |
| |
54 |
81
| if (result == null)
|
55 |
| { |
56 |
38
| result = computeMD5(resourcePath);
|
57 |
37
| _cache.put(resourcePath, result);
|
58 |
| } |
59 |
| |
60 |
80
| return result;
|
61 |
| } |
62 |
| |
63 |
1
| public synchronized void resetEventDidOccur()
|
64 |
| { |
65 |
1
| _cache.clear();
|
66 |
| } |
67 |
| |
68 |
38
| private String computeMD5(String resourcePath)
|
69 |
| { |
70 |
38
| URL url = _classResolver.getResource(resourcePath);
|
71 |
| |
72 |
38
| if (url == null)
|
73 |
1
| throw new ApplicationRuntimeException(AssetMessages.noSuchResource(resourcePath));
|
74 |
| |
75 |
37
| InputStream stream = null;
|
76 |
| |
77 |
37
| try
|
78 |
| { |
79 |
37
| MessageDigest digest = MessageDigest.getInstance("MD5");
|
80 |
| |
81 |
37
| stream = new BufferedInputStream(url.openStream());
|
82 |
| |
83 |
37
| digestStream(digest, stream);
|
84 |
| |
85 |
37
| stream.close();
|
86 |
37
| stream = null;
|
87 |
| |
88 |
37
| byte[] bytes = digest.digest();
|
89 |
37
| char[] encoded = Hex.encodeHex(bytes);
|
90 |
| |
91 |
37
| return new String(encoded);
|
92 |
| } |
93 |
| catch (IOException ex) |
94 |
| { |
95 |
0
| throw new ApplicationRuntimeException(AssetMessages.unableToReadResource(
|
96 |
| resourcePath, |
97 |
| ex)); |
98 |
| } |
99 |
| catch (Exception ex) |
100 |
| { |
101 |
0
| throw new ApplicationRuntimeException(ex);
|
102 |
| } |
103 |
| finally |
104 |
| { |
105 |
37
| IOUtils.close(stream);
|
106 |
| } |
107 |
| } |
108 |
| |
109 |
37
| private void digestStream(MessageDigest digest, InputStream stream) throws IOException
|
110 |
| { |
111 |
37
| byte[] buffer = new byte[BUFFER_SIZE];
|
112 |
| |
113 |
37
| while (true)
|
114 |
| { |
115 |
96
| int length = stream.read(buffer);
|
116 |
| |
117 |
96
| if (length < 0)
|
118 |
37
| return;
|
119 |
| |
120 |
59
| digest.update(buffer, 0, length);
|
121 |
| } |
122 |
| } |
123 |
| |
124 |
29
| public void setClassResolver(ClassResolver classResolver)
|
125 |
| { |
126 |
29
| _classResolver = classResolver;
|
127 |
| } |
128 |
| } |