Skip to content

Commit

Permalink
testing for AWS client creation and node property creation in sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Feb 10, 2013
1 parent bef8650 commit 86f2e2a
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 37 deletions.
22 changes: 18 additions & 4 deletions fcrepo-glacier/pom.xml
Expand Up @@ -7,9 +7,7 @@
<groupId>org.fcrepo</groupId>
<version>4.0-SNAPSHOT</version>
</parent>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-glacier</artifactId>
<version>4.0-SNAPSHOT</version>
<name>fcrepo-glacier</name>
<url>http://maven.apache.org</url>
<properties>
Expand All @@ -19,13 +17,11 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.modeshape</groupId>
<artifactId>modeshape-jcr-api</artifactId>
<version>${modeshape.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand All @@ -37,10 +33,28 @@
<artifactId>aws-java-sdk</artifactId>
<version>1.3.27</version>
</dependency>
<dependency>
<groupId>org.modeshape</groupId>
<artifactId>modeshape-jcr</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.modeshape</groupId>
<artifactId>modeshape-jcr</artifactId>
<version>${modeshape.version}</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>5.2.0.Final</version>
<classifier>tests</classifier>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
</project>
Expand Up @@ -3,19 +3,25 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.jcr.Binary;
import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.ConstraintViolationException;

import org.modeshape.jcr.api.JcrConstants;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.modeshape.jcr.api.sequencer.Sequencer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.glacier.AmazonGlacierClient;
Expand All @@ -26,31 +32,54 @@

public class GlacierBackupSequencer extends Sequencer {

public static String GLACIER_BACKUP_MIXIN = "org.fcrepo:glacier-backup";
public static String GLACIER_LOCATION_PROPERTY = "org.fcrepo.glacier:location";
public static String GLACIER_ARCHIVE_ID_PROPERTY = "org.fcrepo.glacier:archive-id";
public static String GLACIER_CHECKSUM_PROPERTY = "org.fcrepo.glacier:checksum";
public static String GLACIER_BACKUP_MIXIN = "fcrepo:glacier.backup";
public static String GLACIER_LOCATION_PROPERTY = "glacier:location";
public static String GLACIER_VAULT_PROPERTY = "glacier:vault";
public static String GLACIER_ARCHIVE_ID_PROPERTY = "glacier:archive.id";
public static String GLACIER_CHECKSUM_PROPERTY = "glacier:checksum";

private static Logger LOG = LoggerFactory.getLogger(GlacierBackupSequencer.class);

private AmazonGlacierClient m_client;
private String m_defaultArchive;
AmazonGlacierClient client;
String defaultVault;
String accessKey;
String secretKey;
String credentials;
String endpoint;

public GlacierBackupSequencer(AmazonGlacierClient client, String defaultArchive) {
m_client = client;
m_defaultArchive = defaultArchive;
public GlacierBackupSequencer() {
}


@Override
public void initialize( NamespaceRegistry registry,
NodeTypeManager nodeTypeManager ) throws RepositoryException, IOException {
super.initialize(registry, nodeTypeManager);
if (credentials != null) {
InputStream in = GlacierBackupSequencer.class.getResourceAsStream(credentials);
Properties props = new Properties();
props.load(in);
accessKey = props.getProperty("accessKey");
secretKey = props.getProperty("secretKey");
}

AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
client = new AmazonGlacierClient(creds);
client.setEndpoint(endpoint);
}

@Override
public boolean execute(Property inputProperty, Node outputNode, Context context)
throws Exception {
System.err.println("SEQUENCER: Property change: " + inputProperty.getName());
if (JcrConstants.JCR_DATA.equals(inputProperty.getName())) {
if (!outputNode.canAddMixin(GLACIER_BACKUP_MIXIN)) {
throw new ConstraintViolationException("Cannot add mixin \"" + GLACIER_BACKUP_MIXIN + "\" to this node");
}
Binary inputBinary = inputProperty.getBinary();
InputStream in = inputBinary.getStream();
long inputSize = inputBinary.getSize();
UploadArchiveRequest request = getRequest(context);
UploadArchiveRequest request = new UploadArchiveRequest();
File outFile = null;

// read input to get tree hash, cache it in a temporary file
Expand All @@ -71,35 +100,31 @@ public boolean execute(Property inputProperty, Node outputNode, Context context)
LOG.warn("input Binary size did not match bytes read: {} != {}", inputSize, read);
}
request.setContentLength(read);
request.setChecksum(TreeHashGenerator.calculateTreeHash(treeIn.getChecksums()));

String checksum = TreeHashGenerator.calculateTreeHash(treeIn.getChecksums());
request.setChecksum(checksum);
request.setBody(new FileInputStream(outFile));

request.setContentLength(inputBinary.getSize());
String vaultName = (outputNode.hasProperty(GLACIER_VAULT_PROPERTY)) ? outputNode.getProperty(GLACIER_VAULT_PROPERTY).getString() : defaultVault;
request.setVaultName(vaultName);

request.setContentLength(inputBinary.getSize());
//TODO Need to calculate the checksum!
request.setChecksum("foo");
UploadArchiveResult result = m_client.uploadArchive(request);

UploadArchiveResult result = client.uploadArchive(request);
outputNode.addMixin(GLACIER_BACKUP_MIXIN);
outputNode.setProperty(GLACIER_LOCATION_PROPERTY, result.getLocation());
outputNode.setProperty(GLACIER_ARCHIVE_ID_PROPERTY, result.getArchiveId());
outputNode.setProperty(GLACIER_CHECKSUM_PROPERTY, checksum);
outputNode.setProperty(GLACIER_VAULT_PROPERTY, vaultName);
if (outFile != null) {
outFile.delete();
}
System.err.println("SEQUENCER: " + outputNode.getPath());
return true;
} else {
return false;
}
}

public UploadArchiveRequest getRequest(Context context) {
UploadArchiveRequest result = new UploadArchiveRequest();
//TODO we should look for a property specifying the archive
result.setRequestCredentials(getCredentials(context));
return result;
}

public AWSCredentials getCredentials(Context context) {
return new BasicAWSCredentials("","");
}


}
20 changes: 20 additions & 0 deletions fcrepo-glacier/src/main/resources/config/glacier-backup.cnd
@@ -0,0 +1,20 @@
/*
* JCR node types for use with Fedora
*/
<fcrepo = 'http://fcrepo.org/'>
<glacier = 'http://fcrepo.org/glacier/'>
<jcr = 'http://www.jcp.org/jcr/1.0'>
<nt = 'http://www.jcp.org/jcr/nt/1.0'>

/*
* <mix = 'http://www.jcp.org/jcr/mix/1.0'>
*
* [fcrepo:resource] > mix:created, mix:lastModified, mix:lockable, mix:versionable mixin abstract
* - fcrepo:created (STRING) COPY
*/

[fcrepo:glacier.backup] > nt:file mixin
- glacier:checksum (STRING) mandatory
- glacier:archive.id (STRING) mandatory
- glacier:location (STRING) mandatory
- glacier:vault (STRING) mandatory
5 changes: 0 additions & 5 deletions fcrepo-glacier/src/main/resources/glacier-backup.cnd

This file was deleted.

@@ -0,0 +1,49 @@
package org.fcrepo.events;

import static org.modeshape.jcr.api.observation.Event.Sequencing.NODE_SEQUENCED;
import static org.modeshape.jcr.api.observation.Event.Sequencing.OUTPUT_PATH;
import static org.modeshape.jcr.api.observation.Event.Sequencing.SELECTED_PATH;
import static org.modeshape.jcr.api.observation.Event.Sequencing.SEQUENCED_NODE_ID;
import static org.modeshape.jcr.api.observation.Event.Sequencing.SEQUENCED_NODE_PATH;
import static org.modeshape.jcr.api.observation.Event.Sequencing.SEQUENCER_NAME;
import static org.modeshape.jcr.api.observation.Event.Sequencing.USER_ID;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;

import org.modeshape.jcr.api.observation.Event;

public class SequencingTestListener implements EventListener {

private ConcurrentHashMap<String, Event> sequencingEvents;
private final Map<String, Node> sequencedNodes = new HashMap<String, Node>();

public SequencingTestListener(ConcurrentHashMap<String, Event> sequencingEvents) {
this.sequencingEvents = sequencingEvents;
}

@Override
public void onEvent(EventIterator events) {
while (events.hasNext()) {
try {
Event event = (Event)events.nextEvent();
Map info = event.getInfo();
for (Object key: info.keySet()){
System.err.println("EVENT: " + key + " => " + info.get(key));
}
sequencingEvents.putIfAbsent((String)event.getInfo().get(SEQUENCED_NODE_PATH), event);

} catch (Exception e) {
throw new RuntimeException(e);
}
}

}

}
Expand Up @@ -59,7 +59,8 @@ public void testSequencerValid()
when(upload.getLocation()).thenReturn(UPLOAD_LOC);
AmazonGlacierClient client = mock(AmazonGlacierClient.class);
when(client.uploadArchive(any(UploadArchiveRequest.class))).thenReturn(upload);
Sequencer test = new GlacierBackupSequencer(client,DEFAULT_ARCHIVE);
GlacierBackupSequencer test = new GlacierBackupSequencer();
test.client = client;
Property input = mock(Property.class);
InMemoryBinaryValue binary = mock(InMemoryBinaryValue.class);
String binString = "the quick brown fox";
Expand Down Expand Up @@ -87,7 +88,8 @@ public void testSequencerInvalid()
when(upload.getLocation()).thenReturn(UPLOAD_LOC);
AmazonGlacierClient client = mock(AmazonGlacierClient.class);
when(client.uploadArchive(any(UploadArchiveRequest.class))).thenReturn(upload);
Sequencer test = new GlacierBackupSequencer(client,DEFAULT_ARCHIVE);
GlacierBackupSequencer test = new GlacierBackupSequencer();
test.client = client;
Property input = mock(Property.class);
InMemoryBinaryValue binary = mock(InMemoryBinaryValue.class);
String binString = "the quick brown fox";
Expand Down

0 comments on commit 86f2e2a

Please sign in to comment.