Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove org.modeshape exports from fcrepo-connector-file bundle
  • Loading branch information
acoburn authored and Andrew Woods committed May 15, 2015
1 parent 31e068b commit dff2d5f
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 76 deletions.
3 changes: 1 addition & 2 deletions fcrepo-connector-file/pom.xml
Expand Up @@ -64,8 +64,7 @@
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Export-Package>
org.fcrepo.connector;version=${project.version},
org.fcrepo.connector.file;version=${project.version},
org.modeshape.connector.filesystem;version=${project.version}
org.fcrepo.connector.file;version=${project.version}
</Export-Package>
<Import-Package>
org.fcrepo.kernel,
Expand Down
@@ -0,0 +1,223 @@
/**
* Copyright 2015 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.fcrepo.connector.file;

import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.infinispan.schematic.Schematic;
import org.infinispan.schematic.document.Document;
import org.infinispan.schematic.document.EditableDocument;
import org.infinispan.schematic.document.Json;
import org.modeshape.jcr.cache.document.DocumentTranslator;
import org.modeshape.jcr.spi.federation.ExtraPropertiesStore;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;

import java.util.Collections;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
* An implementation of ExtraPropertyStore, based on
* org.modeshape.connector.filesystem.JsonSidecarExtraPropertyStore that stores the
* properties in a separate configured directory than the filesystem federation itself.
*
* @author Mike Durbin
* @author acoburn
*/
public class ExternalJsonSidecarExtraPropertyStore implements ExtraPropertiesStore {

private FedoraFileSystemConnector connector;

private File propertyStoreRoot;

private final DocumentTranslator translator;

/**
* 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 FedoraFileSystemConnector connector,
final DocumentTranslator translator,
final File propertyStoreRoot) {
this.connector = connector;
this.translator = translator;
this.propertyStoreRoot = propertyStoreRoot;
}

protected File sidecarFile(final String id) {
final File file;
if (connector.isRoot(id)) {
file = new File(propertyStoreRoot, "federation-root.modeshape.json");
} else {
String ext = ".modeshape.json";
if (connector.isContentNode(id)) {
ext = ".content.modeshape.json";
}
final File f = new File(connector.fileFor(id).getAbsolutePath() + ext);

final Path propertyFileInFederation = f.getAbsoluteFile().toPath();
final Path relativePath = connector.fileFor("/")
.getAbsoluteFile().toPath().relativize(propertyFileInFederation);
file = propertyStoreRoot.toPath().resolve(relativePath).toFile();
}
if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
throw new RepositoryRuntimeException("Unable to create directories " + file.getParentFile() + ".");
}
return file;
}

/**
* This is a trivial reimplementation of the private modeshape implementation in
* org.modeshape.connector.filesystem.JsonSidecarExtraPropertyStore
*
* See: https://github.com/ModeShape/modeshape/blob/modeshape-4.2.0.Final/modeshape-jcr/src/main/java/
* org/modeshape/connector/filesystem/JsonSidecarExtraPropertyStore.java#L139
*
* @param id the identifier for the sidecar file
* @return whether the file was deleted
*/
@Override
public boolean removeProperties(final String id) {
final File file = sidecarFile(id);
if (!file.exists()) {
return false;
}
file.delete();
return true;
}


/**
* This is a trivial reimplementation of the private modeshape implementation in
* org.modeshape.connector.filesystem.JsonSidecarExtraPropertyStore
*
* See: https://github.com/ModeShape/modeshape/blob/modeshape-4.2.0.Final/modeshape-jcr/src/main/java/
* org/modeshape/connector/filesystem/JsonSidecarExtraPropertyStore.java#L60
*
* @param id the identifier for the sidecar file
* @return a map of the properties associated with the given configuration
*/
@Override
public Map<Name, Property> getProperties(final String id) {
final File sidecarFile = sidecarFile(id);
if (!sidecarFile.exists()) {
return Collections.emptyMap();
}
try {
final Document document = Json.read(new FileInputStream(sidecarFile), false);
final Map<Name, Property> results = new HashMap<Name, Property>();
translator.getProperties(document, results);
return results;
} catch (IOException e) {
throw new RepositoryRuntimeException(id, e);
}
}

/**
* This is a trivial reimplementation of the private modeshape implementation in
* org.modeshape.connector.filesystem.JsonSidecarExtraPropertyStore
*
* See: https://github.com/ModeShape/modeshape/blob/modeshape-4.2.0.Final/modeshape-jcr/src/main/java/
* org/modeshape/connector/filesystem/JsonSidecarExtraPropertyStore.java#L74
*
* @param id the id for the sidecar file
* @param properties the keys/values to set in the specified sidecar configuration
*/
@Override
public void updateProperties(final String id, final Map<Name, Property> properties ) {
final File sidecarFile = sidecarFile(id);
try {
EditableDocument document = null;
if (!sidecarFile.exists()) {
if (properties.isEmpty()) {
return;
}
sidecarFile.createNewFile();
document = Schematic.newDocument();
} else {
final Document existing = Json.read(new FileInputStream(sidecarFile), false);
document = Schematic.newDocument(existing);
}
for (Map.Entry<Name, Property> entry : properties.entrySet()) {
final Property property = entry.getValue();
if (property == null) {
translator.removeProperty(document, entry.getKey(), null, null);
} else {
translator.setProperty(document, property, null, null);
}
}
Json.write(document, new FileOutputStream(sidecarFile));
} catch (IOException e) {
throw new RepositoryRuntimeException(id, e);
}
}

/**
* This is a trivial reimplementation of the private modeshape implementation in
* org.modeshape.connector.filesystem.JsonSidecarExtraPropertyStore
*
* See: https://github.com/ModeShape/modeshape/blob/modeshape-4.2.0.Final/modeshape-jcr/src/main/java/
* org/modeshape/connector/filesystem/JsonSidecarExtraPropertyStore.java#L102
*
* @param id the id for the sidecar file
* @param properties the keys/values to set in the specified sidecar configuration
*/
@Override
public void storeProperties(final String id, final Map<Name, Property> properties ) {
final File sidecarFile = sidecarFile(id);
try {
if (!sidecarFile.exists()) {
if (properties.isEmpty()) {
return;
}
sidecarFile.createNewFile();
}
final EditableDocument document = Schematic.newDocument();
for (Property property : properties.values()) {
if (property == null) {
continue;
}
translator.setProperty(document, property, null, null);
}
Json.write(document, new FileOutputStream(sidecarFile));
} catch (IOException e) {
throw new RepositoryRuntimeException(id, e);
}
}

/**
* This is a trivial reimplementation of the private modeshape implementation in
* org.modeshape.connector.filesystem.JsonSidecarExtraPropertyStore
*
* See: https://github.com/ModeShape/modeshape/blob/modeshape-4.2.0.Final/modeshape-jcr/src/main/java/
* org/modeshape/connector/filesystem/JsonSidecarExtraPropertyStore.java#L156
*
* @param id the id for the sidecar file
* @return whether the specified sidecar configuration exists
*/
@Override
public boolean contains(final String id) {
return sidecarFile(id).exists();
}

}
Expand Up @@ -41,7 +41,6 @@

import org.fcrepo.kernel.exception.RepositoryRuntimeException;
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;
Expand Down Expand Up @@ -165,6 +164,24 @@ protected boolean shouldCacheProperties() {
return extraPropertiesStore() != null && (!isReadonly() || this.propertiesDirectory != null);
}


/**
* Pass-thru to the parent class in order to make this function public
*
* @param id the node ID to test
* @return whether the id corresponds to the root location
*/
@Override
public boolean isRoot(final String id) {
return super.isRoot(id);
}

/**
* Pass-thru to the parent class in order to make this function public
*
* @param file the file used to compute a sha1 hash
* @return the sha1 hash of the file contents
*/
@Override
public String sha1(final File file) {
final String cachedSha1 = getCachedSha1(file);
Expand All @@ -174,7 +191,6 @@ public String sha1(final File file) {
return cachedSha1;
}


private String getCachedSha1(final File file) {
final String id = idFor(file) + JCR_CONTENT_SUFFIX;
if (extraPropertiesStore() != null) {
Expand Down Expand Up @@ -210,8 +226,6 @@ private String computeAndCacheSha1(final File file) {
return sha1;
}



private static void decorateObjectNode(final DocumentReader docReader, final DocumentWriter docWriter) {
if (!docReader.getMixinTypeNames().contains(FEDORA_CONTAINER)) {
LOGGER.trace("Adding mixin: {}, to {}", FEDORA_CONTAINER, docReader.getDocumentId());
Expand Down Expand Up @@ -325,4 +339,10 @@ protected DocumentReader readDocument( final Document document ) {
public boolean isReadonly() {
return true;
}

@Override
public boolean isContentNode(final String id) {
return super.isContentNode(id);
}

}

This file was deleted.

Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.modeshape.connector.filesystem;
package org.fcrepo.connector.file;

import org.apache.commons.io.FileUtils;
import org.junit.Assert;
Expand All @@ -38,7 +38,7 @@ public class ExternalJsonSidecarExtraPropertyStoreTest {
public void testSidecarFile() throws IOException {
final File tmp = createTempDirectory("filesystem-federation").toFile();
try {
final FileSystemConnector mockConnector = mock(FileSystemConnector.class);
final FedoraFileSystemConnector mockConnector = mock(FedoraFileSystemConnector.class);
final DocumentTranslator mockTranslator = mock(DocumentTranslator.class);
when(mockConnector.fileFor("/")).thenReturn(new File(FEDERATION_ROOT));
when(mockConnector.isContentNode("/")).thenReturn(false);
Expand Down

0 comments on commit dff2d5f

Please sign in to comment.