/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 2000-2003 Lucas Bruand. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Just4Log" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
package net.sf.just4log.integration.ant;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import net.sf.just4log.JustLog;
import net.sf.just4log.transform.Transform;
import org.apache.bcel.classfile.ClassFormatException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
/**
* @author Lucas Bruand
* @version $Id: Just4LogTask.java,v 1.8 2003/10/20 17:32:12 lbruand Exp $
*/
public class Just4LogTask extends MatchingTask {
private static String CLASSID = "$Id: Just4LogTask.java,v 1.8 2003/10/20 17:32:12 lbruand Exp $";
/**
*
*/
public Just4LogTask() {
super();
}
private Path src;
public Path createSrc() {
if (src == null) {
src = new Path(getProject());
}
return src.createPath();
}
public void setEnters(boolean level) {
Transform.level_enters = level ? Transform.SIMPLEENTER : Transform.NOENTER;
}
public boolean getEnters() {
return (Transform.level_enters == Transform.SIMPLEENTER);
}
public void setExits(boolean level) {
Transform.level_exits = level ? Transform.SIMPLEEXIT : Transform.NOEXIT;
}
public boolean getExits() {
return (Transform.level_exits == Transform.SIMPLEEXIT);
}
private Path compileClasspath;
/**
* Set the classpath to be used for this compilation.
*
* @param classpath an Ant Path object containing the compilation classpath.
*/
public void setClasspath(Path classpath) {
if (compileClasspath == null) {
compileClasspath = classpath;
} else {
compileClasspath.append(classpath);
}
}
/** Gets the classpath to be used for this compilation. */
public Path getClasspath() {
return compileClasspath;
}
/**
* Adds a path to the classpath.
*/
public Path createClasspath() {
if (compileClasspath == null) {
compileClasspath = new Path(getProject());
}
return compileClasspath.createPath();
}
/**
* Adds a reference to a classpath defined elsewhere.
*/
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
}
private File destDir;
/**
* @param file
*/
public void setDestDir(File file) {
destDir = file;
}
/**
* @return
*/
public File getDestDir() {
return destDir;
}
/**
* Check that all required attributes have been set and nothing
* silly has been entered.
*
* @since Ant 1.5
*/
protected void checkParameters() throws BuildException {
if (destDir != null && !destDir.isDirectory()) {
throw new BuildException(
"destination directory \""
+ destDir
+ "\" does not exist "
+ "or is not a directory",
getLocation());
}
}
/* (non-Javadoc)
* @see org.apache.tools.ant.Task#execute()
*/
public void execute() throws BuildException {
super.execute();
checkParameters();
// Setting the classpath.
if (compileClasspath != null) {
JustLog.setClasspath(compileClasspath.toString());
}
String[] list = src.list();
for (int i = 0; i < list.length; i++) {
File srcDir = getProject().resolveFile(list[i]);
if (!srcDir.exists()) {
throw new BuildException(
"srcdir \"" + srcDir.getPath() + "\" does not exist!",
getLocation());
}
DirectoryScanner ds = this.getDirectoryScanner(srcDir);
String[] files = ds.getIncludedFiles();
int j;
for (j = 0; j < files.length; j++) {
String one = files[j];
File source = new File(srcDir, one);
File destination = new File(getDestDir(), one);
if (!destination.getParentFile().exists()) {
log("Making dir: "+destination.getParentFile(), Project.MSG_VERBOSE);
destination.getParentFile().mkdirs();
}
if (source.lastModified() <= destination.lastModified()) {
log(source+" omitted as "+destination+" is up to date", Project.MSG_VERBOSE);
continue;
}
if (one.endsWith(".class")) {
try {
log("source: "+source + " to "+destination, Project.MSG_VERBOSE);
JustLog.speedup(source, destination);
log("Payload: " + 100*(destination.length()-source.length())/source.length()+ "%");
} catch (ClassFormatException e) {
throw new BuildException(one + " is not a class file", e);
} catch (IOException e) {
throw new BuildException(one + " has an IO problem", e);
}
}
if (one.endsWith(".jar")
|| one.endsWith(".ear")
|| one.endsWith(".zip")
|| one.endsWith(".war")) {
try {
JustLog.speedupZip(new FileInputStream(source), new FileOutputStream(destination));
} catch (FileNotFoundException e) {
throw new BuildException(one + " is not found", e);
} catch (IOException e) {
StackTraceElement[] st=e.getStackTrace();
StringBuffer sb= new StringBuffer();
for(int h=0; h<st.length; h++) {
sb.append(st[h].toString());
sb.append("\n");
}
throw new BuildException(one + " has an IO problem: "+sb.toString(), e);
}
}
}
}
}
}