| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.javaflow.ant; |
| 18 | |
|
| 19 | |
import java.io.File; |
| 20 | |
import java.io.FileInputStream; |
| 21 | |
import java.io.FileOutputStream; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.util.jar.JarInputStream; |
| 24 | |
import java.util.jar.JarOutputStream; |
| 25 | |
import org.apache.commons.javaflow.bytecode.transformation.ResourceTransformer; |
| 26 | |
import org.apache.commons.javaflow.bytecode.transformation.asm.AsmClassTransformer; |
| 27 | |
import org.apache.commons.javaflow.bytecode.transformation.bcel.BcelClassTransformer; |
| 28 | |
import org.apache.commons.javaflow.utils.RewritingUtils; |
| 29 | |
import org.apache.tools.ant.BuildException; |
| 30 | |
import org.apache.tools.ant.DirectoryScanner; |
| 31 | |
import org.apache.tools.ant.Project; |
| 32 | |
import org.apache.tools.ant.types.FileSet; |
| 33 | |
import org.apache.tools.ant.taskdefs.MatchingTask; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | 0 | public class AntRewriteTask extends MatchingTask { |
| 42 | |
|
| 43 | |
private ResourceTransformer transformer; |
| 44 | |
|
| 45 | |
private File dstDir; |
| 46 | |
private File srcDir; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public void setDstDir(final File pFile) { |
| 53 | 0 | dstDir = pFile; |
| 54 | 0 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public void setSrcDir(final File pFile) { |
| 62 | 0 | srcDir = pFile; |
| 63 | 0 | fileset.setDir(srcDir); |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public void setMode(String name) { |
| 78 | 0 | name = name.toLowerCase(); |
| 79 | 0 | if(name.equals("bcel")) |
| 80 | 0 | transformer = new BcelClassTransformer(); |
| 81 | |
else |
| 82 | 0 | if(name.equals("asm")) |
| 83 | 0 | transformer = new AsmClassTransformer(); |
| 84 | |
else |
| 85 | 0 | throw new BuildException("Unrecognized mode: "+name); |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
protected void checkParameters() throws BuildException { |
| 95 | 0 | checkDir(srcDir,"srcDir"); |
| 96 | 0 | checkDir(dstDir,"dstDir"); |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
private void checkDir(final File pDir, final String pDescription) { |
| 100 | 0 | if (pDir == null) { |
| 101 | 0 | throw new BuildException("no " + pDescription + " directory is specified", getLocation()); |
| 102 | |
} |
| 103 | 0 | if (!pDir.exists()) { |
| 104 | 0 | throw new BuildException(pDescription + " directory \"" + pDir + "\" does not exist", getLocation()); |
| 105 | |
} |
| 106 | 0 | if (!pDir.isDirectory()) { |
| 107 | 0 | throw new BuildException(pDescription + " directory \"" + pDir + "\" is not a directory", getLocation()); |
| 108 | |
} |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
public void execute() throws BuildException { |
| 112 | |
|
| 113 | 0 | final DirectoryScanner ds = fileset.getDirectoryScanner(getProject()); |
| 114 | 0 | final String[] fileNames = ds.getIncludedFiles(); |
| 115 | |
|
| 116 | |
|
| 117 | 0 | if(transformer==null) |
| 118 | 0 | transformer = new BcelClassTransformer(); |
| 119 | |
|
| 120 | |
try { |
| 121 | 0 | for (int i = 0; i < fileNames.length; i++) { |
| 122 | 0 | final String fileName = fileNames[i]; |
| 123 | |
|
| 124 | 0 | final File source = new File(srcDir, fileName); |
| 125 | 0 | final File destination = new File(dstDir, fileName); |
| 126 | |
|
| 127 | 0 | if (!destination.getParentFile().exists()) { |
| 128 | 0 | log("Creating dir: " + destination.getParentFile(), Project.MSG_VERBOSE); |
| 129 | 0 | destination.getParentFile().mkdirs(); |
| 130 | |
} |
| 131 | |
|
| 132 | 0 | if (source.lastModified() < destination.lastModified()) { |
| 133 | 0 | log("Omitting " + source + " as " + destination + " is up to date", Project.MSG_VERBOSE); |
| 134 | 0 | continue; |
| 135 | |
} |
| 136 | |
|
| 137 | 0 | if (fileName.endsWith(".class")) { |
| 138 | 0 | log("Rewriting " + source + " to " + destination, Project.MSG_VERBOSE); |
| 139 | 0 | System.out.println("Rewriting " + source); |
| 140 | |
|
| 141 | 0 | RewritingUtils.rewriteClassFile( source, transformer, destination ); |
| 142 | |
} |
| 143 | |
|
| 144 | 0 | if (fileName.endsWith(".jar") |
| 145 | |
|| fileName.endsWith(".ear") |
| 146 | |
|| fileName.endsWith(".zip") |
| 147 | |
|| fileName.endsWith(".war")) { |
| 148 | |
|
| 149 | 0 | log("Rewriting " + source + " to " + destination, Project.MSG_VERBOSE); |
| 150 | |
|
| 151 | 0 | RewritingUtils.rewriteJar( |
| 152 | |
new JarInputStream(new FileInputStream(source)), |
| 153 | |
transformer, |
| 154 | |
new JarOutputStream(new FileOutputStream(destination)) |
| 155 | |
); |
| 156 | |
|
| 157 | |
} |
| 158 | |
} |
| 159 | 0 | } catch (IOException e) { |
| 160 | 0 | throw new BuildException(e); |
| 161 | 0 | } |
| 162 | |
|
| 163 | 0 | } |
| 164 | |
} |