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.wsdl;
043:
044: import org.openide.ErrorManager;
045: import org.netbeans.modules.websvc.registry.jaxrpc.Wsdl2Java;
046: import org.netbeans.modules.websvc.registry.model.WebServiceData;
047: import org.netbeans.modules.websvc.registry.nodes.WebServicesPortNode;
048: import org.netbeans.modules.websvc.registry.util.Util;
049: import com.sun.xml.rpc.processor.ProcessorOptions;
050: import com.sun.xml.rpc.processor.config.Configuration;
051: import com.sun.xml.rpc.processor.config.WSDLModelInfo;
052: import com.sun.xml.rpc.processor.model.Operation;
053: import com.sun.xml.rpc.processor.model.Port;
054: import com.sun.xml.rpc.processor.model.java.JavaMethod;
055: import com.sun.xml.rpc.processor.model.java.JavaParameter;
056: import com.sun.xml.rpc.processor.modeler.ModelerException;
057: import com.sun.xml.rpc.processor.modeler.wsdl.WSDLModelerBase;
058: import com.sun.xml.rpc.processor.util.ClientProcessorEnvironment;
059: import com.sun.xml.rpc.spi.model.JavaInterface;
060: import com.sun.xml.rpc.spi.model.Model;
061: import com.sun.xml.rpc.spi.model.Service;
062: import com.sun.xml.rpc.util.JAXRPCClassFactory;
063: import java.io.ByteArrayOutputStream;
064: import java.io.File;
065: import java.io.IOException;
066: import java.io.StringWriter;
067: import java.net.URL;
068: import java.util.HashSet;
069: import java.util.Iterator;
070: import java.util.Properties;
071: import java.util.Set;
072: import org.openide.util.NbBundle;
073:
074: /**
075: *
076: * @author Winston Prakash
077: */
078: public class WSDLInfo {
079:
080: File errorFile;
081: private String program = "wscompile";
082: private String outputDir;
083: private String nonClassOutputDir;
084: private String sourceOutputDir;
085: private URL wsdlUrl;
086: private String packageName = "";
087: private String errorMessage;
088: private boolean hasOperations;
089:
090: /**
091: * The following is taken from com.sun.xml.rpc.processor.modeler.wsdl.WSDLModelerBase where it's used to set
092: * the documentation property on Model.
093: */
094: private static final String WSDL_DOCUMENTATION = "com.sun.xml.rpc.processor.modeler.wsdl.documentation";
095:
096: public static final String SERVICE_DISPLAYNAME_TOKEN = "%%DISPLAY_NAME%%";
097: public static final String TOKEN_SEPARATOR = "@";
098: public static final String SIG_SEPARATOR = "|";
099: public static final String RETURN_SEPARATOR = "!";
100: public static final String PARAM_SEPARATOR = ":";
101:
102: private Model wsdlModel;
103: Wsdl2Java wsdl2Java;
104:
105: boolean removeGeneratedFiles = true;
106:
107: public WSDLInfo() {
108: try {
109: errorFile = File.createTempFile("wscompile", "error");
110: } catch (IOException exc) {
111: ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,
112: exc);
113: }
114: }
115:
116: public boolean hasOperations() {
117: return hasOperations;
118: }
119:
120: public void setWsdlUrl(URL url) {
121: wsdlUrl = url;
122: }
123:
124: public URL getWsdlUrl() {
125: return wsdlUrl;
126: }
127:
128: public void setPackageName(String pkgName) {
129: packageName = pkgName;
130: }
131:
132: public String getPackageName() {
133: if (packageName == null) {
134: packageName = Wsdl2Java.DEFAULT_TARGET_PACKAGE;
135: }
136: return packageName;
137: }
138:
139: public String getErrorMessage() {
140: StringWriter errorWriter = new StringWriter();
141: StringBuffer strBuffer = new StringBuffer();
142: errorWriter.write("<B>"
143: + NbBundle.getMessage(WSDLInfo.class,
144: "WSDLINFO_ERROR_PARSING") + " </B>");
145: errorWriter.write("<BR><B><I>" + errorMessage + "</I></B>");
146: return errorWriter.toString();
147: }
148:
149: public void setOutputDirectory(String dir) {
150: outputDir = dir;
151: }
152:
153: public String getOutputDirectory() {
154: if (outputDir == null) {
155: File outDir = new File(System.getProperty("user.home"));
156: return outDir.getAbsolutePath();
157: }
158: return outputDir;
159: }
160:
161: public void setRemoveGeneratedFiles(boolean remove) {
162: removeGeneratedFiles = remove;
163: }
164:
165: public boolean create() {
166: /*
167: wsdl2Java = new Wsdl2Java();
168: wsdl2Java.setOutputDirectory(getOutputDirectory());
169: wsdl2Java.setWsdlUrl( getWsdlUrl());
170: if(!wsdl2Java.execute(errorFile)) {
171: return false;
172: }
173: //if (removeGeneratedFiles) removeTemporaryFiles()
174:
175: */
176:
177: /**
178: * First we need to get create a JAXRPC WSDLModeler
179: */
180:
181: WSDLModelInfo modelInfo = new WSDLModelInfo();
182: ClientProcessorEnvironment env = new ClientProcessorEnvironment(
183: new ByteArrayOutputStream(), null, null);
184:
185: Configuration config = new Configuration(env);
186:
187: modelInfo = new WSDLModelInfo();
188:
189: config.setModelInfo(modelInfo);
190: modelInfo.setParent(config);
191:
192: modelInfo.setLocation(getWsdlUrl().toExternalForm());
193: Properties options = new Properties();
194: options.put(ProcessorOptions.VALIDATE_WSDL_PROPERTY, "true");
195: options
196: .put(ProcessorOptions.SEARCH_SCHEMA_FOR_SUBTYPES,
197: "true");
198: options.put(ProcessorOptions.USE_WSI_BASIC_PROFILE, "false");
199:
200: WSDLModelerBase modeler = JAXRPCClassFactory.newInstance()
201: .createWSDLModeler(modelInfo, options);
202:
203: /**
204: * Now that we have the modeler, we need to build a model.
205: */
206: Model tmpModel = null;
207: try {
208: tmpModel = modeler.buildModel();
209: } catch (/*Modeler*/Exception me) {
210: /**
211: * try to find a ParseException in the nested exceptions to get the cause
212: */
213: errorMessage = " Exception = " + me.getLocalizedMessage();
214: ;
215: return false;
216: }
217:
218: this .setWSDLModel(tmpModel);
219:
220: return true;
221: }
222:
223: private void setWSDLModel(Model inModel) {
224: wsdlModel = inModel;
225: }
226:
227: public Set getWebServices() {
228: Set webServices = new HashSet();
229: Model model = getWSDLModel();
230: if (null != model) {
231: for (Iterator services = model.getServices(); services
232: .hasNext();) {
233: WebServiceData wsData = new WebServiceData();
234: wsData.setURL(getWsdlUrl().toString());
235: Service service = (Service) services.next();
236:
237: String serviceName = service.getJavaIntf().getName();
238: wsData.setName(serviceName);
239: serviceName = serviceName.substring(serviceName
240: .lastIndexOf('.') + 1, serviceName.length());
241: String hotsName = getWsdlUrl().getHost();
242: String displayName = serviceName;
243: wsData.setDisplayName(displayName);
244:
245: Iterator ports = service.getPorts();
246: while (ports.hasNext()) {
247: Port port = (Port) ports.next();
248: /**
249: * We need to set a property on the Port for the name because QNAME is not a persistable
250: * as a JavaBean.
251: */
252: port.setProperty(WebServiceData.PORT_PROPERTY_NAME,
253: port.getName().getLocalPart());
254: wsData.addPort(port);
255: wsData.setWebServiceAddress(port.getAddress());
256: String portName = port.getJavaInterface()
257: .getRealName();
258: portName = portName.substring(portName
259: .lastIndexOf('.') + 1, portName.length());
260: wsData.setProxy(portName);
261: /**
262: * Iterator operations = port.getOperations();
263: * int methodCount=0;
264: * while (operations.hasNext()) {
265: * Operation operation = (Operation) operations.next();
266: * JavaMethod method = operation.getJavaMethod();
267: * wsData.addMethod(method.getName());
268: * }
269: */
270: }
271: webServices.add(wsData);
272: }
273: }
274:
275: return webServices;
276: }
277:
278: private Model getWSDLModel() {
279: return wsdlModel;
280: }
281:
282: /**
283: * This method will use the Model to generated service information HTML. The service DISPLAY_NAME
284: * will be a token that expected to ge set by the calling class to allow the caller of this method to
285: * control the display name.
286: */
287: public String getServiceInfo(String inServiceName) {
288: StringWriter strWriter = new StringWriter();
289:
290: Model model = getWSDLModel();
291: if (null != model) {
292: Iterator services = model.getServices();
293: for (int ii = 0; services.hasNext(); ii++) {
294: Service service = (Service) services.next();
295: String serviceName = service.getJavaIntf().getName();
296: if (serviceName.equals(inServiceName)) {
297: serviceName = serviceName
298: .substring(
299: serviceName.lastIndexOf('.') + 1,
300: serviceName.length());
301: strWriter.write("<a name=\"TOPOFPAGE\"> </a>");
302: // !HIE strWriter.write("<FORM NAME=\"service_form." + serviceName + "\" BGCOLOR=\"#e6e6ff\"><P>");
303: strWriter.write("<FORM NAME=\"service_form."
304: + serviceName + "\">");
305: strWriter.write("<TABLE><TR>");
306: strWriter.write("<TD VALIGN=TOP><B>"
307: + NbBundle.getMessage(WSDLInfo.class,
308: "SERVICE_WSDL_NAME") + "</B></TD>");
309: strWriter.write("<TD VALIGN=TOP>" + serviceName
310: + "</TD>");
311: strWriter.write("</TR><TR>");
312: strWriter
313: .write("<TD VALIGN=TOP><LABEL FOR=\"service_name."
314: + serviceName
315: + "\" ><B>"
316: + NbBundle.getMessage(
317: WSDLInfo.class,
318: "SERVICE_DISPLAY_NAME")
319: + "</B></LABEL></TD>");
320: strWriter
321: .write("<TD VALIGN=TOP><INPUT TYPE=TEXT ID=\"service_name."
322: + serviceName
323: + "\" NAME=\"service_name."
324: + serviceName
325: + "\" VALUE=\""
326: + this .SERVICE_DISPLAYNAME_TOKEN
327: + "\" STYLE=\"background: #ade7ff\"></TD>");
328: strWriter.write("</TR><TR>");
329: strWriter
330: .write("<TD VALIGN=TOP><LABEL FOR=\"package_name."
331: + serviceName
332: + "\" ><B>"
333: + NbBundle.getMessage(
334: WSDLInfo.class,
335: "CLIENT_PACKAGE_NAME")
336: + "</B></LABEL></TD>");
337: strWriter
338: .write("<TD VALIGN=TOP><INPUT TYPE=TEXT SIZE=30 ID=\"package_name."
339: + serviceName
340: + "\" NAME=\"package_name."
341: + serviceName
342: + "\" VALUE=\""
343: + Wsdl2Java.DEFAULT_TARGET_PACKAGE
344: + "."
345: + serviceName.toLowerCase()
346: + "\" STYLE=\"background: #ade7ff\"></TD>");
347: strWriter.write("</TR>");
348: String serviceDocumentationContent = (String) service
349: .getProperty(this .WSDL_DOCUMENTATION);
350:
351: if (null != serviceDocumentationContent) {
352: strWriter.write("<TR>");
353: strWriter.write("<TD VALIGN=TOP><LABEL><B>"
354: + NbBundle.getMessage(WSDLInfo.class,
355: "SERVICE_DOCUMENTATION")
356: + "</B></LABEL></TD>");
357: strWriter
358: .write("<TD VALIGN=TOP>"
359: + serviceDocumentationContent
360: + "</TD>");
361: strWriter.write("</TR>");
362: }
363: Iterator ports = service.getPorts();
364: if (!ports.hasNext()) {
365: strWriter.write("</TABLE>");
366: }
367: while (ports.hasNext()) {
368: Port port = (Port) ports.next();
369: JavaInterface javaInterface = port
370: .getJavaInterface();
371: //strWriter.write("Port Name: " + javaInterface.getName() + "\n");
372: String proxy = javaInterface.getName();
373: proxy = proxy.substring(
374: proxy.lastIndexOf('.') + 1, proxy
375: .length());
376: //strWriter.write("Proxy: " + proxy + "_Stub" + "\n");
377: strWriter.write("<TR>");
378: strWriter.write("<TD VALIGN=TOP><B>"
379: + NbBundle.getMessage(WSDLInfo.class,
380: "WEB_SERVICE_PORTNNAME")
381: + "</B></TD>");
382: strWriter.write("<TD VALIGN=TOP>"
383: + port.getName().toString() + "</TD>");
384: strWriter.write("</TR>");
385: strWriter.write("<TR>");
386: strWriter.write("<TD VALIGN=TOP><B>"
387: + NbBundle.getMessage(WSDLInfo.class,
388: "WEB_SERVICE_ADDRESS")
389: + "</B></TD>");
390: strWriter.write("<TD VALIGN=TOP>"
391: + port.getAddress() + "</TD>");
392: strWriter.write("</TR>");
393: Iterator operations = port.getOperations();
394: strWriter.write("<TR>");
395: strWriter.write("<TD VALIGN=TOP><B>"
396: + NbBundle.getMessage(WSDLInfo.class,
397: "METHODS") + "</B></TD>");
398: strWriter.write("</TR>");
399: int methodCount = 0;
400: while (operations.hasNext()) {
401: hasOperations = true;
402: Operation operation = (Operation) operations
403: .next();
404: JavaMethod method = operation
405: .getJavaMethod();
406: strWriter.write("<TR>");
407: strWriter.write("<TD VALIGN=TOP>");
408: /**
409: * Since the neither JavaParameter nor Operation override "equals()", we need to jump through
410: * some hoops to compare the method signatures for methods with the same name. The pattern is
411: *
412: * actionTOKEN_SEPARATORserviceTOKEN_SEPARATORportTOKEN_SEPARATORmethodTOKEN_SEPARATORSIG_SEPARATORreturntypePARAM_SEPARATORparametertypePARAM_SEPARATORparametertype:..:
413: *
414: * -David Botterill 4/22/2004
415: */
416: String signatureString = method
417: .getReturnType().getRealName()
418: + RETURN_SEPARATOR;
419: Iterator paramameters = method
420: .getParameters();
421: while (paramameters.hasNext()) {
422: JavaParameter currentParam = (JavaParameter) paramameters
423: .next();
424: signatureString += currentParam
425: .getType().getRealName()
426: + PARAM_SEPARATOR;
427: }
428:
429: strWriter.write("<A HREF=\""
430: + "test"
431: + TOKEN_SEPARATOR
432: + serviceName
433: + TOKEN_SEPARATOR
434: + port.getName().getLocalPart()
435: + TOKEN_SEPARATOR
436: + method.getName()
437: + TOKEN_SEPARATOR
438: + SIG_SEPARATOR
439: + signatureString
440: + "\">"
441: + NbBundle.getMessage(
442: WSDLInfo.class,
443: "TEST_METHOD") + "</A>");
444: strWriter.write("</TD>");
445: // strWriter.write("<TD COLSPAN=2 VALIGN=TOP>");
446: strWriter.write("<TD VALIGN=TOP>");
447: // !HIE strWriter.write("<UL><LI>");
448: strWriter.write("<I>"
449: + method.getReturnType()
450: .getRealName()
451: + "</I> ");
452: strWriter.write("<B>" + method.getName()
453: + "</B>(");
454: Iterator params = method.getParameters();
455: while (params.hasNext()) {
456: strWriter.write("<I>");
457: JavaParameter param = (JavaParameter) params
458: .next();
459: String parameterType = Util
460: .getParameterType(port, param);
461: strWriter.write(parameterType);
462: if (params.hasNext())
463: strWriter.write(", ");
464: strWriter.write("</I>");
465: }
466: strWriter.write(")");
467: Iterator exceptions = method
468: .getExceptionsList().iterator();
469: while (exceptions.hasNext()) {
470: /**
471: * Bugid: 4970323 - The return type of an exceptions is a String not a JavaException. This
472: * can only be know for sure by reading the current JavaException code since the "JavaMethod.getExceptions()"
473: * method returns an Iterator and the javadoc says nothing of it being a String.
474: */
475: //JavaException exc = (JavaException)exceptions.next();
476: //strWriter.write(exc.getName());
477: /**
478: * Make sure we don't get back a null or an empty String. Check to make sure
479: * the Object is a string in case the API matures and JavaException turns back into a real object with
480: * a "getName" method.
481: */
482: Object currentException = exceptions
483: .next();
484: if (null != currentException
485: && currentException instanceof String
486: && ((String) currentException)
487: .length() > 0) {
488: strWriter
489: .write("<I>   throws   ");
490: strWriter
491: .write((String) currentException);
492: if (params.hasNext())
493: strWriter.write(",  ");
494: strWriter.write("</I>");
495: }
496: }
497: //strWriter.write("<BR>");
498: String operationDocumentationContent = (String) operation
499: .getProperty(this .WSDL_DOCUMENTATION);
500:
501: if (null != operationDocumentationContent) {
502: // !HIE strWriter.write("<BR><B>" + NbBundle.getMessage(WSDLInfo.class, "METHOD_DOCUMENTATION") + "</B>");
503: strWriter
504: .write("<BR>"
505: + operationDocumentationContent);
506: }
507: // !HIE strWriter.write("</LI></UL>");
508: strWriter.write("</TD>");
509: strWriter.write("</TR>");
510: }
511: }
512: strWriter.write("</TABLE>");
513: strWriter.write("</FORM>");
514: if (services.hasNext()) {
515: strWriter.write("<HR>");
516: }
517: /**
518: * we've processed the requested service so break
519: */
520: break;
521: }
522:
523: }
524: } else {
525: strWriter.write("<BR><B>"
526: + NbBundle.getMessage(WSDLInfo.class,
527: "WSDLINFO_ERROR_PARSING") + "</B><BR> ");
528:
529: }
530:
531: return strWriter.toString();
532: }
533:
534: public static void main(String[] args) {
535: System.setProperty("http.proxyHost", "webcache.sfbay.sun.com");
536: System.setProperty("http.proxyPort", "8080");
537:
538: WSDLInfo wsdlInfo = new WSDLInfo();
539: File wsdlFile = new File("D:\\realtimeservice.wsdl");
540:
541: try {
542: wsdlInfo.setWsdlUrl(wsdlFile.toURL());
543: //wsdlInfo.setWsdlUrl(new URL("http://www.lixusnet.com/lixusnet/AddFinder.jws?wsdl"));
544: } catch (Exception exc) {
545: exc.printStackTrace();
546: System.exit(-1);
547: }
548:
549: if (wsdlInfo.create()) {
550: wsdlInfo.getServiceInfo("RealTimeService");
551: } else {
552: System.out.println(wsdlInfo.getErrorMessage());
553: }
554: }
555: }
|