KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ProjectWebModule


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.project;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.*;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.api.java.project.JavaProjectConstants;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectUtils;
31 import org.netbeans.api.project.SourceGroup;
32 import org.netbeans.api.project.Sources;
33 import org.netbeans.api.project.ui.OpenProjects;
34 import org.netbeans.modules.j2ee.common.Util;
35 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
36 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
37 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
38 import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor;
39 import org.netbeans.spi.project.support.ant.AntProjectHelper;
40 import org.netbeans.spi.project.support.ant.EditableProperties;
41 import org.openide.filesystems.FileObject;
42 import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
43 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
44 import org.netbeans.spi.java.classpath.ClassPathProvider;
45 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.NotifyDescriptor;
48 import org.openide.DialogDisplayer;
49 import org.openide.util.NbBundle;
50 import org.netbeans.modules.j2ee.dd.api.webservices.*;
51 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
52 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
53 import org.openide.ErrorManager;
54
55
56 /** A web module implementation on top of project.
57  *
58  * @author Pavel Buzek
59  */

60 public final class ProjectWebModule extends J2eeModuleProvider
61   implements WebModuleImplementation, J2eeModule, ModuleChangeReporter,
62   EjbChangeDescriptor, PropertyChangeListener JavaDoc {
63       
64     public static final String JavaDoc FOLDER_WEB_INF = "WEB-INF";//NOI18N
65
public static final String JavaDoc FOLDER_CLASSES = "classes";//NOI18N
66
public static final String JavaDoc FOLDER_LIB = "lib";//NOI18N
67
public static final String JavaDoc FILE_DD = "web.xml";//NOI18N
68

69     private WebProject project;
70     private UpdateHelper helper;
71     private Set versionListeners = null;
72     private String JavaDoc fakeServerInstId = null; // used to get access to properties of other servers
73

74     private long notificationTimeout = 0; // used to suppress repeating the same messages
75

76     ProjectWebModule (WebProject project, UpdateHelper helper) {
77         this.project = project;
78         this.helper = helper;
79         project.evaluator ().addPropertyChangeListener (this);
80     }
81     
82     public FileObject getDeploymentDescriptor() {
83         return getDeploymentDescriptor(false);
84     }
85
86     public FileObject getDeploymentDescriptor(boolean silent) {
87         FileObject webInfFo = getWebInf(silent);
88         if (webInfFo==null) {
89             return null;
90         }
91         FileObject dd = webInfFo.getFileObject (FILE_DD);
92         if (dd == null && !silent
93                 && (J2eeModule.J2EE_13.equals(getJ2eePlatformVersion ()) ||
94                     J2eeModule.J2EE_14.equals(getJ2eePlatformVersion ()))) {
95             showErrorMessage(NbBundle.getMessage(ProjectWebModule.class,"MSG_WebXmlNotFound", //NOI18N
96
webInfFo.getPath()));
97         }
98         return dd;
99     }
100
101     public String JavaDoc getContextPath () {
102         if(getDeploymentDescriptor() == null) {
103             return null;
104         }
105         return getConfigSupport ().getWebContextRoot ();
106     }
107     
108     public void setContextPath (String JavaDoc path) {
109         if (getDeploymentDescriptor() != null) {
110             getConfigSupport ().setWebContextRoot (path);
111         }
112     }
113     
114     public String JavaDoc getContextPath (String JavaDoc serverInstId) {
115         fakeServerInstId = serverInstId;
116         String JavaDoc result = getContextPath();
117         fakeServerInstId = null;
118         return result;
119     }
120     
121     public void setContextPath (String JavaDoc serverInstId, String JavaDoc path) {
122         fakeServerInstId = serverInstId;
123         setContextPath(path);
124         fakeServerInstId = null;
125     }
126     
127     private void showErrorMessage(final String JavaDoc message) {
128         synchronized (this) {
129             if(new Date().getTime() > notificationTimeout && isProjectOpened()) {
130                 // set timeout to suppress the same messages during next 20 seconds (feel free to adjust the timeout
131
// using more suitable value)
132
notificationTimeout = new Date().getTime() + 20000;
133             } else {
134                 return;
135             }
136         }
137         // DialogDisplayer waits for the AWT thread, blocking the calling
138
// thread -- deadlock-prone, see issue #64888. therefore invoking
139
// only in the AWT thread
140
Runnable JavaDoc r = new Runnable JavaDoc() {
141             public void run() {
142                 if (!SwingUtilities.isEventDispatchThread()) {
143                     SwingUtilities.invokeLater(this);
144                 } else {
145                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
146                 }
147             }
148         };
149         r.run();
150     }
151     
152     public FileObject getDocumentBase () {
153         return getDocumentBase(false);
154     }
155
156     public FileObject getDocumentBase (boolean silent) {
157         FileObject docBase = getFileObject(WebProjectProperties.WEB_DOCBASE_DIR);
158         if (docBase == null && !silent) {
159             String JavaDoc relativePath = helper.getAntProjectHelper().getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR);
160             String JavaDoc path = (relativePath != null ? helper.getAntProjectHelper().resolvePath(relativePath) : null);
161             String JavaDoc errorMessage;
162             if (path != null) {
163                 errorMessage = NbBundle.getMessage(ProjectWebModule.class, "MSG_DocBase_Corrupted", project.getName(), path);
164             } else {
165                 errorMessage = NbBundle.getMessage(ProjectWebModule.class, "MSG_DocBase_Corrupted_Unknown", project.getName());
166             }
167             showErrorMessage(errorMessage);
168         }
169         return docBase;
170     }
171
172     public FileObject[] getJavaSources() {
173         return project.getSourceRoots().getRoots();
174     }
175     
176 // public ClassPath getJavaSources () {
177
// ClassPathProvider cpp = (ClassPathProvider) project.getLookup ().lookup (ClassPathProvider.class);
178
// if (cpp != null) {
179
// return cpp.findClassPath (getFileObject ("src.dir"), ClassPath.SOURCE); //NOI18N
180
// }
181
// return null;
182
// }
183

184     public FileObject getWebInf () {
185         return getWebInf(false);
186     }
187     
188     public File JavaDoc getEnterpriseResourceDirectory() {
189         return getFile(WebProjectProperties.RESOURCE_DIR);
190     }
191     
192     public FileObject getWebInf (boolean silent) {
193         FileObject documentBase = getDocumentBase(silent);
194         if (documentBase == null) {
195             return null;
196         }
197         FileObject webInf = documentBase.getFileObject (FOLDER_WEB_INF);
198         if (webInf == null && !silent) {
199                 showErrorMessage(NbBundle.getMessage(ProjectWebModule.class,"MSG_WebInfCorrupted", //NOI18N
200
documentBase.getPath()));
201         }
202         return webInf;
203     }
204     
205     public FileObject getConfDir() {
206         return getFileObject(WebProjectProperties.CONF_DIR);
207     }
208     
209     public File JavaDoc getConfDirAsFile() {
210         return getFile(WebProjectProperties.CONF_DIR);
211     }
212     
213     public ClassPathProvider getClassPathProvider () {
214         return (ClassPathProvider) project.getLookup ().lookup (ClassPathProvider.class);
215     }
216     
217     public FileObject getArchive () {
218         return getFileObject ("dist.war"); //NOI18N
219
}
220     
221     private FileObject getFileObject(String JavaDoc propname) {
222         String JavaDoc prop = helper.getAntProjectHelper().getStandardPropertyEvaluator().getProperty(propname);
223         if (prop != null) {
224             return helper.getAntProjectHelper().resolveFileObject(prop);
225         } else {
226             return null;
227         }
228     }
229     
230     private File JavaDoc getFile(String JavaDoc propname) {
231         String JavaDoc prop = helper.getAntProjectHelper().getStandardPropertyEvaluator().getProperty(propname);
232         if (prop != null) {
233             return helper.getAntProjectHelper().resolveFile(prop);
234         } else {
235             return null;
236         }
237     }
238     
239     public org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule getJ2eeModule () {
240         return this;
241     }
242     
243     public org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter getModuleChangeReporter () {
244         return this;
245     }
246
247     public FileObject findDeploymentConfigurationFile(String JavaDoc name) {
248         if (name == null) {
249             return null;
250         }
251         name = getConfigSupport().getContentRelativePath(name);
252         if (name == null) {
253             return null;
254         }
255         FileObject documentBase = getDocumentBase();
256         if (documentBase == null) {
257             return null;
258         }
259         return documentBase.getFileObject(name);
260     }
261
262     public File JavaDoc getDeploymentConfigurationFile(String JavaDoc name) {
263         assert name != null : "File name of the deployement configuration file can't be null"; //NOI18N
264

265         String JavaDoc path = getConfigSupport().getContentRelativePath(name);
266         if (path == null) {
267             path = name;
268         }
269         FileObject documentBase = getDocumentBase();
270         if (documentBase == null) {
271             //in case that docbase is null ... but normally it should not be
272
return new File JavaDoc(getConfDirAsFile(), name);
273         }
274         return new File JavaDoc(FileUtil.toFile(documentBase), path);
275     }
276
277     public FileObject getModuleFolder () {
278         return getDocumentBase ();
279     }
280
281     public boolean useDefaultServer () {
282         return false;
283     }
284     
285     public String JavaDoc getServerID () {
286         String JavaDoc inst = getServerInstanceID ();
287         if (inst != null) {
288             String JavaDoc id = Deployment.getDefault().getServerID(inst);
289             if (id != null) {
290                 return id;
291             }
292         }
293         return helper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_TYPE);
294     }
295
296     public String JavaDoc getServerInstanceID () {
297         if (fakeServerInstId != null)
298             return fakeServerInstId;
299         return helper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_INSTANCE);
300     }
301     
302     public void setServerInstanceID(String JavaDoc severInstanceID) {
303         WebProjectProperties.setServerInstance(project, helper, severInstanceID);
304     }
305     
306     public Iterator getArchiveContents () throws java.io.IOException JavaDoc {
307         return new IT (getContentDirectory ());
308     }
309
310     public FileObject getContentDirectory() {
311         return getFileObject ("build.web.dir"); //NOI18N
312
}
313
314     public FileObject getBuildDirectory() {
315         return getFileObject ("build.dir"); //NOI18N
316
}
317
318     public File JavaDoc getContentDirectoryAsFile() {
319         return getFile ("build.web.dir"); //NOI18N
320
}
321
322     public org.netbeans.modules.schema2beans.BaseBean getDeploymentDescriptor (String JavaDoc location) {
323         if (J2eeModule.WEB_XML.equals(location)){
324
325             WebApp webApp = getWebApp ();
326             if (webApp != null) {
327                 //PENDING find a better way to get the BB from WApp and remove the HACK from DDProvider!!
328
return DDProvider.getDefault ().getBaseBean (webApp);
329             }
330         }
331         else if(J2eeModule.WEBSERVICES_XML.equals(location)){
332             Webservices webServices = getWebservices();
333             if(webServices != null){
334                 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getBaseBean(webServices);
335             }
336         }
337         return null;
338     }
339
340     
341     public void uncacheDescriptors() {
342         // this.getConfigSupport().resetStorage();
343
// reset timeout when closing the project
344
notificationTimeout = 0;
345     }
346
347     private WebApp getWebApp () {
348         try {
349             FileObject deploymentDescriptor = getDeploymentDescriptor ();
350             if(deploymentDescriptor != null) {
351                 return DDProvider.getDefault ().getMergedDDRoot (deploymentDescriptor);
352             }
353         } catch (java.io.IOException JavaDoc e) {
354             org.openide.ErrorManager.getDefault ().log (e.getLocalizedMessage ());
355         }
356         return null;
357     }
358     
359     private Webservices getWebservices() {
360         if (Util.isJavaEE5orHigher(project)) {
361             WebServicesSupport wss = WebServicesSupport.getWebServicesSupport(project.getProjectDirectory());
362             try {
363                 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getMergedDDRoot(wss);
364             } catch (IOException JavaDoc ex) {
365                 ErrorManager.getDefault().notify(ex);
366             }
367         } else {
368             FileObject wsdd = getDD();
369             if(wsdd != null) {
370                 try {
371                     return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault()
372                     .getDDRoot(getDD());
373                 } catch (java.io.IOException JavaDoc e) {
374                     org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage());
375                 }
376             }
377         }
378         return null;
379     }
380     
381     public org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor getEjbChanges (long timestamp) {
382         return this;
383     }
384
385     public Object JavaDoc getModuleType () {
386         return J2eeModule.WAR;
387     }
388
389     public String JavaDoc getModuleVersion () {
390         WebApp wapp = getWebApp ();
391         String JavaDoc version = "2.5"; //NOI18N
392
if (wapp != null)
393             version = wapp.getVersion();
394         return version;
395     }
396
397     private Set versionListeners() {
398         if (versionListeners == null) {
399             versionListeners = new HashSet();
400             org.netbeans.modules.j2ee.dd.api.web.WebApp webApp = getWebApp();
401             if (webApp != null) {
402                 PropertyChangeListener JavaDoc l = (PropertyChangeListener JavaDoc) org.openide.util.WeakListeners.create(PropertyChangeListener JavaDoc.class, this, webApp);
403                 webApp.addPropertyChangeListener(l);
404             }
405         }
406         return versionListeners;
407     }
408
409     public void addVersionListener(J2eeModule.VersionListener vl) {
410         versionListeners().add(vl);
411     }
412
413     public void removeVersionListener(J2eeModule.VersionListener vl) {
414         if (versionListeners != null)
415             versionListeners.remove(vl);
416     }
417     
418     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
419         if (evt.getPropertyName().equals(org.netbeans.modules.j2ee.dd.api.web.WebApp.PROPERTY_VERSION)) {
420             for (Iterator i=versionListeners.iterator(); i.hasNext();) {
421                 J2eeModule.VersionListener vl = (J2eeModule.VersionListener) i.next();
422                 String JavaDoc oldVersion = (String JavaDoc) evt.getOldValue();
423                 String JavaDoc newVersion = (String JavaDoc) evt.getNewValue();
424                 vl.versionChanged(oldVersion, newVersion);
425             }
426         } else if (evt.getPropertyName ().equals (WebProjectProperties.J2EE_SERVER_INSTANCE)) {
427             Deployment d = Deployment.getDefault ();
428             String JavaDoc oldServerID = evt.getOldValue () == null ? null : d.getServerID ((String JavaDoc) evt.getOldValue ());
429             String JavaDoc newServerID = evt.getNewValue () == null ? null : d.getServerID ((String JavaDoc) evt.getNewValue ());
430             fireServerChange (oldServerID, newServerID);
431         } else if (WebProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) {
432             String JavaDoc oldValue = (String JavaDoc)evt.getOldValue();
433             String JavaDoc newValue = (String JavaDoc)evt.getNewValue();
434             firePropertyChange(
435                     PROP_ENTERPRISE_RESOURCE_DIRECTORY,
436                     oldValue == null ? null : new File JavaDoc(oldValue),
437                     newValue == null ? null : new File JavaDoc(newValue));
438         }
439     }
440         
441     public String JavaDoc getUrl () {
442          EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
443          String JavaDoc warName = ep.getProperty(WebProjectProperties.WAR_NAME);
444          return warName == null ? "" : ("/"+warName); //NOI18N
445
}
446
447     public boolean isManifestChanged (long timestamp) {
448         return false;
449     }
450
451     public void setUrl (String JavaDoc url) {
452         throw new UnsupportedOperationException JavaDoc ("Cannot customize URL of web module"); //NOI18N
453
}
454
455     public boolean ejbsChanged () {
456         return false;
457     }
458
459     public String JavaDoc[] getChangedEjbs () {
460         return new String JavaDoc[] {};
461     }
462
463     public String JavaDoc getJ2eePlatformVersion () {
464         return helper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_PLATFORM);
465     }
466     
467     public FileObject getDD() {
468        FileObject webInfFo = getWebInf();
469        if (webInfFo==null) {
470            showErrorMessage(NbBundle.getMessage(ProjectWebModule.class,"MSG_WebInfCorrupted"));
471            return null;
472        }
473        return getWebInf().getFileObject(WebServicesConstants.WEBSERVICES_DD, "xml"); // NOI18N
474
}
475     
476     public FileObject[] getSourceRoots() {
477         Sources sources = ProjectUtils.getSources(project);
478         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
479         
480         List roots = new LinkedList();
481         FileObject documentBase = getDocumentBase();
482         if (documentBase != null)
483             roots.add(documentBase);
484         
485         for (int i = 0; i < groups.length; i++) {
486             roots.add(groups[i].getRootFolder());
487         }
488         
489         FileObject[] rootArray = new FileObject[roots.size()];
490         return (FileObject[])roots.toArray(rootArray);
491     }
492     
493     private boolean isProjectOpened() {
494         // XXX workaround: OpenProjects.getDefault() can be null
495
// when called from ProjectOpenedHook.projectOpened() upon IDE startup
496
if (OpenProjects.getDefault() == null)
497             return true;
498         
499         Project[] projects = OpenProjects.getDefault().getOpenProjects();
500         for (int i = 0; i < projects.length; i++) {
501             if (projects[i].equals(project))
502                 return true;
503         }
504         return false;
505     }
506     
507     private static class IT implements Iterator {
508         ArrayList ch;
509         FileObject root;
510         
511         private IT (FileObject f) {
512             this.ch = new ArrayList ();
513             ch.add (f);
514             this.root = f;
515         }
516         
517         public boolean hasNext () {
518             return ! ch.isEmpty();
519         }
520         
521         public Object JavaDoc next () {
522             FileObject f = (FileObject) ch.get(0);
523             ch.remove(0);
524             if (f.isFolder()) {
525                 f.refresh();
526                 FileObject chArr[] = f.getChildren ();
527                 for (int i = 0; i < chArr.length; i++) {
528                     ch.add(chArr [i]);
529                 }
530             }
531             return new FSRootRE (root, f);
532         }
533         
534         public void remove () {
535             throw new UnsupportedOperationException JavaDoc ();
536         }
537         
538     }
539
540     private static final class FSRootRE implements J2eeModule.RootedEntry {
541         FileObject f;
542         FileObject root;
543         
544         FSRootRE (FileObject root, FileObject f) {
545             this.f = f;
546             this.root = root;
547         }
548         
549         public FileObject getFileObject () {
550             return f;
551         }
552         
553         public String JavaDoc getRelativePath () {
554             return FileUtil.getRelativePath (root, f);
555         }
556     }
557 }
558
Popular Tags