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.util;
043:
044: import java.util.*;
045: import java.io.*;
046: import java.net.URL;
047: import java.net.MalformedURLException;
048: import org.w3c.dom.*;
049: import javax.xml.parsers.DocumentBuilder;
050: import javax.xml.parsers.DocumentBuilderFactory;
051: import javax.xml.parsers.ParserConfigurationException;
052: import org.xml.sax.SAXException;
053: import org.openide.*;
054: import org.openide.util.NbBundle;
055: import org.openide.awt.StatusDisplayer;
056: import com.sun.xml.rpc.util.JavaCompilerHelper;
057: import com.sun.xml.rpc.processor.util.ClientProcessorEnvironment;
058: import com.sun.xml.rpc.processor.model.Port;
059: import com.sun.xml.rpc.processor.model.java.JavaParameter;
060: import org.netbeans.modules.websvc.registry.jaxrpc.Wsdl2Java;
061: import org.netbeans.modules.websvc.registry.WebServiceException;
062: import org.netbeans.modules.websvc.registry.model.WebServiceData;
063: import org.netbeans.modules.websvc.registry.jaxrpc.WrapperClientBeanInfoWriter;
064:
065: import org.openide.modules.InstalledFileLocator;
066: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
067: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
068:
069: // BEGIN_NOI18N
070: /**
071: * General utility methods for WSDL documents.
072: */
073: public class Util {
074: // public static final int BUFFER_SIZE = 4096;
075: // public static final String xsdNamespace = "xsd";
076: // final static public String WSDL_FILE_EXTENSION = "wsdl";
077: // /*
078: // * Removes any namespace prefix.
079: // * eg: 'xsd:element' -> 'element'
080: // */
081: // public static String removeNamespace(String tagName) {
082: // int pos = tagName.indexOf(':');
083: // if (pos < 0)
084: // return tagName;
085: // return tagName.substring(pos+1);
086: // }
087: //
088: // /**
089: // * Take an XML Schema data type and make a Java type out of it.
090: // * eg: 'xsd:string' -> 'java.lang.String'
091: // */
092: // /*
093: // public static String xmlSchemaType2JavaType(String typeName) {
094: // if ((xsdNamespace+":string").equals(typeName))
095: // return "java.lang.String";
096: // if ("char_lb_rb".equals(typeName))
097: // return "char[]";
098: // if (typeName.startsWith(xsdNamespace+":"))
099: // return typeName.substring(xsdNamespace.length()+1);
100: // return typeName;
101: // }
102: // */
103: // public static void insertSimpleTypes(Map typeMapping) {
104: // typeMapping.put("java.lang.Integer", "java.lang.Integer");
105: // typeMapping.put("java.lang.Short", "java.lang.Short");
106: // typeMapping.put("java.lang.Long", "java.lang.Long");
107: // typeMapping.put("java.lang.Double", "java.lang.Double");
108: // typeMapping.put("java.lang.Boolean", "java.lang.Boolean");
109: // typeMapping.put("java.lang.Character", "java.lang.Character");
110: // typeMapping.put("java.lang.Float", "java.lang.Float");
111: // typeMapping.put("java.lang.StringBuffer", "java.lang.StringBuffer");
112: // typeMapping.put("java.lang.Byte", "java.lang.Byte");
113: // typeMapping.put("java.lang.String", "java.lang.String");
114: // typeMapping.put("java.math.BigInteger", "java.math.BigInteger");
115: // typeMapping.put("char_lb_rb", "char[]");
116: // typeMapping.put("string", "java.lang.String");
117: // typeMapping.put("int", "int");
118: // typeMapping.put("short", "short");
119: // typeMapping.put("long", "long");
120: // typeMapping.put("double", "double");
121: // typeMapping.put("boolean", "boolean");
122: // typeMapping.put("char", "char");
123: // typeMapping.put("float", "float");
124: // typeMapping.put("byte", "byte");
125: // typeMapping.put("decimal", "java.math.BigDecimal");
126: // typeMapping.put("dateTime", "java.util.Date");
127: // typeMapping.put((xsdNamespace+":string").intern(), "java.lang.String");
128: // typeMapping.put((xsdNamespace+":int").intern(), "int");
129: // typeMapping.put((xsdNamespace+":short").intern(), "short");
130: // typeMapping.put((xsdNamespace+":long").intern(), "long");
131: // typeMapping.put((xsdNamespace+":double").intern(), "double");
132: // typeMapping.put((xsdNamespace+":boolean").intern(), "boolean");
133: // typeMapping.put((xsdNamespace+":char").intern(), "char");
134: // typeMapping.put((xsdNamespace+":float").intern(), "float");
135: // typeMapping.put((xsdNamespace+":byte").intern(), "byte");
136: // typeMapping.put((xsdNamespace+":decimal").intern(), "java.math.BigDecimal");
137: // typeMapping.put((xsdNamespace+":dateTime").intern(), "java.util.Date");
138: // }
139: //
140: // private static Map baseTypes = null;
141: // public static boolean isSerializerNeeded(String type) {
142: // if (baseTypes == null) {
143: // baseTypes = new HashMap();
144: // insertSimpleTypes(baseTypes);
145: // }
146: // return !baseTypes.containsKey(type);
147: // }
148: //
149: // /**
150: // * Given an XML Schema Element, figure out the java type to use.
151: // */
152: // public static String getType(Element el, Map typeMapping,
153: // Map elementTypeMapping) {
154: // if (el == null)
155: // return null;
156: // String elementName = el.getAttribute("name");
157: // String type = null;
158: // if ((xsdNamespace+":element").equals(el.getNodeName())) {
159: // //System.out.println("element name="+elementName);
160: // String typeAttribute = el.getAttribute("type");
161: // if (typeAttribute != null && !typeAttribute.equals("") &&
162: // !typeAttribute.equals("SOAP-ENC:Array")) {
163: // type = (String) typeMapping.get(typeAttribute);
164: // } else {
165: // // Recurse in and see what kind of type this thing is.
166: // type = getType(findFirstNode(el), typeMapping, elementTypeMapping);
167: // }
168: // } else if ((xsdNamespace+":simpleType").equals(el.getNodeName())) {
169: // type = (String) typeMapping.get(elementName);
170: // if (type == null) {
171: // try {
172: // Element restrictionNode = findFirstNodeByName(el, xsdNamespace+":restriction");
173: // type = (String) typeMapping.get(restrictionNode.getAttribute("base"));
174: // } catch (NotFoundException e) {
175: // // It's okay (for now), just go with our default.
176: // }
177: // }
178: // } else if ((xsdNamespace+":complexType").equals(el.getNodeName())) {
179: // Element annotationNode = null;
180: // try {
181: // annotationNode = findFirstNodeByName(el, xsdNamespace+":annotation");
182: // Element appInfoNode = findFirstNodeByName(annotationNode,
183: // xsdNamespace+":appinfo");
184: // try {
185: // Node bindingNode = findFirstNodeByName(appInfoNode, "BINDING_TYPE");
186: // type = bindingNode.getFirstChild().getNodeValue();
187: // } catch (NotFoundException e1) {
188: // try {
189: // Node oldbindingNode = findFirstNodeByName(appInfoNode, "OLDBINDING_TYPE");
190: // type = oldbindingNode.getFirstChild().getNodeValue();
191: // /*if (elementName != null) {
192: // String baseName = GenPresentation.baseName(type).intern();
193: // if (typeMapping.get(baseName) == null) {
194: // // Put our baseName in there too, since
195: // // we might look for it later by that name.
196: // //System.out.println("Putting in "+baseName+" as "+type);
197: // typeMapping.put(baseName, type);
198: // }
199: // }*/
200: // } catch (NotFoundException e2) {
201: // Node beanNode = findFirstNodeByName(appInfoNode, "BEAN_TYPE");
202: // type = beanNode.getFirstChild().getNodeValue();
203: // }
204: // }
205: // } catch (NotFoundException e) {
206: // //e.printStackTrace();
207: // type = "void"; // NOI18N
208: // }
209: // NodeList nodes = el.getChildNodes();
210: // for (int i = 0; i < nodes.getLength(); ++i) {
211: // Node node = nodes.item(i);
212: // if (!(node instanceof Element))
213: // continue;
214: // Element childEl = (Element) node;
215: // if (childEl == annotationNode)
216: // continue;
217: // // Insert our children's types into our mapping.
218: // getType(childEl, typeMapping, elementTypeMapping);
219: // }
220: // } else if ((xsdNamespace+":sequence").equals(el.getNodeName())) {
221: // NodeList nodes = el.getChildNodes();
222: // for (int i = 0; i < nodes.getLength(); ++i) {
223: // Node node = nodes.item(i);
224: // if (!(node instanceof Element))
225: // continue;
226: // Element childEl = (Element) node;
227: // // Insert our children's types into our mapping.
228: // getType(childEl, typeMapping, elementTypeMapping);
229: // }
230: // } else if ((xsdNamespace+":import").equals(el.getNodeName())) {
231: // // Nothing for us to do here....
232: // } else {
233: // //System.out.println("Unfamiliar XML Schema element: "+el.getNodeName()); // NOI18N
234: // return null;
235: // }
236: // if (type == null)
237: // type = "void";
238: // //System.out.println("Found type: elementName="+elementName+" type="+type);
239: // type = type.intern();
240: // if (elementName != null) {
241: // typeMapping.put(elementName.intern(), type);
242: // }
243: // elementTypeMapping.put(el, type);
244: // return type;
245: // }
246: //
247: // /**
248: // * Return a Collection of all child Elements
249: // */
250: // public static Collection getChildElements(Element parent) {
251: // Collection result = new LinkedList();
252: // NodeList children = parent.getChildNodes();
253: // for (int i = 0; i < children.getLength(); ++i) {
254: // Node child = children.item(i);
255: // if (!(child instanceof Element))
256: // continue;
257: // result.add(child);
258: // }
259: // return result;
260: // }
261: //
262: // public static Element findFirstNode(Element node) {
263: // NodeList nodes = node.getChildNodes();
264: // for (int i = 0; i < nodes.getLength(); ++i) {
265: // Node n = nodes.item(i);
266: // if (n instanceof Element)
267: // return (Element) n;
268: // }
269: // return null;
270: // }
271: //
272: // public static Element findFirstNodeByName(Element node, String name) throws NotFoundException {
273: // return findFirstNodeByName(node.getChildNodes(), name);
274: // }
275: //
276: // /**
277: // * Search for a node named @param name. Namespace is ignored.
278: // */
279: // public static Element findFirstNodeByName(NodeList nodes, String name) throws NotFoundException {
280: // name = removeNamespace(name);
281: // for (int i = 0; i < nodes.getLength(); ++i) {
282: // Node n = nodes.item(i);
283: // String localNodeName = n.getLocalName();
284: // if (localNodeName == null) {
285: // localNodeName = removeNamespace(n.getNodeName());
286: // }
287: // if (name.equals(localNodeName))
288: // return (Element) n;
289: // }
290: //
291: //
292: // throw new NotFoundException(MessageFormat.format(NbBundle.getMessage(Util.class,
293: // "MSG_UnableToFindNode"),
294: // new Object[] {name}), name);
295: // }
296: //
297: // public static Element findFirstNodeByNames(Element node, String[] names) throws NotFoundException {
298: // for (int i = 0; i < names.length; ++i)
299: // node = findFirstNodeByName(node, names[i]);
300: // return node;
301: // }
302: //
303: // public static Element findFirstNodeByName(Document doc, String name) throws NotFoundException {
304: // return findFirstNodeByName(doc.getChildNodes(), name);
305: // }
306: //
307: // //Following methods were copied from com.sun.forte4j.webdesigner.xmlcomponent.Util
308: // //because the wsdl module cannot be dependent on jwd.
309: //
310: // public static Class getClass(String className) {
311: // Class cls;
312: // if (className == null)
313: // return null;
314: // if (isClassArray(className)) {
315: // // Recursively figure out what our type is.
316: // // Is this the best way for getting an Array type?
317: // cls = getClass(className.substring(0, className.length()-2));
318: // Object arrayObject = java.lang.reflect.Array.newInstance(cls, 0);
319: // return arrayObject.getClass();
320: // }
321: // //ClassLoader cl = Top Manager.getDefault().currentClassLoader();
322: // //
323: // // Removing Top Manager calls (see Issuezilla 31753). Replace above with:
324: // ClassLoader cl = ClassPath.getClassPath(null, ClassPath.EXECUTE).getClassLoader(false);
325: //
326: // // BEGIN_NOI18N
327: // if ("int".equals(className))
328: // cls = Integer.TYPE;
329: // else if ("long".equals(className))
330: // cls = Long.TYPE;
331: // else if ("char".equals(className))
332: // cls = Character.TYPE;
333: // else if ("short".equals(className))
334: // cls = Short.TYPE;
335: // else if ("double".equals(className))
336: // cls = Double.TYPE;
337: // else if ("float".equals(className))
338: // cls = Float.TYPE;
339: // else if ("byte".equals(className))
340: // cls = Byte.TYPE;
341: // else if ("boolean".equals(className))
342: // cls = Boolean.TYPE;
343: // else if ("void".equals(className))
344: // cls= Void.TYPE;
345: // else {
346: // try {
347: // cls = cl.loadClass(className);
348: // } catch (java.lang.ClassNotFoundException e) {
349: // e.printStackTrace();
350: // cls = null;
351: // }
352: // }
353: // // END_NOI18N
354: // return cls;
355: // }
356: //
357: // public static boolean compile(DataObject[] dataObjects) {
358: // CompilerJob job = new CompilerJob(Compiler.DEPTH_ONE);
359: // for(int i = 0; i < dataObjects.length; i++) {
360: // CompilerCookie comp = (CompilerCookie) dataObjects[i].
361: // getCookie(CompilerCookie.Compile.class);
362: // if (comp != null) {
363: // comp.addToJob(job, Compiler.DEPTH_ONE);
364: // }
365: // }
366: // CompilerTask task = job.start();
367: // return task.isSuccessful();
368: // }
369: //
370: // static public boolean isClassArray(String returnClassName) {
371: // return returnClassName.endsWith("[]"); // NOI18N
372: // }
373: //
374: // static public boolean isCollectionType(String bindingType) {
375: // boolean isCollection = false;
376: // Class c = getClass(bindingType);
377: // if(c != null){
378: // if (java.util.Collection.class.isAssignableFrom(c))
379: // isCollection = true;
380: // }
381: //
382: // return isCollection;
383: // }
384:
385: // change all occurrences of oldSubString to newSubString
386: public static String changeString(String inString,
387: String oldSubString, String newSubString) {
388: if (oldSubString.trim().equals(""))
389: return inString;
390:
391: int start = 0;
392: int end = 0;
393: StringBuffer changedString = new StringBuffer("");
394:
395: end = inString.indexOf(oldSubString, start);
396: while (end != -1) {
397: //add substring before oldSubString and append newSubString
398: changedString.append(inString.substring(start, end)
399: + newSubString);
400:
401: //recompute starting index to end of changed substring
402: start = end + oldSubString.length();
403: //recompute end index
404: end = inString.indexOf(oldSubString, start);
405: }
406:
407: //append remainder of the String, if any
408: changedString.append(inString.substring(start));
409:
410: return changedString.toString();
411: }
412:
413: /**
414: * This method will create a jar file for a given WebServiceData object.
415: *
416: * @param inWSData - the WebServiceData to create the jar file for.
417: * @returns jarfileName - the file name (without path) of the jar file created. null if not created.
418: *
419: *
420: */
421: public static boolean createWSJar(WebServiceData inWSData,
422: OutputStream inOutputStream, String inJarFileName) {
423:
424: String jarFileName = null;
425:
426: OutputStream outputStream = null;
427:
428: FileOutputStream fileOutputStream = null;
429: Date date = new Date();
430: File tmpOutputDir = null;
431: File errorFile = null;
432: try {
433: File tempFile = File.createTempFile("wstemp", "ws");
434: tmpOutputDir = new File(tempFile.getParentFile(), "wstemp"
435: + date.getTime());
436: if (!tmpOutputDir.exists())
437: tmpOutputDir.mkdirs();
438: errorFile = File.createTempFile("wscompile", "error",
439: tempFile.getParentFile());
440:
441: /**
442: * if the outputstream is null, create a temporary directory for the wscompile.
443: */
444: if (null == inOutputStream) {
445: fileOutputStream = new FileOutputStream(errorFile);
446: outputStream = fileOutputStream;
447: } else {
448: outputStream = inOutputStream;
449: }
450: } catch (IOException ioe) {
451: ErrorManager.getDefault().notify(ioe);
452: // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
453: StatusDisplayer.getDefault().setStatusText(
454: NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
455: return false;
456: }
457:
458: /**
459: * Now create the class to do the WSDL to Java conversion
460: */
461: Wsdl2Java wsdl2Java = new Wsdl2Java();
462: /**
463: * Set the output directory to the temp one we just created.
464: */
465: wsdl2Java.setOutputDirectory(tmpOutputDir.getAbsolutePath());
466:
467: /**
468: * Set the package.
469: */
470: wsdl2Java.setPackageName(inWSData.getPackageName());
471:
472: /**
473: * Set the WSDL to use.
474: */
475: try {
476: wsdl2Java.setWsdlUrl(new URL(inWSData.getURL()));
477: } catch (MalformedURLException mfue) {
478: ErrorManager.getDefault().notify(mfue);
479: // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "WS_NOJAR_ERROR"),2);
480: StatusDisplayer.getDefault().setStatusText(
481: NbBundle.getMessage(Util.class, "WS_NOJAR_ERROR"));
482: return false;
483: }
484:
485: /**
486: * Set the proxy information.
487: */
488: System.setProperty("http.proxyHost", WebProxySetter
489: .getInstance().getProxyHost());
490: System.setProperty("http.proxyPort", WebProxySetter
491: .getInstance().getProxyPort());
492:
493: /**
494: * Do it!
495: */
496:
497: /**
498: * Let the user know we'return doing some time consuming process.
499: */
500: StatusDisplayer.getDefault().setStatusText(
501: NbBundle.getMessage(Util.class, "WS_CREATING_JARFILE"));
502:
503: if (!wsdl2Java.execute(inWSData, outputStream)) {
504: ErrorManager.getDefault().log(
505: "Util.createWSJar:"
506: + NbBundle.getMessage(Util.class,
507: "WS_WSDL2JAVA_ERROR"));
508: // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "WS_WSDL2JAVA_ERROR"),2);
509: StatusDisplayer.getDefault().setStatusText(
510: NbBundle.getMessage(Util.class,
511: "WS_WSDL2JAVA_ERROR"));
512: return false;
513: }
514: StatusDisplayer.getDefault().setStatusText(
515: NbBundle.getMessage(Util.class,
516: "WS_CREATING_JARFILE_FINISHED"));
517:
518: /**
519: * Now we need to compile the wrapper client java files.
520: *
521: */
522:
523: ArrayList argList = new ArrayList();
524:
525: argList.add("-d");
526: argList.add(tmpOutputDir.getAbsolutePath());
527: argList.add("-classpath");
528: String classPath = tmpOutputDir.getAbsolutePath()
529: + File.pathSeparator + Util.getRuntimeClassPath();
530: argList.add(classPath);
531: argList.add("-g");
532:
533: /**
534: * Now add the files to be compiled
535: */
536: File wrapperFile = wsdl2Java.getWebserviceClient();
537: argList.add(wrapperFile.getAbsolutePath());
538: File wrapperBeanInfoFile = wsdl2Java
539: .getWebserviceClientBeanInfo();
540: argList.add(wrapperBeanInfoFile.getAbsolutePath());
541:
542: String[] args = (String[]) argList.toArray(new String[0]);
543:
544: // ByteArrayOutputStream javacOutput = new ByteArrayOutputStream();
545:
546: /**
547: * Define a temp file for the compile results.
548: */
549: // String outputDir = System.getProperty("user.home");
550: // File outputDirFile = new File(outputDir);
551: File tempFile = null;
552: try {
553: // tempFile = File.createTempFile("wstemp","compile_errors",outputDirFile);
554: tempFile = File.createTempFile("wstemp", "compile_errors");
555: } catch (IOException ioe) {
556: ErrorManager.getDefault().notify(ioe);
557: // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
558: StatusDisplayer.getDefault().setStatusText(
559: NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
560: return false;
561: }
562:
563: FileOutputStream out = null;
564:
565: try {
566: out = new FileOutputStream(tempFile);
567: } catch (FileNotFoundException fnfe) {
568:
569: ErrorManager.getDefault().notify(fnfe);
570: // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
571: StatusDisplayer.getDefault().setStatusText(
572: NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
573: return false;
574: }
575:
576: JavaCompilerHelper compilerHelper = new JavaCompilerHelper(out);
577:
578: StatusDisplayer.getDefault().setStatusText(
579: NbBundle.getMessage(Util.class,
580: "WS_CLIENTWRAPPER_COMPILING"));
581:
582: boolean result = compilerHelper.compile(args);
583: if (!result) {
584: ErrorManager.getDefault().log(
585: "Util.createWSJar: "
586: + NbBundle.getMessage(Util.class,
587: "WS_CLIENTWRAPPER_COMPILE_ERROR")
588: + tempFile == null ? "" : tempFile
589: .getAbsolutePath());
590: // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
591: StatusDisplayer.getDefault().setStatusText(
592: NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
593: return false;
594: } else {
595: StatusDisplayer.getDefault().setStatusText(
596: NbBundle.getMessage(Util.class,
597: "WS_CLIENTWRAPPER_COMPILE_OK"));
598: /**
599: * clean up the output file since the compile was successful
600: */
601: tempFile.delete();
602: }
603:
604: /**
605: * Now copy the web service icon image to the folder that will be jarred up.
606: */
607: try {
608: // Copy the Image contents from the URL into the new file craeted in the backing folder.
609: URL imageUrl = Util.class
610: .getResource("/org/netbeans/modules/websvc/registry/resources/webservice.png");
611: DataInputStream in = new DataInputStream(imageUrl
612: .openStream());
613: String iconImagePath = tmpOutputDir.getAbsolutePath()
614: + File.separator
615: + inWSData.getPackageName().replace('.',
616: File.separatorChar);
617: File outputFile = new File(
618: iconImagePath,
619: WrapperClientBeanInfoWriter.WEBSERVICE_ICON_FILENAME);
620: DataOutputStream outImage = new DataOutputStream(
621: new FileOutputStream(outputFile));
622:
623: byte[] bytes = new byte[1024];
624: int byteCount = in.read(bytes);
625:
626: while (byteCount > -1) {
627: outImage.write(bytes);
628: byteCount = in.read(bytes);
629: }
630: outImage.flush();
631: outImage.close();
632: in.close();
633: } catch (IOException ioe) {
634: ErrorManager.getDefault().notify(ioe);
635: // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "IMAGE_COPY_ERROR"),2);
636: StatusDisplayer.getDefault()
637: .setStatusText(
638: NbBundle.getMessage(Util.class,
639: "IMAGE_COPY_ERROR"));
640: return false;
641: }
642:
643: /**
644: * Now create the jar file from the output.
645: */
646: File wsJarFile = new File(inJarFileName);
647: JarUtil jarUtil = new JarUtil(wsJarFile);
648: jarUtil.addDirectory(new File(wsdl2Java.getOutputDirectory()));
649:
650: return true;
651: }
652:
653: public static String upperCaseFirstChar(String inString) {
654: if (null == inString) {
655: throw new IllegalArgumentException("Null string passed!");
656: }
657: String returnString = new String(inString);
658: String firstCharacter = returnString.substring(0, 1);
659: returnString = firstCharacter.toUpperCase()
660: + returnString.substring(1);
661:
662: return returnString;
663: }
664:
665: /**
666: * This method will take a WSDL port name like "threat.cfc" and change it to
667: * "ThreatCfc"
668: */
669: public static String getProperPortName(String inPortName) {
670: String returnString = "";
671: /**
672: * parse the string by ".", uppercase each piece, and put them back together
673: */
674:
675: /**
676: * If we don't have a "." in the string, simply uppercase the first char and
677: * return it.
678: */
679: if (inPortName.indexOf(".") == -1) {
680: returnString = Util.upperCaseFirstChar(inPortName);
681: return returnString;
682: }
683:
684: StringTokenizer tokenizer = new StringTokenizer(inPortName, ".");
685: while (tokenizer.hasMoreTokens()) {
686: String currentToken = tokenizer.nextToken();
687: returnString += Util.upperCaseFirstChar(currentToken);
688:
689: }
690:
691: return returnString;
692:
693: }
694:
695: public static String[] getRunTimeJarFiles()
696: throws WebServiceException {
697: /**
698: * Read in the Runtime Jar file Names
699: */
700:
701: ArrayList returnJarFileNames = new ArrayList();
702:
703: DocumentBuilderFactory factory = DocumentBuilderFactory
704: .newInstance();
705: DocumentBuilder builder = null;
706:
707: try {
708: builder = factory.newDocumentBuilder();
709:
710: } catch (ParserConfigurationException pe) {
711: ErrorManager.getDefault().notify(pe);
712: ErrorManager.getDefault().log(
713: "Util.getRunTimeJarFiles: ParserConfigurationException="
714: + pe);
715: throw new WebServiceException(NbBundle.getMessage(
716: Util.class, "ERROR_READING_RUNTIMEJARS"), pe);
717: }
718:
719: Document document = null;
720: try {
721: File runtimeJarsFile = InstalledFileLocator.getDefault()
722: .locate(
723: "config" + File.separator + "WebServices"
724: + File.separator
725: + "websvc_runtimejars.xml", null,
726: false);
727: document = builder.parse(runtimeJarsFile);
728: } catch (SAXException se) {
729: ErrorManager.getDefault().notify(se);
730: ErrorManager.getDefault().log(
731: "Util.getRunTimeJarFiles: SAXException=" + se);
732: throw new WebServiceException(NbBundle.getMessage(
733: Util.class, "ERROR_READING_RUNTIMEJARS"), se);
734: } catch (IOException ioe) {
735: ErrorManager.getDefault().notify(ioe);
736: ErrorManager.getDefault().log(
737: "Util.getRunTimeJarFiles: IOException=" + ioe);
738: throw new WebServiceException(NbBundle.getMessage(
739: Util.class, "ERROR_READING_RUNTIMEJARS"), ioe);
740: }
741:
742: NodeList list = document.getElementsByTagName("Jar");
743:
744: String serverInstanceIDs[] = Deployment.getDefault()
745: .getServerInstanceIDs();
746: J2eePlatform platform = null;
747: for (int i = 0; i < serverInstanceIDs.length; i++) {
748: J2eePlatform p = Deployment.getDefault().getJ2eePlatform(
749: serverInstanceIDs[i]);
750: if (p != null && p.isToolSupported("wscompile")) {
751: platform = p;
752: break;
753: }
754: }
755: File appserverRoot = platform == null ? null : platform
756: .getPlatformRoots()[0];
757: String asRootPath = (appserverRoot != null) ? appserverRoot
758: .getAbsolutePath() : "";
759: asRootPath = asRootPath.replace('\\', '/');
760:
761: Node currentNode = null;
762: for (int ii = 0; ii < list.getLength(); ii++) {
763: currentNode = list.item(ii);
764: String name = currentNode.getNodeName();
765: String localName = currentNode.getLocalName();
766: String value = currentNode.getNodeValue();
767: NamedNodeMap nodeMap = currentNode.getAttributes();
768: Node fileNode = nodeMap.getNamedItem("file");
769: String jarString = "";
770: try {
771: jarString = fileNode.getNodeValue();
772: } catch (DOMException de) {
773: ErrorManager.getDefault().notify(de);
774: ErrorManager.getDefault().log(
775: "Util.getRunTimeJarFiles: DOMException=" + de);
776: throw new WebServiceException(NbBundle.getMessage(
777: Util.class, "ERROR_READING_RUNTIMEJARS"), de);
778: }
779: if (jarString.indexOf("\\{appserv\\.home\\}") > -1) {
780: jarString = jarString.replaceAll(
781: "\\{appserv\\.home\\}", asRootPath);
782: } else {
783: File f = InstalledFileLocator.getDefault().locate(
784: jarString, null, false);
785: if (f != null) {
786: jarString = f.getPath();
787: }
788: }
789: returnJarFileNames.add(jarString);
790: }
791:
792: return (String[]) returnJarFileNames.toArray(new String[0]);
793:
794: }
795:
796: /**
797: * This method will construct the default classpath to be used for running the "wscompile" tool.
798: */
799: public static String getRuntimeClassPath() {
800: String[] jarFileNames = null;
801: String returnClassPath = "";
802: try {
803: jarFileNames = Util.getRunTimeJarFiles();
804: } catch (WebServiceException wse) {
805: // StatusDisplayer.getDefault().displayError(wse.getMessage(),2);
806: StatusDisplayer.getDefault()
807: .setStatusText(wse.getMessage());
808: return returnClassPath;
809: }
810:
811: for (int ii = 0; null != jarFileNames
812: && ii < jarFileNames.length; ii++) {
813: returnClassPath += jarFileNames[ii];
814: /**
815: * If there's another path, add the path separator
816: */
817: if (ii + 1 < jarFileNames.length) {
818: returnClassPath += File.pathSeparator;
819: }
820: }
821:
822: return returnClassPath;
823:
824: }
825:
826: /**
827: * This method will determine if a package name is valid. For each qualification of the package name (part between
828: * the ".", the first character will be checked against Character.isJavaIdentifierStart() then each character will
829: * be checked against Character.isJavaIdentifierPart().
830: */
831: public static boolean isValidPackageName(String inPackageName) {
832: if (null == inPackageName || inPackageName.length() == 0)
833: return false;
834:
835: /**
836: * A "$" seems to be a valid part but messes with the package name so we need to check for it
837: * explicitly.
838: */
839: if (inPackageName.indexOf("$") != -1) {
840: return false;
841: }
842:
843: /**
844: * If we have more than one qualification ("."), we need to check the start of each
845: * one for validity then each character of each one.
846: */
847:
848: StringTokenizer tokenizer = new StringTokenizer(inPackageName,
849: ".");
850:
851: if (tokenizer.hasMoreTokens()) {
852: String currentLevel = null;
853: while (tokenizer.hasMoreTokens()) {
854: currentLevel = (String) tokenizer.nextToken();
855: if (!Character.isJavaIdentifierStart(currentLevel
856: .charAt(0))) {
857: return false;
858: }
859: for (int ii = 0; ii < currentLevel.length(); ii++) {
860: if (!Character.isJavaIdentifierPart(currentLevel
861: .charAt(ii))) {
862: return false;
863: }
864: }
865: }
866: } else {
867: /**
868: * we only have a single qualification for the package name.
869: */
870: if (!Character.isJavaIdentifierStart(inPackageName
871: .charAt(0))) {
872: return false;
873: }
874: for (int ii = 0; ii < inPackageName.length(); ii++) {
875: if (!Character.isJavaIdentifierPart(inPackageName
876: .charAt(ii))) {
877: return false;
878: }
879: }
880: }
881: return true;
882: }
883:
884: /**
885: * This method will determine if an identifier is valid. The first character will be checked against Character.isJavaIdentifierStart() then each character will
886: * be checked against Character.isJavaIdentifierPart().
887: */
888: public static boolean isValidIdentifier(String inIdentifier) {
889: if (null == inIdentifier || inIdentifier.length() == 0)
890: return false;
891:
892: if (!Character.isJavaIdentifierStart(inIdentifier.charAt(0))) {
893: return false;
894: }
895: for (int ii = 0; ii < inIdentifier.length(); ii++) {
896: if (!Character
897: .isJavaIdentifierPart(inIdentifier.charAt(ii))) {
898: return false;
899: }
900: }
901:
902: return true;
903: }
904:
905: /**
906: * Bug fix: 5059732
907: * This method will return the correct type for the parameter. If the JavaParameter is considered a "Holder"
908: * the holder class name will be used.
909: *
910: *TODO: include in JAX-RPC API
911: *
912: * If the parameter is a "Holder" we need the holder type and not the JavaType. This is
913: * typically the case when there is no return type and the parameter's meant to be mutable, pass-by-reference
914: * type parameters. I took the code below directly from the JAX-RPC class:
915: * "com.sun.xml.rpc.processor.generator.StubGenerator" except that I'm not checking the Operation for an Array type.
916: * - David Botterill 6/8/2004
917: *
918: */
919: public static String getParameterType(Port inPort,
920: JavaParameter inParameter) {
921:
922: String parameterType = "";
923: ClientProcessorEnvironment env = new ClientProcessorEnvironment(
924: new ByteArrayOutputStream(), null, null);
925:
926: if (inParameter.isHolder()) {
927: if (inParameter.getHolderName() == null) {
928: parameterType = env.getNames().holderClassName(inPort,
929: inParameter.getType());
930: } else {
931: parameterType = inParameter.getHolderName();
932: }
933: } else {
934: parameterType = inParameter.getType().getName();
935: }
936:
937: return parameterType;
938:
939: }
940: }
941: // END_NOI18N
|