1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.vitam.ingest;
22
23 import fr.gouv.vitam.common.exception.InvalidParseOperationException;
24 import fr.gouv.vitam.common.json.JsonHandler;
25 import fr.gouv.vitam.ingest.external.client.IngestExternalClient;
26 import fr.gouv.vitam.ingest.external.client.IngestExternalClientFactory;
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.Option;
29 import org.waarp.common.guid.GUID;
30 import org.waarp.common.logging.SysErrLogger;
31 import org.waarp.common.logging.WaarpLogger;
32 import org.waarp.common.logging.WaarpLoggerFactory;
33 import org.waarp.common.utility.SystemPropertyUtil;
34 import org.waarp.vitam.common.waarp.ManagerToWaarp;
35 import org.waarp.vitam.common.waarp.ManagerToWaarpFactory;
36
37 import java.io.File;
38 import java.io.FilenameFilter;
39 import java.io.IOException;
40 import java.nio.file.Files;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Properties;
44
45
46
47
48 public class IngestRequestFactory {
49 public static final String DEFAULT_INGEST_FACTORY =
50 "/waarp/data/r66/IngestFactory";
51 static final String ORG_WAARP_INGEST_BASEDIR = "org.waarp.ingest.basedir";
52
53
54
55 private static final WaarpLogger logger =
56 WaarpLoggerFactory.getLogger(IngestRequestFactory.class);
57 private static final String WORK = "work";
58 private static final String BASENAME =
59 IngestRequest.class.getSimpleName() + ".";
60 private static final String EXTENSION = ".json";
61 private static final String RESULT_EXTENSION = ".xml";
62 private static final FilenameFilter JSON_ONLY =
63 (dir, name) -> name.startsWith(BASENAME) && name.endsWith(EXTENSION);
64 private static final IngestRequestFactory FACTORY =
65 new IngestRequestFactory();
66 static boolean vitamTakeCareLocalFile = true;
67
68 static {
69 setBaseDir(new File(DEFAULT_INGEST_FACTORY));
70 }
71
72 private File baseDir;
73 private File workDir;
74 private IngestExternalClientFactory clientFactory =
75 IngestExternalClientFactory.getInstance();
76
77 private IngestRequestFactory() {
78
79 }
80
81
82
83
84
85
86 static Option getDirectoryOption() {
87 Option property =
88 new Option("D", "Use value for property " + ORG_WAARP_INGEST_BASEDIR);
89 property.setArgName("property=value");
90 property.setArgs(2);
91 property.setValueSeparator('=');
92 return property;
93 }
94
95
96
97
98
99
100 static void parseDirectoryOption(CommandLine cmd) {
101 if (cmd.hasOption('D')) {
102 Properties properties = cmd.getOptionProperties("D");
103 setBaseDir(new File(properties.getProperty(ORG_WAARP_INGEST_BASEDIR,
104 DEFAULT_INGEST_FACTORY)));
105 } else if (SystemPropertyUtil.contains(ORG_WAARP_INGEST_BASEDIR)) {
106 setBaseDir(new File(SystemPropertyUtil.get(ORG_WAARP_INGEST_BASEDIR,
107 DEFAULT_INGEST_FACTORY)));
108 } else {
109 setBaseDir(new File(DEFAULT_INGEST_FACTORY));
110 }
111 }
112
113
114
115
116
117
118 static void setBaseDir(File baseDirToSet) {
119 FACTORY.baseDir = baseDirToSet;
120 FACTORY.baseDir.mkdirs();
121 FACTORY.workDir = new File(FACTORY.baseDir, WORK);
122 FACTORY.workDir.mkdirs();
123 }
124
125
126
127
128 public static IngestRequestFactory getInstance() {
129 return FACTORY;
130 }
131
132
133
134
135 void setBaseDir() {
136 baseDir = FACTORY.baseDir;
137 workDir = FACTORY.workDir;
138 }
139
140
141
142
143 File getBaseDir() {
144 return baseDir;
145 }
146
147
148
149
150 public IngestExternalClient getClient() {
151 return clientFactory.getClient();
152 }
153
154
155
156
157
158
159 public ManagerToWaarp getManagerToWaarp(IngestRequest ingestRequest) {
160 return ManagerToWaarpFactory
161 .getManagerToWaarp(ingestRequest.getWaarpModel());
162 }
163
164
165
166
167
168
169
170
171 synchronized void saveNewIngestRequest(IngestRequest ingestRequest)
172 throws InvalidParseOperationException {
173 File newFile = new File(baseDir, getNewName());
174 ingestRequest.setJsonPath(newFile.getName());
175 JsonHandler.writeAsFile(ingestRequest, newFile);
176 }
177
178
179
180
181 private static String getNewName() {
182 synchronized (FACTORY) {
183 GUID guid = new GUID();
184 return BASENAME + guid.getId() + EXTENSION;
185 }
186 }
187
188
189
190
191
192
193
194
195
196
197 synchronized boolean saveIngestRequest(IngestRequest ingestRequest)
198 throws InvalidParseOperationException {
199 File existingFile = new File(baseDir, ingestRequest.getJsonPath());
200 if (existingFile.canRead()) {
201 JsonHandler.writeAsFile(ingestRequest, existingFile);
202 return true;
203 }
204 throw new InvalidParseOperationException("Json File does not exist");
205 }
206
207
208
209
210
211
212
213
214 synchronized boolean removeIngestRequest(IngestRequest ingestRequest) {
215 if (ingestRequest.getJsonPath() != null) {
216 File existingFile = new File(baseDir, ingestRequest.getJsonPath());
217 boolean status = deleteFile(existingFile);
218
219 if (vitamTakeCareLocalFile) {
220 File sourceFile = new File(ingestRequest.getPath());
221 status &= deleteFile(sourceFile);
222 }
223
224 File xmlAtrFile = getXmlAtrFile(ingestRequest);
225 status &= deleteFile(xmlAtrFile);
226 IngestRequest.IngestStep.endSessionMachineSate(ingestRequest.step);
227
228 while (existingFile.exists()) {
229 try {
230 FACTORY.wait(10);
231 } catch (InterruptedException ignore) {
232 logger.debug(ignore);
233 }
234 }
235 return status;
236 }
237 return false;
238 }
239
240
241
242
243
244
245
246
247 private boolean deleteFile(File file) {
248 if (file.canRead()) {
249 try {
250 Files.delete(file.toPath());
251 } catch (IOException e) {
252 logger.warn("Cannot delete file", e);
253 return false;
254 }
255 }
256 return true;
257 }
258
259
260
261
262
263
264 File getXmlAtrFile(IngestRequest ingestRequest) {
265 return new File(workDir, ingestRequest.getJsonPath() + RESULT_EXTENSION);
266 }
267
268
269
270
271 synchronized List<IngestRequest> getExistingIngests() {
272 List<IngestRequest> list = new ArrayList<>();
273 File[] files = baseDir.listFiles(JSON_ONLY);
274 if (files != null) {
275 for (File file : files) {
276 try {
277 IngestRequest ingestRequest =
278 JsonHandler.getFromFile(file, IngestRequest.class);
279 list.add(ingestRequest);
280 } catch (InvalidParseOperationException ignored) {
281
282 SysErrLogger.FAKE_LOGGER.ignoreLog(ignored);
283 }
284 }
285 }
286 return list;
287 }
288
289
290
291
292
293
294
295
296 synchronized IngestRequest getSpecificIngestRequest(String filename)
297 throws InvalidParseOperationException {
298 File file = new File(baseDir, filename);
299 if (file.exists()) {
300 return JsonHandler.getFromFile(file, IngestRequest.class);
301 }
302 throw new InvalidParseOperationException("Cannot find " + filename);
303 }
304 }