Permalink
Browse files

rolling back to just a single bag, almost working

  • Loading branch information...
1 parent e747b29 commit 335379cc8cafab55c2bca4a7b431f0810c3b7b29 @barmintor barmintor committed Apr 9, 2013
@@ -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;
@@ -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";
@@ -49,7 +53,9 @@
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
@@ -118,7 +124,7 @@ public void shutdown() {
threadPool.shutdown();
getLogger().trace("Threadpool shutdown.");
}
-
+
@Override
public Document getDocumentById(String id) {
getLogger().trace("Entering getDocumentById()...");
@@ -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()
@@ -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);
}
@@ -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
@@ -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() {
@@ -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;
}
}
@@ -85,6 +85,7 @@ public void updateProperties(String id, Map<Name, Property> properties) {
ImmutableMap.Builder<Name, Property> properties =
ImmutableMap.builder();
File bagInfo = bagInfoFile(id);
+ if (bagInfo.exists()) {
try {
try (BufferedReader buf =
new BufferedReader(new FileReader(bagInfo))) {
@@ -132,6 +133,7 @@ public void updateProperties(String id, Map<Name, Property> properties) {
} catch (Exception ex) {
throw new DocumentStoreException(id, ex);
}
+ }
return properties.build();
}
@@ -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;
@@ -59,6 +59,7 @@
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);
@@ -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) {
@@ -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;
@@ -24,6 +27,9 @@
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/master.xml")
public abstract class AbstractResourceIT {
+
+ @Inject
+ protected Repository repo;
protected Logger logger;
@@ -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());
}
}
@@ -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">
@@ -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 => /" ]

0 comments on commit 335379c

Please sign in to comment.