Skip to content

Commit

Permalink
merging master changes
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Apr 8, 2013
2 parents 0e47c87 + 3bab1bc commit 663b426
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/main/java/org/fcrepo/federation/bagit/BagItConnector.java
Expand Up @@ -104,7 +104,7 @@ public void initialize(NamespaceRegistry registry,
directoryAbsolutePathLength =
directoryAbsolutePath.length() - FILE_SEPARATOR.length(); // does NOT include the separator

setExtraPropertiesStore(new BagItExtraPropertiesStore());
setExtraPropertiesStore(new BagItExtraPropertiesStore(this));
getLogger().trace("Initialized.");
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(1);
threadPool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, workQueue);
Expand Down Expand Up @@ -204,7 +204,7 @@ public Document getDocumentById(String id) {

// Add the extra properties (if there are any), overwriting any properties with the same names
// (e.g., jcr:primaryType, jcr:mixinTypes, jcr:mimeType, etc.) ...
writer.addProperties(extraPropertiesStore().getProperties(id));
writer.addProperties(new BagItExtraPropertiesStore(this).getProperties(id));
getLogger().trace("Leaving getDocumentById().");
return writer.document();
}
Expand Down Expand Up @@ -233,4 +233,7 @@ void changeTagFile(File file) {
// do some tagFile stuff
}

protected File fileFor( String id ) {
return super.fileFor(id);
}
}
@@ -1,35 +1,207 @@

package org.fcrepo.federation.bagit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.modeshape.jcr.cache.DocumentStoreException;
import org.modeshape.jcr.federation.spi.Connector;
import org.modeshape.jcr.federation.spi.ExtraPropertiesStore;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;
import org.modeshape.jcr.value.PropertyFactory;
import org.modeshape.jcr.value.ValueFactories;
import org.modeshape.jcr.value.ValueFactory;

/**
* ExtraPropertiesStore implementation which stores properties in bag-info.txt.
* @see https://tools.ietf.org/html/draft-kunze-bagit-08#section-2.2.2
**/
public class BagItExtraPropertiesStore implements ExtraPropertiesStore {
private BagItConnector connector;
private ValueFactories factories;
private ValueFactory<String> stringFactory;
private PropertyFactory propertyFactory;

protected BagItExtraPropertiesStore( BagItConnector connector )
{
this.connector = connector;
this.factories = this.connector.getContext().getValueFactories();
this.stringFactory = factories.getStringFactory();
this.propertyFactory = this.connector.getContext().getPropertyFactory();
}

@Override
public void storeProperties(String id, Map<Name, Property> properties) {
// TODO Auto-generated method stub
PrintWriter out = null;
try
{
try
{
File bagInfo = bagInfoFile(id);
out = new PrintWriter( new FileWriter(bagInfo) );
for ( Map.Entry<Name,Property> entry : properties.entrySet() )
{
Name name = entry.getKey();
Property prop = entry.getValue();
String line = stringFactory.create( name ) + ": "
+ stringFactory.create( prop );
out.println( wrapLine(line) );
}
}
finally
{
out.close();
}
}
catch ( Exception ex )
{
throw new DocumentStoreException(id,ex);
}
}

@Override
public void updateProperties(String id, Map<Name, Property> properties) {
// TODO Auto-generated method stub

Map<Name,Property> existing = getProperties(id);
for ( Map.Entry<Name,Property> entry : properties.entrySet() )
{
Name name = entry.getKey();
Property prop = entry.getValue();
if ( prop == null )
{
existing.remove(name);
}
else
{
existing.put(name, prop);
}
}
}

@Override
public Map<Name, Property> getProperties(String id) {
// TODO Auto-generated method stub
return null;
Map<Name,Property> properties = new HashMap<Name,Property>();
BufferedReader buf = null;
try
{
try
{
File bagInfo = bagInfoFile(id);
System.err.println("XXX bagInfoFile(" + id + "):"+bagInfo.getAbsolutePath());
if (!bagInfo.exists() ) { return NO_PROPERTIES; }
else if ( !bagInfo.canRead() )
{
throw new DocumentStoreException(
"id", "Can't read " + bagInfo.getAbsolutePath()
);
}
buf = new BufferedReader( new FileReader(bagInfo) );
String key = null;
String val = null;
for ( String line = null; (line=buf.readLine()) != null; )
{
if ( key != null &&
(line.startsWith(" ") || line.startsWith("\t")) )
{
// continuation of previous line
if ( val == null )
{
val = line.trim();
}
else
{
val += " " + line.trim();
}
}
else
{
// process completed value
if ( key != null && val != null )
{
addProperty( properties, key, val );
}
key = null;
val = null;

// start new value
if ( line.indexOf(":") != -1 )
{
key = line.substring(0,line.indexOf(":") ).trim();
val = line.substring(line.indexOf(":") + 1 ).trim();
}
}
}
if ( key != null && val != null )
{
addProperty( properties, key, val );
}
}
finally
{
buf.close();
}
}
catch ( Exception ex )
{
throw new DocumentStoreException(id,ex);
}
return properties;
}

@Override
public boolean removeProperties(String id) {
// TODO Auto-generated method stub
return false;
File bagInfo = bagInfoFile(id);
if ( !bagInfo.exists() )
{
return false;
}
else
{
return bagInfo.delete();
}
}

private File bagInfoFile( String id )
{
File dir = connector.fileFor(id);
return new File( dir, "bag-info.txt" );
}
private void addProperty( Map<Name,Property> properties, String key,
String val )
{
Name name = factories.getNameFactory().create(key);
Property prop = propertyFactory.create(name, val);
properties.put( name, prop );
}
private static String wrapLine(String value)
{
if ( value == null || value.length() < 79 )
{
return value;
}
StringBuffer wrapped = new StringBuffer();
String[] words = value.split(" ");
StringBuffer line = new StringBuffer(words[0]);
for ( int i = 1; i < words.length; i++ )
{
if ( words[i].length() + line.length() < 79 )
{
line.append(" " + words[i]);
}
else
{
wrapped.append( line.toString() + "\n");
line.setLength(0);
line.append(" "+words[i]);
}
}
if ( line.length() > 0 ) { wrapped.append(line.toString()); }
return wrapped.toString();
}

}
@@ -0,0 +1,47 @@

package org.fcrepo.federation.bagit;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.junit.Before;
import org.junit.Test;

import java.util.Map;

import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;


public class BagItExtraPropertiesStoreTest {

BagItExtraPropertiesStore store;

@Before
public void setUp() {
BagItConnector connector = mock(BagItConnector.class);
store = new BagItExtraPropertiesStore(connector);
}

/*
@Test
public void testRead() {
// TODO read a properties file from disk
}
@Test
public void testWrite() {
// TODO write a properties file to disk then read it back
}
@Test
public void testUpdate() {
// TODO update a properties file and make sure new props saved, delete dprops removed
}
@Test
public void testRemove() {
// TODO remove a properties file
}
*/
}

0 comments on commit 663b426

Please sign in to comment.