at.spardat.xma.boot.antext.JarDeltas 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: JarDeltas.java 2369 2008-03-11 15:47:22Z s3460 $
*
*/
package at.spardat.xma.boot.antext;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import at.spardat.xma.boot.cache.VersionNumber;
import at.spardat.xma.boot.transform.HashChecksum;
import at.spardat.xma.xdelta.JarPatcher;
/**
* @author s2877
* @since version_number
*/
public class JarDeltas extends Task {
File target;
File versionsDir;
String strMinVersion="0_0_0";
boolean testDeltas=true;
boolean no99=false;
String jarName;
File targetDir;
VersionNumber minVersion;
VersionNumber version;
String hash;
at.spardat.xma.xdelta.JarDelta jarDelta = new at.spardat.xma.xdelta.JarDelta();
JarPatcher jarPatcher = new JarPatcher();
/**
* Set the jar-file of the new version. This file must contain a version
* number in its name. e.g.: xmartclient_1.6.0.jar
* The delta files are generated in the same directory as the target file.
* @param target the jar-file for which the deltas to the previous versions shall be generated.
*/
public void setTarget(File target) {
this.target=target;
}
/**
* Set the minimal version number for which deltas shall be generated.
* Previous versions older than minVersion are ignored.
* The version number must be in the form: __ eg.: 1_3_0
* This parameter is optional. Default is 0_0_0
* @param minVersion
*/
public void setMinVersion(String minVersion) {
this.strMinVersion = minVersion;
}
/**
* Set the directory where to search for version subdirectorys.
* e.g. w:/imc/repository/xma/xma_runtime
* The version subdirectorys must contain jar files of the same name
* as the target file with differend version numbers included in their names.
* @param versionsDir
*/
public void setVersionsDir(File versionsDir) {
this.versionsDir = versionsDir;
}
/**
* Set if the generated deltas shall be tested.
* This parameter is optional. Default is true.
* @param testDeltas
*/
public void setTestDeltas(boolean testDeltas) {
this.testDeltas = testDeltas;
}
/**
* Set if version 99.99.99 should be ignored.
* If true no deltas are generated if the target file contains the
* version number _99.99.99.
* This parameter is optional. Default is false.
* @param no99
*/
public void setNo99(boolean no99) {
this.no99 = no99;
}
public void execute() throws BuildException {
if(!target.exists()) throw new BuildException(target.getAbsolutePath()+" not found");
targetDir = target.getParentFile();
jarName = target.getName();
if(jarName.indexOf("SNAPSHOT")>=0){
System.out.println("no delta generation for SNAPSHOT versions supported");
return;
}
version = VersionNumber.parse(jarName);
if(version==null) throw new BuildException(target.getAbsolutePath()+" does not contain a valid version number");
if(no99) {
System.out.println("delta generation deactivated for 99_99_99 version");
if(version.equals(new VersionNumber(99,99,99))) return;
}
if(!versionsDir.exists()) throw new BuildException(versionsDir.getAbsolutePath()+" not found");
Matcher matcher = Pattern.compile("^(\\d+)\\_(\\d+)\\_(\\d+)$").matcher(strMinVersion);
if(matcher.find()){
minVersion = new VersionNumber(
Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3))
);
} else {
throw new BuildException("'"+strMinVersion+"' is not a valid version string");
}
Pattern searchPattern = VersionNumber.searchPattern(jarName);
try {
if(testDeltas) {
hash = HashChecksum.calcJarCheckSum(target);
}
boolean foundDirs=false;
Pattern pattern__ = Pattern.compile("^(\\d+)\\_(\\d+)\\_(\\d+)$");
Pattern patternDots = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)$");
File[] dir = versionsDir.listFiles();
for(int i=0;i=0 && dirVersion.compareTo(version)<0) {
boolean found = false;
File[] jar = dir[i].listFiles();
for(int j=0;jsource.lastModified() && delta.lastModified()>target.lastModified()) {
System.out.println(delta.getAbsolutePath()+" is up to date");
break;
}
System.out.println("creating "+delta.getAbsolutePath());
ZipOutputStream output = new ZipOutputStream(new FileOutputStream(delta));
jarDelta.computeDelta(new ZipFile(source),new ZipFile(target),output);
if(testDeltas) {
File temp = File.createTempFile("test",null,targetDir);
try {
jarPatcher.applyDelta(new ZipFile(jar[j]),new ZipFile(delta),new ZipOutputStream(new FileOutputStream(temp)));
String newHash = HashChecksum.calcJarCheckSum(temp);
if(!hash.equals(newHash)) {
throw new BuildException("patching with "+delta.getAbsolutePath()+" failed");
}
System.out.println("tested "+delta.getAbsolutePath()+" ok");
} finally {
temp.delete();
}
}
break;
}
}
if(!found) {
System.out.println("no previous version of "+jarName+" found in "+dir[i].getAbsolutePath());
}
}
}
}
if(!foundDirs) {
throw new BuildException("no version subdirs under "+versionsDir.getAbsolutePath()+" found");
}
} catch (IOException exc) {
throw new BuildException(exc);
}
}
}