1 17 package org.apache.tools.ant.taskdefs.optional.metamata; 18 19 import java.io.File ; 20 import java.io.FileWriter ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.DirectoryScanner; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.Task; 30 import org.apache.tools.ant.taskdefs.Execute; 31 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 32 import org.apache.tools.ant.types.Commandline; 33 import org.apache.tools.ant.types.CommandlineJava; 34 import org.apache.tools.ant.types.FileSet; 35 import org.apache.tools.ant.types.Path; 36 import org.apache.tools.ant.util.FileUtils; 37 import org.apache.tools.ant.util.JavaEnvUtils; 38 39 47 public abstract class AbstractMetamataTask extends Task { 48 49 54 protected Path classPath = null; 55 56 57 protected Path sourcePath = null; 58 59 64 protected File metamataHome = null; 65 66 67 protected CommandlineJava cmdl = new CommandlineJava(); 68 69 70 protected Vector fileSets = new Vector (); 71 72 73 protected File optionsFile = null; 74 75 protected Hashtable includedFiles = null; 78 79 public AbstractMetamataTask() { 80 } 81 82 83 protected AbstractMetamataTask(String className) { 84 cmdl.setVm(JavaEnvUtils.getJreExecutable("java")); 85 cmdl.setClassname(className); 86 } 87 88 92 public void setHome(final File value) { 93 this.metamataHome = value; 94 } 95 96 99 public void setMetamatahome(final File value) { 100 setHome(value); 101 } 102 103 107 public Path createClasspath() { 108 if (classPath == null) { 109 classPath = new Path(getProject()); 110 } 111 return classPath; 112 } 113 114 118 public Path createSourcepath() { 119 if (sourcePath == null) { 120 sourcePath = new Path(getProject()); 121 } 122 return sourcePath; 123 } 124 125 135 public Commandline.Argument createJvmarg() { 136 return cmdl.createVmArgument(); 137 } 138 139 143 public void setMaxmemory(String max) { 144 cmdl.setMaxmemory(max); 145 } 146 147 148 156 public void addFileSet(FileSet fs) { 157 fileSets.addElement(fs); 158 } 159 160 161 public void execute() throws BuildException { 162 try { 163 setUp(); 164 ExecuteStreamHandler handler = createStreamHandler(); 165 execute0(handler); 166 } finally { 167 cleanUp(); 168 } 169 } 170 171 172 protected void setUp() throws BuildException { 173 checkOptions(); 174 175 File jar = getMetamataJar(metamataHome); 177 final Path classPath = cmdl.createClasspath(getProject()); 178 classPath.createPathElement().setLocation(jar); 179 180 final Commandline.Argument vmArgs = cmdl.createVmArgument(); 182 vmArgs.setValue("-Dmetamata.home=" + metamataHome.getAbsolutePath()); 183 184 includedFiles = scanSources(new Hashtable ()); 186 log(includedFiles.size() + " files added for audit", Project.MSG_VERBOSE); 189 190 Vector options = getOptions(); 192 optionsFile = createTmpFile(); 193 generateOptionsFile(optionsFile, options); 194 Commandline.Argument args = cmdl.createArgument(); 195 args.setLine("-arguments " + optionsFile.getAbsolutePath()); 196 } 197 198 202 protected abstract ExecuteStreamHandler createStreamHandler(); 203 204 205 206 protected void execute0(ExecuteStreamHandler handler) throws BuildException { 207 final Execute process = new Execute(handler); 208 log(cmdl.describeCommand(), Project.MSG_VERBOSE); 209 process.setCommandline(cmdl.getCommandline()); 210 try { 211 if (process.execute() != 0) { 212 throw new BuildException("Metamata task failed."); 213 } 214 } catch (IOException e) { 215 throw new BuildException("Failed to launch Metamata task", e); 216 } 217 } 218 219 220 protected void cleanUp() { 221 if (optionsFile != null) { 222 optionsFile.delete(); 223 optionsFile = null; 224 } 225 } 226 227 228 protected final File getMetamataJar(File home) { 229 return new File (home, "lib/metamata.jar"); 230 } 231 232 233 protected void checkOptions() throws BuildException { 234 if (metamataHome == null || !metamataHome.exists()) { 236 throw new BuildException("'home' must point to Metamata home directory."); 237 } 238 File jar = getMetamataJar(metamataHome); 239 if (!jar.exists()) { 240 throw new BuildException(jar + " does not exist. Check your metamata installation."); 241 } 242 } 243 244 245 protected abstract Vector getOptions(); 246 247 248 protected void generateOptionsFile(File tofile, Vector options) throws BuildException { 249 FileWriter fw = null; 250 try { 251 fw = new FileWriter (tofile); 252 PrintWriter pw = new PrintWriter (fw); 253 final int size = options.size(); 254 for (int i = 0; i < size; i++) { 255 pw.println(options.elementAt(i)); 256 } 257 pw.flush(); 258 } catch (IOException e) { 259 throw new BuildException("Error while writing options file " + tofile, e); 260 } finally { 261 if (fw != null) { 262 try { 263 fw.close(); 264 } catch (IOException ignored) { 265 } 266 } 267 } 268 } 269 270 271 protected Hashtable getFileMapping() { 272 return includedFiles; 273 } 274 275 278 protected static final void addAllVector(Vector dest, Enumeration files) { 279 while (files.hasMoreElements()) { 280 dest.addElement(files.nextElement()); 281 } 282 } 283 284 protected final File createTmpFile() { 285 File tmpFile = FileUtils.newFileUtils() 286 .createTempFile("metamata", ".tmp", getProject().getBaseDir()); 287 tmpFile.deleteOnExit(); 288 return tmpFile; 289 } 290 291 295 296 protected Hashtable scanSources(Hashtable map) { 297 Hashtable files = new Hashtable (); 298 for (int i = 0; i < fileSets.size(); i++) { 299 FileSet fs = (FileSet) fileSets.elementAt(i); 300 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 301 ds.scan(); 302 String [] f = ds.getIncludedFiles(); 303 log(i + ") Adding " + f.length + " files from directory " 304 + ds.getBasedir(), Project.MSG_VERBOSE); 305 for (int j = 0; j < f.length; j++) { 306 String pathname = f[j]; 307 if (pathname.endsWith(".java")) { 308 File file = new File (ds.getBasedir(), pathname); 309 String classname = pathname.substring(0, pathname.length() - ".java".length()); 311 classname = classname.replace(File.separatorChar, '.'); 312 files.put(file.getAbsolutePath(), classname); } 314 } 315 } 316 return files; 317 } 318 319 protected Hashtable scanSources(final Hashtable mapping, final String [] entries) { 320 final Vector javaFiles = new Vector (512); 321 for (int i = 0; i < entries.length; i++) { 322 final File f = new File (entries[i]); 323 if (f.isDirectory()) { 324 DirectoryScanner ds = new DirectoryScanner(); 325 ds.setBasedir(f); 326 ds.setIncludes(new String []{"**/*.java"}); 327 ds.scan(); 328 String [] included = ds.getIncludedFiles(); 329 for (int j = 0; j < included.length; j++) { 330 javaFiles.addElement(new File (f, included[j])); 331 } 332 } else if (entries[i].endsWith(".java")) { 333 javaFiles.addElement(f); 334 } 335 } 336 final int count = javaFiles.size(); 338 for (int i = 0; i < count; i++) { 339 File file = (File ) javaFiles.elementAt(i); 340 String pathname = Path.translateFile(file.getAbsolutePath()); 341 String classname = pathname.substring(0, pathname.length() - ".java".length()); 342 classname = classname.replace(File.separatorChar, '.'); 343 mapping.put(pathname, classname); 344 } 345 return mapping; 346 } 347 348 } 349 | Popular Tags |