1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.classpath; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Set ; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.netbeans.api.project.ant.AntArtifact; 36 import org.netbeans.api.project.libraries.Library; 37 import org.netbeans.api.project.libraries.LibraryManager; 38 import org.netbeans.spi.project.support.ant.AntProjectHelper; 39 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 40 import org.netbeans.spi.project.support.ant.PropertyUtils; 41 import org.netbeans.spi.project.support.ant.ReferenceHelper; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.w3c.dom.Text ; 47 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProjectType; 48 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 49 50 55 public class ClassPathSupport { 56 57 public final static String ELEMENT_INCLUDED_LIBRARIES = "included-library"; 59 private static String [] ejbjarElemOrder = new String [] { "name", "minimum-ant-version", "explicit-platform", "use-manifest", "included-library", "web-services", "source-roots", "test-roots" }; 60 61 private static final String ATTR_FILES = "files"; private static final String ATTR_DIRS = "dirs"; 64 private PropertyEvaluator evaluator; 65 private ReferenceHelper referenceHelper; 66 private AntProjectHelper antProjectHelper; 67 private Set wellKnownPaths; 68 private String libraryPrefix; 69 private String librarySuffix; 70 private String antArtifactPrefix; 71 72 73 public ClassPathSupport( PropertyEvaluator evaluator, 74 ReferenceHelper referenceHelper, 75 AntProjectHelper antProjectHelper, 76 String wellKnownPaths[], 77 String libraryPrefix, 78 String librarySuffix, 79 String antArtifactPrefix ) { 80 this.evaluator = evaluator; 81 this.referenceHelper = referenceHelper; 82 this.antProjectHelper = antProjectHelper; 83 this.wellKnownPaths = wellKnownPaths == null ? null : new HashSet ( Arrays.asList( wellKnownPaths ) ); 84 this.libraryPrefix = libraryPrefix; 85 this.librarySuffix = librarySuffix; 86 this.antArtifactPrefix = antArtifactPrefix; 87 } 88 89 91 public Iterator itemsIterator( String propertyValue, String includedLibrariesElement ) { 92 return itemsList( propertyValue, includedLibrariesElement ).iterator(); 94 } 95 96 public List itemsList( String propertyValue, String includedLibrariesElement ) { 97 98 List includedItems = ( includedLibrariesElement != null ) ? 100 getIncludedLibraries( antProjectHelper, includedLibrariesElement ) : 101 Collections.EMPTY_LIST; 102 103 String pe[] = PropertyUtils.tokenizePath( propertyValue == null ? "": propertyValue ); List items = new ArrayList ( pe.length ); 105 for( int i = 0; i < pe.length; i++ ) { 106 String property = EjbJarProjectProperties.getAntPropertyName( pe[i] ); 107 Item item; 108 109 if ( isWellKnownPath( pe[i] ) ) { 111 item = Item.create( pe[i], false ); 113 } 114 else if ( isLibrary( pe[i] ) ) { 115 String libraryName = pe[i].substring( libraryPrefix.length(), pe[i].lastIndexOf('.') ); Library library = LibraryManager.getDefault().getLibrary( libraryName ); 118 if ( library == null ) { 119 item = Item.createBroken( Item.TYPE_LIBRARY, pe[i], includedItems.contains( property ) ); 120 } 121 else { 122 item = Item.create( library, pe[i], includedItems.contains( property ) ); 123 } 124 } 125 else if ( isAntArtifact( pe[i] ) ) { 126 Object [] ret = referenceHelper.findArtifactAndLocation(pe[i]); 128 if ( ret[0] == null || ret[1] == null ) { 129 item = Item.createBroken( Item.TYPE_ARTIFACT, pe[i], includedItems.contains ( property ) ); 130 } 131 else { 132 AntArtifact artifact = (AntArtifact)ret[0]; 134 URI uri = (URI )ret[1]; 135 File usedFile = antProjectHelper.resolveFile(evaluator.evaluate(pe[i])); 136 File artifactFile = new File (artifact.getScriptLocation().toURI().resolve(uri).normalize()); 137 if (usedFile.equals(artifactFile)) { 138 item = Item.create( artifact, uri, pe[i], includedItems.contains ( property ) ); 139 } 140 else { 141 item = Item.createBroken( Item.TYPE_ARTIFACT, pe[i], includedItems.contains ( property ) ); 142 } 143 } 144 } else { 145 String eval = evaluator.evaluate( pe[i] ); 147 File f = null; 148 if (eval != null) { 149 f = antProjectHelper.resolveFile( eval ); 150 } 151 152 if ( f == null || !f.exists() ) { 153 item = Item.createBroken( Item.TYPE_JAR, pe[i], includedItems.contains ( property ) ); 154 } 155 else { 156 item = Item.create( f, pe[i], includedItems.contains ( property ) ); 157 } 158 } 159 160 items.add( item ); 161 162 } 163 164 return items; 165 166 } 167 168 172 public String [] encodeToStrings( Iterator classpath, String includedLibrariesElement ) { 173 174 ArrayList result = new ArrayList (); 175 ArrayList includedLibraries = new ArrayList (); 176 177 List cp = new LinkedList (); 178 179 while( classpath.hasNext() ) { 180 181 Item item = (Item)classpath.next(); 182 cp.add(item); 183 String reference = null; 184 185 switch( item.getType() ) { 186 187 case Item.TYPE_JAR: 188 reference = item.getReference(); 189 if ( item.isBroken() ) { 190 break; 191 } 192 if (reference == null) { 193 File file = item.getFile(); 195 reference = referenceHelper.createForeignFileReference(file, null); 197 item.setReference(reference); 198 } 199 break; 200 case Item.TYPE_LIBRARY: 201 reference = item.getReference(); 202 if ( item.isBroken() ) { 203 break; 204 } 205 Library library = item.getLibrary(); 206 if (reference == null) { 207 if ( library == null ) { 208 break; 209 } 210 reference = getLibraryReference( item ); 211 item.setReference(reference); 212 } 213 break; 214 case Item.TYPE_ARTIFACT: 215 reference = item.getReference(); 216 if ( item.isBroken() ) { 217 break; 218 } 219 AntArtifact artifact = (AntArtifact)item.getArtifact(); 220 if ( reference == null) { 221 if ( artifact == null ) { 222 break; 223 } 224 reference = referenceHelper.addReference( item.getArtifact(), item.getArtifactURI()); 225 item.setReference(reference); 226 } 227 break; 228 case Item.TYPE_CLASSPATH: 229 reference = item.getReference(); 230 break; 231 } 232 233 if ( reference != null ) { 234 result.add( reference ); 235 236 if ( includedLibrariesElement != null && item.isIncludedInDeployment() ) { 238 includedLibraries.add( EjbJarProjectProperties.getAntPropertyName( reference ) ); 239 } 240 } 241 242 } 243 244 if ( includedLibrariesElement != null ) 245 putIncludedLibraries( includedLibraries, cp, antProjectHelper, includedLibrariesElement ); 246 247 String [] items = new String [ result.size() ]; 248 for( int i = 0; i < result.size(); i++) { 249 if ( i < result.size() - 1 ) { 250 items[i] = result.get( i ) + ":"; 251 } 252 else { 253 items[i] = (String )result.get( i ); } 255 } 256 257 return items; 258 } 259 260 public String getLibraryReference( Item item ) { 261 if ( item.getType() != Item.TYPE_LIBRARY ) { 262 throw new IllegalArgumentException ( "Item must be of type LIBRARY" ); 263 } 264 return libraryPrefix + item.getLibrary().getName() + librarySuffix; 265 } 266 267 269 private boolean isWellKnownPath( String property ) { 270 return wellKnownPaths == null ? false : wellKnownPaths.contains( property ); 271 } 272 273 private boolean isAntArtifact( String property ) { 274 return antArtifactPrefix == null ? false : property.startsWith( antArtifactPrefix ); 275 } 276 277 private boolean isLibrary( String property ) { 278 if ( libraryPrefix != null && property.startsWith( libraryPrefix ) ) { 279 return librarySuffix == null ? true : property.endsWith( librarySuffix ); 280 } 281 else { 282 return false; 283 } 284 285 } 286 287 289 293 private static List getIncludedLibraries( AntProjectHelper antProjectHelper, String includedLibrariesElement ) { 294 assert antProjectHelper != null; 295 assert includedLibrariesElement != null; 296 297 Element data = antProjectHelper.getPrimaryConfigurationData( true ); 298 NodeList libs = data.getElementsByTagNameNS( EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, includedLibrariesElement ); 299 List libraries = new ArrayList ( libs.getLength() ); 300 for ( int i = 0; i < libs.getLength(); i++ ) { 301 Element item = (Element )libs.item( i ); 302 libraries.add( findText( item )); 303 } 304 return libraries; 305 } 306 307 311 private static void putIncludedLibraries( List libraries, List classpath, AntProjectHelper antProjectHelper, String includedLibrariesElement ) { 312 assert libraries != null; 313 assert antProjectHelper != null; 314 assert includedLibrariesElement != null; 315 316 Element data = antProjectHelper.getPrimaryConfigurationData( true ); 317 NodeList libs = data.getElementsByTagNameNS( EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, includedLibrariesElement ); 318 while ( libs.getLength() > 0 ) { 319 Node n = libs.item( 0 ); 320 n.getParentNode().removeChild( n ); 321 } 322 323 Document doc = data.getOwnerDocument(); 324 for (Iterator i = libraries.iterator(); i.hasNext();) { 325 String libraryName = (String )i.next(); 326 for(int idx = 0; idx < classpath.size(); idx++ ) { 328 ClassPathSupport.Item item = (ClassPathSupport.Item)classpath.get(idx); 329 String libraryPropName = "${" + libraryName + "}"; 330 if(libraryPropName.equals(item.getReference())) { 331 appendChildElement(data, createLibraryElement(doc, libraryName, item, includedLibrariesElement), ejbjarElemOrder); 332 } 333 } 334 } 335 336 antProjectHelper.putPrimaryConfigurationData( data, true ); 337 } 338 339 349 private static List findSubElements(Element parent) throws IllegalArgumentException { 350 NodeList l = parent.getChildNodes(); 351 List elements = new ArrayList (l.getLength()); 352 for (int i = 0; i < l.getLength(); i++) { 353 Node n = l.item(i); 354 if (n.getNodeType() == Node.ELEMENT_NODE) { 355 elements.add((Element )n); 356 } else if (n.getNodeType() == Node.TEXT_NODE) { 357 String text = ((Text )n).getNodeValue(); 358 if (text.trim().length() > 0) { 359 throw new IllegalArgumentException ("non-ws text encountered in " + parent + ": " + text); } 361 } else if (n.getNodeType() == Node.COMMENT_NODE) { 362 } else { 364 throw new IllegalArgumentException ("unexpected non-element child of " + parent + ": " + n); } 366 } 367 return elements; 368 } 369 370 377 private static void appendChildElement(Element parent, Element el, String [] order) { 378 Element insertBefore = null; 379 List l = Arrays.asList(order); 380 int index = l.indexOf(el.getLocalName()); 381 assert index != -1 : el.getLocalName()+" was not found in "+l; Iterator it = findSubElements(parent).iterator(); 383 while (it.hasNext()) { 384 Element e = (Element )it.next(); 385 int index2 = l.indexOf(e.getLocalName()); 386 assert index2 != -1 : e.getLocalName()+" was not found in "+l; if (index2 > index) { 388 insertBefore = e; 389 break; 390 } 391 } 392 parent.insertBefore(el, insertBefore); 393 } 394 395 private static Element createLibraryElement(Document doc, String pathItem, Item item, String includedLibrariesElement ) { 396 Element libraryElement = doc.createElementNS( EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, includedLibrariesElement ); 397 ArrayList files = new ArrayList (); 398 ArrayList dirs = new ArrayList (); 399 EjbJarProjectProperties.getFilesForItem(item, files, dirs); 400 if (files.size() > 0) { 401 libraryElement.setAttribute(ATTR_FILES, "" + files.size()); 402 } 403 if (dirs.size() > 0) { 404 libraryElement.setAttribute(ATTR_DIRS, "" + dirs.size()); 405 } 406 407 libraryElement.appendChild( doc.createTextNode( pathItem ) ); 408 return libraryElement; 409 } 410 411 417 private static String findText( Element parent ) { 418 NodeList l = parent.getChildNodes(); 419 for ( int i = 0; i < l.getLength(); i++ ) { 420 if ( l.item(i).getNodeType() == Node.TEXT_NODE ) { 421 Text text = (Text )l.item( i ); 422 return text.getNodeValue(); 423 } 424 } 425 return null; 426 } 427 428 430 432 public static class Item { 433 434 public static final int TYPE_JAR = 0; 436 public static final int TYPE_LIBRARY = 1; 437 public static final int TYPE_ARTIFACT = 2; 438 public static final int TYPE_CLASSPATH = 3; 439 440 private static final String BROKEN = "BrokenReference"; 443 private Object object; 444 private URI artifactURI; 445 private int type; 446 private String property; 447 private boolean includedInDeployment; 448 private String raw; 449 450 private Item( int type, Object object, String property, boolean included, String raw ) { 451 this.type = type; 452 this.object = object; 453 this.property = property; 454 this.includedInDeployment = included; 455 this.raw = raw; 456 } 457 458 private Item( int type, Object object, URI artifactURI, String property, boolean included ) { 459 this( type, object, property, included,null ); 460 this.artifactURI = artifactURI; 461 } 462 463 465 466 public static Item create( Library library, String property, boolean included ) { 467 if ( library == null ) { 468 throw new IllegalArgumentException ( "library must not be null" ); } 470 String libraryName = library.getName(); 471 return new Item( TYPE_LIBRARY, library, property, included, EjbJarProjectProperties.LIBRARY_PREFIX + libraryName + EjbJarProjectProperties.LIBRARY_SUFFIX); 472 } 473 474 public static Item create( AntArtifact artifact, URI artifactURI, String property, boolean included ) { 475 if ( artifactURI == null ) { 476 throw new IllegalArgumentException ( "artifactURI must not be null" ); } 478 if ( artifact == null ) { 479 throw new IllegalArgumentException ( "artifact must not be null" ); } 481 return new Item( TYPE_ARTIFACT, artifact, artifactURI, property, included ); 482 } 483 484 public static Item create( File file, String property, boolean included ) { 485 if ( file == null ) { 486 throw new IllegalArgumentException ( "file must not be null" ); } 488 return new Item( TYPE_JAR, file, property, included, null ); 489 } 490 491 public static Item create( String property, boolean included ) { 492 if ( property == null ) { 493 throw new IllegalArgumentException ( "property must not be null" ); } 495 return new Item ( TYPE_CLASSPATH, null, property, included, null ); 496 } 497 498 public static Item createBroken( int type, String property, boolean included ) { 499 if ( property == null ) { 500 throw new IllegalArgumentException ( "property must not be null in broken items" ); } 502 return new Item( type, BROKEN, property, included, null ); 503 } 504 505 507 public String getRaw() { 508 return raw; 509 } 510 511 public int getType() { 512 return type; 513 } 514 515 public Library getLibrary() { 516 if ( getType() != TYPE_LIBRARY ) { 517 throw new IllegalArgumentException ( "Item is not of required type - LIBRARY" ); } 519 if (isBroken()) { 520 return null; 521 } 522 return (Library)object; 523 } 524 525 public File getFile() { 526 if ( getType() != TYPE_JAR ) { 527 throw new IllegalArgumentException ( "Item is not of required type - JAR" ); } 529 if (isBroken()) { 530 return null; 531 } 532 return (File )object; 533 } 534 535 public AntArtifact getArtifact() { 536 if ( getType() != TYPE_ARTIFACT ) { 537 throw new IllegalArgumentException ( "Item is not of required type - ARTIFACT" ); } 539 if (isBroken()) { 540 return null; 541 } 542 return (AntArtifact)object; 543 } 544 545 public URI getArtifactURI() { 546 if ( getType() != TYPE_ARTIFACT ) { 547 throw new IllegalArgumentException ( "Item is not of required type - ARTIFACT" ); } 549 return artifactURI; 550 } 551 552 553 public String getReference() { 554 return property; 555 } 556 557 public void setReference(String property) { 558 this.property = property; 559 } 560 561 public boolean isIncludedInDeployment() { 562 return includedInDeployment; 570 } 571 572 public void setIncludedInDeployment(boolean includedInDeployment) { 573 this.includedInDeployment = includedInDeployment; 574 } 575 576 public boolean isBroken() { 577 return object == BROKEN; 578 } 579 580 public String toString() { 581 return "artifactURI=" + artifactURI 582 + ", type=" + type 583 + ", property=" + property 584 + ", includedInDeployment=" + includedInDeployment 585 + ", raw=" + raw 586 + ", object=" + object; 587 } 588 589 public int hashCode() { 590 591 int hash = getType(); 592 593 if ( object == BROKEN ) { 594 return BROKEN.hashCode(); 595 } 596 597 switch ( getType() ) { 598 case TYPE_ARTIFACT: 599 hash += getArtifact().getType().hashCode(); 600 hash += getArtifact().getScriptLocation().hashCode(); 601 hash += getArtifactURI().hashCode(); 602 break; 603 case TYPE_CLASSPATH: 604 hash += property.hashCode(); 605 break; 606 default: 607 hash += object.hashCode(); 608 } 609 610 return hash; 611 } 612 613 public boolean equals( Object itemObject ) { 614 615 if ( !( itemObject instanceof Item ) ) { 616 return false; 617 } 618 619 Item item = (Item)itemObject; 620 621 if ( getType() != item.getType() ) { 622 return false; 623 } 624 625 if ( isBroken() != item.isBroken() ) { 626 return false; 627 } 628 629 if ( isBroken() ) { 630 return getReference().equals( item.getReference() ); 631 } 632 633 switch ( getType() ) { 634 case TYPE_ARTIFACT: 635 if ( getArtifact().getType() != item.getArtifact().getType() ) { 636 return false; 637 } 638 639 if ( !getArtifact().getScriptLocation().equals( item.getArtifact().getScriptLocation() ) ) { 640 return false; 641 } 642 643 if ( !getArtifactURI().equals( item.getArtifactURI() ) ) { 644 return false; 645 } 646 return true; 647 case TYPE_CLASSPATH: 648 return property.equals( item.property ); 649 default: 650 return this.object.equals( item.object ); 651 } 652 653 } 654 655 } 656 657 } 658 659 | Popular Tags |