use of org.apache.commons.io.monitor.FileAlterationListener in project tutorials by eugenp.
the class FileMonitor method startFileMonitor.
/**
* @param folder
* @throws Exception
*/
public static void startFileMonitor(File folder) throws Exception {
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor = new FileAlterationMonitor(5000);
FileAlterationListener fal = new FileAlterationListenerAdaptor() {
@Override
public void onFileCreate(File file) {
// on create action
}
@Override
public void onFileDelete(File file) {
// on delete action
}
};
observer.addListener(fal);
monitor.addObserver(observer);
monitor.start();
}
use of org.apache.commons.io.monitor.FileAlterationListener in project syncope by apache.
the class SyncopeEnduserApplication method init.
@Override
protected void init() {
super.init();
// read enduser.properties
Properties props = PropertyUtils.read(getClass(), ENDUSER_PROPERTIES, "enduser.directory").getLeft();
domain = props.getProperty("domain", SyncopeConstants.MASTER_DOMAIN);
adminUser = props.getProperty("adminUser");
Args.notNull(adminUser, "<adminUser>");
anonymousUser = props.getProperty("anonymousUser");
Args.notNull(anonymousUser, "<anonymousUser>");
anonymousKey = props.getProperty("anonymousKey");
Args.notNull(anonymousKey, "<anonymousKey>");
captchaEnabled = Boolean.parseBoolean(props.getProperty("captcha"));
Args.notNull(captchaEnabled, "<captcha>");
xsrfEnabled = Boolean.parseBoolean(props.getProperty("xsrf"));
Args.notNull(xsrfEnabled, "<xsrf>");
String scheme = props.getProperty("scheme");
Args.notNull(scheme, "<scheme>");
String host = props.getProperty("host");
Args.notNull(host, "<host>");
String port = props.getProperty("port");
Args.notNull(port, "<port>");
String rootPath = props.getProperty("rootPath");
Args.notNull(rootPath, "<rootPath>");
String useGZIPCompression = props.getProperty("useGZIPCompression");
Args.notNull(useGZIPCompression, "<useGZIPCompression>");
maxUploadFileSizeMB = props.getProperty("maxUploadFileSizeMB") == null ? null : Integer.valueOf(props.getProperty("maxUploadFileSizeMB"));
clientFactory = new SyncopeClientFactoryBean().setAddress(scheme + "://" + host + ":" + port + "/" + rootPath).setContentType(SyncopeClientFactoryBean.ContentType.JSON).setUseCompression(BooleanUtils.toBoolean(useGZIPCompression));
// read customForm.json
try (InputStream is = getClass().getResourceAsStream("/" + CUSTOM_FORM_FILE)) {
customForm = MAPPER.readValue(is, new TypeReference<HashMap<String, CustomAttributesInfo>>() {
});
File enduserDir = new File(props.getProperty("enduser.directory"));
boolean existsEnduserDir = enduserDir.exists() && enduserDir.canRead() && enduserDir.isDirectory();
if (existsEnduserDir) {
File customFormFile = FileUtils.getFile(enduserDir, CUSTOM_FORM_FILE);
if (customFormFile.exists() && customFormFile.canRead() && customFormFile.isFile()) {
customForm = MAPPER.readValue(FileUtils.openInputStream(customFormFile), new TypeReference<HashMap<String, CustomAttributesInfo>>() {
});
}
}
FileAlterationObserver observer = existsEnduserDir ? new FileAlterationObserver(enduserDir, pathname -> StringUtils.contains(pathname.getPath(), CUSTOM_FORM_FILE)) : new FileAlterationObserver(getClass().getResource("/" + CUSTOM_FORM_FILE).getFile(), pathname -> StringUtils.contains(pathname.getPath(), CUSTOM_FORM_FILE));
FileAlterationMonitor monitor = new FileAlterationMonitor(5000);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
@Override
public void onFileChange(final File file) {
try {
LOG.trace("{} has changed. Reloading form customization configuration.", CUSTOM_FORM_FILE);
customForm = MAPPER.readValue(FileUtils.openInputStream(file), new TypeReference<HashMap<String, CustomAttributesInfo>>() {
});
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
@Override
public void onFileCreate(final File file) {
try {
LOG.trace("{} has been created. Loading form customization configuration.", CUSTOM_FORM_FILE);
customForm = MAPPER.readValue(FileUtils.openInputStream(file), new TypeReference<HashMap<String, CustomAttributesInfo>>() {
});
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
@Override
public void onFileDelete(final File file) {
LOG.trace("{} has been deleted. Resetting form customization configuration.", CUSTOM_FORM_FILE);
customForm = null;
}
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
} catch (Exception e) {
throw new WicketRuntimeException("Could not read " + CUSTOM_FORM_FILE, e);
}
// mount resources
ClassPathScanImplementationLookup classPathScanImplementationLookup = (ClassPathScanImplementationLookup) getServletContext().getAttribute(EnduserInitializer.CLASSPATH_LOOKUP);
for (final Class<? extends AbstractResource> resource : classPathScanImplementationLookup.getResources()) {
Resource annotation = resource.getAnnotation(Resource.class);
if (annotation == null) {
LOG.debug("No @Resource annotation found on {}, ignoring", resource.getName());
} else {
try {
final AbstractResource instance = resource.newInstance();
mountResource(annotation.path(), new ResourceReference(annotation.key()) {
private static final long serialVersionUID = -128426276529456602L;
@Override
public IResource getResource() {
return instance;
}
});
} catch (Exception e) {
LOG.error("Could not instantiate {}", resource.getName(), e);
}
}
}
// mount captcha resource only if captcha is enabled
if (captchaEnabled) {
mountResource("/api/captcha", new ResourceReference("captcha") {
private static final long serialVersionUID = -128426276529456602L;
@Override
public IResource getResource() {
return new CaptchaResource();
}
});
}
}
use of org.apache.commons.io.monitor.FileAlterationListener in project tutorials by eugenp.
the class DirectoryMonitoringExample method main.
public static void main(String[] args) throws Exception {
FileAlterationObserver observer = new FileAlterationObserver(System.getProperty("user.home"));
FileAlterationMonitor monitor = new FileAlterationMonitor(POLL_INTERVAL);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
@Override
public void onFileCreate(File file) {
LOG.debug("File: " + file.getName() + " created");
}
@Override
public void onFileDelete(File file) {
LOG.debug("File: " + file.getName() + " deleted");
}
@Override
public void onFileChange(File file) {
LOG.debug("File: " + file.getName() + " changed");
}
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
}
use of org.apache.commons.io.monitor.FileAlterationListener in project serverless by bluenimble.
the class KeysMonitor method start.
public void start(final BlueNimble bluenimble) throws Exception {
File folder = BlueNimble.keysFolder();
FileAlterationObserver observer = new FileAlterationObserver(folder);
final FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
@Override
public void onFileCreate(File file) {
if (!file.isFile() || !file.getName().endsWith(CliSpec.KeysExt) || file.length() == 0) {
return;
}
try {
BlueNimble.loadKeys(bluenimble, file);
BlueNimble.useKeys(file.getName().substring(0, file.getName().lastIndexOf(Lang.DOT)));
} catch (Exception e) {
bluenimble.printer().error(e.getMessage());
}
}
@Override
public void onFileChange(File file) {
if (!file.isFile() || !file.getName().endsWith(CliSpec.KeysExt) || file.length() == 0) {
return;
}
try {
BlueNimble.loadKeys(bluenimble, file);
BlueNimble.useKeys(file.getName().substring(0, file.getName().lastIndexOf(Lang.DOT)));
} catch (Exception e) {
bluenimble.printer().error(e.getMessage());
}
}
};
observer.addListener(listener);
monitor.addObserver(observer);
new Thread() {
public void run() {
try {
monitor.start();
} catch (Exception e) {
}
}
}.start();
}
Aggregations