KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > api > webservices > 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.webservices;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.math.BigDecimal JavaDoc;
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import org.netbeans.modules.j2ee.dd.impl.webservices.WebServicesProxy;
29 import org.netbeans.modules.schema2beans.Common;
30 import org.openide.filesystems.*;
31 import org.xml.sax.*;
32 import java.util.Map JavaDoc;
33 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
34 import org.w3c.dom.Document JavaDoc;
35
36 public final class DDProvider {
37     
38     private static final DDProvider ddProvider = new DDProvider();
39     private Map JavaDoc ddMap;
40     private Map JavaDoc<MetadataUnit, Webservices> annotationDDMap;
41     
42     /** Creates a new instance of WebSvcModule */
43     private DDProvider() {
44         //ddMap=new java.util.WeakHashMap(5);
45
ddMap = new java.util.HashMap JavaDoc(5);
46         annotationDDMap = new java.util.WeakHashMap JavaDoc<MetadataUnit, Webservices>(5);
47     }
48     
49     /**
50     * Accessor method for DDProvider singleton
51     * @return DDProvider object
52     */

53     public static DDProvider getDefault() {
54         return ddProvider;
55     }
56     
57     public Webservices getMergedDDRoot(MetadataUnit mu) throws IOException JavaDoc {
58         if (mu == null) {
59             return null;
60         }
61         Webservices xmlRoot = getDDRoot(mu.getDeploymentDescriptor());
62         // for J2ee 1.4 and lower delegate to XML-only method
63
if (xmlRoot != null && !xmlRoot.getVersion().equals(new BigDecimal JavaDoc(Webservices.VERSION_1_2))) {
64             return xmlRoot;
65         }
66         return null;
67     }
68
69     /**
70      * Returns the root of deployment descriptor bean graph for given file object.
71      * The method is useful for clints planning to read only the deployment descriptor
72      * or to listen to the changes.
73      * @param fo FileObject representing the xml file
74      * @return Webservices object - root of the deployment descriptor bean graph
75      */

76     public synchronized Webservices getDDRoot(FileObject fo) throws java.io.IOException JavaDoc {
77         if(fo == null) return null;
78         WebServicesProxy webSvcProxy = getFromCache (fo);
79         if (webSvcProxy!=null) {
80             return webSvcProxy;
81         }
82         
83         fo.addFileChangeListener(new FileChangeAdapter() {
84             public void fileChanged(FileEvent evt) {
85                 FileObject fo=evt.getFile();
86                 try {
87                     WebServicesProxy webSvcProxy = getFromCache (fo);
88                     String JavaDoc version = null;
89                     if (webSvcProxy!=null) {
90                         try {
91                             DDParse parseResult = parseDD(fo);
92                             version = parseResult.getVersion();
93                             setProxyErrorStatus(webSvcProxy, parseResult);
94                             Webservices newValue = createWebSvcJar(parseResult);
95                             // replacing original file in proxy Webservices
96
if (!version.equals(webSvcProxy.getVersion().toString())) {
97                                 webSvcProxy.setOriginal(newValue);
98                             } else {// the same version
99
// replacing original file in proxy Webservices
100
if (webSvcProxy.getOriginal()==null) {
101                                     webSvcProxy.setOriginal(newValue);
102                                 } else {
103                                     webSvcProxy.getOriginal().merge(newValue,Webservices.MERGE_UPDATE);
104                                 }
105                             }
106                         } catch (SAXException ex) {
107                             if (ex instanceof SAXParseException) {
108                                 webSvcProxy.setError((SAXParseException)ex);
109                             } else if ( ex.getException() instanceof SAXParseException) {
110                                 webSvcProxy.setError((SAXParseException)ex.getException());
111                             }
112                             webSvcProxy.setStatus(Webservices.STATE_INVALID_UNPARSABLE);
113                             // cbw if the state of the xml file transitions from
114
// parsable to unparsable this could be due to a user
115
// change or cvs change. We would like to still
116
// receive events when the file is restored to normal
117
// so lets not set the original to null here but wait
118
// until the file becomes parsable again to do a merge
119
//webSvcProxy.setOriginal(null);
120
webSvcProxy.setProxyVersion(Webservices.VERSION_1_1);
121                         }
122                     }
123                 } catch (java.io.IOException JavaDoc ex){}
124             }
125         });
126         
127         try {
128             DDParse parseResult = parseDD(fo);
129             Webservices original = createWebSvcJar(parseResult);
130             webSvcProxy = new WebServicesProxy(original,parseResult.getVersion());
131             setProxyErrorStatus(webSvcProxy, parseResult);
132         } catch (SAXException ex) {
133             // XXX lets throw an exception here
134
webSvcProxy = new WebServicesProxy(org.netbeans.modules.j2ee.dd.impl.webservices.model_1_2.Webservices.createGraph(),"1.2");
135             webSvcProxy.setStatus(Webservices.STATE_INVALID_UNPARSABLE);
136             if (ex instanceof SAXParseException) {
137                 webSvcProxy.setError((SAXParseException)ex);
138             } else if ( ex.getException() instanceof SAXParseException) {
139                 webSvcProxy.setError((SAXParseException)ex.getException());
140             }
141         }
142         ddMap.put(fo, /*new WeakReference*/ (webSvcProxy));
143         return webSvcProxy;
144     }
145
146     /**
147      * Returns the root of deployment descriptor bean graph for given file object.
148      * The method is useful for clients planning to modify the deployment descriptor.
149      * Finally the {@link org.netbeans.modules.j2ee.dd.impl.webservices.model_1_1.Webservices#write(org.openide.filesystems.FileObject)} should be used
150      * for writing the changes.
151      * @param fo FileObject representing the xml file
152      * @return Webservices object - root of the deployment descriptor bean graph
153      */

154     public Webservices getDDRootCopy(FileObject fo) throws java.io.IOException JavaDoc {
155         return (Webservices)getDDRoot(fo).clone();
156     }
157
158     private WebServicesProxy getFromCache (FileObject fo) {
159          return (WebServicesProxy) ddMap.get(fo);
160     }
161     
162     /**
163      * Returns the root of deployment descriptor bean graph for java.io.File object.
164      *
165      * @param is source representing the xml file
166      * @return Webservices object - root of the deployment descriptor bean graph
167      */

168     public Webservices getDDRoot(InputSource is) throws IOException JavaDoc, SAXException {
169         DDParse parse = parseDD(is);
170         Webservices webSvcJar = createWebSvcJar(parse);
171         WebServicesProxy proxy = new WebServicesProxy(webSvcJar, webSvcJar.getVersion().toString());
172         setProxyErrorStatus(proxy, parse);
173         return proxy;
174     }
175     
176     // PENDING j2eeserver needs BaseBean - this is a temporary workaround to avoid dependency of web project on DD impl
177
/** Convenient method for getting the BaseBean object from CommonDDBean object
178      * @deprecated DO NOT USE - TEMPORARY WORKAROUND !!!!
179      */

180     public org.netbeans.modules.schema2beans.BaseBean getBaseBean(org.netbeans.modules.j2ee.dd.api.common.CommonDDBean bean) {
181         if (bean instanceof org.netbeans.modules.schema2beans.BaseBean) return (org.netbeans.modules.schema2beans.BaseBean)bean;
182         else if (bean instanceof WebServicesProxy) return (org.netbeans.modules.schema2beans.BaseBean) ((WebServicesProxy)bean).getOriginal();
183         return null;
184     }
185
186     private static void setProxyErrorStatus(WebServicesProxy webSvcProxy, DDParse parse) {
187         SAXParseException error = parse.getWarning();
188         webSvcProxy.setError(error);
189         if (error!=null) {
190             webSvcProxy.setStatus(Webservices.STATE_INVALID_PARSABLE);
191         } else {
192             webSvcProxy.setStatus(Webservices.STATE_VALID);
193         }
194     }
195     
196     private static Webservices createWebSvcJar(DDParse parse) throws SAXException{
197           Webservices jar = null;
198           String JavaDoc version = parse.getVersion();
199           if (Webservices.VERSION_1_1.equals(version)) {
200               try {
201                 return new org.netbeans.modules.j2ee.dd.impl.webservices.model_1_1.Webservices(parse.getDocument(), Common.USE_DEFAULT_VALUES);
202               } catch (RuntimeException JavaDoc ex) {
203                   throw new SAXException(ex.getMessage());
204               }
205           } else if (Webservices.VERSION_1_2.equals(version)) {
206               try {
207                 return new org.netbeans.modules.j2ee.dd.impl.webservices.model_1_2.Webservices(parse.getDocument(), Common.USE_DEFAULT_VALUES);
208               } catch (RuntimeException JavaDoc ex) {
209                   throw new SAXException(ex.getMessage());
210               }
211           }
212           
213           return jar;
214     }
215   
216     private static class DDResolver implements EntityResolver {
217         static DDResolver resolver;
218         static synchronized DDResolver getInstance() {
219             if (resolver==null) {
220                 resolver=new DDResolver();
221             }
222             return resolver;
223         }
224         public InputSource resolveEntity (String JavaDoc publicId, String JavaDoc systemId) {
225             if ("http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd".equals(systemId)) {
226                 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/j2ee_web_services_1_1.xsd"); //NOI18N
227
} else if ("http://java.sun.com/xml/ns/j2ee/j2ee_web_services_1_1.xsd".equals(systemId)) {
228                 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/j2ee_web_services_1_1.xsd"); //NOI18N
229
} else if ("http://www.ibm.com/webservices/xsd/javaee_web_services_1_2.xsd".equals(systemId)) {
230                 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/javaee_web_services_1_2.xsd"); //NOI18N
231
} else if ("http://java.sun.com/xml/ns/j2ee/javaee_web_services_1_2.xsd".equals(systemId)) {
232                 return new InputSource("nbres:/org/netbeans/modules/j2ee/dd/impl/resources/javaee_web_services_1_2.xsd"); //NOI18N
233
} else {
234                 // use the default behaviour
235
return null;
236             }
237         }
238     }
239     
240     private static class ErrorHandler implements org.xml.sax.ErrorHandler JavaDoc {
241         private int errorType=-1;
242         SAXParseException error;
243
244         public void warning(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
245             if (errorType<0) {
246                 errorType=0;
247                 error=sAXParseException;
248             }
249             //throw sAXParseException;
250
}
251         public void error(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
252             if (errorType<1) {
253                 errorType=1;
254                 error=sAXParseException;
255             }
256             //throw sAXParseException;
257
}
258         public void fatalError(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
259             errorType=2;
260             throw sAXParseException;
261         }
262         
263         public int getErrorType() {
264             return errorType;
265         }
266         public SAXParseException getError() {
267             return error;
268         }
269     }
270
271     public SAXParseException parse(FileObject fo)
272     throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
273         DDParse parseResult = parseDD(fo);
274         return parseResult.getWarning();
275     }
276     
277     private DDParse parseDD (FileObject fo)
278     throws SAXException, java.io.IOException JavaDoc {
279         return parseDD(fo.getInputStream());
280     }
281     
282     private DDParse parseDD (InputStream JavaDoc is)
283     throws SAXException, java.io.IOException JavaDoc {
284         return parseDD(new InputSource(is));
285     }
286     
287     private DDParse parseDD (InputSource is)
288     throws SAXException, java.io.IOException JavaDoc {
289         DDProvider.ErrorHandler errorHandler = new DDProvider.ErrorHandler();
290         DocumentBuilder JavaDoc parser=null;
291         try {
292             DocumentBuilderFactory JavaDoc fact = DocumentBuilderFactory.newInstance();
293             parser = fact.newDocumentBuilder();
294         } catch (ParserConfigurationException JavaDoc ex) {
295             throw new SAXException(ex.getMessage());
296         }
297         parser.setErrorHandler(errorHandler);
298         parser.setEntityResolver(DDProvider.DDResolver.getInstance());
299         Document JavaDoc d = parser.parse(is);
300         SAXParseException error = errorHandler.getError();
301         return new DDParse(d, error);
302     }
303     
304     /**
305      * This class represents one parse of the deployment descriptor
306      */

307     private static class DDParse {
308         private Document JavaDoc document;
309         private SAXParseException saxException;
310         private String JavaDoc version;
311         public DDParse(Document JavaDoc d, SAXParseException saxEx) {
312             document = d;
313             saxException = saxEx;
314             extractVersion();
315         }
316         
317         /**
318          * @return document from last parse
319          */

320         public Document JavaDoc getDocument() {
321             return document;
322         }
323         
324         /**
325          * @return version of deployment descriptor.
326          */

327         private void extractVersion () {
328             // This is the default version
329
String JavaDoc ver = document.getDocumentElement().getAttribute("version"); //NOI18N
330
version = (Webservices.VERSION_1_1.equals(ver))?Webservices.VERSION_1_1:Webservices.VERSION_1_2;
331         }
332         
333         public String JavaDoc getVersion() {
334             return version;
335         }
336         
337         /**
338          * @return validation error encountered during the parse
339          */

340         public SAXParseException getWarning() {
341             return saxException;
342         }
343     }
344     
345 }
346
Popular Tags