blob: 3b4836657cff48c5d7e7ca181230662e88c12b60 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.jcr.resource.internal;
import static javax.jcr.observation.Event.NODE_ADDED;
import static javax.jcr.observation.Event.NODE_REMOVED;
import static javax.jcr.observation.Event.PROPERTY_ADDED;
import static javax.jcr.observation.Event.PROPERTY_CHANGED;
import static javax.jcr.observation.Event.PROPERTY_REMOVED;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.api.observation.JackrabbitEvent;
import org.apache.sling.api.resource.observation.ResourceChange;
import org.apache.sling.api.resource.observation.ResourceChange.ChangeType;
import org.apache.sling.spi.resource.provider.ObservationReporter;
import org.apache.sling.spi.resource.provider.ObserverConfiguration;
/**
* The <code>JcrResourceListener</code> listens for JCR observation
* events and creates resource change events which are sent through
* the {@link ObservationReporter}.
*/
public class JcrResourceListener implements EventListener, Closeable {
private volatile ObserverConfiguration config;
private final JcrListenerBaseConfig baseConfig;
public JcrResourceListener(final JcrListenerBaseConfig listenerConfig,
final ObserverConfiguration config)
throws RepositoryException {
this.baseConfig = listenerConfig;
this.config = config;
this.baseConfig.register(this, config);
}
public void update(final ObserverConfiguration cfg) {
this.config = cfg;
}
/**
* Dispose this listener.
*/
@Override
public void close() throws IOException {
// unregister from observations
this.baseConfig.unregister(this);
}
/**
* @see javax.jcr.observation.EventListener#onEvent(javax.jcr.observation.EventIterator)
*/
@Override
public void onEvent(final EventIterator events) {
final Map<String, ResourceChange> addedEvents = new HashMap<String, ResourceChange>();
final Map<String, ResourceChange> changedEvents = new HashMap<String, ResourceChange>();
final Map<String, ResourceChange> removedEvents = new HashMap<String, ResourceChange>();
AtomicBoolean refreshedSession = new AtomicBoolean(false);
while ( events.hasNext() ) {
final Event event = events.nextEvent();
try {
final String eventPath = event.getPath();
final int type = event.getType();
if ( type == PROPERTY_ADDED
|| type == PROPERTY_REMOVED
|| type == PROPERTY_CHANGED ) {
final int lastSlash = eventPath.lastIndexOf('/');
final String rsrcPath = stripNtFilePath(eventPath.substring(0, lastSlash), refreshedSession);
if ( !addedEvents.containsKey(rsrcPath)
&& !removedEvents.containsKey(rsrcPath)
&& !changedEvents.containsKey(rsrcPath) ) {
changedEvents.put(rsrcPath, createResourceChange(event, rsrcPath, ChangeType.CHANGED));
}
} else {
if ( type == NODE_ADDED ) {
// add is stronger than update
changedEvents.remove(eventPath);
addedEvents.put(eventPath, createResourceChange(event, eventPath, ChangeType.ADDED));
} else if ( type == NODE_REMOVED) {
// remove is stronger than add and change
addedEvents.remove(eventPath);
changedEvents.remove(eventPath);
removedEvents.put(eventPath, createResourceChange(event, eventPath, ChangeType.REMOVED));
}
}
} catch (final RepositoryException e) {
this.baseConfig.getLogger().error("Error during modification: {}", e);
}
}
final List<ResourceChange> changes = new ArrayList<ResourceChange>();
changes.addAll(addedEvents.values());
changes.addAll(removedEvents.values());
changes.addAll(changedEvents.values());
this.baseConfig.getReporter().reportChanges(this.config, changes, false);
}
private ResourceChange createResourceChange(final Event event,
final String path,
final ChangeType changeType) {
final String fullPath = this.baseConfig.getPathMapper().mapJCRPathToResourcePath(path);
final boolean isExternal = this.isExternal(event);
final String userId;
if (!isExternal) {
userId = event.getUserID();
} else {
userId = null;
}
return new JcrResourceChange(changeType, fullPath, isExternal, userId);
}
private boolean isExternal(final Event event) {
if ( event instanceof JackrabbitEvent) {
final JackrabbitEvent jEvent = (JackrabbitEvent)event;
return jEvent.isExternal();
}
return false;
}
private static final String JCR_CONTENT_POSTFIX = "/" + JcrConstants.JCR_CONTENT;
private String stripNtFilePath(final String path, final AtomicBoolean refreshedSession) {
if (!path.endsWith(JCR_CONTENT_POSTFIX)) {
return path;
}
try {
if ( refreshedSession.compareAndSet(false, true) ) {
baseConfig.getSession().refresh(false);
}
final Node node = baseConfig.getSession().getNode(path);
final Node parent = node.getParent();
if (parent.isNodeType(JcrConstants.NT_FILE)) {
return parent.getPath();
} else {
return path;
}
} catch (final RepositoryException e) {
return path;
}
}
@Override
public String toString() {
return "JcrResourceListener [" + config + "]";
}
}