KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > classpath > WebProjectClassPathExtender


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.web.project.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.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.Mutex;
35
36 import org.netbeans.api.project.libraries.Library;
37 import org.netbeans.api.project.libraries.LibraryManager;
38 import org.netbeans.api.project.ant.AntArtifact;
39 import org.netbeans.api.project.ProjectManager;
40 import org.netbeans.api.project.Project;
41 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
42 import org.netbeans.spi.project.support.ant.AntProjectHelper;
43 import org.netbeans.spi.project.support.ant.ReferenceHelper;
44 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
45 import org.netbeans.spi.project.support.ant.EditableProperties;
46
47 import org.netbeans.modules.web.project.UpdateHelper;
48 import org.netbeans.modules.web.project.ui.customizer.AntArtifactChooser;
49 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
50 import org.openide.util.RequestProcessor;
51
52
53 public class WebProjectClassPathExtender implements ProjectClassPathExtender, PropertyChangeListener JavaDoc {
54     
55     private static final String JavaDoc CP_CLASS_PATH = "javac.classpath"; //NOI18N
56
private static final String JavaDoc DEFAULT_WEB_MODULE_ELEMENT_NAME = ClassPathSupport.TAG_WEB_MODULE_LIBRARIES;
57
58     private Project project;
59     private UpdateHelper helper;
60     private PropertyEvaluator eval;
61     
62     private ClassPathSupport cs;
63     
64     private volatile boolean projectDeleted;
65
66     public WebProjectClassPathExtender (Project project, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper) {
67         this.project = project;
68         this.helper = helper;
69         this.eval = eval;
70         
71         this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(),
72                                         WebProjectProperties.WELL_KNOWN_PATHS,
73                                         WebProjectProperties.LIBRARY_PREFIX,
74                                         WebProjectProperties.LIBRARY_SUFFIX,
75                                         WebProjectProperties.ANT_ARTIFACT_PREFIX );
76         eval.addPropertyChangeListener(this); //listen for changes of libraries list
77
registerLibraryListeners();
78     }
79
80     public boolean addLibrary(final Library library) throws IOException JavaDoc {
81         return addLibraries(CP_CLASS_PATH, new Library[] { library }, DEFAULT_WEB_MODULE_ELEMENT_NAME);
82     }
83     
84     public boolean addLibraries(final String JavaDoc classPathId, final Library[] libraries, final String JavaDoc webModuleElementName) throws IOException JavaDoc {
85         assert libraries != null : "Parameter cannot be null"; //NOI18N
86
try {
87             return ((Boolean JavaDoc)ProjectManager.mutex().writeAccess(
88                     new Mutex.ExceptionAction () {
89                         public Object JavaDoc run() throws Exception JavaDoc {
90                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
91                             String JavaDoc raw = props.getProperty(classPathId);
92                             List JavaDoc resources = cs.itemsList( raw, webModuleElementName );
93                             boolean added = false;
94                             for (int i = 0; i < libraries.length; i++) {
95                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( libraries[i], null, ClassPathSupport.Item.PATH_IN_WAR_LIB);
96                                 if (!resources.contains(item)) {
97                                     resources.add (item);
98                                     added = true;
99                                 }
100                             }
101                             if (added) {
102                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator(), webModuleElementName );
103                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
104
props.setProperty(classPathId, itemRefs);
105                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
106                                 //update lib references in private properties
107
EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
108                                 ArrayList JavaDoc l = new ArrayList JavaDoc ();
109                                 l.addAll(resources);
110                                 l.addAll(cs.itemsList(props.getProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES));
111                                 WebProjectProperties.storeLibrariesLocations(l.iterator(), privateProps);
112                                 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
113                                 ProjectManager.getDefault().saveProject(project);
114                                 return Boolean.TRUE;
115                             }
116                             return Boolean.FALSE;
117                         }
118                     }
119             )).booleanValue();
120         } catch (Exception JavaDoc e) {
121             if (e instanceof IOException JavaDoc) {
122                 throw (IOException JavaDoc) e;
123             }
124             else {
125                 Exception JavaDoc t = new IOException JavaDoc ();
126                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
127             }
128         }
129     }
130
131     public boolean addArchiveFile(final FileObject archiveFile) throws IOException JavaDoc {
132         return addArchiveFiles(CP_CLASS_PATH, new FileObject[] { archiveFile }, DEFAULT_WEB_MODULE_ELEMENT_NAME);
133     }
134     
135     public boolean addArchiveFiles(final String JavaDoc classPathId, final FileObject[] archiveFiles, final String JavaDoc webModuleElementName) throws IOException JavaDoc {
136         assert archiveFiles != null : "Parameter cannot be null"; //NOI18N
137
try {
138             return ((Boolean JavaDoc)ProjectManager.mutex().writeAccess(
139                     new Mutex.ExceptionAction () {
140                         public Object JavaDoc run() throws Exception JavaDoc {
141                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
142                             String JavaDoc raw = props.getProperty(classPathId);
143                             List JavaDoc resources = cs.itemsList( raw, webModuleElementName );
144                             boolean added = false;
145                             for (int i = 0; i < archiveFiles.length; i++) {
146                                 File JavaDoc f = FileUtil.toFile (archiveFiles[i]);
147                                 if (f == null ) {
148                                     throw new IllegalArgumentException JavaDoc ("The file must exist on disk"); //NOI18N
149
}
150                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null, archiveFiles[i].isFolder() ? ClassPathSupport.Item.PATH_IN_WAR_DIR : ClassPathSupport.Item.PATH_IN_WAR_LIB);
151                                 if (!resources.contains(item)) {
152                                     resources.add (item);
153                                     added = true;
154                                 }
155                             }
156                             if (added) {
157                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator(), webModuleElementName );
158                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
159
props.setProperty(classPathId, itemRefs);
160                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
161                                 ProjectManager.getDefault().saveProject(project);
162                                 return Boolean.TRUE;
163                             }
164                             return Boolean.FALSE;
165                         }
166                     }
167             )).booleanValue();
168         } catch (Exception JavaDoc e) {
169             if (e instanceof IOException JavaDoc) {
170                 throw (IOException JavaDoc) e;
171             }
172             else {
173                 Exception JavaDoc t = new IOException JavaDoc ();
174                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
175             }
176         }
177     }
178     
179     // TODO: AB: AntArtifactItem should not be in LibrariesChooser
180

181     public boolean addAntArtifact (AntArtifact artifact, URI JavaDoc artifactElement) throws IOException JavaDoc {
182         return addAntArtifacts(CP_CLASS_PATH, new AntArtifactChooser.ArtifactItem[] { new AntArtifactChooser.ArtifactItem(artifact, artifactElement) }, DEFAULT_WEB_MODULE_ELEMENT_NAME);
183     }
184
185     public boolean addAntArtifacts(final String JavaDoc classPathId, final AntArtifactChooser.ArtifactItem[] artifactItems, final String JavaDoc webModuleElementName) throws IOException JavaDoc {
186         assert artifactItems != null : "Parameter cannot be null"; //NOI18N
187
try {
188             return ((Boolean JavaDoc)ProjectManager.mutex().writeAccess(
189                     new Mutex.ExceptionAction () {
190                         public Object JavaDoc run() throws Exception JavaDoc {
191                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
192                             String JavaDoc raw = props.getProperty (classPathId);
193                             List JavaDoc resources = cs.itemsList( raw, webModuleElementName );
194                             boolean added = false;
195                             for (int i = 0; i < artifactItems.length; i++) {
196                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, ClassPathSupport.Item.PATH_IN_WAR_LIB);
197                                 if (!resources.contains(item)) {
198                                     resources.add (item);
199                                     added = true;
200                                 }
201                             }
202                             if (added) {
203                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator(), webModuleElementName );
204                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
205
props.setProperty (classPathId, itemRefs);
206                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
207                                 ProjectManager.getDefault().saveProject(project);
208                                 return Boolean.TRUE;
209                             }
210                             return Boolean.FALSE;
211                         }
212                     }
213             )).booleanValue();
214         } catch (Exception JavaDoc e) {
215             if (e instanceof IOException JavaDoc) {
216                 throw (IOException JavaDoc) e;
217             }
218             else {
219                 Exception JavaDoc t = new IOException JavaDoc ();
220                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
221             }
222         }
223     }
224
225     public ClassPathSupport getClassPathSupport () {
226         return cs;
227     }
228     
229     private void registerLibraryListeners () {
230         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
231
Library libs [] = LibraryManager.getDefault().getLibraries();
232         for (int i = 0; i < libs.length; i++) {
233             libs [i].removePropertyChangeListener(this);
234         }
235         HashSet JavaDoc set = new HashSet JavaDoc();
236         set.addAll(cs.itemsList(props.getProperty(WebProjectProperties.JAVAC_CLASSPATH), WebProjectProperties.TAG_WEB_MODULE_LIBRARIES));
237         set.addAll(cs.itemsList(props.getProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES));
238         Iterator JavaDoc i = set.iterator();
239         while (i.hasNext()) {
240             ClassPathSupport.Item item = (ClassPathSupport.Item)i.next();
241             if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken()) {
242                 item.getLibrary().addPropertyChangeListener(this);
243             }
244         }
245     }
246     
247     public void propertyChange (PropertyChangeEvent JavaDoc e) {
248         if (projectDeleted) {
249             return;
250         }
251         if (e.getSource().equals(eval) && (e.getPropertyName().equals(WebProjectProperties.JAVAC_CLASSPATH)
252             || e.getPropertyName().equals(WebProjectProperties.WAR_CONTENT_ADDITIONAL))) {
253                 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
254
String JavaDoc javacCp = props.getProperty(WebProjectProperties.JAVAC_CLASSPATH);
255                 if (javacCp != null) {
256                     registerLibraryListeners();
257             if (ProjectManager.getDefault().isValid(project))
258             storeLibLocations();
259                 }
260         } else if (e.getPropertyName().equals(Library.PROP_CONTENT)) {
261             storeLibLocations();
262         }
263     }
264     
265     private void storeLibLocations() {
266         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
267             public void run() {
268                 ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
269                     public void run() {
270                         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
271
//update lib references in private properties
272
EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
273                         List JavaDoc wmLibs = cs.itemsList(props.getProperty(WebProjectProperties.JAVAC_CLASSPATH), WebProjectProperties.TAG_WEB_MODULE_LIBRARIES);
274                         List JavaDoc additionalLibs = cs.itemsList(props.getProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES);
275                         cs.encodeToStrings(wmLibs.iterator(), WebProjectProperties.TAG_WEB_MODULE_LIBRARIES);
276                         cs.encodeToStrings(additionalLibs.iterator(), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES);
277                         HashSet JavaDoc set = new HashSet JavaDoc();
278                         set.addAll(wmLibs);
279                         set.addAll(additionalLibs);
280                         WebProjectProperties.storeLibrariesLocations(set.iterator(), privateProps);
281                         helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
282
283                         try {
284                             ProjectManager.getDefault().saveProject(project);
285                         } catch (IOException JavaDoc e) {
286                             ErrorManager.getDefault().notify(e);
287                         }
288                     }
289                 });
290             }
291         });
292     }
293
294     public void notifyDeleting() {
295         projectDeleted = true;
296         eval.removePropertyChangeListener(this);
297     }
298 }
299
Popular Tags