KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > config > api > 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.websvc.wsdl.config.api;
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.util.Map JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30
31 import org.xml.sax.*;
32
33 import org.openide.util.NbBundle;
34 import org.openide.filesystems.*;
35
36 import org.netbeans.modules.websvc.wsdl.config.ConfigurationProxy;
37
38 /**
39  * Provides access to JAX-RPC configuration root (org.netbeans.modules.websvc.wsdl.config.api.Configuration)
40  *
41  * Copied/Inspired by DDProvider from web/ddapi
42  *
43  * @author Peter Williams
44  */

45 public final class DDProvider {
46     private static DDProvider ddProvider;
47     private Map JavaDoc ddMap;
48     private Map JavaDoc baseBeanMap;
49     private Map JavaDoc errorMap;
50     private FCA fileChangeListener;
51
52     private DDProvider() {
53         ddMap=new java.util.HashMap JavaDoc(5);
54         baseBeanMap=new java.util.HashMap JavaDoc(5);
55         errorMap=new java.util.HashMap JavaDoc(5);
56         fileChangeListener = new FCA ();
57     }
58
59     /**
60     * Accessor method for DDProvider singleton
61     * @return DDProvider object
62     */

63     public static synchronized DDProvider getDefault() {
64         if (ddProvider==null) {
65             ddProvider = new DDProvider();
66         }
67         return ddProvider;
68     }
69
70
71     /**
72      * Returns the root of configuration bean graph for given file object.
73      * The method is useful for clients planning to read only the configuration
74      * or to listen to the changes.
75      * @param fo FileObject representing the ???-config.xml file
76      * @return Configuration object - root of the configuration bean graph
77      */

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

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

159     public Configuration getDDRoot(File JavaDoc f) throws IOException JavaDoc, SAXException {
160         return createConfiguration(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.modules.websvc.wsdl.config.api.CommonDDBean bean) {
170
// if (bean instanceof org.netbeans.modules.schema2beans.BaseBean) {
171
// return (org.netbeans.modules.schema2beans.BaseBean)bean;
172
// } else if (bean instanceof ConfigurationProxy) {
173
// return (org.netbeans.modules.schema2beans.BaseBean) ((ConfigurationProxy)bean).getOriginal();
174
// }
175
// return null;
176
// }
177

178     private static Configuration createConfiguration(java.io.InputStream JavaDoc is) throws java.io.IOException JavaDoc, SAXException {
179         try {
180             return org.netbeans.modules.websvc.wsdl.config.impl.Configuration.createGraph(is);
181         } catch (RuntimeException JavaDoc ex) {
182             throw new SAXException (ex.getMessage());
183         }
184     }
185
186     private static class DDResolver implements EntityResolver {
187         static DDResolver resolver;
188         static synchronized DDResolver getInstance() {
189             if(resolver==null) {
190                 resolver=new DDResolver();
191             }
192             return resolver;
193         }
194         public InputSource resolveEntity (String JavaDoc publicId, String JavaDoc systemId) {
195             String JavaDoc resource=null;
196             // return a proper input source
197
//
198
// !PW for schema documents, EntityResolvers do not seem to ever be called.
199
// so this entire object (DDResolver) may be unnecessary.
200
if("http://java.sun.com/xml/ns/jax-rpc/ri/config".equals(systemId)) {
201                 resource="/org/netbeans/modules/websvc/wsdl/resources/jax-rpc-ri-config_1_1.xsd"; //NOI18N
202
}
203             if(resource==null) {
204                 return null;
205             }
206             java.net.URL JavaDoc url = this.getClass().getResource(resource);
207             return new InputSource(url.toString());
208         }
209     }
210
211     private static class ErrorHandler implements org.xml.sax.ErrorHandler JavaDoc {
212         private int errorType=-1;
213         SAXParseException error;
214
215         public void warning(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
216 // System.out.println("ERROR HANDLER (warning): " + sAXParseException.getMessage());
217
if (errorType<0) {
218                 errorType=0;
219                 error=sAXParseException;
220             }
221             //throw sAXParseException;
222
}
223         public void error(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
224 // System.out.println("ERROR HANDLER (error): " + sAXParseException.getMessage());
225
if (errorType<1) {
226                 errorType=1;
227                 error=sAXParseException;
228             }
229             //throw sAXParseException;
230
}
231         public void fatalError(org.xml.sax.SAXParseException JavaDoc sAXParseException) throws org.xml.sax.SAXException JavaDoc {
232 // System.out.println("ERROR HANDLER (fatal): " + sAXParseException.getMessage());
233
errorType=2;
234             throw sAXParseException;
235         }
236
237         public int getErrorType() {
238             return errorType;
239         }
240         public SAXParseException getError() {
241             return error;
242         }
243     }
244     
245     public SAXParseException parse (FileObject fo)
246             throws org.xml.sax.SAXException JavaDoc, java.io.IOException JavaDoc {
247         DDProvider.ErrorHandler errorHandler = new DDProvider.ErrorHandler();
248         try {
249             SAXParser JavaDoc parser = createSAXParserFactory().newSAXParser();
250             XMLReader reader = parser.getXMLReader();
251             reader.setErrorHandler(errorHandler);
252             reader.setEntityResolver(DDProvider.DDResolver.getInstance());
253
254             reader.parse(new InputSource(fo.getInputStream()));
255             SAXParseException error = errorHandler.getError();
256             if (error!=null) {
257                 return error;
258             }
259         } catch (ParserConfigurationException JavaDoc ex) {
260             throw new org.xml.sax.SAXException JavaDoc(ex.getMessage());
261         } catch (SAXException ex) {
262             throw ex;
263         }
264         return null;
265     }
266     
267     /** Method that retrieves SAXParserFactory to get the parser prepared to validate against XML schema
268      */

269     private static SAXParserFactory JavaDoc createSAXParserFactory() throws ParserConfigurationException JavaDoc {
270         try {
271             SAXParserFactory JavaDoc fact = SAXParserFactory.newInstance();
272             if (fact!=null) {
273                 try {
274                     fact.getClass().getMethod("getSchema", new Class JavaDoc[]{}); //NOI18N
275
return fact;
276                 } catch (NoSuchMethodException JavaDoc ex) {}
277             }
278             return (SAXParserFactory JavaDoc) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); // NOI18N
279
} catch (Exception JavaDoc ex) {
280             throw new ParserConfigurationException JavaDoc(ex.getMessage());
281         }
282     }
283
284     private class FCA extends FileChangeAdapter {
285         public void fileChanged(FileEvent evt) {
286             FileObject fo=evt.getFile();
287             try {
288                 ConfigurationProxy configuration = getFromCache (fo);
289                 Configuration orig = getOriginalFromCache (fo);
290                 if (configuration!=null) {
291                     try {
292                         // preparsing
293
SAXParseException error = parse(fo);
294                         if (error!=null) {
295 // System.out.println("Update Error: " + fo.getNameExt() + ", " + error.getMessage());
296
configuration.setError(error);
297                             configuration.setStatus(Configuration.STATE_INVALID_PARSABLE);
298                         } else {
299 // System.out.println("Successful Update: " + fo.getNameExt());
300
configuration.setError(null);
301                             configuration.setStatus(Configuration.STATE_VALID);
302                         }
303                         Configuration original = createConfiguration(fo.getInputStream());
304                         baseBeanMap.put(fo, new WeakReference JavaDoc (original));
305                         errorMap.put(fo, configuration.getError ());
306
307                         // replacing original file in proxy Configuration
308
if (configuration.getOriginal()==null) {
309 // System.out.println("Update: replacing...");
310
configuration.setOriginal(original);
311                         } else {
312 // System.out.println("Update: merging...");
313
configuration.getOriginal().merge(original,Configuration.MERGE_UPDATE);
314                         }
315                     } catch (SAXException ex) {
316 // System.out.println("Update Error: " + fo.getNameExt() + ", " + ex.getMessage());
317
if (ex instanceof SAXParseException) {
318                             configuration.setError((SAXParseException)ex);
319                         } else if ( ex.getException() instanceof SAXParseException) {
320                             configuration.setError((SAXParseException)ex.getException());
321                         }
322                         configuration.setStatus(Configuration.STATE_INVALID_UNPARSABLE);
323                         configuration.setOriginal(null);
324                     }
325                 } else if (orig != null) {
326                     try {
327                         Configuration original = createConfiguration(fo.getInputStream());
328                         if (original.getClass().equals (orig.getClass())) {
329                             orig.merge(original,Configuration.MERGE_UPDATE);
330                         } else {
331                             baseBeanMap.put(fo, new WeakReference JavaDoc (original));
332                         }
333                     } catch (SAXException ex) {
334                         baseBeanMap.remove(fo);
335                     }
336                 }
337             } catch (java.io.IOException JavaDoc ex){}
338         }
339     }
340 }
Popular Tags