1 19 20 package org.netbeans.modules.websvc.registry; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Date ; 27 import java.util.EmptyStackException ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 import java.util.Stack ; 33 import java.beans.PropertyChangeEvent ; 34 import java.beans.PropertyChangeListener ; 35 import java.beans.PropertyChangeSupport ; 36 import java.util.StringTokenizer ; 37 import org.openide.xml.XMLUtil; 38 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 import javax.xml.parsers.SAXParser ; 43 import javax.xml.parsers.SAXParserFactory ; 44 import javax.xml.parsers.ParserConfigurationException ; 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 72 public class RegistryViewImpl implements WebServicesRegistryView, PropertyChangeListener { 73 74 public RegistryViewImpl() { 75 WebServiceListModel.getInstance().addPropertyChangeListener(this); 76 } 77 78 public Node getRegistryRootNode() { 80 Node rootNode = null; 81 82 try { 83 FileSystem defaultFileSystem = Repository.getDefault().getDefaultFileSystem(); 84 FileObject fo = defaultFileSystem.findResource("UI/Runtime"); 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 101 Node [] result = null; 102 ArrayList foundServices = new ArrayList (); 103 104 WebServiceListModel model = WebServiceListModel.getInstance(); 105 Set registeredServices = model.getWebServiceSet(); 106 List serviceNames = getServiceNames(wsdlFile); 107 108 for (Iterator nameIter = serviceNames.iterator(); nameIter.hasNext(); ) { 109 String searchName = (String ) nameIter.next(); 110 searchName = removeSpacesFromServiceName(searchName); 111 112 for (Iterator 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 removeSpacesFromServiceName(String serviceName) { 129 if ((serviceName!=null) && (serviceName.indexOf(" ") > -1)) { String result = ""; StringTokenizer serviceNameTokenizer = new StringTokenizer (serviceName, " ", false); while (serviceNameTokenizer.hasMoreTokens()) { 133 StringBuffer token = new StringBuffer (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 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 wsdlUrl, boolean replaceService) { 161 return registerService(wsdlUrl, "default", replaceService); 162 } 163 164 private boolean registerService(URL wsdlUrl, String groupId, boolean replaceService) { 165 boolean result = false; 166 167 WSDLInfo wsdLInfo = new WSDLInfo(); 169 wsdLInfo.setWsdlUrl(wsdlUrl); 170 wsdLInfo.setPackageName("webservice"); wsdLInfo.setRemoveGeneratedFiles(true); 172 173 if(wsdLInfo.create()) { 174 Set webServices = wsdLInfo.getWebServices(); 175 if(webServices != null && webServices.size() > 0) { 176 Iterator iter = webServices.iterator(); 177 boolean duplicateFound = false; 178 WebServiceListModel wsListModel = WebServiceListModel.getInstance(); 179 180 HashSet addFailedWebServices = new HashSet (); 183 addFailedWebServices.addAll(webServices); 184 185 while(iter.hasNext()){ 186 WebServiceData wsData = (WebServiceData) iter.next(); 187 String targetGroupId = groupId; 188 WebServiceData existingWS = wsListModel.findService(wsData); 189 if (existingWS!=null) { 190 String existingWSGroupId = existingWS.getGroupId(); 191 if (existingWSGroupId!=null) targetGroupId = existingWS.getGroupId(); 192 if(replaceService) { 193 wsListModel.removeWebService(existingWS); 195 } else { 196 continue; 198 } 199 } 200 201 String jarFileName = System.getProperty("netbeans.user") +"/websvc/" + "webservice" + new Date ().getTime() + ".jar"; 203 204 if(!Util.createWSJar(wsData, null, jarFileName)) { 205 continue; 207 } 208 209 wsData.setProxyJarFileName(jarFileName); 212 213 wsListModel.addWebService(wsData); 215 216 WebServiceGroup wsGroup = wsListModel.getWebServiceGroup(targetGroupId); 218 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 result = (addFailedWebServices.size() == 0); 233 } 234 } else { 235 } 237 238 return result; 239 } 240 241 private List getServiceNames(FileObject wsdlFile) { 242 List result = Collections.EMPTY_LIST; 243 244 try { 245 org.xml.sax.XMLReader 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 (wsdlFile.getInputStream())); 249 result = handler.getServiceNameList(); 250 } catch(SAXException ex) { 251 } catch(IOException ex) { 253 } 255 256 return result; 257 } 258 259 261 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); 262 263 public void addPropertyChangeListener(PropertyChangeListener listener) { 264 propertyChangeSupport.addPropertyChangeListener(listener); 265 } 266 267 public void removePropertyChangeListener(PropertyChangeListener listener) { 268 propertyChangeSupport.removePropertyChangeListener(listener); 269 } 270 271 public void propertyChange(PropertyChangeEvent 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 serviceName) { 286 propertyChangeSupport.firePropertyChange(WEB_SERVICE_REMOVED, serviceName, null); 287 } 288 289 private static final class ServiceNameParser extends DefaultHandler { 290 291 private static final String W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl"; 292 private static final String W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/"; 293 294 private ArrayList serviceNameList; 295 296 ServiceNameParser() { 297 serviceNameList = new ArrayList (); 298 } 299 300 public void startElement(String uri, String localname, String qname, Attributes attributes) throws SAXException { 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 getServiceNameList() { 309 return serviceNameList; 310 } 311 } 312 } 313 | Popular Tags |