-
Notifications
You must be signed in to change notification settings - Fork 173
/
DirectoryDeployer.java
172 lines (136 loc) · 4.21 KB
/
DirectoryDeployer.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
/*
* Copyright (c) 2014 Villu Ruusmann
*
* This file is part of Openscoring
*
* Openscoring is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Openscoring is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Openscoring. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openscoring.client;
import java.io.File;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import com.beust.jcommander.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DirectoryDeployer extends Application {
@Parameter (
names = {"--model-collection"},
description = "The URI of the model collection",
required = true
)
private String modelCollection = null;
@Parameter (
names = {"--dir"},
description = "The directory to watch for PMML file additions and removals",
required = true
)
private File dir = null;
/**
* A set of "managed" model identifiers.
*/
private Set<String> identifiers = new LinkedHashSet<>();
static
public void main(String... args) throws Exception {
run(DirectoryDeployer.class, args);
}
@Override
public void run() throws Exception {
Path root = (getDir()).toPath();
try(DirectoryStream<Path> children = Files.newDirectoryStream(root)){
for(Path child : children){
process(StandardWatchEventKinds.ENTRY_CREATE, child);
}
}
try(FileSystem fileSystem = root.getFileSystem()){
try(WatchService watcher = fileSystem.newWatchService()){
WatchKey key = root.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
if(key.isValid()){
processKey(key, root);
}
while(key.reset()){
try {
key = watcher.take();
} catch(InterruptedException ie){
break;
}
if(key.isValid()){
processKey(key, root);
}
}
}
}
}
@SuppressWarnings({"rawtypes", "unchecked"})
private void processKey(WatchKey key, Path root) throws Exception {
List<WatchEvent<Path>> events = (List)key.pollEvents();
for(WatchEvent<Path> event : events){
Path child = root.resolve(event.context());
process(event.kind(), child);
}
}
private void process(WatchEvent.Kind<Path> kind, Path path) throws Exception {
String id = (path.getFileName()).toString();
// Use the name part of the file name (ie. everything to the left of the first dot character) as the model identifier
int dot = id.indexOf('.');
if(dot > -1){
id = id.substring(0, dot);
} // End if
if(("").equals(id)){
return;
} // End if
if((StandardWatchEventKinds.ENTRY_CREATE).equals(kind)){
if(!Files.isRegularFile(path)){
return;
}
logger.info("Deploying model {}", id);
Deployer deployer = new Deployer();
deployer.setModel(getModelCollection() + "/" + id);
deployer.setFile(path.toFile());
deployer.run();
this.identifiers.add(id);
} else
if((StandardWatchEventKinds.ENTRY_DELETE).equals(kind)){
boolean status = this.identifiers.remove(id);
if(!status){
return;
}
logger.info("Undeploying model {}", id);
Undeployer undeployer = new Undeployer();
undeployer.setModel(getModelCollection() + "/" + id);
undeployer.run();
}
}
public String getModelCollection(){
return this.modelCollection;
}
public void setModelCollection(String modelCollection){
this.modelCollection = modelCollection;
}
public File getDir(){
return this.dir;
}
public void setDir(File dir){
this.dir = dir;
}
private static final Logger logger = LoggerFactory.getLogger(DirectoryDeployer.class);
}