KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > AppClientProvider


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.j2ee.clientproject;
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.Date JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import org.netbeans.api.java.project.JavaProjectConstants;
35 import org.netbeans.api.project.Project;
36 import org.netbeans.api.project.ProjectUtils;
37 import org.netbeans.api.project.SourceGroup;
38 import org.netbeans.api.project.Sources;
39 import org.netbeans.api.project.ui.OpenProjects;
40 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
41 //import org.netbeans.modules.j2ee.common.Util;
42
import org.netbeans.modules.j2ee.dd.api.client.AppClient;
43 import org.netbeans.modules.j2ee.dd.api.client.DDProvider;
44 //import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
45
import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor;
46 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
47 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
48 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter;
49 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
50 import org.netbeans.modules.j2ee.spi.ejbjar.CarImplementation;
51 import org.netbeans.modules.schema2beans.BaseBean;
52 //import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
53
import org.netbeans.modules.websvc.api.client.WebServicesClientConstants;
54 import org.netbeans.spi.java.classpath.ClassPathProvider;
55 import org.netbeans.spi.project.support.ant.AntProjectHelper;
56 import org.netbeans.spi.project.support.ant.EditableProperties;
57 import org.openide.DialogDisplayer;
58 //import org.openide.ErrorManager;
59
import org.openide.NotifyDescriptor;
60 import org.openide.filesystems.FileObject;
61 import org.openide.filesystems.FileUtil;
62 import org.openide.util.NbBundle;
63 import org.openide.util.WeakListeners;
64
65 /**
66  * @author jungi
67  */

68 public final class AppClientProvider extends J2eeModuleProvider
69         implements CarImplementation, J2eeModule, ModuleChangeReporter, EjbChangeDescriptor, PropertyChangeListener JavaDoc {
70     
71     public static final String JavaDoc FILE_DD = "application-client.xml";//NOI18N
72

73     private final AppClientProject project;
74     private final AntProjectHelper helper;
75     private Set JavaDoc<J2eeModule.VersionListener> versionListeners;
76     
77     private long notificationTimeout = 0; // used to suppress repeating the same messages
78

79     AppClientProvider(AppClientProject project, AntProjectHelper helper) {
80         this.project = project;
81         this.helper = helper;
82         project.evaluator().addPropertyChangeListener(this);
83     }
84     
85     public FileObject getDeploymentDescriptor() {
86         FileObject metaInfFo = getMetaInf();
87         if (metaInfFo == null) {
88             return null;
89         }
90         return metaInfFo.getFileObject(FILE_DD);
91     }
92     
93     public FileObject[] getJavaSources() {
94         return project.getSourceRoots().getRoots();
95     }
96     
97     public FileObject getMetaInf() {
98         FileObject metaInf = getFileObject(AppClientProjectProperties.META_INF);
99         if (metaInf == null) {
100             String JavaDoc version = project.getAPICar().getJ2eePlatformVersion();
101             if (needConfigurationFolder(version)) {
102                 String JavaDoc relativePath = helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.META_INF);
103                 String JavaDoc path = (relativePath != null ? helper.resolvePath(relativePath) : "");
104                 showErrorMessage(NbBundle.getMessage(AppClientProvider.class, "MSG_MetaInfCorrupted", project.getName(), path));
105             }
106         }
107         return metaInf;
108     }
109     
110     /** Package-private for unit test only. */
111     static boolean needConfigurationFolder(final String JavaDoc version) {
112         return AppClientProjectProperties.J2EE_1_3.equals(version) ||
113                 AppClientProjectProperties.J2EE_1_4.equals(version);
114     }
115     
116     public File JavaDoc getMetaInfAsFile() {
117         return getFile(AppClientProjectProperties.META_INF);
118     }
119     
120     public File JavaDoc getEnterpriseResourceDirectory() {
121         return getFile(AppClientProjectProperties.RESOURCE_DIR);
122     }
123     
124     public FileObject findDeploymentConfigurationFile(String JavaDoc name) {
125         FileObject metaInf = getMetaInf();
126         if (metaInf == null) {
127             return null;
128         }
129         return metaInf.getFileObject(name);
130     }
131     
132     public File JavaDoc getDeploymentConfigurationFile(String JavaDoc name) {
133         return new File JavaDoc(getMetaInfAsFile(), name);
134     }
135     
136     public ClassPathProvider getClassPathProvider() {
137         return (ClassPathProvider) project.getLookup().lookup(ClassPathProvider.class);
138     }
139     
140     public FileObject getArchive() {
141         return getFileObject(AppClientProjectProperties.DIST_JAR);
142     }
143     
144     private FileObject getFileObject(String JavaDoc propname) {
145         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
146         if (prop != null) {
147             return helper.resolveFileObject(prop);
148         }
149         
150         return null;
151     }
152     
153     private File JavaDoc getFile(String JavaDoc propname) {
154         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
155         if (prop != null) {
156             return helper.resolveFile(prop);
157         }
158         return null;
159     }
160     
161     public J2eeModule getJ2eeModule() {
162         return this;
163     }
164     
165     public ModuleChangeReporter getModuleChangeReporter() {
166         return this;
167     }
168     
169     public boolean useDefaultServer() {
170         return true;
171     }
172     
173     public String JavaDoc getServerID() {
174         return helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.J2EE_SERVER_TYPE);
175     }
176     
177     public String JavaDoc getServerInstanceID() {
178         return helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.J2EE_SERVER_INSTANCE);
179     }
180     
181     public void setServerInstanceID(String JavaDoc serverInstanceID) {
182         assert serverInstanceID != null : "passed serverInstanceID cannot be null";
183         AppClientProjectProperties.setServerInstance(project, helper, serverInstanceID);
184     }
185     
186     public Iterator JavaDoc getArchiveContents() throws IOException JavaDoc {
187         return new IT(getContentDirectory());
188     }
189     
190     public FileObject getContentDirectory() {
191         return getFileObject(AppClientProjectProperties.BUILD_CLASSES_DIR);
192     }
193     
194     public FileObject getBuildDirectory() {
195         return getFileObject(AppClientProjectProperties.BUILD_DIR);
196     }
197     
198     public File JavaDoc getContentDirectoryAsFile() {
199         return getFile(AppClientProjectProperties.BUILD_CLASSES_DIR);
200     }
201     
202     public BaseBean getDeploymentDescriptor(String JavaDoc location) {
203         if (J2eeModule.CLIENT_XML.equals(location)){
204             AppClient appClient = Utils.getAppClient(project);
205             if (appClient != null) {
206                 //PENDING find a better way to get the BB from WApp and remove the HACK from DDProvider!!
207
return DDProvider.getDefault().getBaseBean(appClient);
208             }
209         }/* else if(J2eeModule.EJBSERVICES_XML.equals(location)){
210             Webservices webServices = getWebservices();
211             if(webServices != null){
212                 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getBaseBean(webServices);
213             }
214         }*/

215         return null;
216     }
217     /*
218     private Webservices getWebservices() {
219         if (Util.isJavaEE5orHigher(project)) {
220             WebServicesSupport wss = WebServicesSupport.getWebServicesSupport(project.getProjectDirectory());
221             try {
222                 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getMergedDDRoot(wss);
223             } catch (IOException ex) {
224                 ErrorManager.getDefault().notify(ex);
225             }
226         } else {
227             FileObject wsdd = getDD();
228             if(wsdd != null) {
229                 try {
230                     return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault()
231                             .getDDRoot(getDD());
232                 } catch (IOException e) {
233                     ErrorManager.getDefault().log(e.getLocalizedMessage());
234                 }
235             }
236         }
237         
238         return null;
239     }
240     */

241     public FileObject getDD() {
242         FileObject metaInfFo = getMetaInf();
243         if (metaInfFo==null) {
244             return null;
245         }
246         return metaInfFo.getFileObject(WebServicesClientConstants.WEBSERVICES_DD, "xml"); // NOI18N
247
}
248     
249     public EjbChangeDescriptor getEjbChanges(long timestamp) {
250         return this;
251     }
252     
253     public Object JavaDoc getModuleType() {
254         return J2eeModule.CLIENT;
255     }
256     
257     public String JavaDoc getModuleVersion() {
258         AppClient ac = Utils.getAppClient(project);
259         return (ac == null) ? AppClient.VERSION_1_4 /* fallback */ : ac.getVersion().toString();
260     }
261     
262     private Set JavaDoc<J2eeModule.VersionListener> versionListeners() {
263         if (versionListeners == null) {
264             versionListeners = new HashSet JavaDoc<J2eeModule.VersionListener>();
265             AppClient appClient = Utils.getAppClient(project);
266             if (appClient != null) {
267                 PropertyChangeListener JavaDoc l = (PropertyChangeListener JavaDoc) WeakListeners.create(PropertyChangeListener JavaDoc.class, this, appClient);
268                 appClient.addPropertyChangeListener(l);
269             }
270         }
271         return versionListeners;
272     }
273     
274     public void addVersionListener(J2eeModule.VersionListener vl) {
275         versionListeners().add(vl);
276     }
277     
278     public void removeVersionListener(J2eeModule.VersionListener vl) {
279         if (versionListeners != null) {
280             versionListeners.remove(vl);
281         }
282     }
283     
284     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
285         if (evt.getPropertyName().equals(AppClient.PROPERTY_VERSION)) {
286             for (J2eeModule.VersionListener vl : versionListeners) {
287                 String JavaDoc oldVersion = (String JavaDoc) evt.getOldValue();
288                 String JavaDoc newVersion = (String JavaDoc) evt.getNewValue();
289                 vl.versionChanged(oldVersion, newVersion);
290             }
291         } else if (evt.getPropertyName().equals(AppClientProjectProperties.J2EE_SERVER_INSTANCE)) {
292             Deployment d = Deployment.getDefault();
293             String JavaDoc oldServerID = evt.getOldValue() == null ? null : d.getServerID((String JavaDoc) evt.getOldValue());
294             String JavaDoc newServerID = evt.getNewValue() == null ? null : d.getServerID((String JavaDoc) evt.getNewValue());
295             fireServerChange(oldServerID, newServerID);
296         } else if (AppClientProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) {
297             String JavaDoc oldValue = (String JavaDoc)evt.getOldValue();
298             String JavaDoc newValue = (String JavaDoc)evt.getNewValue();
299             firePropertyChange(
300                     PROP_ENTERPRISE_RESOURCE_DIRECTORY,
301                     oldValue == null ? null : new File JavaDoc(oldValue),
302                     newValue == null ? null : new File JavaDoc(newValue));
303         }
304     }
305     
306     public String JavaDoc getUrl() {
307         EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
308         String JavaDoc name = ep.getProperty(AppClientProjectProperties.JAR_NAME);
309         return name == null ? "" : ('/' + name);
310     }
311     
312     public boolean isManifestChanged(long timestamp) {
313         return false;
314     }
315     
316     public void setUrl(String JavaDoc url) {
317         throw new UnsupportedOperationException JavaDoc("Cannot customize URL of Application Client module"); // NOI18N
318
}
319     
320     public boolean ejbsChanged() {
321         return false;
322     }
323     
324     public String JavaDoc[] getChangedEjbs() {
325         return new String JavaDoc[] {};
326     }
327     
328     public String JavaDoc getJ2eePlatformVersion() {
329         return helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.J2EE_PLATFORM);
330     }
331     
332     public FileObject[] getSourceRoots() {
333         Sources sources = ProjectUtils.getSources(project);
334         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
335         List JavaDoc<FileObject> roots = new LinkedList JavaDoc<FileObject>();
336         FileObject metaInf = getMetaInf();
337         if (metaInf != null) {
338             roots.add(metaInf);
339         }
340         for (int i = 0; i < groups.length; i++) {
341             roots.add(groups[i].getRootFolder());
342         }
343         FileObject[] rootArray = new FileObject[roots.size()];
344         return roots.toArray(rootArray);
345     }
346     
347     private void showErrorMessage(final String JavaDoc message) {
348         // only display the messages if the project is open
349
if(new Date JavaDoc().getTime() > notificationTimeout && isProjectOpen()) {
350             // DialogDisplayer waits for the AWT thread, blocking the calling
351
// thread -- deadlock-prone, see issue #64888. therefore invoking
352
// only in the AWT thread
353
Runnable JavaDoc r = new Runnable JavaDoc() {
354                 public void run() {
355                     if (!SwingUtilities.isEventDispatchThread()) {
356                         SwingUtilities.invokeLater(this);
357                     } else {
358                         DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
359                     }
360                 }
361             };
362             r.run();
363             
364             // set timeout to suppress the same messages during next 20 seconds (feel free to adjust the timeout
365
// using more suitable value)
366
notificationTimeout = new Date JavaDoc().getTime() + 20000;
367         }
368     }
369     
370     private boolean isProjectOpen() {
371         // OpenProjects.getDefault() is null when this method is called upon
372
// IDE startup from the project's impl of ProjectOpenHook
373
if (OpenProjects.getDefault() != null) {
374             Project[] projects = OpenProjects.getDefault().getOpenProjects();
375             for (int i = 0; i < projects.length; i++) {
376                 if (projects[i].equals(project)) {
377                     return true;
378                 }
379             }
380             return false;
381         } else {
382             // be conservative -- don't know anything about the project
383
// so consider it open
384
return true;
385         }
386     }
387     
388     private static class IT implements Iterator JavaDoc {
389         
390         Enumeration JavaDoc ch;
391         FileObject root;
392         
393         private IT(FileObject f) {
394             this.ch = f.getChildren(true);
395             this.root = f;
396         }
397         
398         public boolean hasNext() {
399             return ch.hasMoreElements();
400         }
401         
402         public Object JavaDoc next() {
403             FileObject f = (FileObject) ch.nextElement();
404             return new FSRootRE(root, f);
405         }
406         
407         public void remove() {
408             throw new UnsupportedOperationException JavaDoc();
409         }
410         
411     }
412     
413     private static final class FSRootRE implements J2eeModule.RootedEntry {
414         
415         FileObject f;
416         FileObject root;
417         
418         FSRootRE(FileObject root, FileObject f) {
419             this.f = f;
420             this.root = root;
421         }
422         
423         public FileObject getFileObject() {
424             return f;
425         }
426         
427         public String JavaDoc getRelativePath() {
428             return FileUtil.getRelativePath(root, f);
429         }
430         
431     }
432     
433 }
434
Popular Tags