at.spardat.xma.boot.antext.CopyWithDeltas Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* @(#) $Id: CopyWithDeltas.java 2318 2008-02-04 10:04:04Z s2877 $
*
*/
package at.spardat.xma.boot.antext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* Ant Task to copy a specified file together with its delta files to previous versions into a
* specified target directory. If there allready exists the same files in the target directory,
* only files with newer timestamps are copied.
* @author s2877
*/
public class CopyWithDeltas extends Task {
final static Pattern pattern = Pattern.compile("_(\\d+)\\.(\\d+)\\.(\\d+)\\.");
File file;
File toDir;
boolean preservelastmodified=true;
GenPreloadFile.Version minVersion;
/**
* Default constructor needed by Ant.
*/
public CopyWithDeltas() { }
/**
* Set the File to copy. This file is copied together with all delta versions of
* this file found in the same directory. The filename should contain a version number.
* If the filename does not contain a version number, no delta versions will be found or copied.
* @param file to copy together with is delta files from previous versions.
*/
public void setFile(File file) {
this.file = file;
}
/**
* Set the minimal version for delta files to copy. Delta files from older versions
* will not be copied. Optional, default is 0.0.0
* @param minVersion minimal version to copy
*/
public void setMinVersion(String minVersion) {
this.minVersion = new GenPreloadFile.Version(minVersion,true);
}
/**
* Set the target directory for the copies. The file and its delta versions are
* copied to this directory.
* @param toDir the target directory
*/
public void setToDir(File toDir) {
this.toDir = toDir;
}
/**
* Set if the modification timestamp of the copied files remains unchanged.
* Optional, default is true.
* @param preservelastmodified If true the modification time is not changed.
* If false the modification timestamp is set to the current data and time.
*/
public void setPreservelastmodified(boolean preservelastmodified) {
this.preservelastmodified = preservelastmodified;
}
/**
* Copies the specified file together with its delta files to previous versions into the
* specified target directory.
*/
public void execute() throws BuildException {
if(!file.exists()) throw new BuildException(file.getAbsolutePath()+" not found");
if(!toDir.exists()) {
if(!toDir.mkdirs()) throw new BuildException("error creating "+toDir.getAbsolutePath());
}
File fromDir = file.getParentFile();
FilenameFilter filter = new FilenameFilter() {
final Pattern pattern = searchPattern(file.getName());
public boolean accept(File dir, String name) {
if(file.getName().equals(name)) return true;
if(pattern!=null) {
Matcher matcher = pattern.matcher(name);
if(matcher.matches()) {
if(minVersion!=null) {
GenPreloadFile.Version oldVersion = new GenPreloadFile.Version(matcher.group(1),matcher.group(2),matcher.group(3));
return minVersion.compareTo(oldVersion)<=0;
}
return true;
}
}
return false;
}
};
String[] names = fromDir.list(filter);
if(names==null) throw new BuildException("error reading "+fromDir);
for(int i=0;i_.jar -> _(\\d+)\\.(\\d+)\\.(\\d+)\\-.xdelta.jar
* @param filename must not contain any path seperator (/or\).
* @return the search expresion or null if the filename does not contain a version number.
*/
private Pattern searchPattern(String filename) {
Matcher matcher = pattern.matcher(filename);
if(matcher.find()) {
String search = filename.substring(0,matcher.start())
+ pattern.pattern().substring(0,pattern.pattern().length()-1)
+ "-"
+ matcher.group(0).substring(1,matcher.group(0).length())
+ "xdelta"
+ filename.substring(matcher.end()-1,filename.length());
// escape all unescaped "."
search = search.replaceAll("([^\\\\])\\.","$1\\\\.");
return Pattern.compile("^"+search+"$");
} else {
return null;
}
}
/**
* Copies one file. If the file allready exists in the target directory
* with the same or newer timestamp, it is not copied.
* @param file to copy
* @param toDir target directory
* @param preservelastmodified if true the copied file gets the modification
* timestamp of the source file.
*/
private void copy(File file,File toDir,boolean preservelastmodified) {
File toFile = new File(toDir,file.getName());
if(toFile.exists()&&toFile.lastModified()>=file.lastModified()) return;
FileInputStream in = null;
FileOutputStream out= null;
try {
in = new FileInputStream(file);
out= new FileOutputStream(toFile);
byte[] buffer = new byte[64*1024];
for(int size=in.read(buffer);size>0;size=in.read(buffer)) {
out.write(buffer,0,size);
}
} catch (IOException exc) {
throw new BuildException("error copying "+file.getAbsolutePath()+" to "+toDir.getAbsolutePath(),exc);
} finally {
if(in!=null) try { in.close(); } catch(IOException e) {}
if(out!=null) try { out.close(); } catch(IOException e) {}
}
// reset modification timestamp
if(preservelastmodified) {
toFile.setLastModified(file.lastModified());
}
}
}