-
Notifications
You must be signed in to change notification settings - Fork 195
/
FkHitRefresh.java
260 lines (242 loc) · 7.37 KB
/
FkHitRefresh.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.fork;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Opt;
import org.takes.rq.RqHeaders;
/**
* Fork by hit-refresh header.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.9
* @see TkFork
*/
@EqualsAndHashCode
public final class FkHitRefresh implements Fork {
/**
* Command to execute.
*/
private final Runnable exec;
/**
* Target.
*/
private final Take take;
/**
* A handle for tracking changes.
*/
private final HitRefreshHandle handle;
/**
* Ctor.
* @param file Directory to watch
* @param cmd Command to execute
* @param tke Target
*/
public FkHitRefresh(final File file, final String cmd, final Take tke) {
this(file, Arrays.asList(cmd.split(" ")), tke);
}
/**
* Ctor.
* @param file Directory to watch
* @param cmd Command to execute
* @param tke Target
*/
public FkHitRefresh(final File file, final List<String> cmd,
final Take tke) {
this(
file,
new Runnable() {
@Override
public void run() {
try {
new ProcessBuilder().command(cmd).start();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}
},
tke
);
}
/**
* Ctor.
* @param file Directory to watch
* @param cmd Command to execute
* @param tke Target
*/
public FkHitRefresh(final File file, final Runnable cmd, final Take tke) {
this(
cmd,
tke,
new HitRefreshHandle(file)
);
}
/**
* Ctor.
* @param cmd Command to execute
* @param tke Target
* @param handle Hit refresh handle
*/
private FkHitRefresh(final Runnable cmd, final Take tke,
final HitRefreshHandle handle) {
this.exec = cmd;
this.take = tke;
this.handle = handle;
}
@Override
public Opt<Response> route(final Request req) throws IOException {
final Iterator<String> header =
new RqHeaders.Base(req).header("X-Takes-HitRefresh").iterator();
final Opt<Response> resp;
if (header.hasNext()) {
if (this.handle.expired()) {
this.exec.run();
this.handle.touch();
}
resp = new Opt.Single<>(this.take.act(req));
} else {
resp = new Opt.Empty<>();
}
return resp;
}
/**
* A handle for serving hit-refresh feature.
*/
private static final class HitRefreshHandle {
/**
* Directory to watch.
*/
private final File dir;
/**
* Internal state. Flag file touched on every exec run.
* Instantiated at first touch.
*/
private final List<File> flag;
/**
* A lock for concurrent access to flag file.
*/
private final ReentrantReadWriteLock lock;
/**
* Ctor.
* @param dir Directory to watch
*/
HitRefreshHandle(final File dir) {
this(
dir,
new ReentrantReadWriteLock()
);
}
/**
* Ctor.
* @param dir Directory to watch
* @param lock Lock for access to flag file
*/
private HitRefreshHandle(final File dir,
final ReentrantReadWriteLock lock) {
this.dir = dir;
this.lock = lock;
this.flag = new CopyOnWriteArrayList<>();
}
/**
* Create the file to touch, if it is not yet created.
* @return The file to touch
* @throws IOException If fails
*/
public File touchedFile() throws IOException {
if (this.flag.isEmpty()) {
this.lock.writeLock().lock();
final File file = File.createTempFile("take", ".txt");
file.deleteOnExit();
this.flag.add(file);
this.lock.writeLock().unlock();
this.touch();
}
return this.flag.get(0);
}
/**
* Touch the temporary file.
* @throws IOException If fails
*/
public void touch() throws IOException {
try (OutputStream out = Files.newOutputStream(
this.touchedFile().toPath()
)) {
out.write('+');
}
}
/**
* Expired?
* @return TRUE if expired
* @throws IOException If fails
*/
private boolean expired() throws IOException {
final boolean expired;
if (this.flag.isEmpty()) {
expired = true;
} else {
expired = this.directoryUpdated();
}
return expired;
}
/**
* Directory contents updated?
* @return TRUE if contents were updated
* @todo #744:30min `null` constant usage in the function
* `directoryUpdated` is violating the key principles, defined
* in README.md file and must be eliminated.
*/
private boolean directoryUpdated() {
final long recent;
try {
this.lock.readLock().lock();
recent = this.flag.get(0).lastModified();
} finally {
this.lock.readLock().unlock();
}
final File[] files = this.dir.listFiles();
boolean expired = false;
if (files != null) {
for (final File file : files) {
if (file.lastModified() > recent) {
expired = true;
break;
}
}
}
return expired;
}
}
}