KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > api > web > DDProvider


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.dd.api.web;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import org.netbeans.api.java.classpath.ClassPath;
30 import org.netbeans.modules.j2ee.metadata.ClassPathSupport;
31 import org.netbeans.modules.j2ee.dd.impl.web.WebAppProxy;
32 import org.netbeans.modules.j2ee.dd.impl.web.WebParseUtils;
33 import org.netbeans.modules.j2ee.dd.impl.common.DDUtils;
34 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
35 import org.netbeans.modules.web.api.webmodule.WebModule;
36 import org.netbeans.spi.java.classpath.PathResourceImplementation;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.*;
39 import org.xml.sax.*;
40 import java.util.Map JavaDoc;
41 import org.openide.loaders.DataObject;
42 import org.openide.loaders.DataObjectNotFoundException;
43
44 /**
45  * Provides access to Deployment Descriptor root ({@link org.netbeans.modules.j2ee.dd.api.web.WebApp} object)
46  *
47  * @author Milan Kuchtiak
48  */

49
50 public final class DDProvider {
51     private static DDProvider ddProvider;
52     private Map JavaDoc ddMap;
53     private Map JavaDoc<MetadataUnit, WebApp> annotationDDMap;
54     private Map JavaDoc baseBeanMap;
55     private Map JavaDoc errorMap;
56     private FCA fileChangeListener;
57     private Map JavaDoc musMap;
58     
59     /** Creates a new instance of WebModule */
60     private DDProvider() {
61         ddMap=new java.util.HashMap JavaDoc(5);
62         annotationDDMap = new HashMap JavaDoc<MetadataUnit, WebApp>(5);
63         baseBeanMap=new java.util.HashMap JavaDoc(5);
64         errorMap=new java.util.HashMap JavaDoc(5);
65         musMap=new HashMap JavaDoc(5);
66         fileChangeListener = new FCA();
67     }
68     
69     /**
70      * Accessor method for DDProvider singleton
71      * @return DDProvider object
72      */

73     public static synchronized DDProvider getDefault() {
74         if (ddProvider==null) ddProvider = new DDProvider();
75         return ddProvider;
76     }
77     
78     /**
79      * Returns the root of deployment descriptor bean graph for given file object.
80      * The method is useful for clints planning to read only the deployment descriptor
81      * or to listen to the changes.
82      * @param fo FileObject representing the web.xml file
83      * @return WebApp object - root of the deployment descriptor bean graph
84      */

85     public WebApp getMergedDDRoot(FileObject fo) throws IOException JavaDoc {
86         if (fo == null) {
87             throw new IllegalArgumentException JavaDoc("FileObject is null"); //NOI18N;
88
}
89         
90         WebModule wm = WebModule.getWebModule(fo);
91         if(wm != null) {
92             //the MetadataUnits are cached; a key is the WM's DD FO
93
MetadataUnit mu = (MetadataUnit)musMap.get(wm.getDeploymentDescriptor());
94             if(mu == null) {
95                 mu = new SimpleMetadataUnit(wm.getDeploymentDescriptor(), wm.getJavaSources());
96                 musMap.put(wm.getDeploymentDescriptor(), mu);
97             }
98             return getMergedDDRoot(mu);
99         } else {
100             return getDDRoot(fo);
101         }
102     }
103     
104     public WebApp getMergedDDRoot(MetadataUnit mu) throws IOException JavaDoc {
105         WebApp xmlRoot = getDDRoot(mu.getDeploymentDescriptor());
106         if (xmlRoot != null) { // && !xmlRoot.getVersion().equals(WebApp.VERSION_2_5)) {
107
// TODO find a better resolution for this hack
108
return xmlRoot;
109         }
110         return null;
111     }
112
113     public WebApp getDDRoot(FileObject fo) throws java.io.IOException JavaDoc {
114         WebAppProxy webApp = null;
115         
116         synchronized (ddMap) {
117             webApp = getFromCache(fo);
118             if (webApp!=null) {
119                 return webApp;
120             }
121         }
122         
123         fo.addFileChangeListener(fileChangeListener);
124         
125         String JavaDoc version = null;
126         SAXParseException error = null;
127         try {
128             WebApp original = null;
129             synchronized (baseBeanMap) {
130                 original = getOriginalFromCache(fo);
131                 if (original == null) {
132                     version = WebParseUtils.getVersion(fo.getInputStream());
133                     // preparsing
134
error = parse(fo);
135                     original = DDUtils.createWebApp(fo.getInputStream(), version);
136                     baseBeanMap.put(fo.getURL(), new WeakReference JavaDoc(original));
137                     errorMap.put(fo.getURL(), error);
138                 } else {
139                     version = original.getVersion();
140                     error = (SAXParseException) errorMap.get(fo.getURL());
141                 }
142             }
143             webApp = new WebAppProxy(original, version);
144             if (error != null) {
145                 webApp.setStatus(WebApp.STATE_INVALID_PARSABLE);
146                 webApp.setError(error);
147             }
148         } catch (SAXException ex) {
149             webApp = new WebAppProxy(null, version);
150             webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE);
151             if (ex instanceof SAXParseException) {
152                 webApp.setError((SAXParseException) ex);
153             } else if (ex.getException() instanceof SAXParseException) {
154                 webApp.setError((SAXParseException) ex.getException());
155             }
156         }
157         ddMap.put(fo.getURL(), new WeakReference JavaDoc(webApp));
158         return webApp;
159     }
160     
161     /**
162      * Returns the root of deployment descriptor bean graph for given file object.
163      * The method is useful for clients planning to modify the deployment descriptor.
164      * Finally the {@link org.netbeans.modules.j2ee.dd.api.web.WebApp#write(org.openide.filesystems.FileObject)} should be used
165      * for writing the changes.
166      * @param fo FileObject representing the web.xml file
167      * @return WebApp object - root of the deployment descriptor bean graph
168      */

169     public WebApp getDDRootCopy(FileObject fo) throws java.io.IOException JavaDoc {
170         return (WebApp)getDDRoot(fo).clone();
171     }
172     
173     private WebAppProxy getFromCache(FileObject fo) throws java.io.IOException JavaDoc {
174         if (fo == null) {
175             return null;
176         }
177         WeakReference JavaDoc wr = (WeakReference JavaDoc) ddMap.get(fo.getURL());
178         if (wr == null) {
179             return null;
180         }
181         WebAppProxy webApp = (WebAppProxy) wr.get();
182         if (webApp == null) {
183             ddMap.remove(fo.getURL());
184         }
185         return webApp;
186     }
187     
188     private WebApp getOriginalFromCache(FileObject fo) throws java.io.IOException JavaDoc {
189         WeakReference JavaDoc wr = (WeakReference JavaDoc) baseBeanMap.get(fo.getURL());
190         if (wr == null) {
191             return null;
192         }
193         WebApp webApp = (WebApp) wr.get();
194         if (webApp == null) {
195             baseBeanMap.remove(fo.getURL());
196             errorMap.remove(fo.getURL());
197             if (ddMap.get(fo.getURL()) == null) {
198             }
199         }
200         return webApp;
201     }
202     
203     /**
204      * Returns the root of deployment descriptor bean graph for java.io.File object.
205      *
206      * @param f File representing the web.xml file
207      * @return WebApp object - root of the deployment descriptor bean graph
208      */

209     public WebApp getDDRoot(File JavaDoc f) throws IOException JavaDoc, SAXException {
210         return DDUtils.createWebApp(new FileInputStream JavaDoc(f), WebParseUtils.getVersion(new FileInputStream JavaDoc(f)));
211     }
212     
213     /** Convenient method for getting the BaseBean object from CommonDDBean object.
214      * The j2eeserver module needs BaseBean to implement jsr88 API.
215      * This is a temporary workaround until the implementation of jsr88 moves into ddapi
216      * or the implementation in j2eeserver gets changed.
217      * @deprecated do not use - temporary workaround that exposes the schema2beans implementation
218      */

219     public org.netbeans.modules.schema2beans.BaseBean getBaseBean(org.netbeans.modules.j2ee.dd.api.common.CommonDDBean bean) {
220         if (bean instanceof org.netbeans.modules.schema2beans.BaseBean) return (org.netbeans.modules.schema2beans.BaseBean)bean;
221         else if (bean instanceof WebAppProxy) return (org.netbeans.modules.schema2beans.BaseBean) ((WebAppProxy)bean).getOriginal();
222         return null;
223     }
224     
225     public SAXParseException parse(FileObject fo)
226     throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
227         return WebParseUtils.parse(fo);
228     }
229   
230     
231     /**
232      * Removes the entries associated with the given <code>fo</code> from
233      * the various caches that this class utilizes.
234      * @param fo
235      */

236     private void removeFromCache(FileObject fo){
237         try{
238             URL JavaDoc foUrl = fo.getURL();
239             ddMap.remove(foUrl);
240             baseBeanMap.remove(foUrl);
241             errorMap.remove(foUrl);
242             musMap.remove(fo);
243         } catch (FileStateInvalidException ex) {
244             ErrorManager.getDefault().notify(ex);
245         }
246     }
247     
248     private class FCA extends FileChangeAdapter {
249         public void fileChanged(FileEvent evt) {
250             FileObject fo=evt.getFile();
251             try {
252                 if (DataObject.find(fo) != null) {
253                     return;
254                 }
255             } catch (DataObjectNotFoundException e) {
256             }
257             try {
258                 synchronized (ddMap) {
259                     synchronized (baseBeanMap) {
260                         WebAppProxy webApp = getFromCache(fo);
261                         WebApp orig = getOriginalFromCache(fo);
262                         if (webApp!=null) {
263                             String JavaDoc version = null;
264                             try {
265                                 version = WebParseUtils.getVersion(fo.getInputStream());
266                                 // preparsing
267
SAXParseException error = parse(fo);
268                                 if (error!=null) {
269                                     webApp.setError(error);
270                                     webApp.setStatus(WebApp.STATE_INVALID_PARSABLE);
271                                 } else {
272                                     webApp.setError(null);
273                                     webApp.setStatus(WebApp.STATE_VALID);
274                                 }
275                                 WebApp original = DDUtils.createWebApp(fo.getInputStream(), version);
276                                 baseBeanMap.put(fo.getURL(), new WeakReference JavaDoc(original));
277                                 errorMap.put(fo.getURL(), webApp.getError());
278                                 webApp.merge(original, WebApp.MERGE_UPDATE);
279                             } catch (SAXException ex) {
280                                 if (ex instanceof SAXParseException) {
281                                     webApp.setError((SAXParseException)ex);
282                                 } else if ( ex.getException() instanceof SAXParseException) {
283                                     webApp.setError((SAXParseException)ex.getException());
284                                 }
285                                 webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE);
286                                 webApp.setOriginal(null);
287                                 webApp.setProxyVersion(version);
288                             }
289                         } else if (orig != null) {
290                             String JavaDoc version = null;
291                             try {
292                                 version = WebParseUtils.getVersion(fo.getInputStream());
293                                 WebApp original = DDUtils.createWebApp(fo.getInputStream(), version);
294                                 if (original.getClass().equals(orig.getClass())) {
295                                     orig.merge(original,WebApp.MERGE_UPDATE);
296                                 } else {
297                                     baseBeanMap.put(fo.getURL(), new WeakReference JavaDoc(original));
298                                 }
299                             } catch (SAXException ex) {
300                                 baseBeanMap.remove(fo.getURL());
301                             }
302                         }
303                     }
304                 }
305             } catch (java.io.IOException JavaDoc ex){}
306         }
307         
308         public void fileDeleted(FileEvent fe) {
309             // need to remove cache entries, see #76431.
310
removeFromCache(fe.getFile());
311         }
312         
313         
314     }
315     
316     private class SimpleMetadataUnit implements MetadataUnit {
317         
318         private FileObject dd;
319         private FileObject[] roots;
320         
321         public SimpleMetadataUnit(FileObject dd, FileObject[] javaSources) {
322             this.dd = dd;
323             this.roots = javaSources;
324         }
325         
326         public FileObject getDeploymentDescriptor() {
327             return dd;
328         }
329         
330         public ClassPath getClassPath() {
331             if (roots.length > 0) {
332                 FileObject fo = roots[0];
333                 return ClassPathSupport.createWeakProxyClassPath(new ClassPath[] {
334                     ClassPath.getClassPath(fo, ClassPath.SOURCE),
335                     ClassPath.getClassPath(fo, ClassPath.COMPILE)
336                 });
337             } else {
338                 return org.netbeans.spi.java.classpath.support.ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
339             }
340         }
341         
342         
343         
344     }
345 }
346
Popular Tags