blob: 5677ba1da2e1d6959e6f4d2c53d2ea4c18880147 (
plain)
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
|
package moe.yuuta.dn42peering.agent.provision;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.OpenOptions;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class FileChange extends Change {
private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
public enum Action {
CREATE_AND_WRITE,
OVERWRITE,
DELETE
}
public FileChange(@Nonnull String path,
@Nullable String contents,
@Nonnull String action) {
super(path, contents, action);
Action.valueOf(action); // Verify
}
@Nonnull
@Override
public Future<Void> execute(@Nonnull Vertx vertx) {
switch (Action.valueOf(action)) {
case CREATE_AND_WRITE:
logger.info("Writing " + id);
return vertx.fileSystem().open(id, new OpenOptions()
.setCreateNew(true)
.setTruncateExisting(true)
.setWrite(true))
.compose(asyncFile -> {
return asyncFile.write(Buffer.buffer(to == null ? "" : to), 0)
.compose(_v -> Future.succeededFuture(asyncFile));
})
.compose(asyncFile -> {
return asyncFile.close();
});
case OVERWRITE:
logger.info("Overwriting " + id);
return vertx.fileSystem().open(id, new OpenOptions()
.setCreateNew(false)
.setTruncateExisting(true)
.setWrite(true))
.compose(asyncFile -> {
return asyncFile.write(Buffer.buffer(to == null ? "" : to), 0)
.compose(_v -> Future.succeededFuture(asyncFile));
})
.compose(asyncFile -> {
return asyncFile.close();
});
case DELETE:
logger.info("Deleting " + id);
return vertx.fileSystem().delete(id);
default:
throw new UnsupportedOperationException("Unknown file change action " + action);
}
}
}
|