use of org.apache.sling.api.resource.observation.ResourceChange in project sling by apache.
the class NewJobSender method onChange.
@Override
public void onChange(final List<ResourceChange> resourceChanges) {
for (final ResourceChange resourceChange : resourceChanges) {
logger.debug("Received event {}", resourceChange);
final String path = resourceChange.getPath();
final int topicStart = this.configuration.getLocalJobsPath().length() + 1;
final int topicEnd = path.indexOf('/', topicStart);
if (topicEnd != -1) {
final String topic = path.substring(topicStart, topicEnd).replace('.', '/');
final String jobId = path.substring(topicEnd + 1);
if (path.indexOf("_", topicEnd + 1) != -1) {
// only job id and topic are guaranteed
final Dictionary<String, Object> properties = new Hashtable<>();
properties.put(NotificationConstants.NOTIFICATION_PROPERTY_JOB_ID, jobId);
properties.put(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC, topic);
// we also set internally the queue name
final String queueName = this.configuration.getQueueConfigurationManager().getQueueInfo(topic).queueName;
properties.put(Job.PROPERTY_JOB_QUEUE_NAME, queueName);
final Event jobEvent = new Event(NotificationConstants.TOPIC_JOB_ADDED, properties);
// as this is send within handling an event, we do sync call
this.eventAdmin.sendEvent(jobEvent);
}
}
}
}
use of org.apache.sling.api.resource.observation.ResourceChange in project sling by apache.
the class FileMonitor method collectResourceChanges.
private List<ResourceChange> collectResourceChanges(final Monitorable monitorable, final ChangeType changeType) {
List<ResourceChange> changes = new ArrayList<>();
if (monitorable.status instanceof ContentFileStatus) {
ContentFile contentFile = ((ContentFileStatus) monitorable.status).contentFile;
if (changeType == ChangeType.CHANGED) {
ContentElement content = contentFile.getContent();
// we cannot easily report the diff of resource changes between two content files
// so we simulate a removal of the toplevel node and then add all nodes contained in the current content file again.
changes.add(buildContentResourceChange(ChangeType.REMOVED, transformPath(monitorable.path)));
addContentResourceChanges(changes, ChangeType.ADDED, content, transformPath(monitorable.path));
} else {
addContentResourceChanges(changes, changeType, contentFile.getContent(), transformPath(monitorable.path));
}
} else {
changes.add(buildContentResourceChange(changeType, transformPath(monitorable.path)));
}
return changes;
}
use of org.apache.sling.api.resource.observation.ResourceChange in project sling by apache.
the class FileMonitorTest method testRemoveFolder.
@Test
public void testRemoveFolder() throws Exception {
List<ResourceChange> changes = resourceListener.getChanges();
assertTrue(changes.isEmpty());
File folder1 = new File(tempDir, "folder1");
FileUtils.deleteDirectory(folder1);
Thread.sleep(WAIT_INTERVAL);
assertEquals(2, changes.size());
assertChange(changes, "/fs-test", ChangeType.CHANGED);
assertChange(changes, "/fs-test/folder1", ChangeType.REMOVED);
}
use of org.apache.sling.api.resource.observation.ResourceChange in project sling by apache.
the class FileMonitorTest method testAddFile.
@Test
public void testAddFile() throws Exception {
List<ResourceChange> changes = resourceListener.getChanges();
assertTrue(changes.isEmpty());
File file1c = new File(tempDir, "folder1/file1c.txt");
FileUtils.write(file1c, "newcontent");
Thread.sleep(WAIT_INTERVAL);
assertEquals(2, changes.size());
assertChange(changes, "/fs-test/folder1", ChangeType.CHANGED);
assertChange(changes, "/fs-test/folder1/file1c.txt", ChangeType.ADDED);
}
use of org.apache.sling.api.resource.observation.ResourceChange in project sling by apache.
the class FileVaultFileMonitorTest method testUpdateFile.
@Test
public void testUpdateFile() throws Exception {
List<ResourceChange> changes = resourceListener.getChanges();
assertTrue(changes.isEmpty());
File file = new File(tempDir, "jcr_root/content/dam/talk.png/_jcr_content/renditions/web.1280.1280.png");
FileUtils.touch(file);
Thread.sleep(WAIT_INTERVAL);
assertEquals(1, changes.size());
assertChange(changes, "/content/dam/talk.png/jcr:content/renditions/web.1280.1280.png", ChangeType.CHANGED);
}
Aggregations