1 50 51 package net.sf.just4log.integration.ant; 52 53 import java.io.File ; 54 import java.io.FileInputStream ; 55 import java.io.FileNotFoundException ; 56 import java.io.FileOutputStream ; 57 import java.io.IOException ; 58 59 import net.sf.just4log.JustLog; 60 import net.sf.just4log.transform.Transform; 61 62 import org.apache.bcel.classfile.ClassFormatException; 63 import org.apache.tools.ant.BuildException; 64 import org.apache.tools.ant.DirectoryScanner; 65 import org.apache.tools.ant.Project; 66 import org.apache.tools.ant.taskdefs.MatchingTask; 67 import org.apache.tools.ant.types.Path; 68 import org.apache.tools.ant.types.Reference; 69 70 74 75 public class Just4LogTask extends MatchingTask { 76 private static String CLASSID = "$Id: Just4LogTask.java,v 1.8 2003/10/20 17:32:12 lbruand Exp $"; 77 80 public Just4LogTask() { 81 super(); 82 } 83 private Path src; 84 public Path createSrc() { 85 if (src == null) { 86 src = new Path(getProject()); 87 } 88 return src.createPath(); 89 } 90 91 public void setEnters(boolean level) { 92 Transform.level_enters = level ? Transform.SIMPLEENTER : Transform.NOENTER; 93 } 94 95 public boolean getEnters() { 96 return (Transform.level_enters == Transform.SIMPLEENTER); 97 } 98 99 public void setExits(boolean level) { 100 Transform.level_exits = level ? Transform.SIMPLEEXIT : Transform.NOEXIT; 101 } 102 103 public boolean getExits() { 104 return (Transform.level_exits == Transform.SIMPLEEXIT); 105 } 106 107 108 private Path compileClasspath; 109 114 public void setClasspath(Path classpath) { 115 if (compileClasspath == null) { 116 compileClasspath = classpath; 117 } else { 118 compileClasspath.append(classpath); 119 } 120 } 121 122 123 public Path getClasspath() { 124 return compileClasspath; 125 } 126 127 130 public Path createClasspath() { 131 if (compileClasspath == null) { 132 compileClasspath = new Path(getProject()); 133 } 134 return compileClasspath.createPath(); 135 } 136 137 140 public void setClasspathRef(Reference r) { 141 createClasspath().setRefid(r); 142 } 143 144 145 private File destDir; 146 147 150 public void setDestDir(File file) { 151 destDir = file; 152 } 153 156 public File getDestDir() { 157 return destDir; 158 } 159 165 protected void checkParameters() throws BuildException { 166 if (destDir != null && !destDir.isDirectory()) { 167 throw new BuildException( 168 "destination directory \"" 169 + destDir 170 + "\" does not exist " 171 + "or is not a directory", 172 getLocation()); 173 } 174 } 175 176 179 public void execute() throws BuildException { 180 super.execute(); 181 checkParameters(); 182 if (compileClasspath != null) { 184 JustLog.setClasspath(compileClasspath.toString()); 185 } 186 String [] list = src.list(); 187 for (int i = 0; i < list.length; i++) { 188 File srcDir = getProject().resolveFile(list[i]); 189 if (!srcDir.exists()) { 190 throw new BuildException( 191 "srcdir \"" + srcDir.getPath() + "\" does not exist!", 192 getLocation()); 193 } 194 195 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 196 String [] files = ds.getIncludedFiles(); 197 int j; 198 for (j = 0; j < files.length; j++) { 199 String one = files[j]; 200 201 File source = new File (srcDir, one); 202 File destination = new File (getDestDir(), one); 203 if (!destination.getParentFile().exists()) { 204 log("Making dir: "+destination.getParentFile(), Project.MSG_VERBOSE); 205 destination.getParentFile().mkdirs(); 206 } 207 208 209 if (source.lastModified() <= destination.lastModified()) { 210 log(source+" omitted as "+destination+" is up to date", Project.MSG_VERBOSE); 211 continue; 212 } 213 if (one.endsWith(".class")) { 214 try { 215 log("source: "+source + " to "+destination, Project.MSG_VERBOSE); 216 JustLog.speedup(source, destination); 217 218 log("Payload: " + 100*(destination.length()-source.length())/source.length()+ "%"); 219 } catch (ClassFormatException e) { 220 throw new BuildException(one + " is not a class file", e); 221 } catch (IOException e) { 222 throw new BuildException(one + " has an IO problem", e); 223 } 224 } 225 if (one.endsWith(".jar") 226 || one.endsWith(".ear") 227 || one.endsWith(".zip") 228 || one.endsWith(".war")) { 229 try { 230 JustLog.speedupZip(new FileInputStream (source), new FileOutputStream (destination)); 231 } catch (FileNotFoundException e) { 232 throw new BuildException(one + " is not found", e); 233 } catch (IOException e) { 234 StackTraceElement [] st=e.getStackTrace(); 235 StringBuffer sb= new StringBuffer (); 236 for(int h=0; h<st.length; h++) { 237 sb.append(st[h].toString()); 238 sb.append("\n"); 239 } 240 throw new BuildException(one + " has an IO problem: "+sb.toString(), e); 241 } 242 } 243 } 244 245 } 246 247 } 248 249 } 250 | Popular Tags |