KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ProjectEar


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.earproject;
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.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import org.netbeans.api.java.project.JavaProjectConstants;
35 import org.netbeans.api.project.FileOwnerQuery;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ProjectUtils;
38 import org.netbeans.api.project.SourceGroup;
39 import org.netbeans.api.project.Sources;
40 import org.netbeans.modules.j2ee.api.ejbjar.Car;
41 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
42 import org.netbeans.modules.j2ee.dd.api.application.Application;
43 import org.netbeans.modules.j2ee.dd.api.application.DDProvider;
44 import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor;
45 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
46 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
47 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModuleContainer;
48 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter;
49 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleListener;
50 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider;
51 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
52 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
53 import org.netbeans.modules.j2ee.spi.ejbjar.EarImplementation;
54 import org.netbeans.modules.schema2beans.BaseBean;
55 import org.netbeans.modules.web.api.webmodule.WebModule;
56 import org.netbeans.spi.java.classpath.ClassPathProvider;
57 import org.netbeans.spi.project.support.ant.AntProjectHelper;
58 import org.openide.ErrorManager;
59 import org.openide.filesystems.FileObject;
60 import org.openide.filesystems.FileUtil;
61 import org.openide.util.WeakListeners;
62
63 /**
64  * An enterprise application project's j2eeserver implementation
65  *
66  * @see ProjectEar
67  * @author vince kraemer
68  */

69 public final class ProjectEar extends J2eeAppProvider
70         implements
71         J2eeModule,
72         ModuleChangeReporter,
73         EjbChangeDescriptor,
74         PropertyChangeListener JavaDoc,
75         EarImplementation,
76         J2eeModuleContainer {
77     
78     public static final String JavaDoc FILE_DD = "application.xml";//NOI18N
79

80     private final EarProject project;
81     private Set JavaDoc<J2eeModule.VersionListener> versionListeners;
82     
83     ProjectEar (EarProject project) { // ], AntProjectHelper helper) {
84
this.project = project;
85         AntProjectHelper helper = project.getAntProjectHelper();
86         helper.getStandardPropertyEvaluator().addPropertyChangeListener(this);
87     }
88     
89     public FileObject getDeploymentDescriptor() {
90         FileObject dd = null;
91         FileObject metaInf = getMetaInf();
92         if (metaInf != null) {
93             dd = metaInf.getFileObject(FILE_DD);
94             if (dd == null) {
95                 try {
96                     dd = EarProjectGenerator.setupDD(J2eeModule.JAVA_EE_5, metaInf, project);
97                 } catch (IOException JavaDoc ioe) {
98                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
99                 }
100             }
101         }
102         return dd;
103     }
104     
105     public FileObject getMetaInf() {
106         return project.getOrCreateMetaInfDir();
107     }
108     
109     public File JavaDoc getEnterpriseResourceDirectory() {
110         return project.getFile(EarProjectProperties.RESOURCE_DIR);
111     }
112
113     public ClassPathProvider getClassPathProvider () {
114         return (ClassPathProvider) project.getLookup ().lookup (ClassPathProvider.class);
115     }
116     
117     public FileObject getArchive () {
118         return project.getFileObject (EarProjectProperties.DIST_JAR); //NOI18N
119
}
120     
121     public J2eeModule getJ2eeModule () {
122         return this;
123     }
124     
125     public ModuleChangeReporter getModuleChangeReporter () {
126         return this;
127     }
128     
129     public boolean useDefaultServer () {
130         return false;
131     }
132     
133     public String JavaDoc getServerID () {
134         return project.getServerID(); //helper.getStandardPropertyEvaluator ().getProperty (EarProjectProperties.J2EE_SERVER_TYPE);
135
}
136     
137     public void setServerInstanceID(String JavaDoc severInstanceID) {
138         // TODO: implement when needed
139
}
140
141     public String JavaDoc getServerInstanceID () {
142         return project.getServerInstanceID(); //helper.getStandardPropertyEvaluator ().getProperty (EarProjectProperties.J2EE_SERVER_INSTANCE);
143
}
144     
145     public Iterator JavaDoc getArchiveContents () throws IOException JavaDoc {
146         return new IT (getContentDirectory ());
147     }
148
149     public FileObject getContentDirectory() {
150         return project.getFileObject (EarProjectProperties.BUILD_DIR); //NOI18N
151
}
152
153     public FileObject getBuildDirectory() {
154         return project.getFileObject (EarProjectProperties.BUILD_DIR); //NOI18N
155
}
156
157     public BaseBean getDeploymentDescriptor (String JavaDoc location) {
158         if (! J2eeModule.APP_XML.equals(location)) {
159             return null;
160         }
161         
162         Application earApp = getApplication();
163         if (earApp != null) {
164             //PENDING find a better way to get the BB from WApp and remove the HACK from DDProvider!!
165
return DDProvider.getDefault ().getBaseBean (earApp);
166         }
167         return null;
168     }
169
170     private Application getApplication () {
171         try {
172             return DDProvider.getDefault ().getDDRoot (getDeploymentDescriptor ());
173         } catch (IOException JavaDoc e) {
174             ErrorManager.getDefault ().log (e.getLocalizedMessage ());
175         }
176         return null;
177
178     }
179     
180     public EjbChangeDescriptor getEjbChanges (long timestamp) {
181         return this;
182     }
183
184     public Object JavaDoc getModuleType () {
185         return J2eeModule.EAR;
186     }
187
188     public String JavaDoc getModuleVersion () {
189         Application app = getApplication();
190         return (app == null) ? Application.VERSION_5 /* fallback */ : app.getVersion().toString();
191     }
192
193     private Set JavaDoc<J2eeModule.VersionListener> versionListeners() {
194         if (versionListeners == null) {
195             versionListeners = new HashSet JavaDoc<J2eeModule.VersionListener>();
196             Application app = getApplication();
197             if (app != null) {
198                 PropertyChangeListener JavaDoc l = (PropertyChangeListener JavaDoc) WeakListeners.create(PropertyChangeListener JavaDoc.class, this, app);
199                 app.addPropertyChangeListener(l);
200             }
201         }
202         return versionListeners;
203     }
204
205     public void addVersionListener(J2eeModule.VersionListener vl) {
206         try {
207             versionListeners().add(vl);
208         } catch (UnsupportedOperationException JavaDoc uoe) {
209             // XXX ignoring a UOE
210
}
211     }
212
213     public void removeVersionListener(J2eeModule.VersionListener vl) {
214         if (versionListeners != null) {
215             versionListeners.remove(vl);
216         }
217     }
218     
219     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
220         if (evt.getPropertyName().equals(Application.PROPERTY_VERSION)) {
221             for (J2eeModule.VersionListener vl : versionListeners) {
222                 String JavaDoc oldVersion = (String JavaDoc) evt.getOldValue();
223                 String JavaDoc newVersion = (String JavaDoc) evt.getNewValue();
224                 vl.versionChanged(oldVersion, newVersion);
225             }
226         } else if (EarProjectProperties.J2EE_SERVER_INSTANCE.equals(evt.getPropertyName())) {
227             Deployment d = Deployment.getDefault();
228             String JavaDoc oldServerID = evt.getOldValue() == null ? null : d.getServerID((String JavaDoc)evt.getOldValue ());
229             String JavaDoc newServerID = evt.getNewValue() == null ? null : d.getServerID((String JavaDoc)evt.getNewValue ());
230             fireServerChange (oldServerID, newServerID);
231         } else if (EarProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) {
232             String JavaDoc oldValue = (String JavaDoc)evt.getOldValue();
233             String JavaDoc newValue = (String JavaDoc)evt.getNewValue();
234             firePropertyChange(
235                     PROP_ENTERPRISE_RESOURCE_DIRECTORY,
236                     oldValue == null ? null : new File JavaDoc(oldValue),
237                     newValue == null ? null : new File JavaDoc(newValue));
238         }
239     }
240         
241     public String JavaDoc getUrl () {
242         return "";
243     }
244
245     public boolean isManifestChanged (long timestamp) {
246         return false;
247     }
248
249     public void setUrl (String JavaDoc url) {
250         throw new UnsupportedOperationException JavaDoc ("Cannot customize URL of web module"); // NOI18N
251
}
252
253     public boolean ejbsChanged () {
254         return false;
255     }
256
257     public String JavaDoc[] getChangedEjbs () {
258         return new String JavaDoc[] {};
259     }
260
261     public String JavaDoc getJ2eePlatformVersion () {
262         return project.getJ2eePlatformVersion(); // helper.getStandardPropertyEvaluator ().getProperty (EarProjectProperties.J2EE_PLATFORM);
263
}
264     
265     public FileObject[] getSourceRoots() {
266         Sources sources = ProjectUtils.getSources(project);
267         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
268         FileObject[] roots = new FileObject[groups.length+1];
269         roots[0] = getMetaInf();
270         for (int i=0; i < groups.length; i++) {
271             roots[i+1] = groups[i].getRootFolder();
272         }
273         
274         return roots;
275     }
276  
277     private static class IT implements Iterator JavaDoc {
278         Enumeration JavaDoc ch;
279         FileObject root;
280         
281         private IT (FileObject f) {
282             this.ch = f.getChildren (true);
283             this.root = f;
284         }
285         
286         public boolean hasNext () {
287             return ch.hasMoreElements ();
288         }
289         
290         public Object JavaDoc next () {
291             FileObject f = (FileObject) ch.nextElement ();
292             return new FSRootRE (root, f);
293         }
294         
295         public void remove () {
296             throw new UnsupportedOperationException JavaDoc ();
297         }
298         
299     }
300
301     private static final class FSRootRE implements J2eeModule.RootedEntry {
302         FileObject f;
303         FileObject root;
304         
305         FSRootRE (FileObject root, FileObject f) {
306             this.f = f;
307             this.root = root;
308         }
309         
310         public FileObject getFileObject () {
311             return f;
312         }
313         
314         public String JavaDoc getRelativePath () {
315             return FileUtil.getRelativePath (root, f);
316         }
317     }
318     
319     private Map JavaDoc<String JavaDoc, J2eeModuleProvider> mods = new HashMap JavaDoc<String JavaDoc, J2eeModuleProvider>();
320     
321     void setModules(Map JavaDoc<String JavaDoc, J2eeModuleProvider> mods) {
322         if (null == mods) {
323             throw new IllegalArgumentException JavaDoc("mods"); // NOI18N
324
}
325         this.mods = mods;
326     }
327     
328     public J2eeModule[] getModules(ModuleListener ml) {
329         if (null != ml) {
330             addModuleListener(ml);
331         }
332         J2eeModule[] retVal = new J2eeModule[mods.size()];
333         int i = 0;
334         for (J2eeModuleProvider provider : mods.values()) {
335             retVal[i++] = provider.getJ2eeModule();
336         }
337         return retVal;
338     }
339     
340     public void addModuleProvider(J2eeModuleProvider jmp, String JavaDoc uri) {
341         mods.put(uri, jmp);
342         J2eeModule jm = jmp.getJ2eeModule();
343         fireAddModule(jm);
344     }
345     
346     public void removeModuleProvider(J2eeModuleProvider jmp, String JavaDoc uri) {
347         // J2eeModuleProvider tmp = (J2eeModuleProvider) mods.get(uri);
348
// if (!tmp.equals(jmp)) {
349
// something fishy may be happening here
350
// XXX log it
351
// }
352
J2eeModule jm = jmp.getJ2eeModule();
353         fireRemoveModule(jm);
354         mods.remove(uri);
355     }
356     
357     private final List JavaDoc<ModuleListener> modListeners = new ArrayList JavaDoc<ModuleListener>();
358     
359     private void fireAddModule(J2eeModule jm) {
360         for (ModuleListener ml : modListeners) {
361             try {
362                 ml.addModule(jm);
363             } catch (RuntimeException JavaDoc rex) {
364                 ErrorManager.getDefault().log(rex.getLocalizedMessage());
365             }
366         }
367     }
368
369     private void fireRemoveModule(J2eeModule jm) {
370         for (ModuleListener ml : modListeners) {
371             try {
372                 ml.removeModule(jm);
373             } catch (RuntimeException JavaDoc rex) {
374                 ErrorManager.getDefault().log(rex.getLocalizedMessage());
375             }
376         }
377     }
378     
379     public void addModuleListener(ModuleListener ml) {
380         modListeners.add(ml);
381     }
382     
383     public void removeModuleListener(ModuleListener ml){
384         modListeners.remove(ml);
385     }
386     
387     /**
388      * Returns the provider for the child module specified by given URI.
389      * @param uri the child module URI within the J2EE application.
390      * @return J2eeModuleProvider object
391      */

392     public J2eeModuleProvider getChildModuleProvider(String JavaDoc uri) {
393         return mods.get(uri);
394     }
395     
396     /**
397      * Returns list of providers of every child J2EE module of this J2EE app.
398      * @return array of J2eeModuleProvider objects.
399      */

400     public J2eeModuleProvider[] getChildModuleProviders() {
401         return (J2eeModuleProvider[]) mods.values().toArray(new J2eeModuleProvider[mods.size()]);
402     }
403     
404     public File JavaDoc getDeploymentConfigurationFile(String JavaDoc name) {
405         FileObject moduleFolder = getMetaInf();
406         File JavaDoc configFolder = FileUtil.toFile(moduleFolder);
407         return new File JavaDoc(configFolder, name);
408     }
409
410     public FileObject findDeploymentConfigurationFile (String JavaDoc name) {
411         FileObject moduleFolder = getMetaInf();
412         return moduleFolder == null ? null : moduleFolder.getFileObject(name);
413     }
414     
415     public void addEjbJarModule(EjbJar module) {
416         FileObject childFO = module.getDeploymentDescriptor();
417         if (childFO == null) {
418             childFO = module.getMetaInf();
419         }
420         addModule(childFO);
421     }
422     
423     public void addWebModule(WebModule module) {
424         FileObject childFO = module.getDeploymentDescriptor();
425         if (childFO == null) {
426             childFO = module.getWebInf();
427         }
428         addModule(childFO);
429     }
430     
431     public void addCarModule(Car module) {
432         FileObject childFO = module.getDeploymentDescriptor();
433         if (childFO == null) {
434             childFO = module.getMetaInf();
435         }
436         addModule(childFO);
437     }
438
439     private void addModule(final FileObject childFO) {
440         Project owner = null;
441         if (childFO != null) {
442             owner = FileOwnerQuery.getOwner(childFO);
443         }
444         if (owner == null) {
445             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
446                     "Unable to add module to the Enterpise Application. Owner project not found."); // NOI18N
447
} else {
448             ((EarProjectProperties)project.getProjectProperties()).addJ2eeSubprojects(new Project [] {owner});
449         }
450     }
451     
452 }
453
Popular Tags