| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.logic; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.io.FileOutputStream; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.PrintStream; |
| 22 |
import java.lang.reflect.Method; |
| 23 |
import java.util.Enumeration; |
| 24 |
import java.util.Hashtable; |
| 25 |
import java.util.StringTokenizer; |
| 26 |
import java.util.Vector; |
| 27 |
|
| 28 |
import org.apache.tools.ant.*; |
| 29 |
import org.apache.tools.ant.taskdefs.Property; |
| 30 |
import org.apache.tools.ant.util.FileUtils; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class AntCallBack extends Task { |
| 52 |
|
| 53 |
|
| 54 |
private File dir = null; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private String antFile = null; |
| 60 |
|
| 61 |
|
| 62 |
private String target = null; |
| 63 |
|
| 64 |
|
| 65 |
private String output = null; |
| 66 |
|
| 67 |
|
| 68 |
private boolean inheritAll = true; |
| 69 |
|
| 70 |
|
| 71 |
private boolean inheritRefs = false; |
| 72 |
|
| 73 |
|
| 74 |
private Vector properties = new Vector(); |
| 75 |
|
| 76 |
|
| 77 |
private Vector references = new Vector(); |
| 78 |
|
| 79 |
|
| 80 |
private Project newProject; |
| 81 |
|
| 82 |
|
| 83 |
private PrintStream out = null; |
| 84 |
|
| 85 |
|
| 86 |
private String returnName = null; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public void setInheritAll( boolean value ) { |
| 95 |
inheritAll = value; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void setInheritRefs( boolean value ) { |
| 105 |
inheritRefs = value; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public void init() { |
| 111 |
newProject = new Project(); |
| 112 |
newProject.setJavaVersionProperty(); |
| 113 |
newProject.addTaskDefinition( "property", |
| 114 |
(Class)project.getTaskDefinitions() |
| 115 |
.get( "property" ) ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private void reinit() { |
| 130 |
init(); |
| 131 |
final int count = properties.size(); |
| 132 |
for ( int i = 0; i < count; i++ ) { |
| 133 |
Property p = (Property)properties.elementAt( i ); |
| 134 |
Property newP = (Property)newProject.createTask( "property" ); |
| 135 |
newP.setName( p.getName() ); |
| 136 |
if ( p.getValue() != null ) { |
| 137 |
newP.setValue( p.getValue() ); |
| 138 |
} |
| 139 |
if ( p.getFile() != null ) { |
| 140 |
newP.setFile( p.getFile() ); |
| 141 |
} |
| 142 |
if ( p.getResource() != null ) { |
| 143 |
newP.setResource( p.getResource() ); |
| 144 |
} |
| 145 |
if ( p.getPrefix() != null ) { |
| 146 |
newP.setPrefix( p.getPrefix() ); |
| 147 |
} |
| 148 |
if ( p.getRefid() != null ) { |
| 149 |
newP.setRefid( p.getRefid() ); |
| 150 |
} |
| 151 |
if ( p.getEnvironment() != null ) { |
| 152 |
newP.setEnvironment( p.getEnvironment() ); |
| 153 |
} |
| 154 |
if ( p.getClasspath() != null ) { |
| 155 |
newP.setClasspath( p.getClasspath() ); |
| 156 |
} |
| 157 |
properties.setElementAt( newP, i ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private void initializeProject() { |
| 170 |
newProject.setInputHandler( getProject().getInputHandler() ); |
| 171 |
|
| 172 |
Vector listeners = project.getBuildListeners(); |
| 173 |
final int count = listeners.size(); |
| 174 |
for ( int i = 0; i < count; i++ ) { |
| 175 |
newProject.addBuildListener( (BuildListener)listeners.elementAt( i ) ); |
| 176 |
} |
| 177 |
|
| 178 |
if ( output != null ) { |
| 179 |
File outfile = null; |
| 180 |
if ( dir != null ) { |
| 181 |
outfile = FileUtils.newFileUtils().resolveFile( dir, output ); |
| 182 |
} |
| 183 |
else { |
| 184 |
outfile = getProject().resolveFile( output ); |
| 185 |
} |
| 186 |
try { |
| 187 |
out = new PrintStream( new FileOutputStream( outfile ) ); |
| 188 |
DefaultLogger logger = new DefaultLogger(); |
| 189 |
logger.setMessageOutputLevel( Project.MSG_INFO ); |
| 190 |
logger.setOutputPrintStream( out ); |
| 191 |
logger.setErrorPrintStream( out ); |
| 192 |
newProject.addBuildListener( logger ); |
| 193 |
} |
| 194 |
catch ( IOException ex ) { |
| 195 |
log( "Ant: Can't set output to " + output ); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
Hashtable taskdefs = project.getTaskDefinitions(); |
| 200 |
Enumeration et = taskdefs.keys(); |
| 201 |
while ( et.hasMoreElements() ) { |
| 202 |
String taskName = (String)et.nextElement(); |
| 203 |
if ( taskName.equals( "property" ) ) { |
| 204 |
|
| 205 |
continue; |
| 206 |
} |
| 207 |
Class taskClass = (Class)taskdefs.get( taskName ); |
| 208 |
newProject.addTaskDefinition( taskName, taskClass ); |
| 209 |
} |
| 210 |
|
| 211 |
Hashtable typedefs = project.getDataTypeDefinitions(); |
| 212 |
Enumeration e = typedefs.keys(); |
| 213 |
while ( e.hasMoreElements() ) { |
| 214 |
String typeName = (String)e.nextElement(); |
| 215 |
Class typeClass = (Class)typedefs.get( typeName ); |
| 216 |
newProject.addDataTypeDefinition( typeName, typeClass ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
getProject().copyUserProperties( newProject ); |
| 221 |
|
| 222 |
if ( !inheritAll ) { |
| 223 |
|
| 224 |
|
| 225 |
newProject.setSystemProperties(); |
| 226 |
|
| 227 |
} |
| 228 |
else { |
| 229 |
|
| 230 |
|
| 231 |
Hashtable props = getProject().getProperties(); |
| 232 |
e = props.keys(); |
| 233 |
while ( e.hasMoreElements() ) { |
| 234 |
String arg = e.nextElement().toString(); |
| 235 |
if ( "basedir".equals( arg ) || "ant.file".equals( arg ) ) { |
| 236 |
|
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
String value = props.get( arg ).toString(); |
| 241 |
|
| 242 |
if ( newProject.getProperty( arg ) == null ) { |
| 243 |
|
| 244 |
newProject.setNewProperty( arg, value ); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
protected void handleOutput( String line ) { |
| 258 |
if ( newProject != null ) { |
| 259 |
newProject.demuxOutput( line, false ); |
| 260 |
} |
| 261 |
else { |
| 262 |
super.handleOutput( line ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
protected void handleErrorOutput( String line ) { |
| 274 |
if ( newProject != null ) { |
| 275 |
newProject.demuxOutput( line, true ); |
| 276 |
} |
| 277 |
else { |
| 278 |
super.handleErrorOutput( line ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public void execute() throws BuildException { |
| 289 |
setAntfile( getProject().getProperty( "ant.file" ) ); |
| 290 |
|
| 291 |
File savedDir = dir; |
| 292 |
String savedAntFile = antFile; |
| 293 |
String savedTarget = target; |
| 294 |
try { |
| 295 |
if ( newProject == null ) { |
| 296 |
reinit(); |
| 297 |
} |
| 298 |
|
| 299 |
if ( ( dir == null ) && ( inheritAll ) ) { |
| 300 |
dir = project.getBaseDir(); |
| 301 |
} |
| 302 |
|
| 303 |
initializeProject(); |
| 304 |
|
| 305 |
if ( dir != null ) { |
| 306 |
newProject.setBaseDir( dir ); |
| 307 |
if ( savedDir != null ) { |
| 308 |
newProject.setInheritedProperty( "basedir", |
| 309 |
dir.getAbsolutePath() ); |
| 310 |
} |
| 311 |
} |
| 312 |
else { |
| 313 |
dir = project.getBaseDir(); |
| 314 |
} |
| 315 |
|
| 316 |
overrideProperties(); |
| 317 |
|
| 318 |
if ( antFile == null ) { |
| 319 |
throw new BuildException( "Attribute target is required.", |
| 320 |
location ); |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
File file = FileUtils.newFileUtils().resolveFile( dir, antFile ); |
| 325 |
antFile = file.getAbsolutePath(); |
| 326 |
|
| 327 |
log( "calling target " + ( target != null ? target : "[default]" ) |
| 328 |
+ " in build file " + antFile.toString(), |
| 329 |
Project.MSG_VERBOSE ); |
| 330 |
newProject.setUserProperty( "ant.file", antFile ); |
| 331 |
ProjectHelper.configureProject( newProject, new File( antFile ) ); |
| 332 |
|
| 333 |
if ( target == null ) { |
| 334 |
target = newProject.getDefaultTarget(); |
| 335 |
} |
| 336 |
|
| 337 |
addReferences(); |
| 338 |
|
| 339 |
|
| 340 |
if ( newProject.getBaseDir().equals( project.getBaseDir() ) && |
| 341 |
newProject.getProperty( "ant.file" ).equals( project.getProperty( "ant.file" ) ) && |
| 342 |
getOwningTarget() != null && |
| 343 |
target.equals( this.getOwningTarget().getName() ) ) { |
| 344 |
|
| 345 |
throw new BuildException( "antcallback task calling its own parent " |
| 346 |
+ "target" ); |
| 347 |
} |
| 348 |
|
| 349 |
newProject.executeTarget( target ); |
| 350 |
|
| 351 |
|
| 352 |
if ( returnName != null ) { |
| 353 |
StringTokenizer st = new StringTokenizer( returnName, "," ); |
| 354 |
while ( st.hasMoreTokens() ) { |
| 355 |
String name = st.nextToken().trim(); |
| 356 |
String value = newProject.getUserProperty( name ); |
| 357 |
if ( value != null ) { |
| 358 |
project.setUserProperty( name, value ); |
| 359 |
} |
| 360 |
else { |
| 361 |
value = newProject.getProperty( name ); |
| 362 |
if ( value != null ) { |
| 363 |
project.setProperty( name, value ); |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
finally { |
| 370 |
|
| 371 |
newProject = null; |
| 372 |
if ( output != null && out != null ) { |
| 373 |
try { |
| 374 |
out.close(); |
| 375 |
} |
| 376 |
catch ( final Exception e ) { |
| 377 |
|
| 378 |
} |
| 379 |
} |
| 380 |
dir = savedDir; |
| 381 |
antFile = savedAntFile; |
| 382 |
target = savedTarget; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
private void overrideProperties() throws BuildException { |
| 394 |
Enumeration e = properties.elements(); |
| 395 |
while ( e.hasMoreElements() ) { |
| 396 |
Property p = (Property)e.nextElement(); |
| 397 |
p.setProject( newProject ); |
| 398 |
p.execute(); |
| 399 |
} |
| 400 |
getProject().copyInheritedProperties( newProject ); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
private void addReferences() throws BuildException { |
| 412 |
Hashtable thisReferences = (Hashtable)project.getReferences().clone(); |
| 413 |
Hashtable newReferences = newProject.getReferences(); |
| 414 |
Enumeration e; |
| 415 |
if ( references.size() > 0 ) { |
| 416 |
for ( e = references.elements(); e.hasMoreElements(); ) { |
| 417 |
Reference ref = (Reference)e.nextElement(); |
| 418 |
String refid = ref.getRefId(); |
| 419 |
if ( refid == null ) { |
| 420 |
throw new BuildException( "the refid attribute is required" |
| 421 |
+ " for reference elements" ); |
| 422 |
} |
| 423 |
if ( !thisReferences.containsKey( refid ) ) { |
| 424 |
log( "Parent project doesn't contain any reference '" |
| 425 |
+ refid + "'", |
| 426 |
Project.MSG_WARN ); |
| 427 |
continue; |
| 428 |
} |
| 429 |
|
| 430 |
thisReferences.remove( refid ); |
| 431 |
String toRefid = ref.getToRefid(); |
| 432 |
if ( toRefid == null ) { |
| 433 |
toRefid = refid; |
| 434 |
} |
| 435 |
copyReference( refid, toRefid ); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
if ( inheritRefs ) { |
| 442 |
for ( e = thisReferences.keys(); e.hasMoreElements(); ) { |
| 443 |
String key = (String)e.nextElement(); |
| 444 |
if ( newReferences.containsKey( key ) ) { |
| 445 |
continue; |
| 446 |
} |
| 447 |
copyReference( key, key ); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
private void copyReference( String oldKey, String newKey ) { |
| 464 |
Object orig = project.getReference( oldKey ); |
| 465 |
Class c = orig.getClass(); |
| 466 |
Object copy = orig; |
| 467 |
try { |
| 468 |
Method cloneM = c.getMethod( "clone", new Class[0] ); |
| 469 |
if ( cloneM != null ) { |
| 470 |
copy = cloneM.invoke( orig, new Object[0] ); |
| 471 |
} |
| 472 |
} |
| 473 |
catch ( Exception e ) { |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
if ( copy instanceof ProjectComponent ) { |
| 478 |
( (ProjectComponent)copy ).setProject( newProject ); |
| 479 |
} |
| 480 |
else { |
| 481 |
try { |
| 482 |
Method setProjectM = |
| 483 |
c.getMethod( "setProject", new Class[]{Project.class} ); |
| 484 |
if ( setProjectM != null ) { |
| 485 |
setProjectM.invoke( copy, new Object[]{newProject} ); |
| 486 |
} |
| 487 |
} |
| 488 |
catch ( NoSuchMethodException e ) { |
| 489 |
|
| 490 |
|
| 491 |
} |
| 492 |
catch ( Exception e2 ) { |
| 493 |
String msg = "Error setting new project instance for " |
| 494 |
+ "reference with id " + oldKey; |
| 495 |
throw new BuildException( msg, e2, location ); |
| 496 |
} |
| 497 |
} |
| 498 |
newProject.addReference( newKey, copy ); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public void setDir( File d ) { |
| 511 |
this.dir = d; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public void setAntfile( String s ) { |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
this.antFile = s; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
public void setTarget( String s ) { |
| 536 |
this.target = s; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
public void setOutput( String s ) { |
| 548 |
this.output = s; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
public Property createProperty() { |
| 559 |
if ( newProject == null ) { |
| 560 |
reinit(); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
Property p = new Property(); |
| 566 |
p.setProject( newProject ); |
| 567 |
p.setTaskName( "property" ); |
| 568 |
properties.addElement( p ); |
| 569 |
return p; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
public Property createParam() { |
| 577 |
return createProperty(); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public void setReturn( String r ) { |
| 590 |
returnName = r; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
public void addReference( Reference r ) { |
| 601 |
references.addElement( r ); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
public static class Reference |
| 612 |
extends org.apache.tools.ant.types.Reference { |
| 613 |
|
| 614 |
|
| 615 |
public Reference() { |
| 616 |
super(); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
private String targetid = null; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public void setToRefid( String targetid ) { |
| 630 |
this.targetid = targetid; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
public String getToRefid() { |
| 640 |
return targetid; |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|