Skip to content

Commit

Permalink
Merge branch 'master' into agg-fixity
Browse files Browse the repository at this point in the history
* master:
  Cleaned up unused imports
  • Loading branch information
cbeer committed Mar 12, 2013
2 parents e9d6ec0 + 0baf2dd commit fa4c455
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 88 deletions.
Expand Up @@ -3,13 +3,11 @@

import static com.google.common.collect.ImmutableMap.builder;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.services.PathService.OBJECT_PATH;

import com.googlecode.htmleasy.View;

import java.io.IOException;

import javax.jcr.LoginException;
Expand All @@ -33,7 +31,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

/**
Expand Down
@@ -1,6 +1,29 @@

package org.fcrepo.services;

import com.google.common.collect.Maps;
import static com.google.common.collect.Maps.transformEntries;
import static java.lang.Boolean.FALSE;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.commons.codec.binary.Hex;
import org.fcrepo.utils.LowLevelCacheStore;
import org.infinispan.Cache;
Expand All @@ -13,37 +36,16 @@
import org.modeshape.jcr.value.binary.BinaryStore;
import org.modeshape.jcr.value.binary.BinaryStoreException;
import org.modeshape.jcr.value.binary.infinispan.InfinispanBinaryStore;

import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Maps;

public class LowLevelStorageService {


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


@Inject
private Repository repo;

Expand All @@ -55,38 +57,51 @@ public class LowLevelStorageService {
private static List<LowLevelCacheStore> cacheStores;

private static JcrRepository getRepositoryInstance() {
return (JcrRepository)readOnlySession.getRepository();
return (JcrRepository) readOnlySession.getRepository();
}

public static Map<LowLevelCacheStore, Boolean> applyDigestToBlobs(final Node resource, final MessageDigest digest, final String checksum) throws RepositoryException {
return applyToBlob(resource, new Maps.EntryTransformer<LowLevelCacheStore, InputStream, Boolean>() {
public Boolean transformEntry(LowLevelCacheStore store, InputStream is) {
DigestInputStream ds = null;
try {
ds = new DigestInputStream(is, (MessageDigest)digest.clone());

while (ds.read() != -1);

String calculatedDigest = Hex.encodeHexString(ds.getMessageDigest().digest());

return checksum.equals(calculatedDigest);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return Boolean.FALSE;
}
});
public static Map<LowLevelCacheStore, Boolean> applyDigestToBlobs(
final Node resource, final MessageDigest digest,
final String checksum) throws RepositoryException {
return applyToBlob(
resource,
new Maps.EntryTransformer<LowLevelCacheStore, InputStream, Boolean>() {

public Boolean transformEntry(LowLevelCacheStore store,
InputStream is) {
DigestInputStream ds = null;
try {
ds =
new DigestInputStream(is,
(MessageDigest) digest.clone());

while (ds.read() != -1);

String calculatedDigest =
Hex.encodeHexString(ds.getMessageDigest()
.digest());

return checksum.equals(calculatedDigest);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return FALSE;
}
});

}

public static <T> Map<LowLevelCacheStore, T> applyToBlob(Node resource, Maps.EntryTransformer<LowLevelCacheStore, InputStream, T> transform) throws RepositoryException {
Map<LowLevelCacheStore, T> transformed =
Maps.transformEntries(getBlobs(resource), transform);

return transformed;
public static
<T>
Map<LowLevelCacheStore, T>
applyToBlob(
final Node resource,
final Maps.EntryTransformer<LowLevelCacheStore, InputStream, T> transform)
throws RepositoryException {
return transformEntries(getBlobs(resource), transform);
}

/**
Expand All @@ -95,9 +110,12 @@ public static <T> Map<LowLevelCacheStore, T> applyToBlob(Node resource, Maps.Ent
* @return a map of binary stores and input streams
* @throws RepositoryException
*/
public static Map<LowLevelCacheStore, InputStream> getBlobs(Node resource) throws RepositoryException {
public static Map<LowLevelCacheStore, InputStream> getBlobs(
final Node resource) throws RepositoryException {

BinaryValue v = (BinaryValue) resource.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary();
final BinaryValue v =
(BinaryValue) resource.getNode(JCR_CONTENT).getProperty(
JCR_DATA).getBinary();

return getBlobs(v.getKey());

Expand All @@ -108,11 +126,13 @@ public static Map<LowLevelCacheStore, InputStream> getBlobs(Node resource) throw
* @param key a Modeshape BinaryValue's key.
* @return a map of binary stores and input streams
*/
public static HashMap<LowLevelCacheStore, InputStream> getBlobs(BinaryKey key) {
public static HashMap<LowLevelCacheStore, InputStream> getBlobs(
BinaryKey key) {

HashMap<LowLevelCacheStore, InputStream> blobs = new LinkedHashMap<LowLevelCacheStore, InputStream>();
HashMap<LowLevelCacheStore, InputStream> blobs =
new LinkedHashMap<LowLevelCacheStore, InputStream>();

for( LowLevelCacheStore c : getLowLevelCacheStores()) {
for (LowLevelCacheStore c : getLowLevelCacheStores()) {
try {
blobs.put(c, c.getInputStream(key));
} catch (BinaryStoreException e) {
Expand All @@ -130,14 +150,10 @@ public static HashMap<LowLevelCacheStore, InputStream> getBlobs(BinaryKey key) {
*/
private static BinaryStore getBinaryStore() {
try {

JcrRepository jcrRepository = getRepositoryInstance();

return jcrRepository.getConfiguration().getBinaryStorage().getBinaryStore();

} catch (Exception e) { // boo, catching all exceptions. unfortunately, that's all getBinaryStore promises..
e.printStackTrace();
return null;
return getRepositoryInstance().getConfiguration().getBinaryStorage()
.getBinaryStore();
} catch (Exception e) { // boo, catching all exceptions. unfortunately, that's all getBinaryStore promises..
throw new IllegalStateException(e);
}

}
Expand All @@ -149,40 +165,44 @@ private static BinaryStore getBinaryStore() {
* @return a list of "BinaryCacheStore", an abstraction over a plain BinaryStore or a specific Infinispan Cache
*/
private static List<LowLevelCacheStore> getLowLevelCacheStores() {
// I'm assuming the list of stores doesn't change.. probably not a safe assumption
if(cacheStores != null) {
//TODO I'm assuming the list of stores doesn't change.. probably not a safe assumption
if (cacheStores != null) {
return cacheStores;
}

List<LowLevelCacheStore> stores = new ArrayList<LowLevelCacheStore>();

BinaryStore store = getBinaryStore();

if(store == null) {
if (store == null) {
return stores;
}

// if we have an Infinispan store, it may have multiple stores (or cluster nodes)
if(store instanceof InfinispanBinaryStore) {
InfinispanBinaryStore ispnStore = (InfinispanBinaryStore)store;
if (store instanceof InfinispanBinaryStore) {
InfinispanBinaryStore ispnStore = (InfinispanBinaryStore) store;

//seems like we have to start it, not sure why.
ispnStore.start();

for(Cache c : ispnStore.getCaches()) {
for (Cache<?, ?> c : ispnStore.getCaches()) {

final CacheStore cacheStore = c.getAdvancedCache().getComponentRegistry().getComponent(CacheLoaderManager.class).getCacheStore();
final CacheStore cacheStore =
c.getAdvancedCache().getComponentRegistry()
.getComponent(CacheLoaderManager.class)
.getCacheStore();

// A ChainingCacheStore indicates we (may) have multiple CacheStores at play
if(cacheStore instanceof ChainingCacheStore) {
final ChainingCacheStore chainingCacheStore = (ChainingCacheStore) cacheStore;
if (cacheStore instanceof ChainingCacheStore) {
final ChainingCacheStore chainingCacheStore =
(ChainingCacheStore) cacheStore;

// the stores are a map of the cache store and the configuration; i'm just throwing the configuration away..
for( CacheStore s : chainingCacheStore.getStores().keySet()) {
for (CacheStore s : chainingCacheStore.getStores().keySet()) {
stores.add(new LowLevelCacheStore(store, s));
}

} else {
} else {
// just a nice, simple infinispan cache.
stores.add(new LowLevelCacheStore(store, cacheStore));
}
Expand Down
Expand Up @@ -23,19 +23,14 @@
*/
package org.fcrepo.utils.infinispan;

import org.infinispan.Cache;
import java.io.IOException;
import java.io.InputStream;

import org.infinispan.container.entries.CacheEntry;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.loaders.CacheLoaderException;
import org.infinispan.loaders.CacheStore;
import org.infinispan.marshall.MarshalledValue;
import org.modeshape.common.logging.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;

/**
* Merges chunks from cache and provides InputStream-feeling.
*/
Expand Down Expand Up @@ -134,8 +129,6 @@ protected byte[] nextChunk() throws IOException {
String chunkKey = key+"-"+chunkNumber++;
logger.debug("Read chunk {0}", chunkKey);

byte[] bytes;

try {
final CacheEntry cacheEntry = blobCache.load(chunkKey);

Expand Down

0 comments on commit fa4c455

Please sign in to comment.