MCPcopy
hub / github.com/OpenTSDB/opentsdb / FileSystemValidator

Class FileSystemValidator

src/tools/ConfigMetaType.java:390–443  ·  view source on GitHub ↗

Title: FileSystemValidator Description: Validator for files and directories

Source from the content-addressed store, hash-verified

388 * <p>Description: Validator for files and directories</p>
389 */
390 public static class FileSystemValidator implements ArgValueValidator {
391 private final boolean dir;
392 private final boolean mustExist;
393
394 /**
395 * Creates a new FileSystemValidator
396 * @param dir true for validating directories, false for files
397 * @param mustExist true if the target must exist, false if it can be created if it does not exist
398 */
399 public FileSystemValidator(boolean dir, boolean mustExist) {
400 this.dir = dir;
401 this.mustExist = mustExist;
402 }
403
404 /**
405 * {@inheritDoc}
406 * @see net.opentsdb.tools.ArgValueValidator#validate(net.opentsdb.tools.ConfigArgP.ConfigurationItem)
407 */
408 @Override
409 public void validate(final ConfigurationItem citem) {
410 String value = citem.getValue();
411 File target = new File(value);
412 if(mustExist) {
413 if(!target.exists()) throw new IllegalArgumentException((dir ? "Directory [" : " File [") + value + "] does not exist for " + citem.getName());
414 if(dir) {
415 if(!target.isDirectory()) throw new IllegalArgumentException(("[") + value + "] is a file not a directory for " + citem.getName());
416 } else {
417 if(!target.isFile()) throw new IllegalArgumentException(("[") + value + "] is a directory not a file for " + citem.getName());
418 }
419 } else {
420 if(dir) {
421 if(!target.exists()) {
422 if(!target.mkdirs()) throw new IllegalArgumentException(("Could not create directory [") + value + "] for " + citem.getName());
423 } else {
424 if(!target.isDirectory()) throw new IllegalArgumentException(("[") + value + "] is a file not a directory for " + citem.getName());
425 }
426 } else {
427 if(!target.getParentFile().exists()) {
428 if(!target.getParentFile().mkdirs()) throw new IllegalArgumentException(("Could not create parent directory for file [") + value + "] for " + citem.getName());
429 }
430
431 if(!target.exists()) {
432 try {
433 if(!target.createNewFile()) throw new IllegalArgumentException(("Could not create file [") + value + "] for " + citem.getName());
434 } catch (IOException e) {
435 throw new IllegalArgumentException(("Could not create file [") + value + "] for " + citem.getName());
436 } finally {
437 target.delete();
438 }
439 }
440 }
441 }
442 }
443 }
444
445 /**
446 * <p>Title: TimeZoneValidator</p>

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…