Skip to content

Commit

Permalink
Create a FileArchiver Registration
Browse files Browse the repository at this point in the history
This is actually a required paradigm for this project that had yet to be
implemented. Before this, there was no way for independent developers to
amend custom FileArchivers.

new version! 0.4
  • Loading branch information
Alex Aiezza authored and Alex Aiezza committed May 6, 2016
1 parent ebeadca commit 608839e
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 196 deletions.
28 changes: 14 additions & 14 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="launch4j_configs" including="*Usage.txt" kind="src" path="resources"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="src" path="test_resources"/>
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_73">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="launch4j_configs" including="*Usage.txt" kind="src" path="resources"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="src" path="test_resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_73">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'eclipse'

group = 'rit'
version = 0.3
version = 0.4
jar.baseName = 'flick'
// def uploadSite = 'ftp.bioinformatics.rit.edu'

Expand Down Expand Up @@ -83,6 +83,7 @@ dependencies {
compile 'com.google.guava:guava:18.0'
compile 'net.lingala.zip4j:zip4j:1.3.2'
compile 'it.unimi.dsi:dsiutils:2.2.4'
compile 'org.springframework:spring-context:4.2.5.RELEASE'

testCompile 'junit:junit:4.+'
testCompile 'junit-addons:junit-addons:1.+'
Expand Down
23 changes: 11 additions & 12 deletions src/edu/rit/flick/AbstractFlickFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.io.Files;

import edu.rit.flick.config.Configuration;
import edu.rit.flick.config.FileArchiverExtensionRegistry;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
Expand All @@ -31,19 +32,22 @@
*/
public abstract class AbstractFlickFile implements FlickFile
{
public static final String FILE_NOT_FOUND_EXCEPTION_MESSAGE = "file not found";
public static final String FILE_NOT_FOUND_EXCEPTION_MESSAGE = "file not found";

private static final String FILE_ALREADY_EXISTS_AS_DIRECTORY_EXCEPTION = "already exists as directory";
private static final String FILE_ALREADY_EXISTS_AS_DIRECTORY_EXCEPTION = "already exists as directory";

private static final String CANT_OVERWRITE_EXISTING_FILE_WITHOT_FORCE_FLAG = "\n can't overwrite existing file without --force flag";
private static final String CANT_OVERWRITE_EXISTING_FILE_WITHOT_FORCE_FLAG = "\n can't overwrite existing file without --force flag";

protected final File fileIn, fileOut;
protected final File fileIn, fileOut;

protected final Configuration configuration;
protected final Configuration configuration;

protected final ZipFile flickFile;
protected final ZipFile flickFile;

protected final ZipParameters zParams;
protected final ZipParameters zParams;

protected final FileArchiverExtensionRegistry REGISTRY = FileArchiverExtensionRegistry
.getInstance();

public AbstractFlickFile( final Configuration configuration )
throws ZipException,
Expand All @@ -52,8 +56,6 @@ public AbstractFlickFile( final Configuration configuration )
{
this.configuration = configuration;

init();

final String inputPath = (String) configuration.getOption( INPUT_PATH );
final Object outputPath = configuration.getOption( OUTPUT_PATH );

Expand Down Expand Up @@ -120,7 +122,4 @@ public String getFileOutPath()
{
return fileOut.getPath();
}

protected void init()
{}
}
133 changes: 34 additions & 99 deletions src/edu/rit/flick/DefaultFlickFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@
import java.util.List;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;

import com.google.common.io.Files;

import edu.rit.flick.config.Configuration;
import edu.rit.flick.genetics.FastaFileDeflator;
import edu.rit.flick.genetics.FastaFileInflator;
import edu.rit.flick.genetics.FastqFileDeflator;
import edu.rit.flick.genetics.FastqFileInflator;
import net.lingala.zip4j.core.HeaderReader;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.CentralDirectory;
Expand All @@ -60,10 +55,6 @@ public class DefaultFlickFile extends AbstractFlickFile

private static final String FILE_COULD_NOT_BE_DELETED_WARNING_FORMAT = "File '%s' could not be deleted.%n";

private List<FileDeflator> fileDeflators;

private List<FileInflator> fileInflators;

/**
* @param configuration
* @throws ZipException
Expand All @@ -78,31 +69,6 @@ public DefaultFlickFile( final Configuration configuration )
super( configuration );
}

@Override
protected void init()
{
fileDeflators = new ArrayList<FileDeflator>();
fileInflators = new ArrayList<FileInflator>();

if ( configuration.getFlag( ARCHIVE_MODE ) == DEFLATION_ARCHIVE_MODE )
addDefaultFileDeflators();
else if ( configuration.getFlag( ARCHIVE_MODE ) == INFLATION_ARCHIVE_MODE )
addDefaultFileInflators();
}


private final void addDefaultFileDeflators()
{
registerFileDeflator( new FastaFileDeflator() );
registerFileDeflator( new FastqFileDeflator() );
}

private final void addDefaultFileInflators()
{
registerFileInflator( new FastaFileInflator() );
registerFileInflator( new FastqFileInflator() );
}

private File archiveFile( final File file, final boolean onlyFile )
{
double time = 0;
Expand All @@ -112,32 +78,33 @@ private File archiveFile( final File file, final boolean onlyFile )
{
final String fileExt = "." + Files.getFileExtension( file.getPath() );

if ( getExtensions().contains( fileExt ) )
if ( configuration.getFlag( ARCHIVE_MODE ) == DEFLATION_ARCHIVE_MODE &&
!configuration.getFlag( NO_ZIP_FLAG ) )
if ( configuration.getFlag( ARCHIVE_MODE ) == DEFLATION_ARCHIVE_MODE &&
!configuration.getFlag( NO_ZIP_FLAG ) )
{
final FileDeflator fileDeflator = REGISTRY.getFileDeflator( fileExt );
if ( fileDeflator != null )
{
for ( final FileDeflator deflator : fileDeflators )
if ( deflator.getExtensions().contains( fileExt ) )
{
final long t1 = System.currentTimeMillis();
if ( onlyFile && configuration.getOption( OUTPUT_PATH ) != null )
archivedFile = deflator.deflate( configuration, file,
new File( (String) configuration.getOption( OUTPUT_PATH ) ) );
else archivedFile = deflator.deflate( configuration, file );
time = ( System.currentTimeMillis() - t1 ) / 1000d;
}
} else if ( configuration.getFlag( ARCHIVE_MODE ) == INFLATION_ARCHIVE_MODE &&
!configuration.getFlag( KEEP_ZIPPED_FLAG ) )
for ( final FileInflator inflator : fileInflators )
if ( inflator.getExtensions().contains( fileExt ) )
{
final long t1 = System.currentTimeMillis();
if ( onlyFile && configuration.getOption( OUTPUT_PATH ) != null )
archivedFile = inflator.inflate( configuration, file,
new File( (String) configuration.getOption( OUTPUT_PATH ) ) );
else archivedFile = inflator.inflate( configuration, file );
time = ( System.currentTimeMillis() - t1 ) / 1000d;
}
final long t1 = System.currentTimeMillis();
if ( onlyFile && configuration.getOption( OUTPUT_PATH ) != null )
archivedFile = fileDeflator.deflate( configuration, file,
new File( (String) configuration.getOption( OUTPUT_PATH ) ) );
else archivedFile = fileDeflator.deflate( configuration, file );
time = ( System.currentTimeMillis() - t1 ) / 1000d;
}
} else if ( configuration.getFlag( ARCHIVE_MODE ) == INFLATION_ARCHIVE_MODE &&
!configuration.getFlag( KEEP_ZIPPED_FLAG ) )
{
final FileInflator fileInflator = REGISTRY.getFileInflator( fileExt );
if ( fileInflator != null )
{
final long t1 = System.currentTimeMillis();
if ( onlyFile && configuration.getOption( OUTPUT_PATH ) != null )
archivedFile = fileInflator.inflate( configuration, file,
new File( (String) configuration.getOption( OUTPUT_PATH ) ) );
else archivedFile = fileInflator.inflate( configuration, file );
time = ( System.currentTimeMillis() - t1 ) / 1000d;
}
}

if ( archivedFile != null )
if ( configuration.getFlag( VERBOSE_FLAG ) )
Expand Down Expand Up @@ -241,27 +208,16 @@ public String getDefaultDeflatedExtension()
{
final String fileExt = "." + Files.getFileExtension( fileIn.getPath() );

if ( configuration.getFlag( ARCHIVE_MODE ) == DEFLATION_ARCHIVE_MODE )
{
for ( final FileDeflator fd : fileDeflators )
if ( fd.getExtensions().contains( fileExt ) )
return fd.getDefaultDeflatedExtension();
} else if ( configuration.getFlag( ARCHIVE_MODE ) == INFLATION_ARCHIVE_MODE )
{
for ( final FileInflator fi : fileInflators )
if ( fi.getExtensions().contains( fileExt ) )
return Files.getFileExtension(
fileIn.getPath().replaceAll( fi.getDefaultDeflatedExtension(), "" ) );
}
final String deflatedExtension = REGISTRY.getDeflatedExtension( fileExt );

return DEFAULT_DEFLATED_EXTENSION;
}
if ( deflatedExtension != null )
if ( configuration.getFlag( ARCHIVE_MODE ) == DEFLATION_ARCHIVE_MODE )
return deflatedExtension;
else if ( configuration.getFlag( ARCHIVE_MODE ) == INFLATION_ARCHIVE_MODE )
return Files
.getFileExtension( fileIn.getPath().replaceAll( deflatedExtension, "" ) );

@Override
public List<String> getExtensions()
{
return Stream.concat( fileInflators.stream(), fileDeflators.stream() )
.flatMap( fA -> fA.getExtensions().stream() ).collect( Collectors.toList() );
return DEFAULT_DEFLATED_EXTENSION;
}

@Override
Expand Down Expand Up @@ -346,25 +302,4 @@ public File inflate( final Configuration configuration, final File fileIn, final

return fileOut;
}

private <F extends FileArchiver> void registerFileArchiver(
final F fileArchiver,
final List<F> fileArchivers )
{
if ( fileArchivers.contains( fileArchiver ) )
// TODO nope
return;

fileArchivers.add( fileArchiver );
}

public void registerFileDeflator( final FileDeflator fileDeflator )
{
registerFileArchiver( fileDeflator, fileDeflators );
}

public void registerFileInflator( final FileInflator fileInflator )
{
registerFileArchiver( fileInflator, fileInflators );
}
}
4 changes: 0 additions & 4 deletions src/edu/rit/flick/FileArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
*/
package edu.rit.flick;

import java.util.List;

/**
* @author Alex Aiezza
*
*/
public interface FileArchiver
{
public String getDefaultDeflatedExtension();

public List<String> getExtensions();
}
34 changes: 34 additions & 0 deletions src/edu/rit/flick/RegisterFileDeflatorInflator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* COPYRIGHT (C) 2015 Alex Aiezza. All Rights Reserved.
*
* See the LICENSE for the specific language governing permissions and
* limitations under the License provided with this project.
*/
package edu.rit.flick;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import edu.rit.flick.config.DeflationOptionSet;
import edu.rit.flick.config.InflationOptionSet;

/**
* @author Alex Aiezza
*
*
*/
@Retention ( RetentionPolicy.RUNTIME )
public @interface RegisterFileDeflatorInflator
{
String [] inflatedExtensions();

String deflatedExtension() default "flick";

Class<? extends FileDeflator> fileDeflator() default FlickFile.class;

Class<? extends FileInflator> fileInflator() default FlickFile.class;

Class<? extends DeflationOptionSet> fileDeflatorOptionSet();

Class<? extends InflationOptionSet> fileInflatorOptionSet();
}
11 changes: 7 additions & 4 deletions src/edu/rit/flick/config/DeflationConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
package edu.rit.flick.config;

import edu.rit.flick.genetics.config.FastaDeflationOptionSet;
import edu.rit.flick.genetics.config.FastqDeflationOptionSet;
import static edu.rit.flick.config.DefaultOptionSet.ARCHIVE_MODE;
import static edu.rit.flick.config.DefaultOptionSet.DEFLATION_ARCHIVE_MODE;

/**
* @author Alex Aiezza
Expand All @@ -16,10 +16,13 @@
public class DeflationConfiguration extends AbstractConfiguration
{
{
for ( final DeflationOptionSet dos : FileArchiverExtensionRegistry.getInstance()
.getDeflationOptionSets() )
registerOptionSet( dos );

registerOptionSet( new DeflationOptionSet() );

registerOptionSet( new FastaDeflationOptionSet() );
registerOptionSet( new FastqDeflationOptionSet() );
setFlag( ARCHIVE_MODE, DEFLATION_ARCHIVE_MODE );
}

}
Loading

0 comments on commit 608839e

Please sign in to comment.