1 package org.apache.fulcrum.yaafi.service.shutdown;
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 ShutdownEntry {
39
40
41 private String location;
42
43
44 private byte[] digest;
45
46
47 private InputStreamLocator locator;
48
49
50 private boolean isFirstInvocation;
51
52
53 private Logger logger;
54
55
56 private boolean useSystemExit;
57
58
59
60
61
62
63
64
65
66 public ShutdownEntry(Logger logger, File applicationDir, String location, boolean useSystemExit) {
67 this.isFirstInvocation = true;
68 this.useSystemExit = useSystemExit;
69 this.location = location;
70 this.locator = new InputStreamLocator(applicationDir);
71 this.logger = logger;
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
93 currDigest = this.getDigest(is);
94 is.close();
95 is = null;
96
97 if (this.isFirstInvocation() == true) {
98 isFirstInvocation = false;
99 this.logger.debug("Storing SHA-1 digest of " + this.getLocation());
100 this.setDigest(currDigest);
101 } else {
102 if (equals(this.digest, currDigest) == false) {
103 this.logger.debug("The following resource has changed : " + this.getLocation());
104 this.setDigest(currDigest);
105 result = true;
106 }
107 }
108 }
109
110 return result;
111 } catch (Exception e) {
112 String msg = "The ShutdownService encountered an internal error";
113 this.logger.error(msg, e);
114 return false;
115 } finally {
116 if (is != null) {
117 try {
118 is.close();
119 } catch (Exception e) {
120 String msg = "Can't close the InputStream during error recovery";
121 this.logger.error(msg, e);
122 }
123 }
124 }
125
126 }
127
128
129
130
131 public boolean isUseSystemExit() {
132 return useSystemExit;
133 }
134
135
136
137
138 private boolean isFirstInvocation() {
139 return isFirstInvocation;
140 }
141
142
143
144
145 private String getLocation() {
146 return location;
147 }
148
149
150
151
152
153
154
155 public InputStream locate() throws IOException {
156 return this.locator.locate(this.getLocation());
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 private static boolean equals(byte[] lhs, byte[] rhs) {
190
191 return Arrays.equals(lhs, rhs);
192 }
193
194 }