Skip to content

Commit

Permalink
fix for update to modeshape removed Stratgey and NamedHint.
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelgbanks committed Jun 6, 2013
1 parent 56cc303 commit f6bef32
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 100 deletions.
17 changes: 8 additions & 9 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -37,14 +37,13 @@
import org.fcrepo.utils.FedoraJcrTypes;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.api.JcrTools;
import org.modeshape.jcr.value.binary.StrategyHint;
import org.slf4j.Logger;

import com.codahale.metrics.Histogram;

/**
* Abstraction for a Fedora datastream backed by a JCR node.
*
*
* @author ajs6f
*
*/
Expand Down Expand Up @@ -135,7 +134,7 @@ public Datastream(final Session session, final String dsPath)
Node contentNode = node.getNode(JCR_CONTENT);
decorateContentNode(contentNode);
}

}

/**
Expand All @@ -157,7 +156,7 @@ public InputStream getContent() throws RepositoryException {

/**
* Sets the content of this Datastream.
*
*
* @param content
* @throws RepositoryException
*/
Expand All @@ -176,10 +175,10 @@ public void setContent(final InputStream content, final String contentType,

node.setProperty(FEDORA_CONTENTTYPE, contentType);

StrategyHint hint = null;
String hint = null;


if(storagePolicyDecisionPoint != null) {
if (storagePolicyDecisionPoint != null) {
hint = storagePolicyDecisionPoint.evaluatePolicies(node);
}

Expand Down Expand Up @@ -255,7 +254,7 @@ public URI getContentDigest() throws RepositoryException {
} catch (RepositoryException e) {
logger.error("Could not get content digest - " + e.getMessage());
}
//TODO checksum not stored. recalculating checksum,
//TODO checksum not stored. recalculating checksum,
//however, this would defeat the purpose validating against the checksum
Binary binary = (Binary) contentNode.getProperty(JCR_DATA)
.getBinary();
Expand Down Expand Up @@ -407,7 +406,7 @@ public long getSize() throws RepositoryException {
return getNodePropertySize(node) + getContentSize();

}

private void decorateContentNode(Node contentNode) throws RepositoryException {
if (contentNode == null) {
logger.warn("{}/jcr:content appears to be null!");
Expand All @@ -429,7 +428,7 @@ private void decorateContentNode(Node contentNode) throws RepositoryException {

logger.debug("Decorated data property at path: " + dataProperty.getPath());
}

public static boolean hasMixin(Node node) throws RepositoryException {
NodeType[] nodeTypes = node.getMixinNodeTypes();
if (nodeTypes == null) return false;
Expand Down
Expand Up @@ -22,11 +22,10 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import org.modeshape.jcr.JcrValueFactory;
import org.modeshape.jcr.value.binary.StrategyHint;

/**
* Convenience class with static methods for manipulating Fedora types in the JCR.
*
*
* @author ajs6f
*
*/
Expand Down Expand Up @@ -84,7 +83,7 @@ public boolean apply(final Node node) {
};

/**
* Translates a {@link NodeType} to its {@link String} name.
* Translates a {@link NodeType} to its {@link String} name.
*/
public static Function<NodeType, String> nodetype2name =
new Function<NodeType, String>() {
Expand All @@ -97,7 +96,7 @@ public String apply(final NodeType t) {
};

/**
* Translates a JCR {@link Value} to its {@link String} expression.
* Translates a JCR {@link Value} to its {@link String} expression.
*/
public static Function<Value, String> value2string =
new Function<Value, String>() {
Expand Down Expand Up @@ -149,7 +148,7 @@ public ValueFactory apply(final Node n) {

/**
* Creates a JCR {@link Binary}
*
*
* @param n a {@link Node}
* @param i an {@link InputStream}
* @return a JCR {@link Binary}
Expand All @@ -173,7 +172,7 @@ public static Binary getBinary(final Node n, final InputStream i) {
* @param i an {@link InputStream}
* @return a JCR {@link Binary}
*/
public static Binary getBinary(final Node n, final InputStream i, final StrategyHint hint) {
public static Binary getBinary(final Node n, final InputStream i, final String hint) {
try {
checkArgument(n != null,
"null cannot have a Binary created for it!");
Expand All @@ -187,9 +186,9 @@ public static Binary getBinary(final Node n, final InputStream i, final Strategy
}

/**
* Convenience method for transforming arrays into
* Convenience method for transforming arrays into
* {@link Collection}s through a mapping {@link Function}.
*
*
* @param input A Collection<F>.
* @param f A Function<F,T>.
* @return An ImmutableSet copy of input after transformation by f
Expand Down
3 changes: 1 addition & 2 deletions fcrepo-kernel/src/test/java/org/fcrepo/DatastreamTest.java
Expand Up @@ -42,7 +42,6 @@
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.modeshape.jcr.value.binary.StrategyHint;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
Expand Down Expand Up @@ -114,7 +113,7 @@ public void testSetContent() throws RepositoryException,
PowerMockito.mockStatic(FedoraTypesUtils.class);
when(
FedoraTypesUtils.getBinary(any(Node.class),
any(InputStream.class), any(StrategyHint.class))).thenReturn(mockBin);
any(InputStream.class), any(String.class))).thenReturn(mockBin);
final Binary content = mock(Binary.class);
final Node mockContent = TestHelpers.getContentNodeMock(8);
when(mockDsNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
Expand Down
Expand Up @@ -8,7 +8,6 @@
import org.fcrepo.services.ObjectService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.value.binary.NamedHint;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand Down Expand Up @@ -38,7 +37,7 @@ public void shouldDemonstratePolicyDecisionPoints() throws Exception {
Session session = repo.login();

PolicyDecisionPoint pt = new PolicyDecisionPoint();
pt.addPolicy(new MimeTypePolicy("image/tiff", new NamedHint("tiff-store")));
pt.addPolicy(new MimeTypePolicy("image/tiff", "tiff-store"));

final Node dsNode =
datastreamService.createDatastreamNode(session,
Expand Down
Expand Up @@ -14,7 +14,6 @@
import org.junit.Test;
import org.modeshape.jcr.JcrRepositoryFactory;
import org.modeshape.jcr.value.BinaryKey;
import org.modeshape.jcr.value.binary.NamedHint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -62,7 +61,7 @@ public void setRepository() throws RepositoryException {
null);

pdp = new PolicyDecisionPoint();
pdp.addPolicy(new MimeTypePolicy("image/tiff", new NamedHint("tiff-store")));
pdp.addPolicy(new MimeTypePolicy("image/tiff", "tiff-store"));

datastreamService = new DatastreamService();
datastreamService.setRepository(repo);
Expand Down
Expand Up @@ -26,7 +26,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.value.binary.StrategyHint;

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
Expand All @@ -37,15 +37,15 @@
public class DatastreamServiceTest implements FedoraJcrTypes {

private static final String MOCK_CONTENT_TYPE = "application/test-data";

private static final String JCR_CONTENT = "jcr:content";

private static final String JCR_DATA = "jcr:data";

private Session mockSession;

private Node mockRoot;

private DatastreamService testObj;

@Before
Expand Down Expand Up @@ -78,13 +78,13 @@ public void testCreateDatastreamNode() throws Exception {
when(pdp.evaluatePolicies(mockNode)).thenReturn(null);
testObj.setStoragePolicyDecisionPoint(pdp);
PowerMockito.mockStatic(FedoraTypesUtils.class);
when(FedoraTypesUtils.getBinary(eq(mockNode), eq(mockIS), any(StrategyHint.class))).thenReturn(mockBinary);
when(FedoraTypesUtils.getBinary(eq(mockNode), eq(mockIS), any(String.class))).thenReturn(mockBinary);

final Node actual =
testObj.createDatastreamNode(mockSession, testPath,
MOCK_CONTENT_TYPE, mockIS);
assertEquals(mockNode, actual);

verify(mockContent).setProperty(JCR_DATA, mockBinary);
}

Expand Down
@@ -1,8 +1,6 @@
package org.fcrepo.binary;

import org.fcrepo.utils.FedoraJcrTypes;
import org.modeshape.jcr.value.binary.NamedHint;
import org.modeshape.jcr.value.binary.StrategyHint;
import org.slf4j.Logger;

import javax.jcr.Node;
Expand All @@ -13,29 +11,29 @@

public class MimeTypePolicy implements Policy {

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

private final String mimeType;
private final StrategyHint hint;

public MimeTypePolicy(String mimeType, StrategyHint hint) {
this.mimeType = mimeType;
this.hint = hint;
}

public StrategyHint evaluatePolicy(Node n) {
logger.debug("Evaluating MimeTypePolicy for {} -> {}", mimeType, hint.toString());
try {
final String nodeMimeType = n.getProperty(FEDORA_CONTENTTYPE).getString();
logger.debug("Found mime type {}", nodeMimeType);
if(nodeMimeType.equals(mimeType)) {
return hint;
}
} catch (RepositoryException e) {
logger.warn("Got Exception evaluating policy: {}", e);
return null;
}

return null;
}
private static final Logger logger = getLogger(MimeTypePolicy.class);

private final String mimeType;
private final String hint;

public MimeTypePolicy(String mimeType, String hint) {
this.mimeType = mimeType;
this.hint = hint;
}

public String evaluatePolicy(Node n) {
logger.debug("Evaluating MimeTypePolicy for {} -> {}", mimeType, hint.toString());
try {
final String nodeMimeType = n.getProperty(FEDORA_CONTENTTYPE).getString();
logger.debug("Found mime type {}", nodeMimeType);
if (nodeMimeType.equals(mimeType)) {
return hint;
}
} catch (RepositoryException e) {
logger.warn("Got Exception evaluating policy: {}", e);
return null;
}

return null;
}
}
@@ -1,9 +1,8 @@
package org.fcrepo.binary;

import org.modeshape.jcr.value.binary.StrategyHint;

import javax.jcr.Node;

public interface Policy {
StrategyHint evaluatePolicy(Node n);
String evaluatePolicy(Node n);
}
Expand Up @@ -3,40 +3,39 @@
import javax.jcr.Node;
import java.util.ArrayList;
import java.util.List;
import org.modeshape.jcr.value.binary.StrategyHint;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

public class PolicyDecisionPoint {

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

private List<Policy> policies;
private List<Policy> policies;

public PolicyDecisionPoint() {
logger.debug("Initializing binary PolicyDecisionPoint");
policies = new ArrayList<Policy>();
}
public PolicyDecisionPoint() {
logger.debug("Initializing binary PolicyDecisionPoint");
policies = new ArrayList<Policy>();
}

public void addPolicy(Policy p) {
policies.add(p);
}
public void addPolicy(Policy p) {
policies.add(p);
}

public StrategyHint evaluatePolicies(Node n) {
for(Policy p : policies) {
StrategyHint h = p.evaluatePolicy(n);
if(h != null) {
return h;
}
}
public String evaluatePolicies(Node n) {
for (Policy p : policies) {
String h = p.evaluatePolicy(n);
if (h != null) {
return h;
}
}

return null;
}
return null;
}


public void setPolicies(List<Policy> policies) {
logger.debug("Adding policies to binary PolicyDecisionPoint: {}", policies.toString());
this.policies = policies;
}
public void setPolicies(List<Policy> policies) {
logger.debug("Adding policies to binary PolicyDecisionPoint: {}", policies.toString());
this.policies = policies;
}
}

0 comments on commit f6bef32

Please sign in to comment.