Skip to content

Commit

Permalink
Now with two event filter implementations. NOOP does nothing, Default…
Browse files Browse the repository at this point in the history
… looks only at nodes with a mixin type in the fedora namespace
  • Loading branch information
ajs6f committed Jan 31, 2013
1 parent 9c0e7ee commit ee42888
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
60 changes: 58 additions & 2 deletions src/main/java/org/fcrepo/modeshape/observer/DefaultFilter.java
@@ -1,10 +1,66 @@
package org.fcrepo.modeshape.observer;

import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.jcr.observation.Event;

import org.modeshape.common.SystemFailureException;
import org.modeshape.jcr.api.Repository;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

public class DefaultFilter implements EventFilter {

@Inject
private Repository repository;

// it's safe to keep the session around, because this is a non-mutating actor
private Session session;

@Override
public boolean apply(Event event) {
return true;
Predicate<NodeType> isFedoraNodeType = new Predicate<NodeType>() {
@Override
public boolean apply(NodeType type) {
return type.getName().startsWith("fedora:");
}
};

try {
Node node = null;
try {
node = session.getNode(event.getPath());
} catch (PathNotFoundException e) {
return false; // not a node in the fedora workspace
}
Set<NodeType> types = Sets.newHashSet(node.getMixinNodeTypes());
return Iterables.any(types, isFedoraNodeType);

} catch (LoginException e) {
throw new SystemFailureException(e);
} catch (RepositoryException e) {
throw new SystemFailureException(e);
}
}

@PostConstruct
public void acquireSession() throws LoginException, RepositoryException {
session = repository.login();
}

@PreDestroy
public void releaseSession() {
session.logout();
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/fcrepo/modeshape/observer/NOOPFilter.java
@@ -0,0 +1,10 @@
package org.fcrepo.modeshape.observer;

import javax.jcr.observation.Event;

public class NOOPFilter implements EventFilter {
@Override
public boolean apply(Event event) {
return true;
}
}
Expand Up @@ -35,7 +35,10 @@ public void TestSimpleIntegration() throws RepositoryException {
eventBus.register(this);

Session se = repository.login();
se.getRootNode().addNode("/simple-integration-test");
Node testnode = se.getRootNode().addNode("/object1");
testnode.addMixin("fedora:object");
Node testnode2 = se.getRootNode().addNode("/object2");
testnode2.addMixin("fedora:object");
se.save();
se.logout();

Expand Down

0 comments on commit ee42888

Please sign in to comment.