Skip to content

Commit

Permalink
factored out the BagItWatchService, as we didn't need to subclass Wat…
Browse files Browse the repository at this point in the history
…chService

added events to the connector for when a bag node is added/removed, triggered on manifest file
added integration test code that includes testing for add and remove of projected node events
separated some of the manifest file pattern matching code into a util class
  • Loading branch information
gregjan committed Jun 10, 2013
1 parent eb565ab commit 0706091
Show file tree
Hide file tree
Showing 11 changed files with 517 additions and 434 deletions.
9 changes: 5 additions & 4 deletions pom.xml
Expand Up @@ -9,9 +9,6 @@
<artifactId>fcrepo-bagit-modeshape-federation-connector</artifactId>
<name>fcrepo-bagit-modeshape-federation-connector</name>
<description>Connects ModeShape to a filesystem directory containing BagIt directories.</description>
<properties>
<modeshape.version>3.3-SNAPSHOT</modeshape.version>
</properties>
<dependencies>
<dependency>
<groupId>org.modeshape</groupId>
Expand Down Expand Up @@ -44,6 +41,10 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -57,4 +58,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
65 changes: 48 additions & 17 deletions src/main/java/org/fcrepo/federation/bagit/BagItConnector.java
Expand Up @@ -11,6 +11,8 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
Expand All @@ -29,9 +31,10 @@
import org.modeshape.jcr.api.JcrConstants;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.modeshape.jcr.cache.DocumentStoreException;
import org.modeshape.jcr.federation.spi.ConnectorChangeSet;
import org.modeshape.jcr.federation.spi.DocumentChanges;
import org.modeshape.jcr.federation.spi.DocumentReader;
import org.modeshape.jcr.federation.spi.DocumentWriter;
import org.modeshape.jcr.federation.spi.change.ConnectorChangedSet;
import org.modeshape.jcr.value.BinaryValue;
import org.modeshape.jcr.value.PropertyFactory;
import org.modeshape.jcr.value.ValueFactories;
Expand Down Expand Up @@ -83,6 +86,8 @@ public class BagItConnector extends FileSystemConnector {
// so this is a dummy file for that situation
private File m_directory = TempFile.createTempFile("stub", "stub");

private Path rootPath;

/**
* A string that is created in the {@link #initialize(NamespaceRegistry, NodeTypeManager)} method that represents the absolute
* path to the {@link #m_directory}. This path is removed from an absolute path of a file to obtain the ID of the node.
Expand Down Expand Up @@ -135,6 +140,8 @@ public void initialize(final NamespaceRegistry registry,
directoryAbsolutePathLength =
directoryAbsolutePath.length() - File.separator.length(); // does NOT include the separator

rootPath = Paths.get(directoryAbsolutePath);

setExtraPropertiesStore(new BagItExtraPropertiesStore(this));
getLogger().trace("Initialized.");
final BlockingQueue<Runnable> workQueue =
Expand All @@ -155,11 +162,11 @@ public void shutdown() {
@Override
public Document getDocumentById(final String id) {
getLogger().trace("Entering getDocumentById()...");
getLogger().debug("Received request for document: " + id);
getLogger().trace("Received request for document: " + id);
final File file = fileFor(id);
getLogger().debug(
"Received request for document: " + id + ", resolved to " +
file);
//getLogger().debug(
//"Received request for document: " + id + ", resolved to " +
// file);
if (file == null || isExcluded(file) || !file.exists()) {
return null;
}
Expand All @@ -168,7 +175,7 @@ public Document getDocumentById(final String id) {
final DocumentWriter writer = newDocument(id);
File parentFile = file.getParentFile();
if (isRoot) {
getLogger().debug(
getLogger().trace(
"Determined document: " + id +
" to be the projection root.");
writer.setPrimaryType(NT_FOLDER);
Expand All @@ -189,7 +196,7 @@ public Document getDocumentById(final String id) {
}
}
} else if (isResource) {
getLogger().debug(
getLogger().trace(
"Determined document: " + id + " to be a binary resource.");
final BinaryValue binaryValue = binaryFor(file);
writer.setPrimaryType(NT_RESOURCE);
Expand All @@ -215,8 +222,8 @@ public Document getDocumentById(final String id) {
writer.setNotQueryable();
parentFile = file;
} else if (file.isFile()) {
getLogger().debug(
"Determined document: " + id + " to be a datastream.");
getLogger().trace(
"Determined document: " + id + " to be a datastream.");
writer.setPrimaryType(JcrConstants.NT_FILE);
writer.addProperty(JCR_CREATED, factories().getDateFactory()
.create(file.lastModified()));
Expand All @@ -230,12 +237,12 @@ public Document getDocumentById(final String id) {
isRoot ? JCR_CONTENT_SUFFIX : id + JCR_CONTENT_SUFFIX;
writer.addChild(childId, JCR_CONTENT);
} else {
getLogger().debug(
getLogger().trace(
"Determined document: " + id + " to be a Fedora object.");
final File dataDir =
new File(new File(file.getAbsolutePath()), "data");
getLogger()
.debug("searching data dir " + dataDir.getAbsolutePath());
.trace("searching data dir " + dataDir.getAbsolutePath());
writer.setPrimaryType(NT_FOLDER);
writer.addMixinType(BAGIT_ARCHIVE_TYPE);
writer.addProperty(JCR_CREATED, factories().getDateFactory()
Expand Down Expand Up @@ -307,7 +314,7 @@ protected File fileFor(String id) {
id = id.substring(0, id.length() - JCR_CONTENT_SUFFIX_LENGTH);
}
if ("".equals(id)) {
getLogger().debug(
getLogger().trace(
"#fileFor returning root directory for \"" + id + "\"");
return this.m_directory; // root node
}
Expand All @@ -327,7 +334,7 @@ protected File fileFor(String id) {
final File result =
new File(this.m_directory, id.replace(JCR_PATH_DELIMITER_CHAR,
File.separatorChar));
getLogger().debug(result.getAbsolutePath());
getLogger().trace(result.getAbsolutePath());
//return super.fileFor(id);
return result;
}
Expand Down Expand Up @@ -381,7 +388,7 @@ protected String idFor(final File file) {
id = JCR_PATH_DELIMITER;
}
assert id.startsWith(JCR_PATH_DELIMITER);
System.out.println("idFor = " + id);
//System.out.println("idFor = " + id);
return id;
}

Expand All @@ -407,8 +414,32 @@ protected BagInfo getBagInfo(final String id) {
return result;
}

@Override
protected ConnectorChangedSet newConnectorChangedSet() {
return super.newConnectorChangedSet();
/**
* Sends a change set with a new node event for the bag.
* @param p the path to the bag folder
*/
protected void fireNewBagEvent(Path path) {
ConnectorChangeSet changes = newConnectorChangedSet();
String key = idFor(path.toFile());
Document doc = getDocumentById(key);
DocumentReader reader = readDocument(doc);
getLogger().debug("firing new bag node event with\n\tkey {0}\n\tpathToNode {1}",
key, key);
changes.nodeCreated(key,
"/",
key, reader.getProperties());
changes.publish(null);
}

/**
* @param path the path of the bag folder
*/
public void fireRemoveBagEvent(Path path) {
ConnectorChangeSet changes = newConnectorChangedSet();
String key = idFor(path.toFile());
getLogger().debug("firing remove bag node event with\n\tkey {0}\n\tpathToNode {1}",
key, key);
changes.nodeRemoved(key, "/", key);
changes.publish(null);
}
}
Expand Up @@ -78,10 +78,10 @@ public Map<Name, Property> getProperties(final String id) {

final BagInfo bagInfo = connector.getBagInfo(id);
if (bagInfo == null) {
logger.debug("No bag-info.txt for " + id);
if(!"/".equals(id)) logger.trace("No bag-info.txt for " + id);
return EMPTY;
}
logger.debug("Operating on bagInfoFile(" + id + "):" +
logger.trace("Operating on bagInfoFile(" + id + "):" +
bagInfo.getFilepath());
try {
return bagInfo.getProperties();
Expand Down
190 changes: 0 additions & 190 deletions src/main/java/org/fcrepo/federation/bagit/BagItWatchService.java

This file was deleted.

0 comments on commit 0706091

Please sign in to comment.