KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > api > jaxws > wsdlmodel > WsdlModeler


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.api.jaxws.wsdlmodel;
21
22 import com.sun.tools.ws.processor.model.Model;
23 import com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler;
24 import com.sun.tools.ws.wscompile.AbortException;
25 import com.sun.tools.ws.wscompile.BadCommandLineException;
26 import com.sun.tools.ws.wscompile.ErrorReceiver;
27 import com.sun.tools.ws.wscompile.WsimportOptions;
28 import com.sun.xml.ws.util.JAXWSUtils;
29 import java.io.File JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import org.apache.xml.resolver.CatalogManager;
39 import org.apache.xml.resolver.tools.CatalogResolver;
40 import org.openide.util.RequestProcessor;
41 import org.xml.sax.EntityResolver JavaDoc;
42 import org.xml.sax.SAXParseException JavaDoc;
43
44 /**
45  *
46  * @author mkuchtiak
47  */

48 public class WsdlModeler {
49     
50     private WsdlModel wsdlModel;
51     private WSDLModeler ideWSDLModeler;
52     private URL JavaDoc wsdlUrl;
53     private URL JavaDoc[] bindings;
54     private URL JavaDoc catalog;
55     private EntityResolver JavaDoc entityResolver;
56     private Set JavaDoc<String JavaDoc> bindingFiles;
57     private String JavaDoc packageName;
58     private List JavaDoc<WsdlModelListener> modelListeners;
59     RequestProcessor.Task task;
60     int listenersSize;
61     
62     protected Properties JavaDoc properties;
63     
64     private Throwable JavaDoc creationException;
65     /** Creates a new instance of WsdlModeler */
66     WsdlModeler(URL JavaDoc wsdlUrl) {
67         this.wsdlUrl=wsdlUrl;
68         modelListeners = new ArrayList JavaDoc<WsdlModelListener>();
69         task = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
70             public void run() {
71                 generateWsdlModel();
72                 synchronized (this) {
73                     listenersSize = modelListeners.size();
74                     fireModelCreated(wsdlModel,listenersSize);
75                     removeListeners();
76                 }
77             }
78         },true);
79     }
80     
81     public void setPackageName(String JavaDoc packageName) {
82         this.packageName=packageName;
83     }
84     
85     public String JavaDoc getPackageName() {
86         return packageName;
87     }
88     
89     public void setJAXBBindings(URL JavaDoc[] bindings) {
90         this.bindings=bindings;
91     }
92     
93     public URL JavaDoc[] getJAXBBindings() {
94         return bindings;
95     }
96     
97     public void setCatalog(URL JavaDoc catalog) {
98         this.catalog=catalog;
99     }
100     
101     public URL JavaDoc getCatalog() {
102         return catalog;
103     }
104     
105     void setWsdlUrl(URL JavaDoc url){
106         wsdlUrl = url;
107     }
108     
109     public URL JavaDoc getWsdlUrl(){
110         return wsdlUrl;
111     }
112     
113     public Throwable JavaDoc getCreationException() {
114         return creationException;
115     }
116     
117     public WsdlModel getWsdlModel() {
118         return wsdlModel;
119     }
120     
121     public WsdlModel getAndWaitForWsdlModel() {
122         if (getWsdlModel()==null) generateWsdlModel();
123         return wsdlModel;
124     }
125     
126     public void generateWsdlModel(WsdlModelListener listener) {
127         generateWsdlModel(listener,false);
128     }
129     
130     public void generateWsdlModel(WsdlModelListener listener, boolean forceReload) {
131         if (forceReload) {
132             try {task.waitFinished(10000);} catch (InterruptedException JavaDoc ex) {}
133             addWsdlModelListener(listener);
134             task.schedule(0);
135         } else {
136             addWsdlModelListener(listener);
137             if (task.isFinished()) {
138                 task.schedule(0);
139             }
140         }
141     }
142     
143     private synchronized void generateWsdlModel() {
144         WsimportOptions options = new WsimportOptions();
145         properties = new Properties JavaDoc();
146         bindingFiles = new HashSet JavaDoc<String JavaDoc>();
147         if (bindings!=null) {
148             for (int i=0;i<bindings.length;i++) {
149                 try {
150                     options.addBindings(JAXWSUtils.absolutize(bindings[i].toExternalForm()));
151                 } catch (BadCommandLineException ex) {
152                     Logger.getLogger(this.getClass().getName()).log(Level.FINE, "WsdlModeler.generateWsdlModel", ex); //NOI18N
153
}
154             }
155         }
156         try {
157             options.addWSDL(new File JavaDoc(wsdlUrl.toURI()));
158             options.compatibilityMode = WsimportOptions.EXTENSION;
159             
160             if (packageName!=null) {
161                 options.defaultPackage = packageName;
162             }
163             if(catalog != null) {
164                 CatalogManager manager = new CatalogManager(null);
165                 manager.setCatalogFiles(catalog.toExternalForm());
166                 entityResolver = new CatalogResolver(manager);
167                 options.entityResolver = entityResolver;
168             }
169             
170             options.parseBindings(new IdeErrorReceiver());
171             
172             ideWSDLModeler =
173                     new WSDLModeler(options, new IdeErrorReceiver());
174             Model tmpModel = ideWSDLModeler.buildModel();
175             
176             if (tmpModel!=null) {
177                 wsdlModel=new WsdlModel(tmpModel);
178                 creationException=null;
179             }
180         } catch (Exception JavaDoc ex){
181             wsdlModel=null;
182             creationException=ex;
183             Logger.getLogger(this.getClass().getName()).log(Level.FINE, "WsdlModeler.generateWsdlModel", ex); //NOI18N
184
}
185         
186     }
187     
188     private synchronized void addWsdlModelListener(WsdlModelListener listener) {
189         if (listener!=null)
190             modelListeners.add(listener);
191     }
192     
193     private void removeListeners() {
194         modelListeners.clear();
195     }
196     
197     private void fireModelCreated(WsdlModel model, int listenersSize) {
198         for (int i=0;i<listenersSize;i++) {
199             modelListeners.get(i).modelCreated(model);
200         }
201     }
202     
203     private class IdeErrorReceiver extends ErrorReceiver{
204         public void warning(SAXParseException JavaDoc ex) throws AbortException {
205             Logger.getLogger(this.getClass().getName()).log(Level.FINE,
206                     "WsdlModeler.generateWsdlModel", ex); //NOI18N
207
}
208         
209         public void info(SAXParseException JavaDoc ex) {
210             Logger.getLogger(this.getClass().getName()).log(Level.FINE,
211                     "WsdlModeler.generateWsdlModel", ex); //NOI18N
212
}
213         
214         public void fatalError(SAXParseException JavaDoc ex) throws AbortException {
215             Logger.getLogger(this.getClass().getName()).log(Level.FINE,
216                     "WsdlModeler.generateWsdlModel", ex); //NOI18N
217
}
218         
219         public void error(SAXParseException JavaDoc ex) throws AbortException {
220             Logger.getLogger(this.getClass().getName()).log(Level.FINE,
221                     "WsdlModeler.generateWsdlModel", ex); //NOI18N
222
}
223         
224     }
225 }
226
Popular Tags