All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.boot.antext.XMAChecksum 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
 *******************************************************************************/

/*
 *

 * Created on : 09.07.2003
 * Created by : s3595
 *
 */
package at.spardat.xma.boot.antext;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Jar;
import org.apache.tools.ant.taskdefs.Manifest;

import at.spardat.xma.boot.transform.HashChecksum;


/**
 * XMAChecksum
 *
 * @author s1462 Ferry Malzer (Maf)
 * @version $Id: XMAChecksum.java 2318 2008-02-04 10:04:04Z s2877 $
 *
 */
public class XMAChecksum extends Task  {

    /* verbose output */
    private boolean verbose;

    /* relative path and filename from strdir to the appxml or plugins.xml */
    private File file;

    /** Type of file, default is jar */
    private String type = "jar";

    /**
     *  If true we handle an extern file which has to be jared twice
     */
    private boolean extern = false;


    public XMAChecksum() {
        super();
    }

    /**
     * @return verbose
     */
    public boolean isVerbose() {
        return verbose;
    }

    public boolean isExtern() {
        return extern;
    }

    /**
     * @param file
     */
    public void setFile(File file) {
        this.file = file;
    }

    public void setType(String type) {
        this.type=type;
    }

    /**
     * @param b
     */
    public void setVerbose(boolean b) {
        verbose = b;
    }

    public void setExtern(boolean b) {
        extern = b;
    }

    /* (non-Javadoc)
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() throws BuildException {
        FileOutputStream fos = null;
        try {
            //prepare the MD5 file
            File dest = new File( file.getAbsoluteFile()+AppDescriptor.STR_HASHF_EXT );
            boolean md5isNew = false;
            if (! dest.exists()) {
//                if (type.equals("jar") && isExtern()) {
//                    if (isVerbose()) {
//                        System.out.println("Extern File has to be newly jared before writing digest!");
//                        updateJar("pogus");
//                    }
//                }
                dest.createNewFile();
                md5isNew = true;
                if (isVerbose()) {
                    System.out.println("No MD5 file available for " + file.getAbsolutePath() +". Make a new.");
                }
            }
            if (md5isNew || dest.lastModified() < file.lastModified()) {
                //MD5 file has an older timestamp then the jar file
                //therefore a new MD5 has to be build
                String checksum;
                if("jar".equals(type)) {
                    updateJar("pogus");
                    checksum = HashChecksum.calcJarCheckSum(file);
                    updateJar(checksum);
                } else if("lib".equals(type) || "file".equals(type)){
                    checksum = HashChecksum.calcFileCheckSum(file);
                } else {
                    throw new BuildException("only types 'jar', 'lib' or 'file' supported");
                }
                if (verbose) {
                    System.out.println("File: " + new Date(file.lastModified()));
                    System.out.println("MD5: " + new Date(dest.lastModified()));
                    System.out.println("checksum: " + checksum );
                }

                this.getProject().setProperty( "XMA-Digest", checksum );

                //and wirte in the MD5 file
                fos = new FileOutputStream(dest);
                fos.write(checksum.getBytes());
            }
        } catch (Exception e1) {
            if(verbose) {
                e1.printStackTrace();
            }
            throw new BuildException(e1);
        } finally {
            try {if (fos != null) fos.close(); } catch (Exception e) {}
        }
    }

    /**
     * @param xmaDigest
     * @author s1462
     */
    private void updateJar(String xmaDigest) {
        try {
            //writing checksum direct in the manifest file
            Manifest manifest = new Manifest();
            manifest.addConfiguredAttribute(new Manifest.Attribute("XMA-Digest", xmaDigest));
            Jar jarTask = new Jar();
            jarTask.init();
            jarTask.setProject(this.getProject());
            jarTask.setOwningTarget(this.getOwningTarget());
            jarTask.setTaskName("XMAChecksum");
            jarTask.setTaskType("Jar");
            jarTask.setUpdate(true);
            jarTask.setDestFile(file);
            jarTask.addConfiguredManifest(manifest);
            jarTask.execute();
        } catch (Exception e1) {
            if(verbose) {
                e1.printStackTrace();
            }
            throw new BuildException(e1);
        }
    }

    /* (non-Javadoc)
     * @see org.apache.tools.ant.Task#init()
     */
    public void init() throws BuildException {
        super.init();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy