001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.registry;
043:
044: import java.io.IOException;
045: import java.net.URL;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Date;
049: import java.util.HashSet;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Set;
053: import java.beans.PropertyChangeEvent;
054: import java.beans.PropertyChangeListener;
055: import java.beans.PropertyChangeSupport;
056: import java.util.StringTokenizer;
057:
058: import org.xml.sax.Attributes;
059: import org.xml.sax.SAXException;
060: import org.xml.sax.helpers.DefaultHandler;
061:
062: import org.openide.nodes.Node;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileStateInvalidException;
065: import org.openide.util.Lookup;
066:
067: import org.netbeans.modules.websvc.registry.model.WebServiceGroup;
068: import org.netbeans.modules.websvc.registry.model.WebServiceListModel;
069: import org.netbeans.modules.websvc.registry.model.WebServiceData;
070: import org.netbeans.modules.websvc.registry.nodes.WebServicesNode;
071: import org.netbeans.modules.websvc.registry.nodes.WebServicesRootNode;
072: import org.netbeans.modules.websvc.registry.util.Util;
073: import org.netbeans.modules.websvc.registry.wsdl.WSDLInfo;
074:
075: import org.netbeans.modules.websvc.api.registry.WebServicesRegistryView;
076: import org.openide.util.lookup.Lookups;
077:
078: /**
079: *
080: * @author Peter Williams
081: */
082: public class RegistryViewImpl implements WebServicesRegistryView,
083: PropertyChangeListener {
084:
085: public RegistryViewImpl() {
086: WebServiceListModel.getInstance().addPropertyChangeListener(
087: this );
088: }
089:
090: // !PW Does this have any use?
091: public Node getRegistryRootNode() {
092: Node rootNode = null;
093:
094: Lookup l = Lookups.forPath("UI/Runtime"); // NOI18N
095: rootNode = (WebServicesRootNode) l
096: .lookup(WebServicesRootNode.class);
097:
098: return rootNode;
099: }
100:
101: public Node[] getWebServiceNodes(FileObject wsdlFile) {
102: // locate all service nodes that came from this wsdl file.
103: // I could turn WSDL file into blocks of WebServiceData, that I then search
104: // for in the list model?
105:
106: Node[] result = null;
107: ArrayList foundServices = new ArrayList();
108:
109: WebServiceListModel model = WebServiceListModel.getInstance();
110: Set registeredServices = model.getWebServiceSet();
111: List serviceNames = getServiceNames(wsdlFile);
112:
113: for (Iterator nameIter = serviceNames.iterator(); nameIter
114: .hasNext();) {
115: String searchName = (String) nameIter.next();
116: searchName = removeSpacesFromServiceName(searchName);
117:
118: for (Iterator iter = registeredServices.iterator(); iter
119: .hasNext();) {
120: WebServiceData wsData = (WebServiceData) iter.next();
121: if (searchName.equalsIgnoreCase(wsData.getName())) {
122: foundServices.add(new WebServicesNode(wsData));
123: break;
124: }
125: }
126: }
127:
128: if (foundServices.size() > 0) {
129: result = (Node[]) foundServices
130: .toArray(new Node[foundServices.size()]);
131: }
132:
133: return result;
134: }
135:
136: public static String removeSpacesFromServiceName(String serviceName) {
137: if ((serviceName != null) && (serviceName.indexOf(" ") > -1)) { //NOI18N
138: String result = ""; //NOI18N
139: StringTokenizer serviceNameTokenizer = new StringTokenizer(
140: serviceName, " ", false); //NOI18N
141: while (serviceNameTokenizer.hasMoreTokens()) {
142: StringBuffer token = new StringBuffer(
143: serviceNameTokenizer.nextToken());
144: if (token != null) {
145: token.setCharAt(0, Character.toUpperCase(token
146: .charAt(0)));
147: result = result.concat(token.toString());
148: }
149: }
150: return result;
151: }
152: return serviceName;
153: }
154:
155: public boolean isServiceRegistered(String serviceName) {
156: return WebServiceListModel.getInstance().webServiceExists(
157: serviceName);
158: }
159:
160: public boolean registerService(FileObject wsdlFile,
161: boolean replaceService) {
162: boolean result;
163:
164: try {
165: result = registerService(wsdlFile.getURL(), "default",
166: replaceService);
167: } catch (FileStateInvalidException ex) {
168: result = false;
169: }
170:
171: return result;
172: }
173:
174: public boolean registerService(URL wsdlUrl, boolean replaceService) {
175: return registerService(wsdlUrl, "default", replaceService);
176: }
177:
178: private boolean registerService(URL wsdlUrl, String groupId,
179: boolean replaceService) {
180: boolean result = false;
181:
182: // Retrieve WSDL and convert into a Set of WebServiceData objects
183: WSDLInfo wsdLInfo = new WSDLInfo();
184: wsdLInfo.setWsdlUrl(wsdlUrl);
185: wsdLInfo.setPackageName("webservice"); // NOI18N
186: wsdLInfo.setRemoveGeneratedFiles(true);
187:
188: if (wsdLInfo.create()) {
189: Set webServices = wsdLInfo.getWebServices();
190: if (webServices != null && webServices.size() > 0) {
191: Iterator iter = webServices.iterator();
192: boolean duplicateFound = false;
193: WebServiceListModel wsListModel = WebServiceListModel
194: .getInstance();
195:
196: // Assume that all of the web services adds failed. For each
197: // success, remove the web service from the failed set.
198: HashSet addFailedWebServices = new HashSet();
199: addFailedWebServices.addAll(webServices);
200:
201: while (iter.hasNext()) {
202: WebServiceData wsData = (WebServiceData) iter
203: .next();
204: String targetGroupId = groupId;
205: WebServiceData existingWS = wsListModel
206: .findService(wsData);
207: if (existingWS != null) {
208: String existingWSGroupId = existingWS
209: .getGroupId();
210: if (existingWSGroupId != null)
211: targetGroupId = existingWS.getGroupId();
212: if (replaceService) {
213: // remove old service first
214: wsListModel.removeWebService(existingWS);
215: } else {
216: // !PW Failed: Web service of that name already exists in model
217: continue;
218: }
219: }
220:
221: // Now create the client code for the web service.
222: String jarFileName = System
223: .getProperty("netbeans.user")
224: + "/websvc/"
225: + "webservice"
226: + new Date().getTime() + ".jar";
227:
228: if (!Util.createWSJar(wsData, null, jarFileName)) {
229: // !PW FIXME failed compilation
230: continue;
231: }
232:
233: // Set the jar filename
234: // !PW why isn't this done by Util.createWSJar?!
235: wsData.setProxyJarFileName(jarFileName);
236:
237: // Add it to the list of web services for Server Navigator
238: wsListModel.addWebService(wsData);
239:
240: // Put service in 'default'.
241: WebServiceGroup wsGroup = wsListModel
242: .getWebServiceGroup(targetGroupId);
243: // !PW Fix for 49717 - If this group does not exist yet, create it.
244: if (wsGroup == null) {
245: wsGroup = new WebServiceGroup(targetGroupId);
246: wsListModel.addWebServiceGroup(wsGroup);
247: }
248:
249: wsGroup.add(wsData.getId());
250: wsData.setGroupId(wsGroup.getId());
251:
252: addFailedWebServices.remove(wsData);
253: }
254:
255: // Return true if we added all services in this WSDL file, otherwise false.
256: // !PW Need better error control and messaging.
257: result = (addFailedWebServices.size() == 0);
258: }
259: } else {
260: // !PW Failed to create WsdlInfo (bad URL, bad parse, etc.)
261: }
262:
263: return result;
264: }
265:
266: private List getServiceNames(FileObject wsdlFile) {
267: List result = Collections.EMPTY_LIST;
268:
269: try {
270: org.xml.sax.XMLReader xmlReader = org.openide.xml.XMLUtil
271: .createXMLReader(false, true);
272: ServiceNameParser handler = new ServiceNameParser();
273: xmlReader.setContentHandler(handler);
274: xmlReader.parse(new org.xml.sax.InputSource(wsdlFile
275: .getInputStream()));
276: result = handler.getServiceNameList();
277: } catch (SAXException ex) {
278: // Bogus WSDL, return empty list.
279: } catch (IOException ex) {
280: // Bogus WSDL, return empty list.
281: }
282:
283: return result;
284: }
285:
286: /** Property change support
287: */
288: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
289: this );
290:
291: public void addPropertyChangeListener(
292: PropertyChangeListener listener) {
293: propertyChangeSupport.addPropertyChangeListener(listener);
294: }
295:
296: public void removePropertyChangeListener(
297: PropertyChangeListener listener) {
298: propertyChangeSupport.removePropertyChangeListener(listener);
299: }
300:
301: public void propertyChange(PropertyChangeEvent evt) {
302: if (WebServiceListModel.MODEL_SERVICE_ADDED.equals(evt
303: .getPropertyName())) {
304: WebServiceData wsData = (WebServiceData) evt.getNewValue();
305: fireServiceAdded(new WebServicesNode(wsData));
306: } else if (WebServiceListModel.MODEL_SERVICE_REMOVED.equals(evt
307: .getPropertyName())) {
308: WebServiceData wsData = (WebServiceData) evt.getOldValue();
309: fireServiceRemoved(wsData.getName());
310: }
311: }
312:
313: private void fireServiceAdded(Node wsNode) {
314: propertyChangeSupport.firePropertyChange(WEB_SERVICE_ADDED,
315: null, wsNode);
316: }
317:
318: private void fireServiceRemoved(String serviceName) {
319: propertyChangeSupport.firePropertyChange(WEB_SERVICE_REMOVED,
320: serviceName, null);
321: }
322:
323: private static final class ServiceNameParser extends DefaultHandler {
324:
325: private static final String W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl";
326: private static final String W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/";
327:
328: private ArrayList serviceNameList;
329:
330: ServiceNameParser() {
331: serviceNameList = new ArrayList();
332: }
333:
334: public void startElement(String uri, String localname,
335: String qname, Attributes attributes)
336: throws SAXException {
337: if (W3C_WSDL_SCHEMA.equals(uri)
338: || W3C_WSDL_SCHEMA_SLASH.equals(uri)) {
339: if ("service".equals(localname)) {
340: serviceNameList.add(attributes.getValue("name"));
341: }
342: }
343: }
344:
345: public List/*String*/getServiceNameList() {
346: return serviceNameList;
347: }
348: }
349: }
|