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: package org.netbeans.modules.websvc.core.jaxws.nodes;
042:
043: /** WSDL children (Service elements)
044: *
045: * @author mkuchtiak
046: */
047: import java.io.IOException;
048: import java.net.URI;
049: import java.net.URISyntaxException;
050: import java.net.UnknownHostException;
051: import java.util.ArrayList;
052: import java.util.Collections;
053: import java.util.List;
054: import org.apache.tools.ant.module.api.support.ActionUtils;
055: import org.netbeans.api.project.FileOwnerQuery;
056: import org.netbeans.api.project.Project;
057: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
058: import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
059: import org.netbeans.modules.websvc.api.jaxws.project.GeneratedFilesHelper;
060: import org.netbeans.modules.websvc.api.jaxws.project.config.Client;
061: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel;
062: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener;
063: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler;
064: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService;
065: import org.netbeans.modules.websvc.api.jaxws.project.WSUtils;
066: import org.netbeans.modules.websvc.core.JaxWsUtils;
067: import org.openide.DialogDisplayer;
068: import org.openide.ErrorManager;
069: import org.openide.execution.ExecutorTask;
070: import org.openide.filesystems.FileLock;
071: import org.openide.filesystems.FileObject;
072: import org.openide.filesystems.FileUtil;
073: import org.openide.nodes.Children;
074: import org.openide.nodes.Node;
075: import org.openide.util.NbBundle;
076:
077: public class JaxWsClientChildren extends Children.Keys<WsdlService> {
078:
079: Client client;
080: WsdlModel wsdlModel;
081: FileObject srcRoot;
082:
083: public JaxWsClientChildren(Client client, FileObject srcRoot) {
084: this .client = client;
085: this .srcRoot = srcRoot;
086: }
087:
088: @Override
089: protected void addNotify() {
090: final WsdlModeler wsdlModeler = ((JaxWsClientNode) getNode())
091: .getWsdlModeler();
092: if (wsdlModeler != null) {
093: JAXWSClientSupport clientSupport = JAXWSClientSupport
094: .getJaxWsClientSupport(srcRoot);
095: if (clientSupport != null) {
096: wsdlModeler.setCatalog(clientSupport.getCatalog());
097: }
098: wsdlModel = wsdlModeler.getWsdlModel();
099: if (wsdlModel == null) {
100: wsdlModeler.generateWsdlModel(new WsdlModelListener() {
101:
102: public void modelCreated(WsdlModel model) {
103: wsdlModel = model;
104: ((JaxWsClientNode) getNode()).changeIcon();
105: if (model == null) {
106: DialogDisplayer
107: .getDefault()
108: .notify(
109: new JaxWsUtils.WsImportClientFailedMessage(
110: wsdlModeler
111: .getCreationException()));
112: }
113: updateKeys();
114: }
115: });
116: } else {
117: updateKeys();
118: }
119: }
120: }
121:
122: @Override
123: protected void removeNotify() {
124: setKeys(Collections.<WsdlService> emptySet());
125: }
126:
127: private void updateKeys() {
128: List<WsdlService> keys = null;
129: if (wsdlModel != null) {
130: keys = wsdlModel.getServices();
131: }
132: setKeys(keys == null ? new ArrayList<WsdlService>() : keys);
133: }
134:
135: protected Node[] createNodes(WsdlService key) {
136: return new Node[] { new ServiceNode(key) };
137: }
138:
139: void refreshKeys(boolean downloadWsdl) {
140: this .refreshKeys(downloadWsdl, "");
141: }
142:
143: void refreshKeys(boolean downloadWsdl, String newWsdlUrl) {
144: super .addNotify();
145: // copy to local wsdl first
146: JAXWSClientSupport support = JAXWSClientSupport
147: .getJaxWsClientSupport(srcRoot);
148: final JaxWsClientNode clientNode = (JaxWsClientNode) getNode();
149: if (downloadWsdl) {
150: try {
151: String clientName = clientNode.getName();
152: String oldWsdlUrl = client.getWsdlUrl();
153: boolean jaxWsModelChanged = false;
154: FileObject localWsdl = null;
155: if (newWsdlUrl.length() > 0
156: && !oldWsdlUrl.equals(newWsdlUrl)) {
157: localWsdl = WSUtils.retrieveResource(support
158: .getLocalWsdlFolderForClient(clientName,
159: true), new URI(newWsdlUrl));
160: jaxWsModelChanged = true;
161: } else {
162: WSUtils.retrieveResource(support
163: .getLocalWsdlFolderForClient(clientName,
164: true), new URI(oldWsdlUrl));
165: }
166:
167: if (jaxWsModelChanged) {
168: client.setWsdlUrl(newWsdlUrl);
169: FileObject xmlResourcesFo = support
170: .getLocalWsdlFolderForClient(clientName,
171: false);
172: if (xmlResourcesFo != null) {
173: String localWsdlUrl = FileUtil.getRelativePath(
174: xmlResourcesFo, localWsdl);
175: client.setLocalWsdlFile(localWsdlUrl);
176: }
177:
178: clientNode.getJaxWsModel().write();
179: }
180: // copy resources to WEB-INF[META-INF]/wsdl/client/${clientName}
181: if (client.getWsdlUrl().startsWith("file:")) {
182: FileObject srcRoot = getNode().getLookup().lookup(
183: FileObject.class);
184: Project project = FileOwnerQuery.getOwner(srcRoot);
185: if (project.getLookup().lookup(
186: J2eeModuleProvider.class) != null) {
187: FileObject xmlResorcesFo = support
188: .getLocalWsdlFolderForClient(
189: clientName, false);
190: if (xmlResorcesFo != null) {
191: FileObject wsdlFolder = getWsdlFolderForClient(
192: support, clientName);
193: WSUtils
194: .copyFiles(xmlResorcesFo,
195: wsdlFolder);
196: }
197: }
198: }
199: } catch (URISyntaxException ex) {
200: ErrorManager.getDefault().notify(ex);
201: } catch (UnknownHostException ex) {
202: ErrorManager.getDefault().annotate(
203: ex,
204: NbBundle.getMessage(JaxWsClientChildren.class,
205: "MSG_ConnectionProblem"));
206: return;
207: } catch (IOException ex) {
208: ErrorManager.getDefault().annotate(
209: ex,
210: NbBundle.getMessage(JaxWsClientChildren.class,
211: "MSG_ConnectionProblem"));
212: return;
213: }
214:
215: }
216:
217: final WsdlModeler wsdlModeler = clientNode.getWsdlModeler();
218: clientNode.setModelGenerationFinished(false);
219: clientNode.changeIcon();
220: if (wsdlModeler != null) {
221: wsdlModeler.generateWsdlModel(new WsdlModelListener() {
222:
223: public void modelCreated(WsdlModel model) {
224: wsdlModel = model;
225: clientNode.setModelGenerationFinished(true);
226: clientNode.changeIcon();
227: if (model == null) {
228: DialogDisplayer
229: .getDefault()
230: .notify(
231: new JaxWsUtils.WsImportClientFailedMessage(
232: wsdlModeler
233: .getCreationException()));
234: }
235: updateKeys();
236:
237: if (model != null) {
238: Client client = clientNode.getJaxWsModel()
239: .findClientByName(clientNode.getName());
240: if (client != null) {
241: WsdlService wsdlService = null;
242: boolean jaxWsModelChanged = false;
243: List<WsdlService> wsdlServices = model
244: .getServices();
245: if (wsdlServices != null
246: && wsdlServices.size() > 0) {
247: wsdlService = wsdlServices.get(0);
248: }
249:
250: // test if package name for java artifacts hasn't changed
251: String oldPkgName = client.getPackageName();
252: if (wsdlService != null
253: && !client
254: .isPackageNameForceReplace()) {
255: String javaName = wsdlService
256: .getJavaName();
257: int dotPosition = javaName
258: .lastIndexOf(".");
259: if (dotPosition >= 0) {
260: String newPkgName = javaName
261: .substring(0, dotPosition);
262: if ((oldPkgName == null && newPkgName != null)
263: || (!oldPkgName
264: .equals(newPkgName))) {
265: client
266: .setPackageName(newPkgName);
267: jaxWsModelChanged = true;
268: }
269: }
270: }
271:
272: // save jax-ws model
273: if (jaxWsModelChanged) {
274: try {
275: clientNode.getJaxWsModel().write();
276: } catch (IOException ex) {
277: ErrorManager.getDefault().notify(
278: ErrorManager.ERROR, ex);
279: }
280: }
281: }
282: }
283: }
284: });
285: }
286: // re-generate java artifacts
287: FileObject srcRoot = getNode().getLookup().lookup(
288: FileObject.class);
289: Project project = FileOwnerQuery.getOwner(srcRoot);
290: if (project != null) {
291: FileObject buildImplFo = project.getProjectDirectory()
292: .getFileObject(
293: GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
294: try {
295: String name = client.getName();
296: ExecutorTask wsimportTask = ActionUtils.runTarget(
297: buildImplFo, new String[] {
298: "wsimport-client-clean-" + name,
299: "wsimport-client-" + name }, null); //NOI18N
300: wsimportTask.waitFinished();
301: } catch (IOException ex) {
302: ErrorManager.getDefault().log(ex.getLocalizedMessage());
303: } catch (IllegalArgumentException ex) {
304: ErrorManager.getDefault().log(ex.getLocalizedMessage());
305: }
306: // refresh client artifacts directory due to code copletion
307: String packageName = client.getPackageName();
308: if (packageName != null) {
309: packageName = packageName.replace(".", "/"); //NOI18N
310: FileObject clientArtifactsFolder = project
311: .getProjectDirectory().getFileObject(
312: "build/generated/wsimport/client/"
313: + packageName); //NOI18N
314: if (clientArtifactsFolder != null) {
315: clientArtifactsFolder.refresh();
316: }
317: }
318: }
319: }
320:
321: WsdlModel getWsdlModel() {
322: return wsdlModel;
323: }
324:
325: private FileObject getWsdlFolderForClient(
326: JAXWSClientSupport support, String name) throws IOException {
327: FileObject globalWsdlFolder = support.getWsdlFolder(true);
328: FileObject oldWsdlFolder = globalWsdlFolder
329: .getFileObject("client/" + name);
330: if (oldWsdlFolder != null) {
331: FileLock lock = oldWsdlFolder.lock();
332: try {
333: oldWsdlFolder.delete(lock);
334: } finally {
335: lock.releaseLock();
336: }
337: }
338: FileObject clientWsdlFolder = globalWsdlFolder
339: .getFileObject("client"); //NOI18N
340: if (clientWsdlFolder == null) {
341: clientWsdlFolder = globalWsdlFolder.createFolder("client");
342: } //NOI18N
343: return clientWsdlFolder.createFolder(name);
344: }
345: }
|