KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.java.project.JavaProjectConstants;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.api.project.ant.AntArtifact;
35 import org.netbeans.api.project.libraries.Library;
36 import org.netbeans.modules.java.j2seproject.UpdateHelper;
37 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
38 import org.netbeans.spi.java.project.classpath.ProjectClassPathModifierImplementation;
39 import org.netbeans.spi.project.support.ant.AntProjectHelper;
40 import org.netbeans.spi.project.support.ant.EditableProperties;
41 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
42 import org.netbeans.spi.project.support.ant.ReferenceHelper;
43 import org.openide.ErrorManager;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.util.Mutex;
46 import org.openide.util.MutexException;
47
48 /**
49  *@author Tomas Zezula
50  *
51  */

52 public class J2SEProjectClassPathModifier extends ProjectClassPathModifierImplementation {
53     
54     static final int ADD = 1;
55     static final int REMOVE = 2;
56     
57     private final Project project;
58     private final UpdateHelper helper;
59     private final ReferenceHelper refHelper;
60     private final PropertyEvaluator eval;
61     private final ClassPathSupport cs;
62     
63     /** Creates a new instance of J2SEProjectClassPathModifier */
64     public J2SEProjectClassPathModifier(final Project project, final UpdateHelper helper, final PropertyEvaluator eval, final ReferenceHelper refHelper) {
65         assert project != null;
66         assert helper != null;
67         assert eval != null;
68         assert refHelper != null;
69         this.project = project;
70         this.helper = helper;
71         this.eval = eval;
72         this.refHelper = refHelper;
73         this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(),
74                                         J2SEProjectProperties.WELL_KNOWN_PATHS,
75                                         J2SEProjectProperties.LIBRARY_PREFIX,
76                                         J2SEProjectProperties.LIBRARY_SUFFIX,
77                                         J2SEProjectProperties.ANT_ARTIFACT_PREFIX );
78     }
79     
80     protected SourceGroup[] getExtensibleSourceGroups() {
81         Sources s = (Sources) this.project.getLookup().lookup(Sources.class);
82         assert s != null;
83         return s.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
84     }
85     
86     protected String JavaDoc[] getExtensibleClassPathTypes (SourceGroup sg) {
87         return new String JavaDoc[] {
88             ClassPath.COMPILE,
89             ClassPath.EXECUTE
90         };
91     }
92
93     protected boolean removeRoots(final URL JavaDoc[] classPathRoots, final SourceGroup sourceGroup, final String JavaDoc type) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
94         return handleRoots (classPathRoots, getClassPathProperty(sourceGroup, type), REMOVE);
95     }
96
97     protected boolean addRoots (final URL JavaDoc[] classPathRoots, final SourceGroup sourceGroup, final String JavaDoc type) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
98         return handleRoots (classPathRoots, getClassPathProperty(sourceGroup, type), ADD);
99     }
100     
101     boolean handleRoots (final URL JavaDoc[] classPathRoots, final String JavaDoc classPathProperty, final int operation) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
102         assert classPathRoots != null : "The classPathRoots cannot be null"; //NOI18N
103
assert classPathProperty != null;
104         try {
105             return ProjectManager.mutex().writeAccess(
106                     new Mutex.ExceptionAction<Boolean JavaDoc>() {
107                         public Boolean JavaDoc run() throws Exception JavaDoc {
108                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
109                             String JavaDoc raw = props.getProperty(classPathProperty);
110                             List JavaDoc<ClassPathSupport.Item> resources = cs.itemsList(raw);
111                             boolean changed = false;
112                             for (int i=0; i< classPathRoots.length; i++) {
113                                 assert classPathRoots[i] != null;
114                                 assert classPathRoots[i].toExternalForm().endsWith("/"); //NOI18N
115
URL JavaDoc toAdd = FileUtil.getArchiveFile(classPathRoots[i]);
116                                 if (toAdd == null) {
117                                     toAdd = classPathRoots[i];
118                                 }
119                                 File JavaDoc f = FileUtil.normalizeFile( new File JavaDoc (URI.create(toAdd.toExternalForm())));
120                                 if (f == null ) {
121                                     throw new IllegalArgumentException JavaDoc ("The file must exist on disk"); //NOI18N
122
}
123                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null );
124                                 if (operation == ADD && !resources.contains(item)) {
125                                     resources.add (item);
126                                     changed = true;
127                                 }
128                                 else if (operation == REMOVE && resources.contains(item)) {
129                                     resources.remove(item);
130                                     changed = true;
131                                 }
132                             }
133                             if (changed) {
134                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator() );
135                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
136
props.setProperty(classPathProperty, itemRefs);
137                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
138                                 ProjectManager.getDefault().saveProject(project);
139                                 return true;
140                             }
141                             return false;
142                         }
143                     }
144             );
145         } catch (Exception JavaDoc e) {
146             if (e instanceof IOException JavaDoc) {
147                 throw (IOException JavaDoc) e;
148             }
149             else {
150                 Exception JavaDoc t = new IOException JavaDoc ();
151                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
152             }
153         }
154     }
155
156     protected boolean removeAntArtifacts(final AntArtifact[] artifacts, final URI JavaDoc[] artifactElements, final SourceGroup sourceGroup, final String JavaDoc type) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
157         return handleAntArtifacts (artifacts, artifactElements, getClassPathProperty(sourceGroup, type), REMOVE);
158     }
159
160     protected boolean addAntArtifacts(final AntArtifact[] artifacts, final URI JavaDoc[] artifactElements, final SourceGroup sourceGroup, final String JavaDoc type) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
161         return handleAntArtifacts (artifacts, artifactElements, getClassPathProperty(sourceGroup, type), ADD);
162     }
163     
164     boolean handleAntArtifacts (final AntArtifact[] artifacts, final URI JavaDoc[] artifactElements, final String JavaDoc classPathProperty, final int operation) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
165         assert artifacts != null : "Artifacts cannot be null"; //NOI18N
166
assert artifactElements != null : "ArtifactElements cannot be null"; //NOI18N
167
assert artifacts.length == artifactElements.length : "Each artifact has to have corresponding artifactElement"; //NOI18N
168
assert classPathProperty != null;
169         try {
170             return ProjectManager.mutex().writeAccess(
171                     new Mutex.ExceptionAction<Boolean JavaDoc>() {
172                         public Boolean JavaDoc run() throws Exception JavaDoc {
173                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
174                             String JavaDoc raw = props.getProperty (classPathProperty);
175                             List JavaDoc<ClassPathSupport.Item> resources = cs.itemsList(raw);
176                             boolean changed = false;
177                             for (int i=0; i<artifacts.length; i++) {
178                                 assert artifacts[i] != null;
179                                 assert artifactElements[i] != null;
180                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifacts[i], artifactElements[i], null );
181                                 if (operation == ADD && !resources.contains(item)) {
182                                     resources.add (item);
183                                     changed = true;
184                                 }
185                                 else if (operation == REMOVE && resources.contains(item)) {
186                                     resources.remove(item);
187                                     changed = true;
188                                 }
189                             }
190                             if (changed) {
191                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator() );
192                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
193
props.setProperty (classPathProperty, itemRefs);
194                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
195                                 ProjectManager.getDefault().saveProject(project);
196                                 return true;
197                             }
198                             return false;
199                         }
200                     }
201             );
202         } catch (Exception JavaDoc e) {
203             if (e instanceof IOException JavaDoc) {
204                 throw (IOException JavaDoc) e;
205             }
206             else {
207                 Exception JavaDoc t = new IOException JavaDoc ();
208                 throw (IOException JavaDoc) ErrorManager.getDefault().annotate(t,e);
209             }
210         }
211     }
212     
213     
214     protected boolean removeLibraries(final Library[] libraries, final SourceGroup sourceGroup, final String JavaDoc type) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
215         return handleLibraries (libraries, getClassPathProperty(sourceGroup, type), REMOVE);
216     }
217
218     protected boolean addLibraries(final Library[] libraries, final SourceGroup sourceGroup, final String JavaDoc type) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
219         return handleLibraries (libraries, getClassPathProperty(sourceGroup, type), ADD);
220     }
221     
222     boolean handleLibraries (final Library[] libraries, final String JavaDoc classPathProperty, final int operation) throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
223         assert libraries != null : "Libraries cannot be null"; //NOI18N
224
assert classPathProperty != null;
225         try {
226             return ProjectManager.mutex().writeAccess(
227                     new Mutex.ExceptionAction<Boolean JavaDoc>() {
228                         public Boolean JavaDoc run() throws IOException JavaDoc {
229                             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
230                             String JavaDoc raw = props.getProperty(classPathProperty);
231                             List JavaDoc<ClassPathSupport.Item> resources = cs.itemsList(raw);
232                             List JavaDoc<ClassPathSupport.Item> changed = new ArrayList JavaDoc<ClassPathSupport.Item>(libraries.length);
233                             for (int i=0; i< libraries.length; i++) {
234                                 assert libraries[i] != null;
235                                 ClassPathSupport.Item item = ClassPathSupport.Item.create( libraries[i], null );
236                                 if (operation == ADD && !resources.contains(item)) {
237                                     resources.add (item);
238                                     changed.add(item);
239                                 }
240                                 else if (operation == REMOVE && resources.contains(item)) {
241                                     resources.remove(item);
242                                     changed.add(item);
243                                 }
244                             }
245                             if (!changed.isEmpty()) {
246                                 String JavaDoc itemRefs[] = cs.encodeToStrings( resources.iterator() );
247                                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //PathParser may change the EditableProperties
248
props.setProperty(classPathProperty, itemRefs);
249                                 if (operation == ADD) {
250                                     for (ClassPathSupport.Item item : changed) {
251                                         String JavaDoc prop = cs.getLibraryReference(item);
252                                         prop = prop.substring(2, prop.length()-1); // XXX make a PropertyUtils method for this!
253
ClassPathSupport.relativizeLibraryClassPath(props, helper.getAntProjectHelper(), prop);
254                                     }
255                                 }
256                                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
257                                 ProjectManager.getDefault().saveProject(project);
258                                 return true;
259                             }
260                             return false;
261                         }
262                     }
263             );
264         } catch (MutexException e) {
265             throw (IOException JavaDoc) e.getException();
266         }
267     }
268     
269     private String JavaDoc getClassPathProperty (final SourceGroup sg, final String JavaDoc type) throws UnsupportedOperationException JavaDoc {
270         assert sg != null : "SourceGroup cannot be null"; //NOI18N
271
assert type != null : "Type cannot be null"; //NOI18N
272
final String JavaDoc classPathProperty = ((ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class)).getPropertyName (sg, type);
273         if (classPathProperty == null) {
274             throw new UnsupportedOperationException JavaDoc ("Modification of [" + sg.getRootFolder().getPath() +", " + type + "] is not supported"); //NOI8N
275
}
276         return classPathProperty;
277     }
278 }
279
Popular Tags