KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > EjbJarProvider


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.ejbjarproject;
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.classpath.ClassPath;
29 import org.netbeans.api.java.project.JavaProjectConstants;
30 import org.netbeans.api.project.*;
31 import org.netbeans.api.project.ui.OpenProjects;
32 import org.netbeans.modules.j2ee.metadata.ClassPathSupport;
33 import org.netbeans.modules.j2ee.common.Util;
34 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
35 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
37 import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor;
38 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
39 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
40 import org.netbeans.spi.project.support.ant.AntProjectHelper;
41 import org.openide.ErrorManager;
42 import org.openide.filesystems.FileObject;
43 import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
44 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
45 import org.netbeans.spi.java.classpath.ClassPathProvider;
46 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarImplementation;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.NotifyDescriptor;
49 import org.openide.DialogDisplayer;
50 import org.openide.util.NbBundle;
51 import org.netbeans.spi.project.support.ant.EditableProperties;
52 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
53 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathProviderImpl;
54 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
55
56
57 /** A ejb module implementation on top of project.
58  *
59  * @author Pavel Buzek
60  */

61 public final class EjbJarProvider extends J2eeModuleProvider
62         implements EjbJarImplementation, J2eeModule, ModuleChangeReporter, EjbChangeDescriptor, PropertyChangeListener JavaDoc {
63     
64     public static final String JavaDoc FILE_DD = "ejb-jar.xml";//NOI18N
65

66     private final EjbJarProject project;
67     private final AntProjectHelper helper;
68     private Set versionListeners;
69     private MetadataUnit metadataUnit;
70     private ClassPath metadataClassPath;
71     
72     private long notificationTimeout = 0; // used to suppress repeating the same messages
73

74     EjbJarProvider(EjbJarProject project, AntProjectHelper helper) {
75         this.project = project;
76         this.helper = helper;
77         project.evaluator().addPropertyChangeListener (this);
78     }
79     
80     public FileObject getDeploymentDescriptor() {
81         FileObject ddFO = null;
82         FileObject metaInfFo = getMetaInf();
83         if (metaInfFo != null) {
84             ddFO = metaInfFo.getFileObject(FILE_DD);
85         }
86         if (ddFO == null && !EjbJarProjectProperties.JAVA_EE_5.equals(getJ2eePlatformVersion())) {
87             // ...generate the DD from template...
88
}
89         return ddFO;
90     }
91     
92     /** @deprecated use getJavaSources */
93     public ClassPath getClassPath() {
94         ClassPathProvider cpp = (ClassPathProvider) project.getLookup().lookup(ClassPathProvider.class);
95         if (cpp != null) {
96             return cpp.findClassPath(getFileObject(EjbJarProjectProperties.SRC_DIR), ClassPath.SOURCE);
97         }
98         return null;
99     }
100
101     public FileObject[] getJavaSources() {
102         return project.getSourceRoots().getRoots();
103     }
104     
105     public FileObject getMetaInf() {
106         FileObject metaInf = getFileObject(EjbJarProjectProperties.META_INF);
107         if (metaInf == null) {
108             String JavaDoc version = project.getAPIEjbJar().getJ2eePlatformVersion();
109             if (needConfigurationFolder(version)) {
110                 String JavaDoc relativePath = helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.META_INF);
111                 String JavaDoc path = (relativePath != null ? helper.resolvePath(relativePath) : "");
112                 showErrorMessage(NbBundle.getMessage(EjbJarProvider.class,"MSG_MetaInfCorrupted", project.getName(), path));
113             }
114         }
115         return metaInf;
116     }
117     
118     /** Package-private for unit test only. */
119     static boolean needConfigurationFolder(final String JavaDoc version) {
120         return EjbJarProjectProperties.J2EE_1_3.equals(version) ||
121                 EjbJarProjectProperties.J2EE_1_4.equals(version);
122     }
123     
124     public File JavaDoc getMetaInfAsFile() {
125         return getFile(EjbJarProjectProperties.META_INF);
126     }
127
128     public File JavaDoc getEnterpriseResourceDirectory() {
129         return getFile(EjbJarProjectProperties.RESOURCE_DIR);
130     }
131
132     public FileObject findDeploymentConfigurationFile(String JavaDoc name) {
133         FileObject metaInf = getMetaInf();
134         if (metaInf == null) {
135             return null;
136         }
137         return metaInf.getFileObject(name);
138     }
139     
140     public File JavaDoc getDeploymentConfigurationFile(String JavaDoc name) {
141         return new File JavaDoc(getMetaInfAsFile(), name);
142     }
143     
144     public ClassPathProvider getClassPathProvider() {
145         return (ClassPathProvider) project.getLookup().lookup(ClassPathProvider.class);
146     }
147     
148     public FileObject getArchive() {
149         return getFileObject(EjbJarProjectProperties.DIST_JAR);
150     }
151     
152     private FileObject getFileObject(String JavaDoc propname) {
153         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
154         if (prop != null) {
155             return helper.resolveFileObject(prop);
156         }
157         
158         return null;
159     }
160     
161     private File JavaDoc getFile(String JavaDoc propname) {
162         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
163         if (prop != null) {
164             return helper.resolveFile(prop);
165         }
166         return null;
167     }
168     
169     public org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule getJ2eeModule() {
170         return this;
171     }
172     
173     public org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter getModuleChangeReporter() {
174         return this;
175     }
176     
177     public boolean useDefaultServer() {
178         return false;
179     }
180     
181     public String JavaDoc getServerID() {
182         return helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.J2EE_SERVER_TYPE);
183     }
184     
185     public String JavaDoc getServerInstanceID() {
186         return helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.J2EE_SERVER_INSTANCE);
187     }
188     
189     public void setServerInstanceID(String JavaDoc serverInstanceID) {
190         assert serverInstanceID != null : "passed serverInstanceID cannot be null";
191         EjbJarProjectProperties.setServerInstance(project, helper, serverInstanceID);
192     }
193     
194     public Iterator getArchiveContents() throws java.io.IOException JavaDoc {
195         return new IT(getContentDirectory());
196     }
197     
198     public FileObject getContentDirectory() {
199         return getFileObject(EjbJarProjectProperties.BUILD_CLASSES_DIR);
200     }
201     
202     public FileObject getBuildDirectory() {
203         return getFileObject(EjbJarProjectProperties.BUILD_DIR);
204     }
205     
206     public File JavaDoc getContentDirectoryAsFile() {
207         return getFile(EjbJarProjectProperties.BUILD_CLASSES_DIR);
208     }
209     
210     public org.netbeans.modules.schema2beans.BaseBean getDeploymentDescriptor(String JavaDoc location) {
211         if (J2eeModule.EJBJAR_XML.equals(location)){
212             EjbJar ejbJar = getEjbJar();
213             if (ejbJar != null) {
214                 //PENDING find a better way to get the BB from WApp and remove the HACK from DDProvider!!
215
return DDProvider.getDefault().getBaseBean(ejbJar);
216             }
217         }
218         else if(J2eeModule.EJBSERVICES_XML.equals(location)){
219             Webservices webServices = getWebservices();
220             if(webServices != null){
221                 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getBaseBean(webServices);
222             }
223         }
224         return null;
225     }
226     
227     private EjbJar getEjbJar() {
228         try {
229             org.netbeans.modules.j2ee.api.ejbjar.EjbJar apiEjbJar = project.getAPIEjbJar();
230             if (apiEjbJar.getDeploymentDescriptor() == null && !EjbJarProjectProperties.JAVA_EE_5.equals(apiEjbJar.getJ2eePlatformVersion())) {
231                 return null;
232             } else {
233                 return DDProvider.getDefault().getMergedDDRoot(apiEjbJar.getMetadataUnit());
234             }
235         } catch (java.io.IOException JavaDoc e) {
236             org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage());
237         }
238         return null;
239     }
240     
241     private Webservices getWebservices() {
242         if (Util.isJavaEE5orHigher(project)) {
243             WebServicesSupport wss = WebServicesSupport.getWebServicesSupport(project.getProjectDirectory());
244             try {
245                 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getMergedDDRoot(wss);
246             } catch (IOException JavaDoc ex) {
247                 ErrorManager.getDefault().notify(ex);
248             }
249         } else {
250             FileObject wsdd = getDD();
251             if(wsdd != null) {
252                 try {
253                     return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(wsdd);
254                 } catch (java.io.IOException JavaDoc e) {
255                     org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage());
256                 }
257             }
258         }
259         return null;
260     }
261     
262     public FileObject getDD() {
263         FileObject metaInfFo = getMetaInf();
264         if (metaInfFo==null) {
265             return null;
266         }
267         return metaInfFo.getFileObject(WebServicesConstants.WEBSERVICES_DD, "xml"); // NOI18N
268
}
269     
270     public org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor getEjbChanges(long timestamp) {
271         return this;
272     }
273     
274     public Object JavaDoc getModuleType() {
275         return J2eeModule.EJB;
276     }
277     
278     public String JavaDoc getModuleVersion() {
279         EjbJar ejbJar = getEjbJar();
280         return (ejbJar == null) ? EjbJar.VERSION_2_1 /* fallback */ : ejbJar.getVersion().toString();
281     }
282     
283     private Set versionListeners() {
284         if (versionListeners == null) {
285             versionListeners = new HashSet();
286             org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJar = getEjbJar();
287             if (ejbJar != null) {
288                 PropertyChangeListener JavaDoc l = (PropertyChangeListener JavaDoc) org.openide.util.WeakListeners.create(PropertyChangeListener JavaDoc.class, this, ejbJar);
289                 ejbJar.addPropertyChangeListener(l);
290             }
291         }
292         return versionListeners;
293     }
294     
295     public void addVersionListener(J2eeModule.VersionListener vl) {
296         versionListeners().add(vl);
297     }
298     
299     public void removeVersionListener(J2eeModule.VersionListener vl) {
300         if (versionListeners != null) {
301             versionListeners.remove(vl);
302         }
303     }
304     
305     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
306         if (evt.getPropertyName().equals(org.netbeans.modules.j2ee.dd.api.ejb.EjbJar.PROPERTY_VERSION)) {
307             for (Iterator i=versionListeners.iterator(); i.hasNext();) {
308                 J2eeModule.VersionListener vl = (J2eeModule.VersionListener) i.next();
309                 String JavaDoc oldVersion = (String JavaDoc) evt.getOldValue();
310                 String JavaDoc newVersion = (String JavaDoc) evt.getNewValue();
311                 vl.versionChanged(oldVersion, newVersion);
312             }
313         } else if (evt.getPropertyName ().equals (EjbJarProjectProperties.J2EE_SERVER_INSTANCE)) {
314             Deployment d = Deployment.getDefault ();
315             String JavaDoc oldServerID = evt.getOldValue () == null ? null : d.getServerID ((String JavaDoc) evt.getOldValue ());
316             String JavaDoc newServerID = evt.getNewValue () == null ? null : d.getServerID ((String JavaDoc) evt.getNewValue ());
317             fireServerChange (oldServerID, newServerID);
318         } else if (EjbJarProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) {
319             String JavaDoc oldValue = (String JavaDoc)evt.getOldValue();
320             String JavaDoc newValue = (String JavaDoc)evt.getNewValue();
321             firePropertyChange(
322                     PROP_ENTERPRISE_RESOURCE_DIRECTORY,
323                     oldValue == null ? null : new File JavaDoc(oldValue),
324                     newValue == null ? null : new File JavaDoc(newValue));
325         }
326     }
327     
328     public String JavaDoc getUrl() {
329         EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
330         String JavaDoc name = ep.getProperty(EjbJarProjectProperties.JAR_NAME);
331         return name == null ? "" : ('/' + name);
332     }
333     
334     public boolean isManifestChanged(long timestamp) {
335         return false;
336     }
337     
338     public void setUrl(String JavaDoc url) {
339         throw new UnsupportedOperationException JavaDoc("Cannot customize URL of EJB module"); // NOI18N
340
}
341     
342     public boolean ejbsChanged() {
343         return false;
344     }
345     
346     public String JavaDoc[] getChangedEjbs() {
347         return new String JavaDoc[] {};
348     }
349     
350     public String JavaDoc getJ2eePlatformVersion() {
351         return helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.J2EE_PLATFORM);
352     }
353
354     public MetadataUnit getMetadataUnit() {
355         synchronized (this) {
356             if (metadataUnit == null) {
357                 metadataUnit = new MetadataUnitImpl();
358             }
359             return metadataUnit;
360         }
361     }
362     
363     public FileObject[] getSourceRoots() {
364         Sources sources = ProjectUtils.getSources(project);
365         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
366         List roots = new LinkedList();
367         FileObject metaInf = getMetaInf();
368         if (metaInf != null) {
369             roots.add(metaInf);
370         }
371         
372         for (int i = 0; i < groups.length; i++) {
373             roots.add(groups[i].getRootFolder());
374         }
375         FileObject[] rootArray = new FileObject[roots.size()];
376         return (FileObject[])roots.toArray(rootArray);
377     }
378     
379     private void showErrorMessage(final String JavaDoc message) {
380         // only display the messages if the project is open
381
if(new Date().getTime() > notificationTimeout && isProjectOpen()) {
382             // DialogDisplayer waits for the AWT thread, blocking the calling
383
// thread -- deadlock-prone, see issue #64888. therefore invoking
384
// only in the AWT thread
385
Runnable JavaDoc r = new Runnable JavaDoc() {
386                 public void run() {
387                     if (!SwingUtilities.isEventDispatchThread()) {
388                         SwingUtilities.invokeLater(this);
389                     } else {
390                         DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
391                     }
392                 }
393             };
394             r.run();
395             
396             // set timeout to suppress the same messages during next 20 seconds (feel free to adjust the timeout
397
// using more suitable value)
398
notificationTimeout = new Date().getTime() + 20000;
399         }
400     }
401     
402     private boolean isProjectOpen() {
403         // OpenProjects.getDefault() is null when this method is called upon
404
// IDE startup from the project's impl of ProjectOpenHook
405
if (OpenProjects.getDefault() != null) {
406             Project[] projects = OpenProjects.getDefault().getOpenProjects();
407             for (int i = 0; i < projects.length; i++) {
408                 if (projects[i].equals(project)) {
409                     return true;
410                 }
411             }
412             return false;
413         } else {
414             // be conservative -- don't know anything about the project
415
// so consider it open
416
return true;
417         }
418     }
419
420     private ClassPath getMetadataClassPath() {
421         synchronized (this) {
422             if (metadataClassPath == null) {
423                 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class);
424                 metadataClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] {
425                     cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE),
426                     cpProvider.getJ2eePlatformClassPath(),
427                 });
428             }
429             return metadataClassPath;
430         }
431     }
432
433     private class MetadataUnitImpl implements MetadataUnit {
434         public ClassPath getClassPath() {
435             return getMetadataClassPath();
436         }
437         public FileObject getDeploymentDescriptor() {
438             return EjbJarProvider.this.getDeploymentDescriptor();
439         }
440     }
441
442     private static class IT implements Iterator {
443         java.util.Enumeration JavaDoc ch;
444         FileObject root;
445         
446         private IT(FileObject f) {
447             this.ch = f.getChildren(true);
448             this.root = f;
449         }
450         
451         public boolean hasNext() {
452             return ch.hasMoreElements();
453         }
454         
455         public Object JavaDoc next() {
456             FileObject f = (FileObject) ch.nextElement();
457             return new FSRootRE(root, f);
458         }
459         
460         public void remove() {
461             throw new UnsupportedOperationException JavaDoc();
462         }
463         
464     }
465     
466     private static final class FSRootRE implements J2eeModule.RootedEntry {
467         FileObject f;
468         FileObject root;
469         
470         FSRootRE(FileObject root, FileObject f) {
471             this.f = f;
472             this.root = root;
473         }
474         
475         public FileObject getFileObject() {
476             return f;
477         }
478         
479         public String JavaDoc getRelativePath() {
480             return FileUtil.getRelativePath(root, f);
481         }
482     }
483 }
484
Popular Tags