KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > classpath > AppClientProjectClassPathExtender


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 package org.netbeans.modules.j2ee.clientproject.classpath;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
30 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.Mutex;
35 import org.netbeans.api.project.libraries.Library;
36 import org.netbeans.api.project.ant.AntArtifact;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.api.project.Project;
39 import org.netbeans.api.project.libraries.LibraryManager;
40 import org.netbeans.spi.project.support.ant.AntProjectHelper;
41 import org.netbeans.spi.project.support.ant.ReferenceHelper;
42 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
43 import org.netbeans.spi.project.support.ant.EditableProperties;
44 import org.netbeans.modules.j2ee.clientproject.UpdateHelper;
45 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AntArtifactChooser;
46 import org.openide.util.RequestProcessor;
47
48 public class AppClientProjectClassPathExtender implements ProjectClassPathExtender, PropertyChangeListener JavaDoc {
49     
50     private static final String JavaDoc DEFAULT_CLASS_PATH = AppClientProjectProperties.JAVAC_CLASSPATH;
51     private static final String JavaDoc DEFAULT_INCLUDED_LIBS_ELEMENT = ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES;
52     
53     private Project project;
54     private UpdateHelper helper;
55     private ReferenceHelper refHelper;
56     private PropertyEvaluator eval;
57     
58     
59     private ClassPathSupport cs;
60     
61     private volatile boolean projectDeleted;
62
63     public AppClientProjectClassPathExtender (Project project, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper) {
64         this.project = project;
65         this.helper = helper;
66         this.eval = eval;
67         this.refHelper = refHelper;
68         
69         this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(),
70                                         AppClientProjectProperties.WELL_KNOWN_PATHS,
71                                         AppClientProjectProperties.LIBRARY_PREFIX,
72                                         AppClientProjectProperties.LIBRARY_SUFFIX,
73                                         AppClientProjectProperties.ANT_ARTIFACT_PREFIX );
74         eval.addPropertyChangeListener(this); //listen for changes of libraries list
75
registerLibraryListeners();
76     }
77
78     public boolean addLibrary(final Library library) throws IOException JavaDoc {
79         return addLibraries(DEFAULT_CLASS_PATH, new Library[] { library }, DEFAULT_INCLUDED_LIBS_ELEMENT);
80     }
81
82     /*
83     public boolean addLibrary(final String classPathId, final Library library) throws IOException {
84         assert library != null : "Parameter cannot be null"; //NOI18N
85         try {
86             return ((Boolean)ProjectManager.mutex().writeAccess(
87                     new Mutex.ExceptionAction () {
88                         public Object run() throws IOException {
89                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
90                             String raw = props.getProperty(classPathId);
91                             List resources = cs.itemsList( raw );
92                             ClassPathSupport.Item item = ClassPathSupport.Item.create( library, null );
93                             if (!resources.contains(item)) {
94                                 resources.add (item);
95                                 String itemRefs[] = cs.encodeToStrings( resources.iterator() );
96                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
97                                 props.setProperty(classPathId, itemRefs);
98                                 String prop = cs.getLibraryReference( item );
99                                 prop = prop.substring(2, prop.length()-1); // XXX make a PropertyUtils method for this!
100                                 ClassPathSupport.relativizeLibraryClassPath(props, helper.getAntProjectHelper(), prop);
101                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
102                                 ProjectManager.getDefault().saveProject(project);
103                                 return Boolean.TRUE;
104                             }
105                             return Boolean.FALSE;
106                         }
107                     }
108             )).booleanValue();
109         } catch (MutexException e) {
110             throw (IOException) e.getException();
111         }
112     }
113      */

114
115     public boolean addLibraries(final String JavaDoc classPathId, final Library[] libraries, final String JavaDoc includedLibrariesElement) throws IOException JavaDoc {
116         assert libraries != null : "Parameter cannot be null"; //NOI18N
117
try {
118             return ((Boolean JavaDoc)ProjectManager.mutex().writeAccess(
119                     new Mutex.ExceptionAction () {
120                         public Object JavaDoc run() throws Exception JavaDoc {
121                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
122                             String JavaDoc raw = props.getProperty(classPathId);
123                             List JavaDoc<ClassPathSupport.Item> resources = cs.itemsList( raw, includedLibrariesElement );
124                             boolean added = false;
125                             for (int i = 0; i < libraries.length; i++) {
126                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( libraries[i], null, includedLibrariesElement != null );
127                                 if (!resources.contains(item)) {
128                                     resources.add (item);
129                                     added = true;
130                                 }
131                             }
132                             if (added) {
133                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator(), includedLibrariesElement );
134                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
135
props.setProperty(classPathId, itemRefs);
136                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
137                                 
138                                 //update lib references in private properties
139
EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
140                                 List JavaDoc<ClassPathSupport.Item> l = new ArrayList JavaDoc<ClassPathSupport.Item>();
141                                 l.addAll(resources);
142                                 AppClientProjectProperties.storeLibrariesLocations(l.iterator(), privateProps);
143                                 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
144
145                                 ProjectManager.getDefault().saveProject(project);
146                                 return Boolean.TRUE;
147                             }
148                             return Boolean.FALSE;
149                         }
150                     }
151             )).booleanValue();
152         } catch (Exception JavaDoc e) {
153             if (e instanceof IOException JavaDoc) {
154                 throw (IOException JavaDoc) e;
155             }
156             else {
157                 Exception JavaDoc t = new IOException JavaDoc ();
158                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
159             }
160         }
161     }
162     
163     public boolean addArchiveFile(final FileObject archiveFile) throws IOException JavaDoc {
164         return addArchiveFiles(DEFAULT_CLASS_PATH, new FileObject[] { archiveFile }, DEFAULT_INCLUDED_LIBS_ELEMENT);
165     }
166
167     public boolean addArchiveFiles(final String JavaDoc classPathId, final FileObject[] archiveFiles, final String JavaDoc includedLibrariesElement) throws IOException JavaDoc {
168         assert archiveFiles != null : "Parameter cannot be null"; //NOI18N
169
try {
170             return ((Boolean JavaDoc)ProjectManager.mutex().writeAccess(
171                     new Mutex.ExceptionAction () {
172                         public Object JavaDoc run() throws Exception JavaDoc {
173                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
174                             String JavaDoc raw = props.getProperty(classPathId);
175                             List JavaDoc<ClassPathSupport.Item> resources = cs.itemsList( raw, includedLibrariesElement );
176                             boolean added = false;
177                             for (int i = 0; i < archiveFiles.length; i++) {
178                                 File JavaDoc f = FileUtil.toFile (archiveFiles[i]);
179                                 if (f == null ) {
180                                     throw new IllegalArgumentException JavaDoc ("The file must exist on disk"); //NOI18N
181
}
182                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null, includedLibrariesElement != null );
183                                 if (!resources.contains(item)) {
184                                     resources.add (item);
185                                     added = true;
186                                 }
187                             }
188                             if (added) {
189                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator(), includedLibrariesElement );
190                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
191
props.setProperty(classPathId, itemRefs);
192                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
193                                 ProjectManager.getDefault().saveProject(project);
194                                 return Boolean.TRUE;
195                             }
196                             return Boolean.FALSE;
197                         }
198                     }
199             )).booleanValue();
200         } catch (Exception JavaDoc e) {
201             if (e instanceof IOException JavaDoc) {
202                 throw (IOException JavaDoc) e;
203             }
204             else {
205                 Exception JavaDoc t = new IOException JavaDoc ();
206                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
207             }
208         }
209     }
210     
211     /*
212     public boolean addArchiveFile(final String classPathId, final FileObject archiveFile) throws IOException {
213         assert archiveFile != null : "Parameter cannot be null"; //NOI18N
214         try {
215             return ((Boolean)ProjectManager.mutex().writeAccess(
216                     new Mutex.ExceptionAction () {
217                         public Object run() throws Exception {
218                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
219                             String raw = props.getProperty(classPathId);
220                             List resources = cs.itemsList( raw );
221                             File f = FileUtil.toFile (archiveFile);
222                             if (f == null ) {
223                                 throw new IllegalArgumentException ("The file must exist on disk"); //NOI18N
224                             }
225                             ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null );
226
227                             if (!resources.contains(item)) {
228                                 resources.add (item);
229                                 String itemRefs[] = cs.encodeToStrings( resources.iterator() );
230                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
231                                 props.setProperty(classPathId, itemRefs);
232                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
233                                 ProjectManager.getDefault().saveProject(project);
234                                 return Boolean.TRUE;
235                             }
236                             return Boolean.FALSE;
237                         }
238                     }
239             )).booleanValue();
240         } catch (Exception e) {
241             if (e instanceof IOException) {
242                 throw (IOException) e;
243             }
244             else {
245                 Exception t = new IOException ();
246                 throw (IOException) ErrorManager.getDefault().annotate(t,e);
247             }
248         }
249     }
250      */

251
252     // TODO: AB: AntArtifactItem should not be in LibrariesChooser
253

254     public boolean addAntArtifact(AntArtifact artifact, URI JavaDoc artifactElement) throws IOException JavaDoc {
255         return addAntArtifacts(DEFAULT_CLASS_PATH, new AntArtifactChooser.ArtifactItem[] { new AntArtifactChooser.ArtifactItem(artifact, artifactElement) }, DEFAULT_INCLUDED_LIBS_ELEMENT);
256     }
257
258     public boolean addAntArtifacts(final String JavaDoc classPathId, final AntArtifactChooser.ArtifactItem[] artifactItems, final String JavaDoc includedLibrariesElement) throws IOException JavaDoc {
259         assert artifactItems != null : "Parameter cannot be null"; //NOI18N
260
try {
261             return ((Boolean JavaDoc)ProjectManager.mutex().writeAccess(
262                     new Mutex.ExceptionAction () {
263                         public Object JavaDoc run() throws Exception JavaDoc {
264                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
265                             String JavaDoc raw = props.getProperty (classPathId);
266                             List JavaDoc<ClassPathSupport.Item> resources = cs.itemsList( raw, includedLibrariesElement );
267                             boolean added = false;
268                             for (int i = 0; i < artifactItems.length; i++) {
269                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, includedLibrariesElement != null );
270                                 if (!resources.contains(item)) {
271                                     resources.add (item);
272                                     added = true;
273                                 }
274                             }
275                             if (added) {
276                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator(), includedLibrariesElement );
277                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
278
props.setProperty (classPathId, itemRefs);
279                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
280                                 ProjectManager.getDefault().saveProject(project);
281                                 return Boolean.TRUE;
282                             }
283                             return Boolean.FALSE;
284                         }
285                     }
286             )).booleanValue();
287         } catch (Exception JavaDoc e) {
288             if (e instanceof IOException JavaDoc) {
289                 throw (IOException JavaDoc) e;
290             }
291             else {
292                 Exception JavaDoc t = new IOException JavaDoc ();
293                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
294             }
295         }
296     }
297     
298     /*
299     public boolean addAntArtifact(final String classPathId, final AntArtifact artifact, final URI artifactElement) throws IOException {
300         assert artifact != null : "Parameter cannot be null"; //NOI18N
301         try {
302             return ((Boolean)ProjectManager.mutex().writeAccess(
303                     new Mutex.ExceptionAction () {
304                         public Object run() throws Exception {
305                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
306                             String raw = props.getProperty (classPathId);
307                             List resources = cs.itemsList( raw );
308                             ClassPathSupport.Item item = ClassPathSupport.Item.create( artifact, artifactElement, null );
309                             if (!resources.contains(item)) {
310                                 resources.add (item);
311                                 String itemRefs[] = cs.encodeToStrings( resources.iterator() );
312                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
313                                 props.setProperty (classPathId, itemRefs);
314                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
315                                 ProjectManager.getDefault().saveProject(project);
316                                 return Boolean.TRUE;
317                             }
318                             return Boolean.FALSE;
319                         }
320                     }
321             )).booleanValue();
322         } catch (Exception e) {
323             if (e instanceof IOException) {
324                 throw (IOException) e;
325             }
326             else {
327                 Exception t = new IOException ();
328                 throw (IOException) ErrorManager.getDefault().annotate(t,e);
329             }
330         }
331     }
332      */

333
334     private void registerLibraryListeners () {
335         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
336
Library libs [] = LibraryManager.getDefault().getLibraries();
337         for (int i = 0; i < libs.length; i++) {
338             libs [i].removePropertyChangeListener(this);
339         }
340         Iterator JavaDoc i = cs.itemsIterator(props.getProperty(AppClientProjectProperties.JAVAC_CLASSPATH), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES);
341         while (i.hasNext()) {
342             ClassPathSupport.Item item = (ClassPathSupport.Item)i.next();
343             if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken()) {
344                 item.getLibrary().addPropertyChangeListener(this);
345             }
346         }
347     }
348     
349     public void propertyChange (PropertyChangeEvent JavaDoc e) {
350         if (projectDeleted) {
351             return;
352         }
353         if (e.getSource().equals(eval) && (e.getPropertyName().equals(AppClientProjectProperties.JAVAC_CLASSPATH))) {
354             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
355
String JavaDoc javacCp = props.getProperty(AppClientProjectProperties.JAVAC_CLASSPATH);
356             if (javacCp != null) {
357                 registerLibraryListeners();
358                 storeLibLocations();
359             }
360         } else if (e.getPropertyName().equals(Library.PROP_CONTENT)) {
361             storeLibLocations();
362         }
363     }
364     
365     private void storeLibLocations() {
366         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
367             public void run() {
368                 ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
369                     public void run() {
370                         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
371
//update lib references in private properties
372
EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
373                         List JavaDoc<ClassPathSupport.Item> wmLibs = cs.itemsList(props.getProperty(AppClientProjectProperties.JAVAC_CLASSPATH), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES);
374                         cs.encodeToStrings(wmLibs.iterator(), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES);
375                         AppClientProjectProperties.storeLibrariesLocations(wmLibs.iterator(), privateProps);
376                         helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
377
378                         try {
379                             ProjectManager.getDefault().saveProject(project);
380                         } catch (IOException JavaDoc e) {
381                             ErrorManager.getDefault().notify(e);
382                         }
383                     }
384                 });
385             }
386         });
387     }
388     
389     public void notifyDeleting() {
390         projectDeleted = true;
391         eval.removePropertyChangeListener(this);
392     }
393 }
394
Popular Tags