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-2007 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.visualweb.websvcmgr.consumer;
042:
043: import com.sun.rave.designtime.BeanCreateInfo;
044: import com.sun.rave.designtime.DesignBean;
045: import com.sun.rave.designtime.DisplayItem;
046: import com.sun.rave.designtime.Result;
047: import com.sun.tools.ws.processor.model.java.JavaMethod;
048: import java.awt.Image;
049: import java.awt.datatransfer.DataFlavor;
050: import java.awt.datatransfer.Transferable;
051: import java.awt.datatransfer.UnsupportedFlavorException;
052: import java.io.File;
053: import java.io.IOException;
054: import java.util.LinkedList;
055: import java.util.List;
056: import java.util.Map;
057: import java.util.logging.Level;
058: import java.util.logging.Logger;
059: import org.netbeans.api.java.classpath.ClassPath;
060: import org.netbeans.api.project.Project;
061: import org.netbeans.api.project.libraries.Library;
062: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
063: import org.netbeans.modules.visualweb.websvcmgr.codegen.DataProviderJavaMethod;
064: import org.netbeans.modules.visualweb.websvcmgr.codegen.DataProviderModelMethod;
065: import org.netbeans.modules.visualweb.websvcmgr.util.Util;
066: import org.netbeans.modules.web.api.webmodule.WebModule;
067: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort;
068: import org.netbeans.modules.websvc.manager.api.WebServiceDescriptor;
069: import org.netbeans.modules.websvc.manager.model.WebServiceData;
070: import org.netbeans.modules.websvc.manager.util.WebServiceLibReferenceHelper;
071: import org.netbeans.modules.websvc.saas.model.WsdlSaasMethod;
072: import org.netbeans.modules.websvc.saas.model.WsdlSaasPort;
073: import org.netbeans.modules.websvc.saas.spi.ConsumerFlavorProvider;
074: import org.openide.util.datatransfer.ExTransferable;
075:
076: /**
077: *
078: * @author quynguyen
079: */
080: public class DesignerWebServiceTransferManager implements
081: ConsumerFlavorProvider {
082: static DataFlavor FLAVOR_METHOD_DISPLAY_ITEM;
083: static DataFlavor FLAVOR_PORT_DISPLAY_ITEM;
084:
085: static {
086: try {
087: FLAVOR_METHOD_DISPLAY_ITEM = new DataFlavor(
088: DataFlavor.javaJVMLocalObjectMimeType + "; class="
089: + DisplayItem.class.getName(), // NOI18N
090: "Web Services Method Display Item"); // XXX Localize
091:
092: FLAVOR_PORT_DISPLAY_ITEM = new DataFlavor(
093: DataFlavor.javaJVMLocalObjectMimeType + "; class="
094: + DisplayItem.class.getName(), // NOI18N
095: "Web Services Port Display Item"); // XXX Localize
096: } catch (Exception ex) {
097: throw new AssertionError(ex);
098: }
099: }
100:
101: public DesignerWebServiceTransferManager() {
102: }
103:
104: public Transferable addDataFlavors(Transferable transferable) {
105: try {
106: ExTransferable result = ExTransferable.create(transferable);
107: if (transferable
108: .isDataFlavorSupported(ConsumerFlavorProvider.WSDL_METHOD_FLAVOR)
109: && !transferable
110: .isDataFlavorSupported(FLAVOR_METHOD_DISPLAY_ITEM)) {
111: Object data = transferable
112: .getTransferData(ConsumerFlavorProvider.WSDL_METHOD_FLAVOR);
113: if (data instanceof WsdlSaasMethod) {
114: final WsdlSaasMethod method = (WsdlSaasMethod) data;
115: final JavaMethod javaMethod = method
116: .getJavaMethod();
117: if (javaMethod != null
118: && !Util.hasOutput(javaMethod)) { // NOI18N
119: result.put(new ExTransferable.Single(
120: FLAVOR_PORT_DISPLAY_ITEM) {
121: protected Object getData()
122: throws IOException,
123: UnsupportedFlavorException {
124: return new PortBeanCreateInfo(
125: (WebServiceData) method
126: .getSaas()
127: .getWsdlData(), method
128: .getWsdlPort());
129: }
130: });
131: } else {
132: result.put(new ExTransferable.Single(
133: FLAVOR_METHOD_DISPLAY_ITEM) {
134: protected Object getData()
135: throws IOException,
136: UnsupportedFlavorException {
137: return new MethodBeanCreateInfo(
138: (WebServiceData) method
139: .getSaas()
140: .getWsdlData(), method
141: .getWsdlPort(),
142: javaMethod);
143: }
144: });
145: }
146: return result;
147: }
148: } else if (transferable
149: .isDataFlavorSupported(ConsumerFlavorProvider.PORT_FLAVOR)
150: && !transferable
151: .isDataFlavorSupported(FLAVOR_PORT_DISPLAY_ITEM)) {
152: final Object data = transferable
153: .getTransferData(ConsumerFlavorProvider.PORT_FLAVOR);
154: if (data instanceof WsdlSaasPort) {
155: final WsdlSaasPort port = (WsdlSaasPort) data;
156: result.put(new ExTransferable.Single(
157: FLAVOR_PORT_DISPLAY_ITEM) {
158: protected Object getData() throws IOException,
159: UnsupportedFlavorException {
160: return new PortBeanCreateInfo(
161: (WebServiceData) port
162: .getParentSaas()
163: .getWsdlData(), port
164: .getWsdlPort());
165: }
166: });
167: return result;
168: }
169: }
170: } catch (Exception ex) {
171: Logger.getLogger(this .getClass().getName()).log(Level.INFO,
172: ex.getLocalizedMessage(), ex);
173: }
174:
175: return transferable;
176: }
177:
178: private static WebServiceDescriptor getProxyDescriptorForProject(
179: WebServiceData wsData) {
180: boolean isJ2EE_15 = JsfProjectUtils.isJavaEE5Project(Util
181: .getActiveProject());
182:
183: if (isJ2EE_15) {
184: return wsData.getJaxWsDescriptor();
185: } else {
186: return wsData.getJaxRpcDescriptor();
187: }
188: }
189:
190: private static void addJarReferences(boolean isJ2EE_15,
191: Project project, WebServiceDescriptor descriptor) {
192: if (requiresLibraryDefinitions(isJ2EE_15, project, descriptor)) {
193: addLibraryDefinitions(isJ2EE_15, project, descriptor);
194: }
195:
196: addReferenceArchives(isJ2EE_15, project, descriptor);
197: }
198:
199: private static boolean requiresLibraryDefinitions(
200: boolean isJ2EE_15, Project project,
201: WebServiceDescriptor descriptor) {
202: WebModule wm = WebModule.getWebModule(project
203: .getProjectDirectory());
204:
205: if (wm == null) {
206: return true;
207: } else if (!isJ2EE_15) {
208: ClassPath cp = ClassPath.getClassPath(wm.getDocumentBase(),
209: ClassPath.COMPILE);
210: boolean hasJaxRpc = cp
211: .findResource("javax/xml/rpc/Service.class") != null; // NOI18N
212: hasJaxRpc = hasJaxRpc
213: && cp
214: .findResource("com/sun/xml/rpc/spi/tools/SOAPConstants.class") != null; // NOI18N
215: hasJaxRpc = hasJaxRpc
216: && cp
217: .findResource("com/sun/xml/rpc/client/StubBase.class") != null; // NOI18N
218:
219: return !hasJaxRpc;
220: } else {
221: ClassPath cp = ClassPath.getClassPath(wm.getDocumentBase(),
222: ClassPath.COMPILE);
223: boolean hasJaxWs = cp
224: .findResource("javax/xml/ws/Service.class") != null; // NOI18N
225: hasJaxWs = cp
226: .findResource("com/sun/xml/ws/spi/ProviderImpl.class") != null; // NO18N
227: return !hasJaxWs;
228: }
229: }
230:
231: /**
232: * Adds the predefined library definitions to the lib lists
233: */
234: private static void addLibraryDefinitions(boolean isJ2EE_15,
235: Project project, WebServiceDescriptor descriptor) {
236: List<Library> compiletimeLibs = new LinkedList<Library>();
237: Library webService20LibDef = Util
238: .getWebServiceSupportLibDef(isJ2EE_15);
239:
240: compiletimeLibs.add(webService20LibDef);
241: WebServiceLibReferenceHelper.addLibRefsToProject(project,
242: compiletimeLibs);
243: }
244:
245: /**
246: * Adds the generated WS client and designtime jars to the lib lists
247: */
248: private static void addReferenceArchives(boolean isJ2EE_15,
249: Project project, WebServiceDescriptor descriptor) {
250: List<String> jars = new LinkedList<String>();
251: String pathPrefix = descriptor.getXmlDescriptorFile()
252: .getParent()
253: + File.separator;
254:
255: for (WebServiceDescriptor.JarEntry jar : descriptor.getJars()) {
256: if (jar.getType().equals(
257: DesignerWebServiceExtImpl.VW_DESIGNTIME_JAR)
258: || jar
259: .getType()
260: .equals(
261: WebServiceDescriptor.JarEntry.PROXY_JAR_TYPE)) {
262: jars.add(pathPrefix + jar.getName());
263: }
264:
265: }
266:
267: WebServiceLibReferenceHelper.addArchiveRefsToProject(project,
268: jars);
269: }
270:
271: static class PortBeanCreateInfo implements BeanCreateInfo {
272: private final WebServiceData wsData;
273: private final WsdlPort port;
274:
275: public PortBeanCreateInfo(WebServiceData wsData, WsdlPort port) {
276: this .wsData = wsData;
277: this .port = port;
278: }
279:
280: public String getBeanClassName() {
281: // XXX This hack causes the jars to be added to the project if the
282: // webservice port/method node is even dragged onto the designer
283: // without dropping
284: WebServiceDescriptor wsDescriptor = getProxyDescriptorForProject(wsData);
285: String beanClassName = null;
286:
287: if (wsDescriptor != null) {
288: DesignerWebServiceExtData data = (DesignerWebServiceExtData) wsDescriptor
289: .getConsumerData().get(
290: DesignerWebServiceExtImpl.CONSUMER_ID);
291: if (data != null) {
292: Map<String, String> proxyMap = data
293: .getPortToProxyBeanNameMap();
294: if (proxyMap != null)
295: beanClassName = proxyMap.get(port.getName());
296: }
297: }
298:
299: if (beanClassName != null) {
300: addJarReferences(
301: (wsDescriptor.getWsType() == WebServiceDescriptor.JAX_WS_TYPE),
302: Util.getActiveProject(), wsDescriptor);
303: return beanClassName;
304: } else {
305: return "x";
306: }
307:
308: }
309:
310: public Result beanCreatedSetup(DesignBean designBean) {
311: return Result.SUCCESS;
312: }
313:
314: public String getDisplayName() {
315: return wsData.getName();
316: }
317:
318: public String getDescription() {
319: return "";
320: }
321:
322: public Image getLargeIcon() {
323: return null;
324: }
325:
326: public Image getSmallIcon() {
327: return null;
328: }
329:
330: public String getHelpKey() {
331: return null;
332: }
333:
334: } // End of MethodBeanCreateInfo.
335:
336: static class MethodBeanCreateInfo implements BeanCreateInfo {
337: private WebServiceData wsData;
338: private WsdlPort port;
339: private JavaMethod javaMethod;
340:
341: public MethodBeanCreateInfo(WebServiceData wsData,
342: WsdlPort port, JavaMethod javaMethod) {
343: this .wsData = wsData;
344: this .port = port;
345: this .javaMethod = javaMethod;
346: }
347:
348: public String getBeanClassName() {
349: // XXX This hack causes the jars to be added to the project if the
350: // webservice port/method node is even dragged onto the designer
351: // without dropping
352: WebServiceDescriptor wsDescriptor = getProxyDescriptorForProject(wsData);
353: String methodSig = null;
354:
355: if (wsDescriptor == null) {
356: return "x";
357: } else if (wsDescriptor.getWsType() == WebServiceDescriptor.JAX_WS_TYPE) {
358: methodSig = Util
359: .getMethodSignatureAsString(new DataProviderModelMethod(
360: javaMethod));
361: } else {
362: java.lang.reflect.Method m = Util
363: .getCorrespondingJaxRpcMethod(javaMethod, port
364: .getName(), wsData);
365: methodSig = Util
366: .getMethodSignatureAsString(new DataProviderJavaMethod(
367: m));
368: }
369: DesignerWebServiceExtData data = (DesignerWebServiceExtData) wsDescriptor
370: .getConsumerData().get(
371: DesignerWebServiceExtImpl.CONSUMER_ID);
372:
373: String beanClassName;
374: if (data == null
375: || data.getPortToDataProviderMap().get(
376: port.getName()) == null) {
377: beanClassName = null;
378: } else {
379: beanClassName = data.getPortToDataProviderMap().get(
380: port.getName()).get(methodSig);
381: }
382:
383: if (beanClassName != null) {
384: addJarReferences(
385: (wsDescriptor.getWsType() == WebServiceDescriptor.JAX_WS_TYPE),
386: Util.getActiveProject(), wsDescriptor);
387: return beanClassName;
388: } else {
389: // XXX return something that cannot be instantiated by the designer so the drop will be rejected
390: return "x";
391: }
392:
393: }
394:
395: public Result beanCreatedSetup(DesignBean designBean) {
396: return Result.SUCCESS;
397: }
398:
399: public String getDisplayName() {
400: WebServiceDescriptor wsDescriptor = wsData
401: .getJaxWsDescriptor();
402: if (wsDescriptor == null) {
403: wsDescriptor = wsData.getJaxRpcDescriptor();
404: }
405:
406: String methodSig = Util
407: .getMethodSignatureAsString(new DataProviderModelMethod(
408: javaMethod));
409: DesignerWebServiceExtData data = (DesignerWebServiceExtData) wsDescriptor
410: .getConsumerData().get(
411: DesignerWebServiceExtImpl.CONSUMER_ID);
412: return data.getPortToDataProviderMap().get(
413: port.getJavaName()).get(methodSig);
414: }
415:
416: public String getDescription() {
417: return "";
418: }
419:
420: public Image getLargeIcon() {
421: return null;
422: }
423:
424: public Image getSmallIcon() {
425: return null;
426: }
427:
428: public String getHelpKey() {
429: return null;
430: }
431:
432: } // End of MethodBeanCreateInfo.
433: }
|