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.jaxrpc.nodes;
043:
044: import java.util.ArrayList;
045: import java.util.Iterator;
046: import java.util.List;
047: import javax.swing.SwingUtilities;
048: import org.netbeans.api.project.FileOwnerQuery;
049: import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
050: import org.netbeans.modules.websvc.api.client.WsCompileClientEditorSupport;
051: import org.openide.nodes.FilterNode;
052: import org.openide.nodes.Node;
053: import org.openide.filesystems.FileObject;
054: import org.openide.loaders.DataObject;
055: import org.openide.loaders.DataObjectNotFoundException;
056: import org.openide.util.Lookup;
057:
058: import org.netbeans.modules.websvc.api.registry.WebServicesRegistryView;
059: import org.netbeans.spi.project.support.ant.AntProjectHelper;
060: import org.openide.ErrorManager;
061: import org.openide.filesystems.FileChangeAdapter;
062: import org.openide.filesystems.FileEvent;
063: import org.openide.filesystems.FileUtil;
064:
065: /** Displays web service client nodes representing the web services for which
066: * this module has been enabled to use as a client. Driven by existence of
067: * wsdl files in the wsdl folder of the owner module.
068: *
069: * @author Peter Williams
070: */
071: public class ClientViewChildren extends FilterNode.Children {
072: FileObject wsdlFolder;
073: private final ProjectXmlListener projectXmlListener;
074:
075: public ClientViewChildren(FileObject wsdlFolder)
076: throws DataObjectNotFoundException {
077: super (DataObject.find(wsdlFolder).getNodeDelegate());
078: this .wsdlFolder = wsdlFolder;
079: this .projectXmlListener = new ProjectXmlListener();
080: }
081:
082: protected void addNotify() {
083: super .addNotify();
084: updateKeys();
085: FileObject prjXml = FileOwnerQuery.getOwner(wsdlFolder)
086: .getProjectDirectory().getFileObject(
087: AntProjectHelper.PROJECT_XML_PATH);
088: if (prjXml != null) {
089: prjXml
090: .addFileChangeListener(FileUtil
091: .weakFileChangeListener(projectXmlListener,
092: prjXml));
093: }
094: }
095:
096: private void updateKeys() {
097: List<Node> children = new ArrayList<Node>();
098: try {
099: //we need to set the keys as empty children and
100: //then as right children for refresh to work
101: this .setKeys(children);
102: FileObject[] wsdls = wsdlFolder.getChildren();
103: for (int i = 0; i < wsdls.length; i++) {
104: Node n = DataObject.find(wsdls[i]).getNodeDelegate();
105: children.add(n);
106: }
107: this .setKeys(children);
108: } catch (DataObjectNotFoundException ex) {
109: ErrorManager.getDefault().notify(ex);
110: }
111: }
112:
113: protected Node[] createNodes(Node origNode) {
114: Node[] results = new Node[0]; // Default return value is no nodes created.
115:
116: DataObject wsdlDataObject = (DataObject) origNode
117: .getCookie(DataObject.class);
118:
119: if (wsdlDataObject != null && isClient(wsdlDataObject)) {
120: WebServicesRegistryView registryView = (WebServicesRegistryView) Lookup
121: .getDefault().lookup(WebServicesRegistryView.class);
122: Node[] serviceNodes = registryView
123: .getWebServiceNodes(wsdlDataObject.getPrimaryFile());
124: if (serviceNodes != null) {
125: results = new Node[serviceNodes.length];
126: for (int i = 0; i < serviceNodes.length; i++) {
127: results[i] = new ServiceClientNode(origNode,
128: serviceNodes[i]);
129: }
130: } else {
131: results = new Node[] { new ServiceClientNode(origNode,
132: null) };
133:
134: final FileObject wsdlFileObject = wsdlDataObject
135: .getPrimaryFile();
136: final WebServicesRegistryView regView = registryView;
137: Thread registerThread = new Thread(new Runnable() {
138: public void run() {
139: regView.registerService(wsdlFileObject, true);
140: }
141: }, "RegisterWSClient " + wsdlFileObject.getNameExt()); // NOI18N
142: registerThread.start();
143: }
144: }
145:
146: return results;
147: }
148:
149: /** We assume that any wsdl file in the source wsdl folder is a client wsdl
150: * except when there exists a mapping file in the parent ddfolder (which is
151: * either WEB-INF or META-INF). In this case, the wsdl should be for a service
152: * that is being developed from wsdl rather than from SEI.
153: *
154: * A more stable way would be to only reference clients mentioned in project.xml
155: * but that was a harder list to get at the time (easier now because there
156: * is actually an API for it.
157: */
158: private boolean isClient(DataObject dobj) {
159: FileObject wsdlFileObject = dobj.getPrimaryFile();
160: WebServicesClientSupport wss = WebServicesClientSupport
161: .getWebServicesClientSupport(wsdlFileObject);
162: List wsClients = wss.getServiceClients();
163: Iterator i = wsClients.iterator();
164: while (i.hasNext()) {
165: WsCompileClientEditorSupport.ServiceSettings wsClient = (WsCompileClientEditorSupport.ServiceSettings) i
166: .next();
167: String wsdlFileName = wsdlFileObject.getName();
168: if (wsdlFileName.equals(wsClient.getServiceName())) {
169: return true;
170: }
171: }
172: return false;
173: }
174:
175: private final class ProjectXmlListener extends FileChangeAdapter {
176: public void fileChanged(FileEvent fe) {
177: SwingUtilities.invokeLater(new Runnable() {
178: public void run() {
179: updateKeys();
180: }
181: });
182: }
183: }
184:
185: }
|