
fluximpl.UntarFileActionImpl Maven / Gradle / Ivy
package fluximpl;
import flux.EngineException;
import flux.FlowContext;
import flux.UntarFileAction;
import flux.logging.Level;
import flux.logging.Logger;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.IdentityMapper;
import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import java.io.*;
import java.util.*;
import java.util.zip.GZIPInputStream;
/**
* Implementation for UntarFileAction.
*
* This implementation is based on the Ant Untar task.
*
* @see org.apache.tools.ant.taskdefs.Untar
*/
public class UntarFileActionImpl extends ActionImpl implements UntarFileAction {
private static String ACTION_VARIABLE = "UNTAR_FILE_ACTION_VARIABLE";
private static final int BUFFER_SIZE = 1024;
private Mapper mapperElement = null;
// permissible values for compression attribute
/**
* No compression
*/
private static final String NONE = "none";
/**
* GZIP compression
*/
private static final String GZIP = "gzip";
/**
* BZIP2 compression
*/
private static final String BZIP2 = "bzip2";
public UntarFileActionImpl() {
super(new FlowChartImpl(), "Untar File Action");
}
public UntarFileActionImpl(FlowChartImpl flowChart, String name) {
super(flowChart, name);
}
public Set getHiddenVariableNames() {
// The returned raw type is a Set of String.
@SuppressWarnings("unchecked")
Set set = super.getHiddenVariableNames();
set.add(ACTION_VARIABLE);
return set;
}
public Object execute(FlowContext flowContext) throws Exception {
File sourceFile = new File(getSource());
File targetDir = new File(getDestination());
String value;
int dot = getSource().lastIndexOf('.');
String extension = getSource().substring(dot + 1);
if (extension.equalsIgnoreCase("bz2")) {
value = BZIP2;
} else if (extension.equalsIgnoreCase("gz")) {
value = GZIP;
} else if (extension.equalsIgnoreCase("tar")) {
value = NONE;
} else {
throw new EngineException("Unsupported file type " + getSource() + ". Valid types are tar, bz2, and gz.");
}
expandFile(sourceFile, targetDir, value, flowContext.getLogger());
return null;
}
public void expandFile(File srcF, File dir, String value, Logger logger) throws EngineException {
FileInputStream fis = null;
if (!srcF.exists()) {
throw new EngineException("Unable to expand " + srcF + " as the file does not exist.");
}
try {
fis = new FileInputStream(srcF);
expandStream(srcF.getPath(), fis, dir, value, logger);
} catch (IOException ioe) {
throw new EngineException("Error while expanding " + srcF.getPath() + "\n" + ioe.getMessage());
} finally {
FileUtils.close(fis);
}
}
private void expandStream(String name, InputStream stream, File dir, String value, Logger logger)
throws IOException, EngineException {
TarInputStream tis = null;
try {
tis = new TarInputStream(decompress(name, new BufferedInputStream(stream), value));
logger.info("Expanding: " + name + " into " + dir);
TarEntry te;
FileNameMapper mapper = getMapper();
while ((te = tis.getNextEntry()) != null) {
extractFile(FileUtils.getFileUtils(), null, dir, tis, te.getName(), te.getModTime(), te.isDirectory(), mapper, logger);
}
} finally {
FileUtils.close(tis);
}
}
/**
* This method wraps the input stream with the
* corresponding decompression method
*
* @param name provides location information for BuildException
* @param istream input stream
* @param value compression method
* @return input stream with on-the-fly decompression
* @throws IOException thrown by GZIPInputStream constructor
* @throws EngineException thrown if bzip stream does not
* start with expected magic values
*/
public InputStream decompress(final String name, final InputStream istream, String value)
throws IOException, EngineException {
if (GZIP.equals(value)) {
return new GZIPInputStream(istream);
} else {
if (BZIP2.equals(value)) {
final char[] magic = new char[]{'B', 'Z'};
for (int i = 0; i < magic.length; i++) {
if (istream.read() != magic[i]) {
throw new EngineException("Invalid bz2 file." + name);
}
}
return new CBZip2InputStream(istream);
}
}
return istream;
}
private FileNameMapper getMapper() {
FileNameMapper mapper = null;
if (mapperElement != null) {
mapper = mapperElement.getImplementation();
} else {
mapper = new IdentityMapper();
}
return mapper;
}
private void extractFile(FileUtils fileUtils, File srcF, File dir,
InputStream compressedInputStream,
String entryName, Date entryDate,
boolean isDirectory, FileNameMapper mapper, Logger logger)
throws IOException {
String name = entryName.replace('/', File.separatorChar)
.replace('\\', File.separatorChar);
boolean included = false;
Set includePatterns = new HashSet();
Set excludePatterns = new HashSet();
List incls = getIncludePatterns();
if (incls == null || incls.size() == 0) {
// no include pattern implicitly means includePatterns="**"
incls = new ArrayList();
incls.add("**");
}
for (String incl : incls) {
String pattern = incl.replace('/', File.separatorChar)
.replace('\\', File.separatorChar);
if (pattern.endsWith(File.separator)) {
pattern += "**";
}
includePatterns.add(pattern);
}
List excls = getExcludePatterns();
if (excls != null) {
for (String excl : excls) {
String pattern = excl
.replace('/', File.separatorChar)
.replace('\\', File.separatorChar);
if (pattern.endsWith(File.separator)) {
pattern += "**";
}
excludePatterns.add(pattern);
}
}
for (Iterator iter = includePatterns.iterator();
!included && iter.hasNext();) {
String pattern = (String) iter.next();
included = SelectorUtils.matchPath(pattern, name);
}
for (Iterator iter = excludePatterns.iterator();
included && iter.hasNext();) {
String pattern = (String) iter.next();
included = !SelectorUtils.matchPath(pattern, name);
}
if (!included) {
//Do not process this file
logger.finest("Skipping " + entryName + " as it is excluded or not included.");
return;
}
String[] mappedNames = mapper.mapFileName(entryName);
if (mappedNames == null || mappedNames.length == 0) {
mappedNames = new String[]{entryName};
}
File f = fileUtils.resolveFile(dir, mappedNames[0]);
try {
if (!isOverwriteEnabled() && f.exists() && f.lastModified() >= entryDate.getTime()) {
logger.finest("Skipping " + f + " as it is up-to-date.");
return;
}
logger.finest("Expanding " + entryName + " to " + f);
// create intermediary directories - sometimes zip don't add them
File dirF = f.getParentFile();
if (dirF != null) {
dirF.mkdirs();
}
if (isDirectory) {
f.mkdirs();
} else {
byte[] buffer = new byte[BUFFER_SIZE];
int length = 0;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
while ((length =
compressedInputStream.read(buffer)) >= 0) {
fos.write(buffer, 0, length);
}
fos.close();
fos = null;
} finally {
FileUtils.close(fos);
}
}
fileUtils.setFileLastModified(f, entryDate.getTime());
} catch (FileNotFoundException ex) {
logger.log(Level.WARNING, "Unable to expand to file " + f.getPath(), ex);
}
}
public void verify() throws EngineException {
UntarFileActionVariable var = getVariable();
if (StringUtil.isNullOrEmpty(var.getSource())) {
throw new EngineException("Expected tar file path to be non-null or not empty, but it was.");
}
}
public String getDestination() {
return getVariable().getDestination();
}
public void setDestination(String dest) {
UntarFileActionVariable var = getVariable();
var.setDestination(dest);
putVariable(var);
}
@Override
public void setOverwriteEnabled(boolean overwrite) {
UntarFileActionVariable var = getVariable();
var.setOverwrite(overwrite);
putVariable(var);
}
public boolean isOverwriteEnabled() {
return getVariable().isOverwrite();
}
@Override
public void setIncludePatterns(List includePatterns) {
UntarFileActionVariable var = getVariable();
var.setIncludePatterns(includePatterns);
putVariable(var);
}
public List getIncludePatterns() {
return getVariable().getIncludePatterns();
}
public String getIndexedIncludePatterns(int index) {
List includes = getVariable().getIncludePatterns();
int count = 0;
String include;
for (Iterator iterator = includes.iterator(); iterator.hasNext(); ++count) {
include = (String) iterator.next();
if (index == count) {
return include;
} // if
} // for
throw new IllegalArgumentException(ErrorMessages.makeIllegalBeanPropertyIndex(index, includes.size()));
}
public String[] getIndexedIncludePatterns() {
List includes = getVariable().getIncludePatterns();
String[] resultType = new String[0];
return includes.toArray(resultType);
}
public void setIndexedIncludePatterns(String[] includes) {
List includesList = getVariable().getIncludePatterns();
includesList.clear();
for (int i = 0; i < includes.length; i++) {
String include = includes[i];
setIndexedIncludePatterns(i, include);
} // for
}
public void setIndexedIncludePatterns(int index, String include) {
if (include == null) {
return;
}
UntarFileActionVariable variable = getVariable();
List includes = variable.getIncludePatterns();
includes.add(index, include);
putVariable(variable);
}
@Override
public void setExcludePatterns(List excludePatterns) {
UntarFileActionVariable var = getVariable();
var.setExcludePatterns(excludePatterns);
putVariable(var);
}
public List getExcludePatterns() {
return getVariable().getExcludePatterns();
}
public String getIndexedExcludePatterns(int index) {
List excludes = getVariable().getExcludePatterns();
int count = 0;
String exclude;
for (Iterator iterator = excludes.iterator(); iterator.hasNext(); ++count) {
exclude = (String) iterator.next();
if (index == count) {
return exclude;
} // if
} // for
throw new IllegalArgumentException(ErrorMessages.makeIllegalBeanPropertyIndex(index, excludes.size()));
}
public String[] getIndexedExcludePatterns() {
List excludes = getVariable().getExcludePatterns();
String[] resultType = new String[0];
return excludes.toArray(resultType);
}
public void setIndexedExcludePatterns(String[] excludes) {
List excludesList = getVariable().getExcludePatterns();
excludesList.clear();
for (int i = 0; i < excludes.length; i++) {
String include = excludes[i];
setIndexedExcludePatterns(i, include);
} // for
}
public void setIndexedExcludePatterns(int index, String exclude) {
if (exclude == null) {
return;
}
UntarFileActionVariable variable = getVariable();
List excludes = variable.getExcludePatterns();
excludes.add(index, exclude);
putVariable(variable);
}
public String getSource() {
return getVariable().getSource();
}
public void setSource(String src) {
UntarFileActionVariable var = getVariable();
var.setSource(src);
putVariable(var);
}
private UntarFileActionVariable getVariable() {
if (!getVariableManager().contains(ACTION_VARIABLE)) {
getVariableManager().put(ACTION_VARIABLE, new UntarFileActionVariable());
} // if
return (UntarFileActionVariable) getVariableManager().get(ACTION_VARIABLE);
} // getVariable()
private void putVariable(UntarFileActionVariable variable) {
getVariableManager().put(ACTION_VARIABLE, variable);
} // putVariable()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy