1 package org.apache.fulcrum.yaafi.service.reconfiguration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.security.MessageDigest;
26 import java.util.Arrays;
27
28 import org.apache.avalon.framework.logger.Logger;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.fulcrum.yaafi.framework.util.InputStreamLocator;
31
32
33
34
35
36
37
38 public class ReconfigurationEntry {
39
40
41 private String location;
42
43
44 private String[] serviceList;
45
46
47 private byte[] digest;
48
49
50 private InputStreamLocator locator;
51
52
53 private boolean isFirstInvocation;
54
55
56 private Logger logger;
57
58
59
60
61
62
63
64
65
66 public ReconfigurationEntry(Logger logger, File applicationDir, String location, String[] serviceList) {
67 this.isFirstInvocation = true;
68 this.location = location;
69 this.locator = new InputStreamLocator(applicationDir);
70 this.logger = logger;
71 this.serviceList = serviceList;
72 }
73
74
75
76
77 public boolean hasChanged() {
78 boolean result = false;
79 InputStream is = null;
80 byte[] currDigest = null;
81
82 try {
83
84
85 is = this.locate();
86
87 if (is == null) {
88 String msg = "Unable to find the following resource : " + this.getLocation();
89 this.logger.warn(msg);
90 } else {
91
92 currDigest = this.getDigest(is);
93 is.close();
94 is = null;
95
96 if (this.isFirstInvocation() == true) {
97 isFirstInvocation = false;
98 this.logger.debug("Storing SHA-1 digest of " + this.getLocation());
99 this.setDigest(currDigest);
100 } else {
101 if (equals(this.digest, currDigest) == false) {
102 this.logger.debug("The following resource has changed : " + this.getLocation());
103 this.setDigest(currDigest);
104 result = true;
105 }
106 }
107 }
108
109 return result;
110 } catch (Exception e) {
111 String msg = "The ShutdownService encountered an internal error";
112 this.logger.error(msg, e);
113 return false;
114 } finally {
115 if (is != null) {
116 try {
117 is.close();
118 } catch (Exception e) {
119 String msg = "Can't close the InputStream during error recovery";
120 this.logger.error(msg, e);
121 }
122 }
123 }
124
125 }
126
127
128
129
130 public String[] getServiceList() {
131 return serviceList;
132 }
133
134
135
136
137 private boolean isFirstInvocation() {
138 return isFirstInvocation;
139 }
140
141
142
143
144 private String getLocation() {
145 return location;
146 }
147
148
149
150
151
152
153
154 public InputStream locate() throws IOException {
155 return this.locator.locate(this.getLocation());
156 }
157
158
159
160
161
162
163
164
165 private byte[] getDigest(InputStream is) throws Exception {
166 byte[] result = null;
167 byte[] content = null;
168
169
170 content = IOUtils.toByteArray(is);
171
172 MessageDigest sha1 = MessageDigest.getInstance("SHA1");
173 sha1.update(content);
174 result = sha1.digest();
175
176 return result;
177 }
178
179
180
181
182 private void setDigest(byte[] digest) {
183 this.digest = digest;
184 }
185
186
187
188
189
190
191
192
193 private static boolean equals(byte[] lhs, byte[] rhs) {
194
195 return Arrays.equals(lhs, rhs);
196 }
197
198 }