KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > RegistryViewImpl


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.registry;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.EmptyStackException JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Stack JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.beans.PropertyChangeSupport JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import org.openide.xml.XMLUtil;
38
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.helpers.DefaultHandler JavaDoc;
42 import javax.xml.parsers.SAXParser JavaDoc;
43 import javax.xml.parsers.SAXParserFactory JavaDoc;
44 import javax.xml.parsers.ParserConfigurationException JavaDoc;
45
46 import org.openide.ErrorManager;
47 import org.openide.nodes.Node;
48 import org.openide.filesystems.FileSystem;
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileStateInvalidException;
51 import org.openide.filesystems.Repository;
52 import org.openide.loaders.DataObject;
53 import org.openide.loaders.DataFolder;
54 import org.openide.loaders.FolderLookup;
55 import org.openide.loaders.DataObjectNotFoundException;
56 import org.openide.util.Lookup;
57
58 import org.netbeans.modules.websvc.registry.model.WebServiceGroup;
59 import org.netbeans.modules.websvc.registry.model.WebServiceListModel;
60 import org.netbeans.modules.websvc.registry.model.WebServiceData;
61 import org.netbeans.modules.websvc.registry.nodes.WebServicesNode;
62 import org.netbeans.modules.websvc.registry.nodes.WebServicesRootNode;
63 import org.netbeans.modules.websvc.registry.util.Util;
64 import org.netbeans.modules.websvc.registry.wsdl.WSDLInfo;
65
66 import org.netbeans.modules.websvc.api.registry.WebServicesRegistryView;
67
68 /**
69  *
70  * @author Peter Williams
71  */

72 public class RegistryViewImpl implements WebServicesRegistryView, PropertyChangeListener JavaDoc {
73
74     public RegistryViewImpl() {
75         WebServiceListModel.getInstance().addPropertyChangeListener(this);
76     }
77
78     // !PW Does this have any use?
79
public Node getRegistryRootNode() {
80         Node rootNode = null;
81
82         try {
83             FileSystem defaultFileSystem = Repository.getDefault().getDefaultFileSystem();
84             FileObject fo = defaultFileSystem.findResource("UI/Runtime"); //NOI18N
85
DataFolder df = (DataFolder) DataObject.find(fo);
86             Lookup l = new FolderLookup(df).getLookup();
87             rootNode = (WebServicesRootNode) l.lookup(WebServicesRootNode.class);
88         } catch (DataObjectNotFoundException ex) {
89             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
90             rootNode = null;
91         }
92
93         return rootNode;
94     }
95
96     public Node[] getWebServiceNodes(FileObject wsdlFile) {
97         // locate all service nodes that came from this wsdl file.
98
// I could turn WSDL file into blocks of WebServiceData, that I then search
99
// for in the list model?
100

101         Node [] result = null;
102         ArrayList JavaDoc foundServices = new ArrayList JavaDoc();
103
104         WebServiceListModel model = WebServiceListModel.getInstance();
105         Set JavaDoc registeredServices = model.getWebServiceSet();
106         List JavaDoc serviceNames = getServiceNames(wsdlFile);
107
108         for (Iterator JavaDoc nameIter = serviceNames.iterator(); nameIter.hasNext(); ) {
109             String JavaDoc searchName = (String JavaDoc) nameIter.next();
110             searchName = removeSpacesFromServiceName(searchName);
111
112             for (Iterator JavaDoc iter = registeredServices.iterator(); iter.hasNext(); ) {
113                 WebServiceData wsData = (WebServiceData) iter.next();
114                 if(searchName.equalsIgnoreCase(wsData.getName())) {
115                     foundServices.add(new WebServicesNode(wsData));
116                     break;
117                 }
118             }
119         }
120
121         if(foundServices.size() > 0) {
122             result = (Node []) foundServices.toArray(new Node [foundServices.size()]);
123         }
124
125         return result;
126     }
127
128     public static String JavaDoc removeSpacesFromServiceName(String JavaDoc serviceName) {
129         if ((serviceName!=null) && (serviceName.indexOf(" ") > -1)) { //NOI18N
130
String JavaDoc result = ""; //NOI18N
131
StringTokenizer JavaDoc serviceNameTokenizer = new StringTokenizer JavaDoc(serviceName, " ", false); //NOI18N
132
while (serviceNameTokenizer.hasMoreTokens()) {
133                 StringBuffer JavaDoc token = new StringBuffer JavaDoc(serviceNameTokenizer.nextToken());
134                 if (token != null) {
135                     token.setCharAt(0, Character.toUpperCase(token.charAt(0)));
136                     result = result.concat(token.toString());
137                 }
138             }
139             return result;
140         }
141         return serviceName;
142     }
143     
144     public boolean isServiceRegistered(String JavaDoc serviceName) {
145         return WebServiceListModel.getInstance().webServiceExists(serviceName);
146     }
147
148     public boolean registerService(FileObject wsdlFile, boolean replaceService) {
149         boolean result;
150
151         try {
152             result = registerService(wsdlFile.getURL(), "default", replaceService);
153         } catch(FileStateInvalidException ex) {
154             result = false;
155         }
156
157         return result;
158     }
159
160     public boolean registerService(URL JavaDoc wsdlUrl, boolean replaceService) {
161         return registerService(wsdlUrl, "default", replaceService);
162     }
163
164     private boolean registerService(URL JavaDoc wsdlUrl, String JavaDoc groupId, boolean replaceService) {
165         boolean result = false;
166
167         // Retrieve WSDL and convert into a Set of WebServiceData objects
168
WSDLInfo wsdLInfo = new WSDLInfo();
169         wsdLInfo.setWsdlUrl(wsdlUrl);
170         wsdLInfo.setPackageName("webservice"); // NOI18N
171
wsdLInfo.setRemoveGeneratedFiles(true);
172
173         if(wsdLInfo.create()) {
174             Set JavaDoc webServices = wsdLInfo.getWebServices();
175             if(webServices != null && webServices.size() > 0) {
176                 Iterator JavaDoc iter = webServices.iterator();
177                 boolean duplicateFound = false;
178                 WebServiceListModel wsListModel = WebServiceListModel.getInstance();
179
180                 // Assume that all of the web services adds failed. For each
181
// success, remove the web service from the failed set.
182
HashSet JavaDoc addFailedWebServices = new HashSet JavaDoc();
183                 addFailedWebServices.addAll(webServices);
184
185                 while(iter.hasNext()){
186                     WebServiceData wsData = (WebServiceData) iter.next();
187                     String JavaDoc targetGroupId = groupId;
188                     WebServiceData existingWS = wsListModel.findService(wsData);
189                     if (existingWS!=null) {
190                         String JavaDoc existingWSGroupId = existingWS.getGroupId();
191                         if (existingWSGroupId!=null) targetGroupId = existingWS.getGroupId();
192                         if(replaceService) {
193                             // remove old service first
194
wsListModel.removeWebService(existingWS);
195                         } else {
196                             // !PW Failed: Web service of that name already exists in model
197
continue;
198                         }
199                     }
200
201                     // Now create the client code for the web service.
202
String JavaDoc jarFileName = System.getProperty("netbeans.user") +"/websvc/" + "webservice" + new Date JavaDoc().getTime() + ".jar";
203
204                     if(!Util.createWSJar(wsData, null, jarFileName)) {
205                         // !PW FIXME failed compilation
206
continue;
207                     }
208
209                     // Set the jar filename
210
// !PW why isn't this done by Util.createWSJar?!
211
wsData.setProxyJarFileName(jarFileName);
212
213                     // Add it to the list of web services for Server Navigator
214
wsListModel.addWebService(wsData);
215
216                     // Put service in 'default'.
217
WebServiceGroup wsGroup = wsListModel.getWebServiceGroup(targetGroupId);
218                     // !PW Fix for 49717 - If this group does not exist yet, create it.
219
if(wsGroup == null) {
220                         wsGroup = new WebServiceGroup(targetGroupId);
221                         wsListModel.addWebServiceGroup(wsGroup);
222                     }
223
224                     wsGroup.add(wsData.getId());
225                     wsData.setGroupId(wsGroup.getId());
226
227                     addFailedWebServices.remove(wsData);
228                 }
229
230                 // Return true if we added all services in this WSDL file, otherwise false.
231
// !PW Need better error control and messaging.
232
result = (addFailedWebServices.size() == 0);
233             }
234         } else {
235             // !PW Failed to create WsdlInfo (bad URL, bad parse, etc.)
236
}
237
238         return result;
239     }
240
241     private List JavaDoc getServiceNames(FileObject wsdlFile) {
242         List JavaDoc result = Collections.EMPTY_LIST;
243
244         try {
245             org.xml.sax.XMLReader JavaDoc xmlReader = org.openide.xml.XMLUtil.createXMLReader(false,true);
246             ServiceNameParser handler= new ServiceNameParser();
247             xmlReader.setContentHandler(handler);
248             xmlReader.parse(new org.xml.sax.InputSource JavaDoc(wsdlFile.getInputStream()));
249             result = handler.getServiceNameList();
250         } catch(SAXException JavaDoc ex) {
251             // Bogus WSDL, return empty list.
252
} catch(IOException JavaDoc ex) {
253             // Bogus WSDL, return empty list.
254
}
255
256         return result;
257     }
258
259     /** Property change support
260      */

261     private PropertyChangeSupport JavaDoc propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
262
263     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
264         propertyChangeSupport.addPropertyChangeListener(listener);
265     }
266
267     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
268         propertyChangeSupport.removePropertyChangeListener(listener);
269     }
270
271     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
272         if(WebServiceListModel.MODEL_SERVICE_ADDED.equals(evt.getPropertyName())) {
273             WebServiceData wsData = (WebServiceData) evt.getNewValue();
274             fireServiceAdded(new WebServicesNode(wsData));
275         } else if(WebServiceListModel.MODEL_SERVICE_REMOVED.equals(evt.getPropertyName())) {
276             WebServiceData wsData = (WebServiceData) evt.getOldValue();
277             fireServiceRemoved(wsData.getName());
278         }
279     }
280
281     private void fireServiceAdded(Node wsNode) {
282         propertyChangeSupport.firePropertyChange(WEB_SERVICE_ADDED, null, wsNode);
283     }
284
285     private void fireServiceRemoved(String JavaDoc serviceName) {
286         propertyChangeSupport.firePropertyChange(WEB_SERVICE_REMOVED, serviceName, null);
287     }
288
289     private static final class ServiceNameParser extends DefaultHandler JavaDoc {
290
291         private static final String JavaDoc W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl";
292         private static final String JavaDoc W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/";
293
294         private ArrayList JavaDoc serviceNameList;
295
296         ServiceNameParser() {
297             serviceNameList = new ArrayList JavaDoc();
298         }
299
300         public void startElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname, Attributes JavaDoc attributes) throws SAXException JavaDoc {
301             if(W3C_WSDL_SCHEMA.equals(uri) || W3C_WSDL_SCHEMA_SLASH.equals(uri)) {
302                 if("service".equals(localname)) {
303                     serviceNameList.add(attributes.getValue("name"));
304                 }
305             }
306         }
307
308         public List JavaDoc/*String*/ getServiceNameList() {
309             return serviceNameList;
310         }
311     }
312 }
313
Popular Tags