|
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.Iterator; |
|
24 |
| import java.util.Map; |
|
25 |
| |
|
26 |
| import org.apache.commons.codec.binary.Hex; |
|
27 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
28 |
| import org.apache.hivemind.ClassResolver; |
|
29 |
| import org.apache.hivemind.util.IOUtils; |
|
30 |
| import org.apache.tapestry.event.ReportStatusEvent; |
|
31 |
| import org.apache.tapestry.event.ReportStatusListener; |
|
32 |
| import org.apache.tapestry.event.ResetEventListener; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public class ResourceDigestSourceImpl implements ResourceDigestSource, ResetEventListener, |
|
42 |
| ReportStatusListener |
|
43 |
| { |
|
44 |
| private String _serviceId; |
|
45 |
| |
|
46 |
| private ClassResolver _classResolver; |
|
47 |
| |
|
48 |
| private static final int BUFFER_SIZE = 5000; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| private final Map _cache = new HashMap(); |
|
55 |
| |
|
56 |
237
| public synchronized String getDigestForResource(String resourcePath)
|
|
57 |
| { |
|
58 |
237
| String result = (String) _cache.get(resourcePath);
|
|
59 |
| |
|
60 |
237
| if (result == null)
|
|
61 |
| { |
|
62 |
111
| result = computeMD5(resourcePath);
|
|
63 |
108
| _cache.put(resourcePath, result);
|
|
64 |
| } |
|
65 |
| |
|
66 |
234
| return result;
|
|
67 |
| } |
|
68 |
| |
|
69 |
3
| public synchronized void resetEventDidOccur()
|
|
70 |
| { |
|
71 |
3
| _cache.clear();
|
|
72 |
| } |
|
73 |
| |
|
74 |
51
| public synchronized void reportStatus(ReportStatusEvent event)
|
|
75 |
| { |
|
76 |
51
| event.title(_serviceId);
|
|
77 |
51
| event.property("resource count", _cache.size());
|
|
78 |
| |
|
79 |
51
| Iterator i = _cache.entrySet().iterator();
|
|
80 |
| |
|
81 |
51
| while (i.hasNext())
|
|
82 |
| { |
|
83 |
75
| Map.Entry entry = (Map.Entry) i.next();
|
|
84 |
| |
|
85 |
75
| event.property(entry.getKey().toString(), entry.getValue());
|
|
86 |
| } |
|
87 |
| } |
|
88 |
| |
|
89 |
111
| private String computeMD5(String resourcePath)
|
|
90 |
| { |
|
91 |
111
| URL url = _classResolver.getResource(resourcePath);
|
|
92 |
| |
|
93 |
111
| if (url == null)
|
|
94 |
3
| throw new ApplicationRuntimeException(AssetMessages.noSuchResource(resourcePath));
|
|
95 |
| |
|
96 |
108
| InputStream stream = null;
|
|
97 |
| |
|
98 |
108
| try
|
|
99 |
| { |
|
100 |
108
| MessageDigest digest = MessageDigest.getInstance("MD5");
|
|
101 |
| |
|
102 |
108
| stream = new BufferedInputStream(url.openStream());
|
|
103 |
| |
|
104 |
108
| digestStream(digest, stream);
|
|
105 |
| |
|
106 |
108
| stream.close();
|
|
107 |
108
| stream = null;
|
|
108 |
| |
|
109 |
108
| byte[] bytes = digest.digest();
|
|
110 |
108
| char[] encoded = Hex.encodeHex(bytes);
|
|
111 |
| |
|
112 |
108
| return new String(encoded);
|
|
113 |
| } |
|
114 |
| catch (IOException ex) |
|
115 |
| { |
|
116 |
0
| throw new ApplicationRuntimeException(AssetMessages.unableToReadResource(
|
|
117 |
| resourcePath, |
|
118 |
| ex)); |
|
119 |
| } |
|
120 |
| catch (Exception ex) |
|
121 |
| { |
|
122 |
0
| throw new ApplicationRuntimeException(ex);
|
|
123 |
| } |
|
124 |
| finally |
|
125 |
| { |
|
126 |
108
| IOUtils.close(stream);
|
|
127 |
| } |
|
128 |
| } |
|
129 |
| |
|
130 |
108
| private void digestStream(MessageDigest digest, InputStream stream) throws IOException
|
|
131 |
| { |
|
132 |
108
| byte[] buffer = new byte[BUFFER_SIZE];
|
|
133 |
| |
|
134 |
108
| while (true)
|
|
135 |
| { |
|
136 |
282
| int length = stream.read(buffer);
|
|
137 |
| |
|
138 |
282
| if (length < 0)
|
|
139 |
108
| return;
|
|
140 |
| |
|
141 |
174
| digest.update(buffer, 0, length);
|
|
142 |
| } |
|
143 |
| } |
|
144 |
| |
|
145 |
84
| public void setClassResolver(ClassResolver classResolver)
|
|
146 |
| { |
|
147 |
84
| _classResolver = classResolver;
|
|
148 |
| } |
|
149 |
| |
|
150 |
75
| public void setServiceId(String serviceId)
|
|
151 |
| { |
|
152 |
75
| _serviceId = serviceId;
|
|
153 |
| } |
|
154 |
| } |