Skip to content

Commit

Permalink
rolling back to just a single bag, almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Apr 9, 2013
1 parent e747b29 commit 335379c
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 19 deletions.
81 changes: 71 additions & 10 deletions src/main/java/org/fcrepo/federation/bagit/BagItConnector.java
Expand Up @@ -15,6 +15,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
Expand All @@ -25,15 +26,18 @@
import org.modeshape.jcr.JcrI18n;
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.DocumentChanges;
import org.modeshape.jcr.federation.spi.DocumentWriter;
import org.modeshape.jcr.value.BinaryValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BagItConnector extends FileSystemConnector {

private static final String FILE_SEPARATOR = File.separator;

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

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

Expand All @@ -49,7 +53,9 @@ public class BagItConnector extends FileSystemConnector {

private static final String JCR_ENCODING = "jcr:encoding";

private static final String JCR_CONTENT_SUFFIX = DELIMITER + JCR_CONTENT;
private static final String JCR_CONTENT_SUFFIX = FILE_SEPARATOR + JCR_CONTENT;

private static final int JCR_CONTENT_SUFFIX_LENGTH = JCR_CONTENT_SUFFIX.length();

/**
* A boolean flag that specifies whether this connector should add the 'mix:mimeType' mixin to the 'nt:resource' nodes to
Expand Down Expand Up @@ -118,7 +124,7 @@ public void shutdown() {
threadPool.shutdown();
getLogger().trace("Threadpool shutdown.");
}

@Override
public Document getDocumentById(String id) {
getLogger().trace("Entering getDocumentById()...");
Expand Down Expand Up @@ -175,7 +181,9 @@ public Document getDocumentById(String id) {
getLogger().debug(
"Determined document: " + id + " to be a Fedora object.");
final File dataDir =
new File(file.getAbsolutePath() + DELIMITER + "data");
new File(file.getAbsolutePath() + FILE_SEPARATOR + "data");
getLogger().debug("searching data dir " +
dataDir.getAbsolutePath());
writer.setPrimaryType(NT_FOLDER);
writer.addMixinType(FEDORA_OBJECT);
writer.addProperty(JCR_CREATED, factories().getDateFactory()
Expand All @@ -190,7 +198,7 @@ public Document getDocumentById(String id) {
// We use identifiers that contain the file/directory name ...
String childName = child.getName();
String childId =
isRoot ? DELIMITER + childName : id + DELIMITER +
isRoot ? FILE_SEPARATOR + childName : id + FILE_SEPARATOR +
childName;
writer.addChild(childId, childName);
}
Expand All @@ -199,7 +207,8 @@ public Document getDocumentById(String id) {

if (!isRoot) {
// Set the reference to the parent ...
writer.setParents(idFor(parentFile));
String parentId = idFor(parentFile);
writer.setParents(parentId);
}

// Add the extra properties (if there are any), overwriting any properties with the same names
Expand All @@ -212,13 +221,13 @@ public Document getDocumentById(String id) {
@Override
public void storeDocument(Document document) {
// TODO Auto-generated method stub

getLogger().debug("storeDocument(" + document.toString() + ")");
}

@Override
public void updateDocument(DocumentChanges documentChanges) {
// TODO Auto-generated method stub

getLogger().debug("updateDocument(" + documentChanges.toString() + ")");
}

File getBagItDirectory() {
Expand All @@ -233,7 +242,59 @@ void changeTagFile(File file) {
// do some tagFile stuff
}

@Override
protected File fileFor( String id ) {
return super.fileFor(id);
assert id.startsWith(DELIMITER);
if (id.endsWith(DELIMITER)) {
id = id.substring(0, id.length() - DELIMITER.length());
}
if (isContentNode(id)) {
id = id.substring(0, id.length() - JCR_CONTENT_SUFFIX_LENGTH);
}
if ("".equals(id)) return this.directory; // root node

if (isContentNode(id)) {
id = id.substring(0, id.length() - JCR_CONTENT_SUFFIX_LENGTH);
}

File result = new File(this.directory, "data" + id.replaceAll(DELIMITER, FILE_SEPARATOR));
getLogger().debug(result.getAbsolutePath());
//return super.fileFor(id);
return result;
}

@Override
protected boolean isExcluded(File file) {
return !file.exists();
}

@Override
/**
* DIRECTLY COPIED UNTIL WE SORT OUT HOW TO EFFECTIVELY SUBCLASS
* Utility method for determining the node identifier for the supplied file. Subclasses may override this method to change the
* format of the identifiers, but in that case should also override the {@link #fileFor(String)},
* {@link #isContentNode(String)}, and {@link #isRoot(String)} methods.
*
* @param file the file; may not be null
* @return the node identifier; never null
* @see #isRoot(String)
* @see #isContentNode(String)
* @see #fileFor(String)
*/
protected String idFor( File file ) {
String path = file.getAbsolutePath();
if (!path.startsWith(directoryAbsolutePath)) {
if (directory.getAbsolutePath().equals(path)) {
// This is the root
return FILE_SEPARATOR;
}
String msg = JcrI18n.fileConnectorNodeIdentifierIsNotWithinScopeOfConnector.text(getSourceName(), directoryPath, path);
throw new DocumentStoreException(path, msg);
}
String id = path.substring(directoryAbsolutePathLength + 5); // data dir
id = id.replaceAll(Pattern.quote(FILE_SEPARATOR), DELIMITER);
if ("".equals(id)) id = DELIMITER;
assert id.startsWith(DELIMITER);
return id;
}
}
Expand Up @@ -85,6 +85,7 @@ public Map<Name, Property> getProperties(String id) {
ImmutableMap.Builder<Name, Property> properties =
ImmutableMap.builder();
File bagInfo = bagInfoFile(id);
if (bagInfo.exists()) {
try {
try (BufferedReader buf =
new BufferedReader(new FileReader(bagInfo))) {
Expand Down Expand Up @@ -132,6 +133,7 @@ public Map<Name, Property> getProperties(String id) {
} catch (Exception ex) {
throw new DocumentStoreException(id, ex);
}
}
return properties.build();
}

Expand Down
@@ -1,7 +1,7 @@

package org.fcrepo.federation.bagit;

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.*;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
Expand Down Expand Up @@ -59,6 +59,7 @@ public class BagItWatchService implements WatchService {
public BagItWatchService(File bagItDir)
throws IOException {
this();
Paths.get(bagItDir.toURI()).register(delegate, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
for (File file : bagItDir.listFiles()) {
if (isManifest(file)) {
monitorManifest(file);
Expand Down Expand Up @@ -93,13 +94,13 @@ public WatchKey take() throws InterruptedException {
public void monitorTagFile(File input) throws IOException {
Path path = Paths.get(input.toURI());
if (!tagFiles.contains(path)) tagFiles.add(path);
path.register(delegate, ENTRY_MODIFY);
//path.register(delegate, ENTRY_MODIFY);
}

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

boolean isManifest(String fileName) {
Expand Down
Expand Up @@ -5,6 +5,9 @@
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.jcr.Repository;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
Expand All @@ -24,6 +27,9 @@
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/master.xml")
public abstract class AbstractResourceIT {

@Inject
protected Repository repo;

protected Logger logger;

Expand Down
33 changes: 30 additions & 3 deletions src/test/java/org/fcrepo/federation/bagit/BagItConnectorIT.java
Expand Up @@ -2,26 +2,53 @@
package org.fcrepo.federation.bagit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import javax.inject.Inject;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.fcrepo.services.ObjectService;
import org.junit.Ignore;
import org.junit.Test;
import org.modeshape.jcr.api.Session;
import org.modeshape.jcr.api.federation.FederationManager;
import org.modeshape.jcr.cache.NodeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BagItConnectorIT extends AbstractResourceIT {
private static Logger logger = LoggerFactory.getLogger(BagItConnectorIT.class);

@Test
@Ignore
public void tryProgrammaticAccess() throws RepositoryException {
Session session = (Session)repo.login();
NodeKey key = new NodeKey("/objects/testDS");
Node node = session.getNode("/objects/testDS");
logger.info("Got node at " + node.getPath());
NodeIterator nodes = node.getNodes();
assertTrue("/objects/testDS had no child nodes!", nodes.hasNext());
assertEquals("jcr:content", nodes.nextNode().getName());
}

@Test
public void tryOneObject() throws ClientProtocolException, IOException {
public void tryOneObject() throws ClientProtocolException, IOException {
logger.debug("Found objects: " +
EntityUtils.toString(client.execute(
new HttpGet(serverAddress + "objects/")).getEntity()));
final String objName = "BagItFed1";
final String objName = "testDS";
final HttpResponse response =
client.execute(new HttpGet(serverAddress + "objects/" + objName));
assertEquals("Couldn't find federated object!", 200, response
assertEquals(response.getStatusLine().getReasonPhrase(), 200, response
.getStatusLine().getStatusCode());
}
}
2 changes: 1 addition & 1 deletion src/test/resources/logback-test.xml
Expand Up @@ -13,7 +13,7 @@
<logger name="gov.loc" additivity="false" level="TRACE">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.modeshape" additivity="false" level="WARN">
<logger name="org.modeshape" additivity="false" level="TRACE">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.apache.cxf" additivity="false" level="WARN">
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/test_repository.json
Expand Up @@ -7,9 +7,9 @@
"allowCreation" : true
},
"externalSources" : {
"filesystem-objects" : {
"targetDirectory" : {
"classname" : "org.fcrepo.federation.bagit.BagItConnector",
"directoryPath" : "target/test-classes/test-objects",
"directoryPath" : "target/test-classes/test-objects/BagItFed1",
"readonly" : false,
"cacheTtlSeconds" : 5,
"projections" : [ "fedora:/objects => /" ]
Expand Down

0 comments on commit 335379c

Please sign in to comment.