Skip to content

Commit

Permalink
Adding BagItWatchServiceIT
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Apr 26, 2013
1 parent 2e0f1f6 commit a6f0531
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/main/java/org/fcrepo/federation/bagit/BagItConnector.java
Expand Up @@ -31,6 +31,7 @@
import org.modeshape.jcr.cache.DocumentStoreException;
import org.modeshape.jcr.federation.spi.DocumentChanges;
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 All @@ -39,9 +40,11 @@ public class BagItConnector extends FileSystemConnector {

private static final String BAGIT_ARCHIVE_TYPE = "bagit:archive";

private static final char JCR_PATH_DELIMITER_CHAR = '/'; // NOT THE File.pathSeparator;
// NOT THE File.pathSeparator;
private static final char JCR_PATH_DELIMITER_CHAR = '/';

private static final String JCR_PATH_DELIMITER = "/"; // NOT THE File.pathSeparator;
// NOT THE File.pathSeparator;
private static final String JCR_PATH_DELIMITER = "/";

private static final String JCR_LAST_MODIFIED = "jcr:lastModified";

Expand Down Expand Up @@ -294,14 +297,6 @@ File getBagItDirectory() {
return this.m_directory;
}

void changeManifest(final File file) {
// do some manifest stuff
}

void changeTagFile(final File file) {
// do some tagFile stuff
}

@Override
protected File fileFor(String id) {
assert id.startsWith(JCR_PATH_DELIMITER);
Expand Down Expand Up @@ -411,4 +406,9 @@ protected BagInfo getBagInfo(final String id) {
vf.getNameFactory(), new BagConstantsImpl());
return result;
}

@Override
protected ConnectorChangedSet newConnectorChangedSet() {
return super.newConnectorChangedSet();
}
}
Expand Up @@ -96,18 +96,18 @@ public WatchKey take() throws InterruptedException {
}

public void monitorTagFile(final File input) throws IOException {
final Path path = Paths.get(input.toURI());
final Path path = input.toPath();
if (!tagFiles.contains(path)) {
tagFiles.add(path);
//path.register(delegate, ENTRY_MODIFY);
path.register(delegate, ENTRY_MODIFY);
}
}

public void monitorManifest(final File input) throws IOException {
final Path path = Paths.get(input.toURI());
final Path path = input.toPath();
if (!manifests.contains(path)) {
manifests.add(path);
//path.register(delegate, ENTRY_MODIFY);
path.register(delegate, ENTRY_MODIFY);
}
}

Expand Down
40 changes: 27 additions & 13 deletions src/main/java/org/fcrepo/federation/bagit/ManifestMonitor.java
Expand Up @@ -2,13 +2,18 @@
package org.fcrepo.federation.bagit;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.util.List;

import org.modeshape.jcr.federation.spi.change.ConnectorChangedSet;
import org.slf4j.Logger;

public class ManifestMonitor implements Runnable {

private final BagItConnector connector;
Expand All @@ -17,8 +22,13 @@ public class ManifestMonitor implements Runnable {

private volatile boolean shutdown;

private static final Logger logger = getLogger(ManifestMonitor.class);

public ManifestMonitor(final BagItConnector connector)
throws IOException {
logger.debug(
"Initializing ManifestMonitor on BagItConnector on directory: {}",
connector.getBagItDirectory());
this.connector = connector;
this.watchService =
new BagItWatchService(connector.getBagItDirectory());
Expand All @@ -27,37 +37,41 @@ public ManifestMonitor(final BagItConnector connector)

@Override
public void run() {
logger.debug("Now executing ManifestMonitor.run()...");
while (!this.shutdown) {
try {
final WatchKey key = watchService.poll(1, SECONDS);
final WatchKey key = watchService.poll(10, SECONDS);
if (key != null) {
final List<WatchEvent<?>> events = key.pollEvents();
boolean manifest = false;
boolean tagManifest = false;
Path path = null;
final Path path = null;
for (final WatchEvent<?> event : events) {
final Path context = (Path) event.context();
final Kind kind = event.kind();
logger.debug(
"Received an event at context: {} of kind: {}",
context.toAbsolutePath(), kind.name());
final ConnectorChangedSet changes =
connector.newConnectorChangedSet();
if (watchService.isManifest(context)) {
manifest = true;
path = context;

final Boolean manifest = true;
} else if (watchService.isTagManifest(context)) {
tagManifest = true;
path = context;
final Boolean tagManifest = true;
}
}
if (manifest) {
connector.changeManifest(path.toFile());
} else if (tagManifest) {
connector.changeTagFile(path.toFile());
}

}
} catch (final InterruptedException e) {
logger.debug("Now ManifestMonitor.run() interrupted.");
this.shutdown = true;
}
}
}

public void shutdown() {
logger.debug(
"Shutting down ManifestMonitor on BagItConnector on directory: {}",
connector.getBagItDirectory());
this.shutdown = true;
}

Expand Down
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;
import gov.loc.repository.bagit.Bag;
import gov.loc.repository.bagit.BagFactory;
import gov.loc.repository.bagit.PreBag;
Expand Down Expand Up @@ -32,16 +33,14 @@
import org.modeshape.jcr.JcrSession;
import org.modeshape.jcr.api.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/master.xml")
public class BagItConnectorIT {

private static Logger logger = LoggerFactory
.getLogger(BagItConnectorIT.class);
private static Logger logger = getLogger(BagItConnectorIT.class);

@Inject
Repository repo;
Expand Down
104 changes: 104 additions & 0 deletions src/test/java/org/fcrepo/federation/bagit/BagItWatchServiceIT.java
@@ -0,0 +1,104 @@

package org.fcrepo.federation.bagit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;
import gov.loc.repository.bagit.Bag;
import gov.loc.repository.bagit.BagFactory;
import gov.loc.repository.bagit.PreBag;
import gov.loc.repository.bagit.transformer.impl.DefaultCompleter;
import gov.loc.repository.bagit.writer.impl.FileSystemWriter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.JcrSession;
import org.slf4j.Logger;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/master.xml")
public class BagItWatchServiceIT {

private static Logger logger = getLogger(BagItWatchServiceIT.class);

@Inject
Repository repo;

@Test
public void tryFilesystemUpdates() throws Exception {
final JcrSession session = (JcrSession) repo.login();

// create a random bag and move it into the federated directory
final File baseDir = new File("./target/test-classes");
final File srcDir = new File(baseDir, "tmp-objects");
final File dstDir = new File(baseDir, "test-objects");
final long fileSize = 1024L;
makeRandomBags(srcDir, 1, 1, fileSize);
final File srcBag = new File(srcDir, "randomBag0");
final File dstBag = new File(dstDir, "randomBag0");
srcBag.renameTo(dstBag);

// check that the bag shows up in the federation
final Node node = session.getNode("/objects/randomBag0");
logger.info("Got node at " + node.getPath());
final PropertyIterator properties = node.getProperties();
assertTrue(properties.hasNext());
final Property property = node.getProperty("bagit:Bag.Size");
assertNotNull(property);
NodeIterator nodes = node.getNodes();
assertTrue("/objects/randomBag0 had no child nodes!", nodes.hasNext());
final Node child = nodes.nextNode();
nodes = child.getNodes();
assertEquals("jcr:content", nodes.nextNode().getName());

logger.debug("Now try tinkering with a manifest");
final File bagInfo = new File(dstBag, "bag-info.txt");
try (final Writer writer = new FileWriter(bagInfo, true)) {
writer.write("FAKETAG: FAKETAGVALUE");
}

}

static void makeRandomBags(final File baseDir, final int bagCount,
final int fileCount, final long fileSize) throws IOException {
final BagFactory factory = new BagFactory();
final DefaultCompleter completer = new DefaultCompleter(factory);
final FileSystemWriter writer = new FileSystemWriter(factory);
for (int i = 0; i < bagCount; i++) {
logger.debug("Creating random bag: " + i);
final File bagDir = new File(baseDir, "randomBag" + i);
final File dataDir = new File(bagDir, "data");
if (!dataDir.exists()) {
dataDir.mkdirs();
}
for (int j = 0; j < fileCount; j++) {
final File dataFile = new File(dataDir, "randomFile" + j);
final BufferedWriter buf =
new BufferedWriter(new FileWriter(dataFile));
for (long k = 0L; k < fileSize; k++) {
buf.write(String.valueOf((int) (Math.random() * 10)));
}
buf.close();
}
final PreBag pre = factory.createPreBag(bagDir);
final Bag bag =
pre.makeBagInPlace(BagFactory.LATEST, true, completer);
bag.write(writer, bagDir);
}
}
}
9 changes: 3 additions & 6 deletions src/test/resources/logback-test.xml
Expand Up @@ -7,16 +7,13 @@
</encoder>
</appender>

<logger name="org.fcrepo" additivity="false" level="TRACE">
<logger name="org.fcrepo" additivity="false" level="DEBUG">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="gov.loc" additivity="false" level="TRACE">
<logger name="gov.loc" additivity="false" level="WARN">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.modeshape" additivity="false" level="TRACE">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.apache.cxf" additivity="false" level="WARN">
<logger name="org.modeshape" additivity="false" level="WARN">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.apache.http.client" additivity="false" level="DEBUG">
Expand Down

0 comments on commit a6f0531

Please sign in to comment.