KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > freeform > WebModules


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.web.freeform;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Collections JavaDoc;
32 import org.netbeans.spi.java.classpath.ClassPathProvider;
33 import org.openide.filesystems.FileStateInvalidException;
34 import org.w3c.dom.Element JavaDoc;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.FileObject;
37 import org.netbeans.spi.project.AuxiliaryConfiguration;
38 import org.netbeans.api.java.classpath.ClassPath;
39 import org.netbeans.api.java.project.JavaProjectConstants;
40 import org.netbeans.api.java.queries.SourceForBinaryQuery;
41 import org.netbeans.api.project.FileOwnerQuery;
42 import org.netbeans.api.project.Project;
43 import org.netbeans.api.project.ProjectUtils;
44 import org.netbeans.api.project.SourceGroup;
45 import org.netbeans.modules.ant.freeform.spi.support.Util;
46 import org.netbeans.modules.web.api.webmodule.WebModule;
47 import org.netbeans.modules.web.spi.webmodule.WebModuleFactory;
48 import org.netbeans.modules.web.spi.webmodule.WebModuleProvider;
49 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation;
50 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
51 import org.netbeans.spi.project.support.ant.AntProjectEvent;
52 import org.netbeans.spi.project.support.ant.AntProjectHelper;
53 import org.netbeans.spi.project.support.ant.AntProjectListener;
54 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
55 import org.netbeans.spi.project.support.ant.PropertyUtils;
56
57 /**
58  * Web module implementation on top of freeform project.
59  *
60  * @author Pavel Buzek
61  */

62 public class WebModules implements WebModuleProvider, AntProjectListener, ClassPathProvider {
63     
64     private ArrayList JavaDoc modules = new ArrayList JavaDoc ();
65     private HashMap JavaDoc cache = new HashMap JavaDoc ();
66     private Project project;
67     private AntProjectHelper helper;
68     private PropertyEvaluator evaluator;
69     
70     public WebModules (Project project, AntProjectHelper helper, PropertyEvaluator evaluator) {
71         assert project != null;
72         this.project = project;
73         this.helper = helper;
74         this.evaluator = evaluator;
75         helper.addAntProjectListener(this);
76     }
77     
78     public synchronized WebModule findWebModule (FileObject file) {
79         Project owner = FileOwnerQuery.getOwner (file);
80         if (project.equals (owner)) {
81             if (modules.isEmpty()) {
82                 readAuxData ();
83             }
84             for (Iterator JavaDoc iter = modules.iterator (); iter.hasNext ();) {
85                 FFWebModule wm = (FFWebModule) iter.next ();
86                 if (wm.contais (file)) {
87                     if (cache.get (wm) == null) {
88                         cache.put (wm, WebModuleFactory.createWebModule (wm));
89                     }
90                     return (WebModule) cache.get (wm);
91                 }
92             }
93         }
94         return null;
95     }
96
97     public ClassPath findClassPath (FileObject file, String JavaDoc type) {
98         Project owner = FileOwnerQuery.getOwner (file);
99         if (owner != null && owner.equals (project)) {
100             if (modules == null) {
101                 readAuxData ();
102             }
103             for (Iterator JavaDoc iter = modules.iterator (); iter.hasNext ();) {
104                 FFWebModule wm = (FFWebModule) iter.next ();
105                 if (wm.contais (file)) {
106                     return wm.findClassPath (file, type);
107                 }
108             }
109         }
110         return null;
111     }
112     
113     public synchronized void readAuxData () {
114         modules.clear();
115         cache.clear();
116         AuxiliaryConfiguration aux = (AuxiliaryConfiguration)project.getLookup().lookup(AuxiliaryConfiguration.class);
117         assert aux != null;
118         Element JavaDoc web = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true);
119         if (web == null) {
120             return;
121         }
122         List JavaDoc/*<Element>*/ webModules = Util.findSubElements(web);
123         Iterator JavaDoc it = webModules.iterator();
124         while (it.hasNext()) {
125             Element JavaDoc webModulesEl = (Element JavaDoc)it.next();
126             assert webModulesEl.getLocalName().equals("web-module") : webModulesEl;
127             FileObject docRootFO = getFile (webModulesEl, "doc-root"); //NOI18N
128
Element JavaDoc j2eeSpecEl = Util.findElement (webModulesEl, "j2ee-spec-level", WebProjectNature.NS_WEB);
129             String JavaDoc j2eeSpec = j2eeSpecEl == null ? null : evaluator.evaluate (Util.findText (j2eeSpecEl));
130             Element JavaDoc contextPathEl = Util.findElement (webModulesEl, "context-path", WebProjectNature.NS_WEB);
131             String JavaDoc contextPathText = contextPathEl == null ? null : Util.findText (contextPathEl);
132             String JavaDoc contextPath = contextPathText == null ? null : evaluator.evaluate (contextPathText);
133             Element JavaDoc classpathEl = Util.findElement (webModulesEl, "classpath", WebProjectNature.NS_WEB);
134             FileObject [] sources = getSources ();
135             ClassPath cp = classpathEl == null ? null : createClasspath (classpathEl, sources);
136             modules.add (new FFWebModule (docRootFO, j2eeSpec, contextPath, sources, cp));
137         }
138     }
139     
140     private FileObject getFile (Element JavaDoc parent, String JavaDoc fileElName) {
141         Element JavaDoc el = Util.findElement (parent, fileElName, WebProjectNature.NS_WEB);
142         String JavaDoc fname = Util.findText (el);
143         String JavaDoc locationEval = evaluator.evaluate(fname);
144         if (locationEval != null) {
145             File JavaDoc locationFile = helper.resolveFile(locationEval);
146             return FileUtil.toFileObject(locationFile);
147         }
148         return null;
149     }
150
151     private FileObject [] getSources () {
152         SourceGroup sg [] = ProjectUtils.getSources (project).getSourceGroups (JavaProjectConstants.SOURCES_TYPE_JAVA);
153         Set JavaDoc srcRootSet = new HashSet JavaDoc ();
154         for (int i = 0; i < sg.length; i++) {
155             URL JavaDoc entry;
156             try {
157                 entry = sg[i].getRootFolder().getURL();
158             } catch (FileStateInvalidException x) {
159                 throw new AssertionError JavaDoc(x);
160             }
161             // There is important calling this. Withouth calling this, will not work java cc in Jsp editor correctly.
162
SourceForBinaryQuery.Result res = SourceForBinaryQuery.findSourceRoots (entry);
163             FileObject srcForBin [] = res.getRoots ();
164             for (int j = 0; j < srcForBin.length; j++) {
165                 srcRootSet.add (srcForBin [j]);
166             }
167         }
168         
169         FileObject[] roots = new FileObject [sg.length];
170         for (int i = 0; i < sg.length; i++) {
171             roots[i] = sg[i].getRootFolder();
172         }
173         return roots;
174     }
175     
176     /**
177      * Create a classpath from a &lt;classpath&gt; element.
178      */

179     private ClassPath createClasspath(Element JavaDoc classpathEl, FileObject[] sources) {
180 // System.out.println("creating classpath for " + classpathEl);
181
String JavaDoc cp = Util.findText(classpathEl);
182         if (cp == null) {
183             cp = "";
184         }
185         String JavaDoc cpEval = evaluator.evaluate(cp);
186         if (cpEval == null) {
187             return null;
188         }
189         String JavaDoc[] path = PropertyUtils.tokenizePath(cpEval);
190         Set JavaDoc entries = new HashSet JavaDoc();
191         for (int i = 0; i < path.length; i++) {
192             entries.add(helper.resolveFile(path[i]));
193         }
194         if (entries.size() == 0) {
195             // if the classpath element was empty then the classpath
196
// should contain all source roots
197
for (int i = 0; i < sources.length; i++) {
198                 entries.add(FileUtil.toFile(sources[i]));
199             }
200         }
201         URL JavaDoc[] pathURL = new URL JavaDoc[entries.size()];
202         int i = 0;
203         for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
204             File JavaDoc entryFile = (File JavaDoc)it.next();
205             URL JavaDoc entry;
206             try {
207                 entry = entryFile.toURI().toURL();
208                 if (FileUtil.isArchiveFile(entry)) {
209                     entry = FileUtil.getArchiveRoot(entry);
210                 } else {
211                     String JavaDoc s = entry.toExternalForm();
212                     if (!s.endsWith("/")) { // NOI18N
213
// Folder which is not built.
214
entry = new URL JavaDoc(s + '/');
215                     }
216                 }
217             } catch (MalformedURLException JavaDoc x) {
218                 throw new AssertionError JavaDoc(x);
219             }
220             pathURL[i++] = entry;
221         }
222         return ClassPathSupport.createClassPath(pathURL);
223     }
224     
225     public void configurationXmlChanged(AntProjectEvent ev) {
226         readAuxData();
227     }
228     
229     public void propertiesChanged(AntProjectEvent ev) {
230         // ignore
231
}
232     
233     private final class FFWebModule implements WebModuleImplementation {
234         
235         public static final String JavaDoc FOLDER_WEB_INF = "WEB-INF";//NOI18N
236
public static final String JavaDoc FILE_DD = "web.xml";//NOI18N
237

238         private FileObject docRootFO;
239         private FileObject [] sourcesFOs;
240         private ClassPath webClassPath;
241         private ClassPath javaSourcesClassPath;
242         private ClassPath composedClassPath = null;
243         private String JavaDoc j2eeSpec;
244         private String JavaDoc contextPath;
245         
246         FFWebModule (FileObject docRootFO, String JavaDoc j2eeSpec, String JavaDoc contextPath, FileObject sourcesFOs[], ClassPath classPath) {
247             this.docRootFO = docRootFO;
248             this.j2eeSpec = j2eeSpec;
249             this.contextPath = (contextPath == null ? "" : contextPath);
250             this.sourcesFOs = sourcesFOs;
251             this.webClassPath = (classPath == null ? ClassPathSupport.createClassPath(Collections.EMPTY_LIST) : classPath);
252             javaSourcesClassPath = (sourcesFOs == null ? ClassPathSupport.createClassPath(Collections.EMPTY_LIST): ClassPathSupport.createClassPath(sourcesFOs));
253         }
254         
255         boolean contais (FileObject fo) {
256             if (docRootFO == fo || FileUtil.isParentOf (docRootFO , fo))
257                 return true;
258             for (int i = 0; i < sourcesFOs.length; i++) {
259                 if (sourcesFOs [i] == fo || FileUtil.isParentOf (sourcesFOs [i], fo))
260                     return true;
261             }
262             return false;
263         }
264         
265         public FileObject getDocumentBase () {
266             return docRootFO;
267         }
268         
269         public ClassPath findClassPath (FileObject file, String JavaDoc type) {
270            int fileType = getType(file);
271             
272            if (fileType == 0) {
273                if (!type.equals(ClassPath.SOURCE))
274                    return null;
275                else
276                    return javaSourcesClassPath;
277             } else
278                 if (fileType == 1){
279                     if (composedClassPath == null) {
280                         HashSet JavaDoc all = new HashSet JavaDoc();
281                         FileObject[] javaRoots = null;
282                         for (int i = 0; i < sourcesFOs.length; i++){
283                             javaRoots = ClassPath.getClassPath(sourcesFOs[i], type).getRoots();
284                             for (int j = 0; j < javaRoots.length; j++)
285                                 if (!all.contains(javaRoots[j]))
286                                     all.add(javaRoots[j]);
287                         }
288                                                 
289                         for (int i = 0; i < webClassPath.getRoots().length; i++)
290                             if (!all.contains(webClassPath.getRoots()[i]))
291                                 all.add(webClassPath.getRoots()[i]);
292                         
293                         FileObject[] roots = new FileObject[all.size()];
294                         int i = 0;
295                         for (Iterator JavaDoc it = all.iterator(); it.hasNext();)
296                             roots[i++] = (FileObject)it.next();
297
298                         composedClassPath = ClassPathSupport.createClassPath(roots);
299                     }
300                     return composedClassPath;
301                 }
302             return webClassPath;
303         }
304         
305         public String JavaDoc getJ2eePlatformVersion () {
306             return j2eeSpec;
307         }
308         
309         public String JavaDoc getContextPath () {
310             return contextPath;
311         }
312         
313         public String JavaDoc toString () {
314             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("web module in freeform project" +
315                 "\n\tdoc root:" + docRootFO.getPath () +
316                 "\n\tcontext path:" + contextPath +
317                 "\n\tj2ee version:" + j2eeSpec);
318             for (int i = 0; i < sourcesFOs.length; i++) {
319                 sb.append ("\n\tsource root:" + sourcesFOs [i].getPath ());
320             }
321             return sb.toString ();
322         }
323         
324         public FileObject getDeploymentDescriptor () {
325             FileObject winf = getWebInf ();
326             if (winf == null) {
327                 return null;
328             }
329             return winf.getFileObject (FILE_DD);
330         }
331         
332         public FileObject getWebInf () {
333             return getDocumentBase ().getFileObject (FOLDER_WEB_INF);
334         }
335         
336         public FileObject[] getJavaSources() {
337             return sourcesFOs;
338         }
339         
340         /**
341          * Find what a given file represents.
342          * @param file a file in the project
343          * @return one of: <dl>
344          * <dt>0</dt> <dd>java source</dd>
345          * <dt>1</dt> <dd>web pages</dd>
346          * <dt>-1</dt> <dd>something else</dd>
347          * </dl>
348          */

349         private int getType(FileObject file) {
350             //test java source roots
351
for (int i=0; i < sourcesFOs.length; i++) {
352                 FileObject root = sourcesFOs[i];
353                 if (root.equals(file) || FileUtil.isParentOf(root, file)) {
354                     return 0;
355                 }
356             }
357             
358             //test if the file is under the web root
359
FileObject dir = getDocumentBase();
360             if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) {
361                 return 1;
362             }
363             
364             return -1;
365         }
366     }
367 }
368
Popular Tags