KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > web > dd > 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.api.web.dd;
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 org.netbeans.modules.web.dd.impl.WebAppProxy;
27 import org.openide.filesystems.*;
28 import org.xml.sax.*;
29 import java.util.Map JavaDoc;
30 import org.openide.util.NbBundle;
31 import java.net.URL JavaDoc;
32
33 /**
34  * Provides access to Deployment Descriptor root ({@link org.netbeans.api.web.dd.WebApp} object)
35  *
36  * @author Milan Kuchtiak
37  * @deprecated Use the API for web module deployment descriptor in j2ee/ddapi module.
38 */

39
40 public final class DDProvider {
41     private static DDProvider ddProvider;
42     private Map JavaDoc ddMap;
43     private Map JavaDoc baseBeanMap;
44     private Map JavaDoc errorMap;
45     private FCA fileChangeListener;
46
47     private static final String JavaDoc EXCEPTION_PREFIX="version:"; //NOI18N
48

49     /** Creates a new instance of WebModule */
50     private DDProvider() {
51         ddMap=new java.util.HashMap JavaDoc(5);
52         baseBeanMap=new java.util.HashMap JavaDoc(5);
53         errorMap=new java.util.HashMap JavaDoc(5);
54         fileChangeListener = new FCA ();
55     }
56     
57     /**
58     * Accessor method for DDProvider singleton
59     * @return DDProvider object
60     */

61     public static synchronized DDProvider getDefault() {
62         if (ddProvider==null) ddProvider = new DDProvider();
63         return ddProvider;
64     }
65     
66
67     /**
68      * Returns the root of deployment descriptor bean graph for given file object.
69      * The method is useful for clints planning to read only the deployment descriptor
70      * or to listen to the changes.
71      * @param fo FileObject representing the web.xml file
72      * @return WebApp object - root of the deployment descriptor bean graph
73      */

74     public WebApp getDDRoot(FileObject fo) throws java.io.IOException JavaDoc {
75         
76         WebAppProxy webApp = getFromCache (fo);
77         if (webApp!=null) return webApp;
78         
79
80         fo.addFileChangeListener(fileChangeListener);
81         
82         String JavaDoc version=null;
83         SAXParseException error = null;
84         try {
85             WebApp original = getOriginalFromCache (fo);
86             if (original == null) {
87                 version = getVersion(fo.getInputStream());
88                 // preparsing
89
error = parse(fo);
90                 original = createWebApp(fo.getInputStream(), version);
91                 baseBeanMap.put(fo.getURL(), new WeakReference JavaDoc (original));
92             } else {
93                 version = original.getVersion ();
94                 error = (SAXParseException) errorMap.get (fo.getURL ());
95             }
96             webApp=new WebAppProxy(original,version);
97             if (error!=null) {
98                 webApp.setStatus(WebApp.STATE_INVALID_PARSABLE);
99                 webApp.setError(error);
100             }
101         } catch (SAXException ex) {
102             webApp = new WebAppProxy(null,version);
103             webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE);
104             if (ex instanceof SAXParseException) {
105                 webApp.setError((SAXParseException)ex);
106             } else if ( ex.getException() instanceof SAXParseException) {
107                 webApp.setError((SAXParseException)ex.getException());
108             }
109         }
110         ddMap.put(fo.getURL(), new WeakReference JavaDoc (webApp));
111         return webApp;
112     }
113
114     /**
115      * Returns the root of deployment descriptor bean graph for given file object.
116      * The method is useful for clients planning to modify the deployment descriptor.
117      * Finally the {@link org.netbeans.api.web.dd.WebApp#write(org.openide.filesystems.FileObject)} should be used
118      * for writing the changes.
119      * @param fo FileObject representing the web.xml file
120      * @return WebApp object - root of the deployment descriptor bean graph
121      */

122     public WebApp getDDRootCopy(FileObject fo) throws java.io.IOException JavaDoc {
123         return (WebApp)getDDRoot(fo).clone();
124     }
125
126     private WebAppProxy getFromCache (FileObject fo) throws java.io.IOException JavaDoc {
127         WeakReference JavaDoc wr = (WeakReference JavaDoc) ddMap.get(fo.getURL ());
128         if (wr == null) {
129             return null;
130         }
131         WebAppProxy webApp = (WebAppProxy) wr.get ();
132         if (webApp == null) {
133             ddMap.remove (fo.getURL ());
134         }
135         return webApp;
136     }
137     
138     private WebApp getOriginalFromCache (FileObject fo) throws java.io.IOException JavaDoc {
139         WeakReference JavaDoc wr = (WeakReference JavaDoc) baseBeanMap.get(fo.getURL ());
140         if (wr == null) {
141             return null;
142         }
143         WebApp webApp = (WebApp) wr.get ();
144         if (webApp == null) {
145             baseBeanMap.remove (fo.getURL ());
146             errorMap.remove (fo.getURL ());
147             if (ddMap.get (fo.getURL ()) == null) {
148             }
149         }
150         return webApp;
151     }
152
153     /**
154      * Returns the root of deployment descriptor bean graph for java.io.File object.
155      *
156      * @param f File representing the web.xml file
157      * @return WebApp object - root of the deployment descriptor bean graph
158      */

159     public WebApp getDDRoot(File JavaDoc f) throws IOException JavaDoc, SAXException {
160         return createWebApp(new FileInputStream JavaDoc(f), getVersion(new FileInputStream JavaDoc(f)));
161     }
162     
163     /** Convenient method for getting the BaseBean object from CommonDDBean object.
164      * The j2eeserver module needs BaseBean to implement jsr88 API.
165      * This is a temporary workaround until the implementation of jsr88 moves into ddapi
166      * or the implementation in j2eeserver gets changed.
167      * @deprecated do not use - temporary workaround that exposes the schema2beans implementation
168      */

169     public org.netbeans.modules.schema2beans.BaseBean getBaseBean(org.netbeans.api.web.dd.common.CommonDDBean bean) {
170         if (bean instanceof org.netbeans.modules.schema2beans.BaseBean) return (org.netbeans.modules.schema2beans.BaseBean)bean;
171         else if (bean instanceof WebAppProxy) return (org.netbeans.modules.schema2beans.BaseBean) ((WebAppProxy)bean).getOriginal();
172         return null;
173     }
174
175     private static WebApp createWebApp(java.io.InputStream JavaDoc is, String JavaDoc version) throws java.io.IOException JavaDoc, SAXException {
176         try {
177             if (WebApp.VERSION_2_3.equals(version)) {
178                 return org.netbeans.modules.web.dd.impl.model_2_3.WebApp.createGraph(is);
179             } else {
180                 return org.netbeans.modules.web.dd.impl.model_2_4.WebApp.createGraph(is);
181             }
182         } catch (RuntimeException JavaDoc ex) {
183             throw new SAXException (ex.getMessage());
184         }
185     }
186     
187     /** Parsing just for detecting the version SAX parser used
188     */

189     private static String JavaDoc getVersion(java.io.InputStream JavaDoc is) throws java.io.IOException JavaDoc, SAXException {
190         javax.xml.parsers.SAXParserFactory JavaDoc fact = javax.xml.parsers.SAXParserFactory.newInstance();
191         fact.setValidating(false);
192         try {
193             javax.xml.parsers.SAXParser JavaDoc parser = fact.newSAXParser();
194             XMLReader reader = parser.getXMLReader();
195             reader.setContentHandler(new VersionHandler());
196             reader.setEntityResolver(DDResolver.getInstance());
197             try {
198                 reader.parse(new InputSource(is));
199             } catch (SAXException ex) {
200                 is.close();
201                 String JavaDoc message = ex.getMessage();
202                 if (message!=null && message.startsWith(EXCEPTION_PREFIX))
203                     return message.substring(EXCEPTION_PREFIX.length());
204                 else throw new SAXException(NbBundle.getMessage(DDProvider.class, "MSG_cannotParse"),ex);
205             }
206             is.close();
207             throw new SAXException(NbBundle.getMessage(DDProvider.class, "MSG_cannotFindRoot"));
208         } catch(javax.xml.parsers.ParserConfigurationException JavaDoc ex) {
209             throw new SAXException(NbBundle.getMessage(DDProvider.class, "MSG_parserProblem"),ex);
210         }
211     }
212     
213     private static class VersionHandler extends org.xml.sax.helpers.DefaultHandler JavaDoc {
214         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes atts) throws SAXException {
215             if ("web-app".equals(rawName)) { //NOI18N
216
String JavaDoc version = atts.getValue("version"); //NOI18N
217
throw new SAXException(EXCEPTION_PREFIX+(version==null?WebApp.VERSION_2_3:version));
218             }
219         }
220     }
221   
222     private static class DDResolver implements EntityResolver {
223         static DDResolver resolver;
224         static synchronized DDResolver getInstance() {
225             if (resolver==null) {
226                 resolver=new DDResolver();
227             }
228             return resolver;
229         }
230         public InputSource resolveEntity (String JavaDoc publicId, String JavaDoc systemId) {
231             String JavaDoc resource=null;
232             // return a proper input source
233
if ("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN".equals(publicId)) { //NOI18N
234
resource="/org/netbeans/modules/web/dd/impl/resources/web-app_2_3.dtd"; //NOI18N
235
} else if ("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN".equals(publicId)) { //NOI18N
236
resource="/org/netbeans/modules/web/dd/impl/resources/web-app_2_2.dtd"; //NOI18N
237
} else if ("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd".equals(systemId)) {
238                 resource="/org/netbeans/modules/web/dd/impl/resources/web-app_2_4.xsd"; //NOI18N
239
}
240             if (resource==null) return null;
241             java.net.URL JavaDoc url = this.getClass().getResource(resource);
242             return new InputSource(url.toString());
243         }
244     }
245     
246     private static class ErrorHandler implements org.xml.sax.ErrorHandler JavaDoc {
247         private int errorType=-1;
248         SAXParseException error;
249
250         public void warning(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
251             if (errorType<0) {
252                 errorType=0;
253                 error=sAXParseException;
254             }
255             //throw sAXParseException;
256
}
257         public void error(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
258             if (errorType<1) {
259                 errorType=1;
260                 error=sAXParseException;
261             }
262             //throw sAXParseException;
263
}
264         public void fatalError(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
265             errorType=2;
266             throw sAXParseException;
267         }
268         
269         public int getErrorType() {
270             return errorType;
271         }
272         public SAXParseException getError() {
273             return error;
274         }
275     }
276     
277     public SAXParseException parse (FileObject fo)
278             throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
279         DDProvider.ErrorHandler errorHandler = new DDProvider.ErrorHandler();
280         try {
281             XMLReader reader = new org.apache.xerces.parsers.SAXParser();
282             reader.setErrorHandler(errorHandler);
283             reader.setEntityResolver(DDProvider.DDResolver.getInstance());
284             reader.setFeature("http://apache.org/xml/features/validation/schema", true); // NOI18N
285
reader.setFeature("http://xml.org/sax/features/validation", true); // NOI18N
286
reader.setFeature("http://xml.org/sax/features/namespaces", true); // NOI18N
287
reader.parse(new InputSource(fo.getInputStream()));
288             SAXParseException error = errorHandler.getError();
289             if (error!=null) return error;
290         } catch (SAXException ex) {
291             throw ex;
292         }
293         return null;
294     }
295
296     private class FCA extends FileChangeAdapter {
297             public void fileChanged(FileEvent evt) {
298                 FileObject fo=evt.getFile();
299                 try {
300                     WebAppProxy webApp = getFromCache (fo);
301                     WebApp orig = getOriginalFromCache (fo);
302                     if (webApp!=null) {
303                         String JavaDoc version = null;
304                         try {
305                             version = getVersion(fo.getInputStream());
306                             // preparsing
307
SAXParseException error = parse(fo);
308                             if (error!=null) {
309                                 webApp.setError(error);
310                                 webApp.setStatus(WebApp.STATE_INVALID_PARSABLE);
311                             } else {
312                                 webApp.setError(null);
313                                 webApp.setStatus(WebApp.STATE_VALID);
314                             }
315                             WebApp original = createWebApp(fo.getInputStream(), version);
316                             baseBeanMap.put(fo.getURL(), new WeakReference JavaDoc (original));
317                             errorMap.put(fo.getURL(), webApp.getError ());
318                             // replacing original file in proxy WebApp
319
if (!version.equals(webApp.getVersion())) {
320                                 webApp.setOriginal(original);
321                             } else {// the same version
322
// replacing original file in proxy WebApp
323
if (webApp.getOriginal()==null) {
324                                     webApp.setOriginal(original);
325                                 } else {
326                                     webApp.getOriginal().merge(original,WebApp.MERGE_UPDATE);
327                                 }
328                             }
329                         } catch (SAXException ex) {
330                             if (ex instanceof SAXParseException) {
331                                 webApp.setError((SAXParseException)ex);
332                             } else if ( ex.getException() instanceof SAXParseException) {
333                                 webApp.setError((SAXParseException)ex.getException());
334                             }
335                             webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE);
336                             webApp.setOriginal(null);
337                             webApp.setProxyVersion(version);
338                         }
339                     } else if (orig != null) {
340                         String JavaDoc version = null;
341                         try {
342                             version = getVersion(fo.getInputStream());
343                             WebApp original = createWebApp(fo.getInputStream(), version);
344                             if (original.getClass().equals (orig.getClass())) {
345                                 orig.merge(original,WebApp.MERGE_UPDATE);
346                             } else {
347                                 baseBeanMap.put(fo.getURL(), new WeakReference JavaDoc (original));
348                             }
349                         } catch (SAXException ex) {
350                             baseBeanMap.remove(fo.getURL());
351                         }
352                     }
353                 } catch (java.io.IOException JavaDoc ex){}
354             }
355         }
356 }
Popular Tags