KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > classpath > ClassPathSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.j2seproject.classpath;
21
22 import java.io.File JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.api.project.ant.AntArtifact;
31 import org.netbeans.api.project.libraries.Library;
32 import org.netbeans.api.project.libraries.LibraryManager;
33 import org.netbeans.api.queries.CollocationQuery;
34 import org.netbeans.spi.project.support.ant.AntProjectHelper;
35 import org.netbeans.spi.project.support.ant.EditableProperties;
36 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
37 import org.netbeans.spi.project.support.ant.PropertyUtils;
38 import org.netbeans.spi.project.support.ant.ReferenceHelper;
39 import org.openide.filesystems.FileUtil;
40
41 /**
42  *
43  * @author Petr Hrebejk
44  */

45 public class ClassPathSupport {
46                 
47     private PropertyEvaluator evaluator;
48     private ReferenceHelper referenceHelper;
49     private AntProjectHelper antProjectHelper;
50     private Set JavaDoc<String JavaDoc> wellKnownPaths;
51     private String JavaDoc libraryPrefix;
52     private String JavaDoc librarySuffix;
53     private String JavaDoc antArtifactPrefix;
54         
55     /** Creates a new instance of ClassPathSupport */
56     public ClassPathSupport( PropertyEvaluator evaluator,
57                               ReferenceHelper referenceHelper,
58                               AntProjectHelper antProjectHelper,
59                               String JavaDoc[] wellKnownPaths,
60                               String JavaDoc libraryPrefix,
61                               String JavaDoc librarySuffix,
62                               String JavaDoc antArtifactPrefix ) {
63         this.evaluator = evaluator;
64         this.referenceHelper = referenceHelper;
65         this.antProjectHelper = antProjectHelper;
66         this.wellKnownPaths = wellKnownPaths == null ? null : new HashSet JavaDoc<String JavaDoc>(Arrays.asList(wellKnownPaths));
67         this.libraryPrefix = libraryPrefix;
68         this.librarySuffix = librarySuffix;
69         this.antArtifactPrefix = antArtifactPrefix;
70     }
71     
72     /** Creates list of <CODE>Items</CODE> from given property.
73      */

74     public Iterator JavaDoc<Item> itemsIterator( String JavaDoc propertyValue ) {
75         // XXX More performance frendly impl. would retrun a lazzy iterator.
76
return itemsList( propertyValue ).iterator();
77     }
78     
79     public List JavaDoc<Item> itemsList( String JavaDoc propertyValue ) {
80         
81         String JavaDoc pe[] = PropertyUtils.tokenizePath( propertyValue == null ? "": propertyValue ); // NOI18N
82
List JavaDoc<Item> items = new ArrayList JavaDoc<Item>( pe.length );
83         for( int i = 0; i < pe.length; i++ ) {
84             Item item;
85
86             // First try to find out whether the item is well known classpath
87
if ( isWellKnownPath( pe[i] ) ) {
88                 // Some well know classpath
89
item = Item.create( pe[i] );
90             }
91             else if ( isLibrary( pe[i] ) ) {
92                 //Library from library manager
93
String JavaDoc libraryName = pe[i].substring( libraryPrefix.length(), pe[i].lastIndexOf('.') ); //NOI18N
94
Library library = LibraryManager.getDefault().getLibrary( libraryName );
95                 if ( library == null ) {
96                     item = Item.createBroken( Item.TYPE_LIBRARY, pe[i] );
97                 }
98                 else {
99                     item = Item.create( library, pe[i] );
100                 }
101             }
102             else if ( isAntArtifact( pe[i] ) ) {
103                 // Ant artifact from another project
104
Object JavaDoc[] ret = referenceHelper.findArtifactAndLocation(pe[i]);
105                 if ( ret[0] == null || ret[1] == null ) {
106                     item = Item.createBroken( Item.TYPE_ARTIFACT, pe[i] );
107                 }
108                 else {
109                     //fix of issue #55316
110
AntArtifact artifact = (AntArtifact)ret[0];
111                     URI JavaDoc uri = (URI JavaDoc)ret[1];
112                     File JavaDoc usedFile = antProjectHelper.resolveFile(evaluator.evaluate(pe[i]));
113                     File JavaDoc artifactFile = new File JavaDoc (artifact.getScriptLocation().toURI().resolve(uri).normalize());
114                     if (usedFile.equals(artifactFile)) {
115                         item = Item.create( artifact, uri, pe[i] );
116                     }
117                     else {
118                         item = Item.createBroken( Item.TYPE_ARTIFACT, pe[i] );
119                     }
120                 }
121             } else {
122                 // Standalone jar or property
123
String JavaDoc eval = evaluator.evaluate( pe[i] );
124                 File JavaDoc f = null;
125                 if (eval != null) {
126                     f = antProjectHelper.resolveFile( eval );
127                 }
128                 
129                 if ( f == null || !f.exists() ) {
130                     item = Item.createBroken( Item.TYPE_JAR, pe[i] );
131                 }
132                 else {
133                     item = Item.create( f, pe[i] );
134                 }
135             }
136             
137             items.add( item );
138            
139         }
140
141         return items;
142         
143     }
144     
145     /** Converts list of classpath items into array of Strings.
146      * !! This method creates references in the project !!
147      */

148     public String JavaDoc[] encodeToStrings(Iterator JavaDoc<Item> classpath) {
149         
150         ArrayList JavaDoc result = new ArrayList JavaDoc();
151         
152         while( classpath.hasNext() ) {
153
154             Item item = classpath.next();
155             String JavaDoc reference = null;
156             
157             switch( item.getType() ) {
158
159                 case Item.TYPE_JAR:
160                     reference = item.getReference();
161                     if ( item.isBroken() ) {
162                         break;
163                     }
164                     if (reference == null) {
165                         // New file
166
File JavaDoc file = item.getFile();
167                         // pass null as expected artifact type to always get file reference
168
reference = referenceHelper.createForeignFileReference(file, null);
169                     }
170                     break;
171                 case Item.TYPE_LIBRARY:
172                     reference = item.getReference();
173                     if ( item.isBroken() ) {
174                         break;
175                     }
176                     Library library = item.getLibrary();
177                     if (reference == null) {
178                         if ( library == null ) {
179                             break;
180                         }
181                         reference = getLibraryReference( item );
182                     }
183                     break;
184                 case Item.TYPE_ARTIFACT:
185                     reference = item.getReference();
186                     if ( item.isBroken() ) {
187                         break;
188                     }
189                     AntArtifact artifact = (AntArtifact)item.getArtifact();
190                     if ( reference == null) {
191                         if ( artifact == null ) {
192                             break;
193                         }
194                         reference = referenceHelper.addReference( item.getArtifact(), item.getArtifactURI());
195                     }
196                     break;
197                 case Item.TYPE_CLASSPATH:
198                     reference = item.getReference();
199                     break;
200             }
201             
202             if ( reference != null ) {
203                 result.add( reference );
204             }
205
206         }
207
208         String JavaDoc[] items = new String JavaDoc[ result.size() ];
209         for( int i = 0; i < result.size(); i++) {
210             if ( i < result.size() - 1 ) {
211                 items[i] = result.get( i ) + ":";
212             }
213             else {
214                 items[i] = (String JavaDoc)result.get( i ); //NOI18N
215
}
216         }
217         
218         return items;
219     }
220     
221     public String JavaDoc getLibraryReference( Item item ) {
222         if ( item.getType() != Item.TYPE_LIBRARY ) {
223             throw new IllegalArgumentException JavaDoc( "Item must be of type LIBRARY" );
224         }
225         return libraryPrefix + item.getLibrary().getName() + librarySuffix;
226     }
227     
228     // Private methods ---------------------------------------------------------
229

230     private boolean isWellKnownPath( String JavaDoc property ) {
231         return wellKnownPaths == null ? false : wellKnownPaths.contains( property );
232     }
233     
234     private boolean isAntArtifact( String JavaDoc property ) {
235         return antArtifactPrefix == null ? false : property.startsWith( antArtifactPrefix );
236     }
237     
238     private boolean isLibrary( String JavaDoc property ) {
239         if ( libraryPrefix != null && property.startsWith( libraryPrefix ) ) {
240             return librarySuffix == null ? true : property.endsWith( librarySuffix );
241         }
242         else {
243             return false;
244         }
245         
246     }
247         
248     // Innerclasses ------------------------------------------------------------
249

250     /** Item of the classpath.
251      */

252     public static class Item {
253         
254         // Types of the classpath elements
255
public static final int TYPE_JAR = 0;
256         public static final int TYPE_LIBRARY = 1;
257         public static final int TYPE_ARTIFACT = 2;
258         public static final int TYPE_CLASSPATH = 3;
259
260         // Reference to a broken object
261
private static final String JavaDoc BROKEN = "BrokenReference"; // NOI18N
262

263         private Object JavaDoc object;
264         private URI JavaDoc artifactURI;
265         private int type;
266         private String JavaDoc property;
267         
268         private Item( int type, Object JavaDoc object, String JavaDoc property ) {
269             this.type = type;
270             this.object = object;
271             this.property = property;
272         }
273         
274         private Item( int type, Object JavaDoc object, URI JavaDoc artifactURI, String JavaDoc property ) {
275             this( type, object, property );
276             this.artifactURI = artifactURI;
277         }
278               
279         // Factory methods -----------------------------------------------------
280

281         
282         public static Item create( Library library, String JavaDoc property ) {
283             if ( library == null ) {
284                 throw new IllegalArgumentException JavaDoc( "library must not be null" ); // NOI18N
285
}
286             return new Item( TYPE_LIBRARY, library, property );
287         }
288         
289         public static Item create( AntArtifact artifact, URI JavaDoc artifactURI, String JavaDoc property ) {
290             if ( artifactURI == null ) {
291                 throw new IllegalArgumentException JavaDoc( "artifactURI must not be null" ); // NOI18N
292
}
293             if ( artifact == null ) {
294                 throw new IllegalArgumentException JavaDoc( "artifact must not be null" ); // NOI18N
295
}
296             return new Item( TYPE_ARTIFACT, artifact, artifactURI, property );
297         }
298         
299         public static Item create( File JavaDoc file, String JavaDoc property ) {
300             if ( file == null ) {
301                 throw new IllegalArgumentException JavaDoc( "file must not be null" ); // NOI18N
302
}
303             return new Item( TYPE_JAR, file, property );
304         }
305         
306         public static Item create( String JavaDoc property ) {
307             if ( property == null ) {
308                 throw new IllegalArgumentException JavaDoc( "property must not be null" ); // NOI18N
309
}
310             return new Item ( TYPE_CLASSPATH, null, property );
311         }
312         
313         public static Item createBroken( int type, String JavaDoc property ) {
314             if ( property == null ) {
315                 throw new IllegalArgumentException JavaDoc( "property must not be null in broken items" ); // NOI18N
316
}
317             return new Item( type, BROKEN, property );
318         }
319         
320         
321         // Instance methods ----------------------------------------------------
322

323         public int getType() {
324             return type;
325         }
326         
327         public Library getLibrary() {
328             if ( getType() != TYPE_LIBRARY ) {
329                 throw new IllegalArgumentException JavaDoc( "Item is not of required type - LIBRARY" ); // NOI18N
330
}
331             assert object instanceof Library :
332                 "Invalid object type: "+object.getClass().getName()+" instance: "+object.toString()+" expected type: Library"; //NOI18N
333
return (Library)object;
334         }
335         
336         public File JavaDoc getFile() {
337             if ( getType() != TYPE_JAR ) {
338                 throw new IllegalArgumentException JavaDoc( "Item is not of required type - JAR" ); // NOI18N
339
}
340             return (File JavaDoc)object;
341         }
342         
343         public AntArtifact getArtifact() {
344             if ( getType() != TYPE_ARTIFACT ) {
345                 throw new IllegalArgumentException JavaDoc( "Item is not of required type - ARTIFACT" ); // NOI18N
346
}
347             return (AntArtifact)object;
348         }
349         
350         public URI JavaDoc getArtifactURI() {
351             if ( getType() != TYPE_ARTIFACT ) {
352                 throw new IllegalArgumentException JavaDoc( "Item is not of required type - ARTIFACT" ); // NOI18N
353
}
354             return artifactURI;
355         }
356         
357         
358         public String JavaDoc getReference() {
359             return property;
360         }
361         
362         public boolean isBroken() {
363             return object == BROKEN;
364         }
365                         
366         public int hashCode() {
367         
368             int hash = getType();
369
370             if ( object == BROKEN ) {
371                 return BROKEN.hashCode();
372             }
373             
374             switch ( getType() ) {
375                 case TYPE_ARTIFACT:
376                     hash += getArtifact().getType().hashCode();
377                     hash += getArtifact().getScriptLocation().hashCode();
378                     hash += getArtifactURI().hashCode();
379                     break;
380                 case TYPE_CLASSPATH:
381                     hash += property.hashCode();
382                     break;
383                 default:
384                     hash += object.hashCode();
385             }
386
387             return hash;
388         }
389     
390         public boolean equals( Object JavaDoc itemObject ) {
391
392             if ( !( itemObject instanceof Item ) ) {
393                 return false;
394             }
395             
396             Item item = (Item)itemObject;
397
398             if ( getType() != item.getType() ) {
399                 return false;
400             }
401             
402             if ( isBroken() != item.isBroken() ) {
403                 return false;
404             }
405             
406             if ( isBroken() ) {
407                 return getReference().equals( item.getReference() );
408             }
409
410             switch ( getType() ) {
411                 case TYPE_ARTIFACT:
412                     if ( getArtifact().getType() != item.getArtifact().getType() ) {
413                         return false;
414                     }
415
416                     if ( !getArtifact().getScriptLocation().equals( item.getArtifact().getScriptLocation() ) ) {
417                         return false;
418                     }
419
420                     if ( !getArtifactURI().equals( item.getArtifactURI() ) ) {
421                         return false;
422                     }
423                     return true;
424                 case TYPE_CLASSPATH:
425                     return property.equals( item.property );
426                 default:
427                     return object.equals( item.object );
428             }
429
430         }
431                 
432     }
433     
434     /**
435      * Tokenize library classpath and try to relativize all the jars.
436      * @param ep the editable properties in which the result should be stored
437      * @param aph AntProjectHelper used to resolve files
438      * @param libCpProperty the library classpath property
439      */

440     public static boolean relativizeLibraryClassPath (final EditableProperties ep, final AntProjectHelper aph, final String JavaDoc libCpProperty) {
441         String JavaDoc value = PropertyUtils.getGlobalProperties().getProperty(libCpProperty);
442         // bugfix #42852, check if the classpath property is set, otherwise return null
443
if (value == null) {
444             return false;
445         }
446         String JavaDoc[] paths = PropertyUtils.tokenizePath(value);
447         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
448         File JavaDoc projectDir = FileUtil.toFile(aph.getProjectDirectory());
449         for (int i=0; i<paths.length; i++) {
450             File JavaDoc f = aph.resolveFile(paths[i]);
451             if (CollocationQuery.areCollocated(f, projectDir)) {
452                 sb.append(PropertyUtils.relativizeFile(projectDir, f));
453             } else {
454                 return false;
455             }
456             if (i+1<paths.length) {
457                 sb.append(File.pathSeparatorChar);
458             }
459         }
460         if (sb.length() == 0) {
461             return false;
462         }
463         ep.setProperty(libCpProperty, sb.toString());
464         ep.setComment(libCpProperty, new String JavaDoc[]{
465             // XXX this should be I18N! Not least because the English is wrong...
466
"# Property "+libCpProperty+" is set here just to make sharing of project simpler.",
467             "# The library definition has always preference over this property."}, false);
468         return true;
469     }
470     
471     /**
472      * Converts the ant reference to the name of the referenced property
473      * @param ant reference
474      * @param the name of the referenced property
475      */

476     public static String JavaDoc getAntPropertyName( String JavaDoc property ) {
477         if ( property != null &&
478              property.startsWith( "${" ) && // NOI18N
479
property.endsWith( "}" ) ) { // NOI18N
480
return property.substring( 2, property.length() - 1 );
481         }
482         else {
483             return property;
484         }
485     }
486             
487 }
488
Popular Tags