Skip to content

Commit

Permalink
Add external federation properties store.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedurbin authored and Andrew Woods committed May 27, 2014
1 parent 4da6bb1 commit e05a959
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 3,145 deletions.
3,037 changes: 0 additions & 3,037 deletions compile.out

This file was deleted.

Expand Up @@ -31,21 +31,26 @@
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.io.IOException;
import java.util.Map;

import org.infinispan.schematic.document.Document;
import org.modeshape.connector.filesystem.ExternalJsonSidecarExtraPropertyStore;
import org.modeshape.connector.filesystem.FileSystemConnector;
import org.modeshape.jcr.api.value.DateTime;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.modeshape.jcr.federation.spi.DocumentReader;
import org.modeshape.jcr.federation.spi.DocumentWriter;
import org.modeshape.jcr.federation.spi.ExtraPropertiesStore;
import org.modeshape.jcr.value.BinaryValue;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;
import org.modeshape.jcr.value.basic.BasicSingleValueProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;

/**
* This class extends the {@link FileSystemConnector} to enable the autocreation of Fedora-specific datastream and
* content properties.
Expand All @@ -61,6 +66,33 @@ public class FedoraFileSystemConnector extends FileSystemConnector {
private static final String JCR_CONTENT = "jcr:content";
private static final String JCR_CONTENT_SUFFIX = DELIMITER + JCR_CONTENT;

/**
* The string path for a {@link File} object that represents the top-level directory in which properties are
* stored. This is optional for this connector, but if set allows properties to be cached (greatly
* improving performance) for even read-only connectors. When this property is specified the extraPropertiesStore
* should be null (not specified) as it would be overridden by this.
*/
private String propertiesDirectoryPath;
private File propertiesDirectory;

@Override
public void initialize(final NamespaceRegistry registry,
final NodeTypeManager nodeTypeManager) throws RepositoryException, IOException {
super.initialize(registry, nodeTypeManager);

if (propertiesDirectoryPath != null) {
propertiesDirectory = new File(propertiesDirectoryPath);
if (!propertiesDirectory.exists() || !propertiesDirectory.isDirectory()) {
throw new RepositoryException("Configured \"propertiesDirectory\", " + propertiesDirectoryPath
+ ", does not exist or is not a directory.");
}
if (extraPropertiesStore() != null) {
LOGGER.warn("Extra properties store was specified but won't be used!");
}
setExtraPropertiesStore(new ExternalJsonSidecarExtraPropertyStore(this, translator(), propertiesDirectory));
}
}

/**
* This method returns the object/document for the node with the federated arg 'id'.
* <p/>
Expand Down Expand Up @@ -97,13 +129,21 @@ public Document getDocumentById(final String id) {
}

// Persist new properties (if allowed)
if (!isReadonly()) {
if (shouldCacheProperties()) {
saveProperties(docReader);
}

return docWriter.document();
}

/**
* Checks whether internally managed properties can and should be stored to
* an ExtraPropertiesStore.
*/
protected boolean shouldCacheProperties() {
return extraPropertiesStore() != null && (!isReadonly() || this.propertiesDirectory != null);
}

@Override
public String sha1(final File file) {
final String cachedSha1 = getCachedSha1(file);
Expand Down Expand Up @@ -137,8 +177,7 @@ private String computeAndCacheSha1(final File file) {
final String id = idFor(file) + JCR_CONTENT_SUFFIX;
LOGGER.trace("Computing sha1 for {}.", id);
final String sha1 = super.sha1(file);
final ExtraPropertiesStore cachedPropertiesStore = extraPropertiesStore();
if (cachedPropertiesStore != null) {
if (shouldCacheProperties()) {
final Map<Name, Property> updateMap = new HashMap<Name, Property>();
final Property digestProperty = new BasicSingleValueProperty(nameFrom(CONTENT_DIGEST),
asURI("SHA-1", sha1));
Expand Down
@@ -0,0 +1,63 @@
/**
* Copyright 2014 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.modeshape.connector.filesystem;

import org.modeshape.jcr.cache.document.DocumentTranslator;

import java.io.File;

/**
* An extension of JsonSidecarExtraPropertyStore that stores the properties in a
* separate configured directory than the filesystem federation itself. Because
* the class we're extending is package protected this class is in the
* org.modeshape.connector.filesystem package.
*
* @author Mike Durbin
*/
public class ExternalJsonSidecarExtraPropertyStore extends JsonSidecarExtraPropertyStore {

private FileSystemConnector connector;

private File propertyStoreRoot;

/**
* Default constructor.
* @param connector the FileSystemConnector for which this class will store properties.
* @param propertyStoreRoot the root of a filesystem into which properties will be
* serialized.
*/
public ExternalJsonSidecarExtraPropertyStore(final FileSystemConnector connector,
final DocumentTranslator translator,
final File propertyStoreRoot) {
super(connector, translator);
this.connector = connector;
this.propertyStoreRoot = propertyStoreRoot;
}

@Override
protected File sidecarFile(final String id) {
final File propertyFileInFederation = super.sidecarFile(id);
final String relativePath
= propertyFileInFederation.getPath().substring(connector.fileFor("/").getPath().length());
final File file = new File(propertyStoreRoot,
connector.isRoot(id) ? "federation-root.modeshape.json" : relativePath);
if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
throw new RuntimeException("Unable to create directories " + file.getParentFile() + ".");
}
return file;
}
}

0 comments on commit e05a959

Please sign in to comment.