Search in sources :

Example 6 with ResourceChange

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);
            }
        }
    }
}
Also used : Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Example 7 with ResourceChange

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;
}
Also used : ContentFile(org.apache.sling.fsprovider.internal.mapper.ContentFile) ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) ArrayList(java.util.ArrayList) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Example 8 with ResourceChange

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);
}
Also used : ResourceChange(org.apache.sling.api.resource.observation.ResourceChange) File(java.io.File) Test(org.junit.Test)

Example 9 with ResourceChange

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);
}
Also used : ResourceChange(org.apache.sling.api.resource.observation.ResourceChange) File(java.io.File) Test(org.junit.Test)

Example 10 with ResourceChange

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);
}
Also used : ResourceChange(org.apache.sling.api.resource.observation.ResourceChange) File(java.io.File) Test(org.junit.Test)

Aggregations

ResourceChange (org.apache.sling.api.resource.observation.ResourceChange)38 Test (org.junit.Test)20 File (java.io.File)17 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 Event (org.osgi.service.event.Event)2 ImmutableList (com.google.common.collect.ImmutableList)1 SoftReference (java.lang.ref.SoftReference)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 Event (javax.jcr.observation.Event)1 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)1