KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > EJBModules


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.ejbfreeform;
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 org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
32 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
33 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarFactory;
34 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarImplementation;
35 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarProvider;
36 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarsInProject;
37 import org.netbeans.spi.java.classpath.ClassPathProvider;
38 import org.w3c.dom.Element JavaDoc;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.filesystems.FileObject;
41 import org.netbeans.spi.project.AuxiliaryConfiguration;
42 import org.netbeans.api.java.classpath.ClassPath;
43 import org.netbeans.api.java.project.JavaProjectConstants;
44 import org.netbeans.api.java.queries.SourceForBinaryQuery;
45 import org.netbeans.api.project.FileOwnerQuery;
46 import org.netbeans.api.project.Project;
47 import org.netbeans.api.project.ProjectUtils;
48 import org.netbeans.api.project.SourceGroup;
49 import org.netbeans.modules.ant.freeform.spi.support.Util;
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 /**
59  * EJB module implementation on top of freeform project.
60  *
61  * @author Pavel Buzek
62  */

63 public class EJBModules implements EjbJarProvider, EjbJarsInProject, AntProjectListener, ClassPathProvider {
64     
65     private ArrayList JavaDoc modules = new ArrayList JavaDoc ();
66     private HashMap JavaDoc cache = new HashMap JavaDoc ();
67     private Project project;
68     private AntProjectHelper helper;
69     private PropertyEvaluator evaluator;
70         
71     public EJBModules (Project project, AntProjectHelper helper, PropertyEvaluator evaluator) {
72         assert project != null;
73         this.project = project;
74         this.helper = helper;
75         this.evaluator = evaluator;
76         helper.addAntProjectListener(this);
77     }
78     
79     public EjbJar findEjbJar (FileObject file) {
80         Project owner = FileOwnerQuery.getOwner (file);
81         synchronized (this) {
82             if (project.equals (owner)) {
83                 if (modules.isEmpty()) {
84                     readAuxData ();
85                 }
86                 for (Iterator JavaDoc iter = modules.iterator (); iter.hasNext ();) {
87                     FFEJBModule wm = (FFEJBModule) iter.next ();
88                     if (wm.contais (file)) {
89                         if (cache.get (wm) == null) {
90                             cache.put (wm, EjbJarFactory.createEjbJar (wm));
91                         }
92                         return (EjbJar) cache.get (wm);
93                     }
94                 }
95             }
96             return null;
97         }
98     }
99
100     public ClassPath findClassPath (FileObject file, String JavaDoc type) {
101         // findClassPath() should return null for any other type than
102
// ClassPath.SOURCE, see #59031
103
if (!ClassPath.SOURCE.equals (type)) {
104             return null;
105         }
106         Project owner = FileOwnerQuery.getOwner (file);
107         if (owner != null && owner.equals (project)) {
108             if (modules == null) {
109                 readAuxData ();
110             }
111             for (Iterator JavaDoc iter = modules.iterator (); iter.hasNext ();) {
112                 FFEJBModule wm = (FFEJBModule) iter.next ();
113                 if (wm.contais (file)) {
114                     return wm.findClassPath (file, type);
115                 }
116             }
117         }
118         return null;
119     }
120     
121     public synchronized void readAuxData () {
122         modules.clear();
123         cache.clear();
124         AuxiliaryConfiguration aux = (AuxiliaryConfiguration)project.getLookup().lookup(AuxiliaryConfiguration.class);
125         assert aux != null;
126         Element JavaDoc ejb = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB_2, true);
127         String JavaDoc namespace = EJBProjectNature.NS_EJB_2;
128         if (ejb == null) {
129             ejb = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB, true);
130             namespace = EJBProjectNature.NS_EJB;
131         }
132         if (ejb == null) {
133             return;
134         }
135         List JavaDoc/*<Element>*/ ejbModules = Util.findSubElements(ejb);
136         Iterator JavaDoc it = ejbModules.iterator();
137         while (it.hasNext()) {
138             Element JavaDoc ejbModulesEl = (Element JavaDoc)it.next();
139             assert ejbModulesEl.getLocalName().equals("ejb-module") : ejbModulesEl;
140             FileObject configFilesFO = getFile (ejbModulesEl, "config-files"); //NOI18N
141
Element JavaDoc j2eeSpecEl = Util.findElement (ejbModulesEl, "j2ee-spec-level", namespace);
142             String JavaDoc j2eeSpec = j2eeSpecEl == null ? null : evaluator.evaluate (Util.findText (j2eeSpecEl));
143             Element JavaDoc classpathEl = Util.findElement (ejbModulesEl, "classpath", namespace);
144             FileObject [] sources = getSources (classpathEl);
145             ClassPath cp = classpathEl == null ? null : createClasspath (classpathEl, sources);
146             File JavaDoc[] j2eePlatformClasspath = org.netbeans.modules.j2ee.common.Util.getJ2eePlatformClasspathEntries(project);
147             modules.add (new FFEJBModule (configFilesFO, j2eeSpec, sources, cp, j2eePlatformClasspath));
148         }
149     }
150     
151     private FileObject getFile (Element JavaDoc parent, String JavaDoc fileElName) {
152         Element JavaDoc el = Util.findElement (parent, fileElName, EJBProjectNature.NS_EJB_2);
153         if (el == null) {
154             el = Util.findElement (parent, fileElName, EJBProjectNature.NS_EJB);
155         }
156         String JavaDoc fname = Util.findText (el);
157         String JavaDoc locationEval = evaluator.evaluate(fname);
158         if (locationEval != null) {
159             File JavaDoc locationFile = helper.resolveFile(locationEval);
160             return FileUtil.toFileObject(locationFile);
161         }
162         return null;
163     }
164
165     private FileObject [] getSources (Element JavaDoc classpathEl) {
166         String JavaDoc cp = Util.findText(classpathEl);
167         if (cp == null) {
168             cp = "";
169         }
170         String JavaDoc cpEval = evaluator.evaluate(cp);
171         if (cpEval == null) {
172             return null;
173         }
174         String JavaDoc[] path = PropertyUtils.tokenizePath(cpEval);
175         Set JavaDoc srcRootSet = new HashSet JavaDoc ();
176         for (int i = 0; i < path.length; i++) {
177             File JavaDoc entryFile = helper.resolveFile(path[i]);
178             URL JavaDoc entry;
179             try {
180                 entry = entryFile.toURI().toURL();
181                 if (!entryFile.exists () && !entry.toExternalForm ().endsWith ("/")) {
182                         entry = new URL JavaDoc (entry.toExternalForm () + "/");
183                 }
184             } catch (MalformedURLException JavaDoc x) {
185                 throw new AssertionError JavaDoc(x);
186             }
187             if (FileUtil.isArchiveFile(entry)) {
188                 entry = FileUtil.getArchiveRoot(entry);
189             }
190             SourceForBinaryQuery.Result res = SourceForBinaryQuery.findSourceRoots (entry);
191             FileObject srcForBin [] = res.getRoots ();
192             for (int j = 0; j < srcForBin.length; j++) {
193                 srcRootSet.add (srcForBin [j]);
194             }
195             if (srcForBin.length == 0) {
196                 srcRootSet.add(FileUtil.toFileObject(entryFile));
197             }
198         }
199         SourceGroup sg [] = ProjectUtils.getSources (project).getSourceGroups (JavaProjectConstants.SOURCES_TYPE_JAVA);
200         Set JavaDoc filteredSources = new HashSet JavaDoc ();
201         // all Java sources should be added to the result if the classpath element is empty
202
for (int i = 0; i < sg.length; i++) {
203             if (path.length == 0 || srcRootSet.contains (sg [i].getRootFolder ())) {
204                 filteredSources.add (sg [i].getRootFolder ());
205             }
206         }
207         // XXX if the classpath element is empty the result will contain the test roots
208
return (FileObject []) filteredSources.toArray (new FileObject [filteredSources.size ()]);
209     }
210     
211     /**
212      * Create a classpath from a &lt;classpath&gt; element.
213      */

214     private ClassPath createClasspath(Element JavaDoc classpathEl, FileObject[] sources) {
215         String JavaDoc cp = Util.findText(classpathEl);
216         if (cp == null) {
217             cp = "";
218         }
219         String JavaDoc cpEval = evaluator.evaluate(cp);
220         if (cpEval == null) {
221             return null;
222         }
223         String JavaDoc[] path = PropertyUtils.tokenizePath(cpEval);
224         Set JavaDoc entries = new HashSet JavaDoc();
225         for (int i = 0; i < path.length; i++) {
226             entries.add(helper.resolveFile(path[i]));
227         }
228         if (entries.size() == 0) {
229             // if the classpath element was empty then the classpath
230
// should contain all source roots
231
for (int i = 0; i < sources.length; i++) {
232                 entries.add(FileUtil.toFile(sources[i]));
233             }
234         }
235         URL JavaDoc[] pathURL = new URL JavaDoc[entries.size()];
236         int i = 0;
237         for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
238             File JavaDoc entryFile = (File JavaDoc)it.next();
239             URL JavaDoc entry;
240             try {
241                 entry = entryFile.toURI().toURL();
242                 if (FileUtil.isArchiveFile(entry)) {
243                     entry = FileUtil.getArchiveRoot(entry);
244                 } else {
245                     String JavaDoc s = entry.toExternalForm();
246                     if (!s.endsWith("/")) { // NOI18N
247
// Folder which is not built.
248
entry = new URL JavaDoc(s + '/');
249                     }
250                 }
251             } catch (MalformedURLException JavaDoc x) {
252                 throw new AssertionError JavaDoc(x);
253             }
254             pathURL[i++] = entry;
255         }
256         return ClassPathSupport.createClassPath(pathURL);
257     }
258     
259     public void configurationXmlChanged(AntProjectEvent ev) {
260         readAuxData();
261     }
262     
263     public void propertiesChanged(AntProjectEvent ev) {
264         // ignore
265
}
266
267     public EjbJar[] getEjbJars() {
268         if (modules.isEmpty()) {
269             readAuxData ();
270         }
271         EjbJar results [] = new EjbJar[modules.size()];
272         int i = 0;
273         for (Iterator JavaDoc iter = modules.iterator (); iter.hasNext ();) {
274             FFEJBModule ejbm = (FFEJBModule) iter.next ();
275             if (cache.get (ejbm) == null) {
276                 results[i] = EjbJarFactory.createEjbJar (ejbm);
277                 cache.put (ejbm, results[i]);
278             } else {
279                 results[i] = (EjbJar) cache.get(ejbm);
280             }
281         }
282         return results;
283     }
284     
285     private static final class FFEJBModule implements EjbJarImplementation {
286         
287 // public static final String FOLDER_META_INF = "META-INF";//NOI18N
288
public static final String JavaDoc FILE_DD = "ejb-jar.xml";//NOI18N
289

290         private FileObject configFilesFO;
291         private FileObject [] sourcesFOs;
292         private ClassPath classPath;
293         private String JavaDoc j2eeSpec;
294         private MetadataUnit metadataUnit;
295         private ClassPath metadataClassPath;
296         private File JavaDoc[] j2eePlatformClasspath;
297 // private String contextPath;
298

299         FFEJBModule (FileObject configFilesFO, String JavaDoc j2eeSpec, /*String contextPath,*/ FileObject sourcesFOs[], ClassPath classPath, File JavaDoc[] j2eePlatformClasspath) {
300             this.configFilesFO = configFilesFO;
301             this.j2eeSpec = j2eeSpec;
302 // this.contextPath = contextPath;
303
this.sourcesFOs = sourcesFOs;
304             this.classPath = classPath;
305             this.j2eePlatformClasspath = j2eePlatformClasspath;
306         }
307         
308         boolean contais (FileObject fo) {
309             if (configFilesFO == fo || FileUtil.isParentOf (configFilesFO , fo))
310                 return true;
311             for (int i = 0; i < sourcesFOs.length; i++) {
312                 if (sourcesFOs [i] == fo || FileUtil.isParentOf (sourcesFOs [i], fo))
313                     return true;
314             }
315             return false;
316         }
317         
318 // public FileObject getDocumentBase () {
319
// return configFilesFO;
320
// }
321

322         public ClassPath findClassPath (FileObject file, String JavaDoc type) {
323             return classPath;
324         }
325         
326         public String JavaDoc getJ2eePlatformVersion () {
327             return j2eeSpec;
328         }
329         
330 // public String getContextPath () {
331
// return contextPath;
332
// }
333

334         public String JavaDoc toString () {
335             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("EJB module in freeform project" +
336                 "\n\tconfig files:" + configFilesFO.getPath () +
337 // "\n\tcontext path:" + contextPath +
338
"\n\tj2ee version:" + j2eeSpec);
339             for (int i = 0; i < sourcesFOs.length; i++) {
340                 sb.append ("\n\tsource root:" + sourcesFOs [i].getPath ());
341             }
342             return sb.toString ();
343         }
344         
345         public FileObject getDeploymentDescriptor () {
346             return getMetaInf ().getFileObject (FILE_DD);
347         }
348         
349         public FileObject getMetaInf () {
350             return configFilesFO;
351         }
352
353         public FileObject[] getJavaSources() {
354             return sourcesFOs;
355         }
356
357         public MetadataUnit getMetadataUnit() {
358             synchronized (this) {
359                 if (metadataUnit == null) {
360                     metadataUnit = new MetadataUnitImpl();
361                 }
362                 return metadataUnit;
363             }
364         }
365         
366         private class MetadataUnitImpl implements MetadataUnit {
367             public ClassPath getClassPath() {
368                 return classPath;
369             }
370             public FileObject getDeploymentDescriptor() {
371                 return FFEJBModule.this.getDeploymentDescriptor();
372             }
373         }
374
375     }
376
377 }
378
Popular Tags