KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > classpath > EjbJarProjectClassPathExtender


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

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