1 45 package org.exolab.jms.plugins.proxygen; 46 47 import java.io.File ; 48 import java.io.FileOutputStream ; 49 import java.io.IOException ; 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.StringTokenizer ; 54 55 import org.apache.tools.ant.AntClassLoader; 56 import org.apache.tools.ant.BuildException; 57 import org.apache.tools.ant.DirectoryScanner; 58 import org.apache.tools.ant.Project; 59 import org.apache.tools.ant.taskdefs.Javac; 60 import org.apache.tools.ant.taskdefs.MatchingTask; 61 import org.apache.tools.ant.types.Path; 62 import org.apache.tools.ant.types.Reference; 63 import org.apache.tools.ant.util.FileNameMapper; 64 import org.apache.tools.ant.util.GlobPatternMapper; 65 import org.apache.tools.ant.util.SourceFileScanner; 66 67 68 74 public class ProxyGeneratorTask extends MatchingTask { 75 76 79 private File _base; 80 81 85 private File _sourceBase; 86 87 90 private String _classname; 91 92 95 private List _adapters = new ArrayList (); 96 97 101 private boolean _debug = false; 102 103 106 private Path _compileClasspath; 107 108 109 112 public ProxyGeneratorTask() { 113 } 114 115 120 public void setBase(File base) { 121 _base = base; 122 } 123 124 129 public File getBase() { 130 return _base; 131 } 132 133 138 public void setClassname(String classname) { 139 _classname = classname; 140 } 141 142 147 public String getClassname() { 148 return _classname; 149 } 150 151 156 public void setAdapters(String adapters) { 157 StringTokenizer tokens = new StringTokenizer (adapters, ","); 158 while (tokens.hasMoreTokens()) { 159 String classname = tokens.nextToken(); 160 Adapter adapter = new Adapter(); 161 adapter.setClassname(classname); 162 addConfiguredAdapter(adapter); 163 } 164 } 165 166 171 public void addConfiguredAdapter(Adapter adapter) { 172 _adapters.add(adapter.getClassname()); 173 } 174 175 180 public void setSourceBase(File sourceBase) { 181 _sourceBase = sourceBase; 182 } 183 184 189 public File getSourceBase() { 190 return _sourceBase; 191 } 192 193 194 200 public void setDebug(boolean debug) { 201 _debug = debug; 202 } 203 204 209 public boolean getDebug() { 210 return _debug; 211 } 212 213 218 public void setClasspath(Path classpath) { 219 if (_compileClasspath == null) { 220 _compileClasspath = classpath; 221 } else { 222 _compileClasspath.append(classpath); 223 } 224 } 225 226 231 public Path createClasspath() { 232 if (_compileClasspath == null) { 233 _compileClasspath = new Path(project); 234 } 235 return _compileClasspath.createPath(); 236 } 237 238 243 public void setClasspathRef(Reference reference) { 244 createClasspath().setRefid(reference); 245 } 246 247 252 public Path getClasspath() { 253 return _compileClasspath; 254 } 255 256 261 public void execute() throws BuildException { 262 if (_base == null) { 263 throw new BuildException("base attribute must be set!", location); 264 } 265 if (!_base.exists()) { 266 throw new BuildException("base does not exist", location); 267 } 268 if (!_base.isDirectory()) { 269 throw new BuildException("base is not a directory", location); 270 } 271 272 if (_sourceBase != null) { 273 if (!_sourceBase.exists()) { 274 throw new BuildException("sourceBase does not exist", 275 location); 276 } 277 if (!_sourceBase.isDirectory()) { 278 throw new BuildException("sourceBase is not a directory", 279 location); 280 } 281 } else { 282 _sourceBase = _base; 283 } 284 285 String [] files; 286 String [] generateList; 287 288 if (_classname == null) { 289 DirectoryScanner scanner = getDirectoryScanner(_base); 292 files = scanner.getIncludedFiles(); 293 } else { 294 files = new String []{ 296 _classname.replace('.', File.separatorChar) + ".class"}; 297 } 298 generateList = scanDir(files); 299 300 int count = generateList.length; 301 if (count > 0) { 302 log("Generating " + count + " prox" + (count > 1 ? "ies" : "y") 303 + " to " + _base, Project.MSG_INFO); 304 305 Path classpath = getCompileClasspath(); 306 AntClassLoader loader = new AntClassLoader(project, classpath); 307 308 for (int i = 0; i < count; ++i) { 309 generate(generateList[i], loader); 310 } 311 312 Javac javac = new Javac(); 313 javac.setProject(project); 314 javac.createSrc().setLocation(_sourceBase); 315 javac.setDestdir(_base); 316 javac.setDebug(_debug); 317 javac.setClasspath(classpath); 318 javac.execute(); 319 } 320 } 321 322 325 public static final class Adapter { 326 327 330 private String _classname; 331 332 337 public void setClassname(String classname) { 338 _classname = classname; 339 } 340 341 346 public String getClassname() { 347 return _classname; 348 } 349 } 350 351 360 protected String generate(String classname, ClassLoader loader) 361 throws BuildException { 362 363 String path = classname.replace('.', File.separatorChar) 364 + "__Proxy.java"; 365 File file = new File (_sourceBase, path); 366 File parent = file.getParentFile(); 367 if (parent.exists()) { 368 if (!parent.isDirectory()) { 369 throw new BuildException("Cannot generate sources to " 370 + parent 371 + ": path is not a directory", location); 372 } 373 } else if (!parent.mkdirs()) { 374 throw new BuildException("Failed to create directory " + parent, 375 location); 376 } 377 378 log("Generating proxy " + file, Project.MSG_DEBUG); 379 380 FileOutputStream stream = null; 381 try { 382 stream = new FileOutputStream (file); 383 Class clazz = loader.loadClass(classname); 384 Class [] adapters = getAdapters(loader); 385 ProxyGenerator generator = new ProxyGenerator(clazz, adapters); 386 generator.generate(stream); 387 stream.close(); 388 } catch (ClassNotFoundException exception) { 389 throw new BuildException("proxygen failed - class not found: " 390 + exception.getMessage(), exception, 391 location); 392 } catch (IOException exception) { 393 throw new BuildException( 394 "proxygen failed - I/O error: " + exception.getMessage(), 395 exception, location); 396 } catch (Exception exception) { 397 throw new BuildException( 398 "proxygen failed: " + exception.getMessage(), 399 exception, location); 400 } finally { 401 if (stream != null) { 402 try { 403 stream.close(); 404 } catch (IOException exception) { 405 throw new BuildException("proxygen failed - I/O error: " 406 + exception.getMessage(), 407 exception, location); 408 } 409 } 410 } 411 return path; 412 } 413 414 419 protected Path getCompileClasspath() { 420 Path classpath = new Path(project); 421 422 classpath.setLocation(_base); 425 426 if (getClasspath() == null) { 427 classpath.addExisting(Path.systemClasspath); 428 } else { 429 classpath.addExisting(getClasspath().concatSystemClasspath("last")); 430 } 431 432 return classpath; 433 } 434 435 442 protected String [] scanDir(String [] files) { 443 ArrayList result = new ArrayList (); 444 String [] newFiles = files; 445 446 SourceFileScanner scanner = new SourceFileScanner(this); 447 FileNameMapper mapper = new GlobPatternMapper(); 448 mapper.setFrom("*.class"); 449 mapper.setTo("*__Proxy.java"); 450 newFiles = scanner.restrict(files, _base, _sourceBase, mapper); 451 452 for (int i = 0; i < newFiles.length; i++) { 453 String classname = newFiles[i].replace(File.separatorChar, '.'); 454 classname = classname.substring(0, classname.lastIndexOf(".class")); 455 result.add(classname); 456 } 457 return (String []) result.toArray(new String [0]); 458 } 459 460 467 private Class [] getAdapters(ClassLoader loader) 468 throws ClassNotFoundException { 469 Class [] result = new Class [_adapters.size()]; 470 Iterator iterator = _adapters.iterator(); 471 for (int i = 0; iterator.hasNext(); ++i) { 472 String classname = (String ) iterator.next(); 473 Class adapter = loader.loadClass(classname); 474 result[i] = adapter; 475 } 476 return result; 477 } 478 } 479 480 | Popular Tags |