KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > util > Util


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.registry.util;
21
22 import java.util.*;
23 import java.io.*;
24 import java.net.URL JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import org.w3c.dom.*;
27 import javax.xml.parsers.DocumentBuilder JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.openide.*;
32 import org.openide.util.NbBundle;
33 import org.openide.awt.StatusDisplayer;
34 import com.sun.xml.rpc.util.JavaCompilerHelper;
35 import com.sun.xml.rpc.processor.util.ClientProcessorEnvironment;
36 import com.sun.xml.rpc.processor.model.Port;
37 import com.sun.xml.rpc.processor.model.java.JavaParameter;
38 import org.netbeans.modules.websvc.registry.jaxrpc.Wsdl2Java;
39 import org.netbeans.modules.websvc.registry.WebServiceException;
40 import org.netbeans.modules.websvc.registry.model.WebServiceData;
41 import org.netbeans.modules.websvc.registry.jaxrpc.WrapperClientBeanInfoWriter;
42
43 import org.openide.modules.InstalledFileLocator;
44 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
45 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
46
47 // BEGIN_NOI18N
48
/**
49  * General utility methods for WSDL documents.
50  */

51 public class Util {
52 // public static final int BUFFER_SIZE = 4096;
53
// public static final String xsdNamespace = "xsd";
54
// final static public String WSDL_FILE_EXTENSION = "wsdl";
55
// /*
56
// * Removes any namespace prefix.
57
// * eg: 'xsd:element' -> 'element'
58
// */
59
// public static String removeNamespace(String tagName) {
60
// int pos = tagName.indexOf(':');
61
// if (pos < 0)
62
// return tagName;
63
// return tagName.substring(pos+1);
64
// }
65
//
66
// /**
67
// * Take an XML Schema data type and make a Java type out of it.
68
// * eg: 'xsd:string' -> 'java.lang.String'
69
// */
70
// /*
71
// public static String xmlSchemaType2JavaType(String typeName) {
72
// if ((xsdNamespace+":string").equals(typeName))
73
// return "java.lang.String";
74
// if ("char_lb_rb".equals(typeName))
75
// return "char[]";
76
// if (typeName.startsWith(xsdNamespace+":"))
77
// return typeName.substring(xsdNamespace.length()+1);
78
// return typeName;
79
// }
80
// */
81
// public static void insertSimpleTypes(Map typeMapping) {
82
// typeMapping.put("java.lang.Integer", "java.lang.Integer");
83
// typeMapping.put("java.lang.Short", "java.lang.Short");
84
// typeMapping.put("java.lang.Long", "java.lang.Long");
85
// typeMapping.put("java.lang.Double", "java.lang.Double");
86
// typeMapping.put("java.lang.Boolean", "java.lang.Boolean");
87
// typeMapping.put("java.lang.Character", "java.lang.Character");
88
// typeMapping.put("java.lang.Float", "java.lang.Float");
89
// typeMapping.put("java.lang.StringBuffer", "java.lang.StringBuffer");
90
// typeMapping.put("java.lang.Byte", "java.lang.Byte");
91
// typeMapping.put("java.lang.String", "java.lang.String");
92
// typeMapping.put("java.math.BigInteger", "java.math.BigInteger");
93
// typeMapping.put("char_lb_rb", "char[]");
94
// typeMapping.put("string", "java.lang.String");
95
// typeMapping.put("int", "int");
96
// typeMapping.put("short", "short");
97
// typeMapping.put("long", "long");
98
// typeMapping.put("double", "double");
99
// typeMapping.put("boolean", "boolean");
100
// typeMapping.put("char", "char");
101
// typeMapping.put("float", "float");
102
// typeMapping.put("byte", "byte");
103
// typeMapping.put("decimal", "java.math.BigDecimal");
104
// typeMapping.put("dateTime", "java.util.Date");
105
// typeMapping.put((xsdNamespace+":string").intern(), "java.lang.String");
106
// typeMapping.put((xsdNamespace+":int").intern(), "int");
107
// typeMapping.put((xsdNamespace+":short").intern(), "short");
108
// typeMapping.put((xsdNamespace+":long").intern(), "long");
109
// typeMapping.put((xsdNamespace+":double").intern(), "double");
110
// typeMapping.put((xsdNamespace+":boolean").intern(), "boolean");
111
// typeMapping.put((xsdNamespace+":char").intern(), "char");
112
// typeMapping.put((xsdNamespace+":float").intern(), "float");
113
// typeMapping.put((xsdNamespace+":byte").intern(), "byte");
114
// typeMapping.put((xsdNamespace+":decimal").intern(), "java.math.BigDecimal");
115
// typeMapping.put((xsdNamespace+":dateTime").intern(), "java.util.Date");
116
// }
117
//
118
// private static Map baseTypes = null;
119
// public static boolean isSerializerNeeded(String type) {
120
// if (baseTypes == null) {
121
// baseTypes = new HashMap();
122
// insertSimpleTypes(baseTypes);
123
// }
124
// return !baseTypes.containsKey(type);
125
// }
126
//
127
// /**
128
// * Given an XML Schema Element, figure out the java type to use.
129
// */
130
// public static String getType(Element el, Map typeMapping,
131
// Map elementTypeMapping) {
132
// if (el == null)
133
// return null;
134
// String elementName = el.getAttribute("name");
135
// String type = null;
136
// if ((xsdNamespace+":element").equals(el.getNodeName())) {
137
// //System.out.println("element name="+elementName);
138
// String typeAttribute = el.getAttribute("type");
139
// if (typeAttribute != null && !typeAttribute.equals("") &&
140
// !typeAttribute.equals("SOAP-ENC:Array")) {
141
// type = (String) typeMapping.get(typeAttribute);
142
// } else {
143
// // Recurse in and see what kind of type this thing is.
144
// type = getType(findFirstNode(el), typeMapping, elementTypeMapping);
145
// }
146
// } else if ((xsdNamespace+":simpleType").equals(el.getNodeName())) {
147
// type = (String) typeMapping.get(elementName);
148
// if (type == null) {
149
// try {
150
// Element restrictionNode = findFirstNodeByName(el, xsdNamespace+":restriction");
151
// type = (String) typeMapping.get(restrictionNode.getAttribute("base"));
152
// } catch (NotFoundException e) {
153
// // It's okay (for now), just go with our default.
154
// }
155
// }
156
// } else if ((xsdNamespace+":complexType").equals(el.getNodeName())) {
157
// Element annotationNode = null;
158
// try {
159
// annotationNode = findFirstNodeByName(el, xsdNamespace+":annotation");
160
// Element appInfoNode = findFirstNodeByName(annotationNode,
161
// xsdNamespace+":appinfo");
162
// try {
163
// Node bindingNode = findFirstNodeByName(appInfoNode, "BINDING_TYPE");
164
// type = bindingNode.getFirstChild().getNodeValue();
165
// } catch (NotFoundException e1) {
166
// try {
167
// Node oldbindingNode = findFirstNodeByName(appInfoNode, "OLDBINDING_TYPE");
168
// type = oldbindingNode.getFirstChild().getNodeValue();
169
// /*if (elementName != null) {
170
// String baseName = GenPresentation.baseName(type).intern();
171
// if (typeMapping.get(baseName) == null) {
172
// // Put our baseName in there too, since
173
// // we might look for it later by that name.
174
// //System.out.println("Putting in "+baseName+" as "+type);
175
// typeMapping.put(baseName, type);
176
// }
177
// }*/
178
// } catch (NotFoundException e2) {
179
// Node beanNode = findFirstNodeByName(appInfoNode, "BEAN_TYPE");
180
// type = beanNode.getFirstChild().getNodeValue();
181
// }
182
// }
183
// } catch (NotFoundException e) {
184
// //e.printStackTrace();
185
// type = "void"; // NOI18N
186
// }
187
// NodeList nodes = el.getChildNodes();
188
// for (int i = 0; i < nodes.getLength(); ++i) {
189
// Node node = nodes.item(i);
190
// if (!(node instanceof Element))
191
// continue;
192
// Element childEl = (Element) node;
193
// if (childEl == annotationNode)
194
// continue;
195
// // Insert our children's types into our mapping.
196
// getType(childEl, typeMapping, elementTypeMapping);
197
// }
198
// } else if ((xsdNamespace+":sequence").equals(el.getNodeName())) {
199
// NodeList nodes = el.getChildNodes();
200
// for (int i = 0; i < nodes.getLength(); ++i) {
201
// Node node = nodes.item(i);
202
// if (!(node instanceof Element))
203
// continue;
204
// Element childEl = (Element) node;
205
// // Insert our children's types into our mapping.
206
// getType(childEl, typeMapping, elementTypeMapping);
207
// }
208
// } else if ((xsdNamespace+":import").equals(el.getNodeName())) {
209
// // Nothing for us to do here....
210
// } else {
211
// //System.out.println("Unfamiliar XML Schema element: "+el.getNodeName()); // NOI18N
212
// return null;
213
// }
214
// if (type == null)
215
// type = "void";
216
// //System.out.println("Found type: elementName="+elementName+" type="+type);
217
// type = type.intern();
218
// if (elementName != null) {
219
// typeMapping.put(elementName.intern(), type);
220
// }
221
// elementTypeMapping.put(el, type);
222
// return type;
223
// }
224
//
225
// /**
226
// * Return a Collection of all child Elements
227
// */
228
// public static Collection getChildElements(Element parent) {
229
// Collection result = new LinkedList();
230
// NodeList children = parent.getChildNodes();
231
// for (int i = 0; i < children.getLength(); ++i) {
232
// Node child = children.item(i);
233
// if (!(child instanceof Element))
234
// continue;
235
// result.add(child);
236
// }
237
// return result;
238
// }
239
//
240
// public static Element findFirstNode(Element node) {
241
// NodeList nodes = node.getChildNodes();
242
// for (int i = 0; i < nodes.getLength(); ++i) {
243
// Node n = nodes.item(i);
244
// if (n instanceof Element)
245
// return (Element) n;
246
// }
247
// return null;
248
// }
249
//
250
// public static Element findFirstNodeByName(Element node, String name) throws NotFoundException {
251
// return findFirstNodeByName(node.getChildNodes(), name);
252
// }
253
//
254
// /**
255
// * Search for a node named @param name. Namespace is ignored.
256
// */
257
// public static Element findFirstNodeByName(NodeList nodes, String name) throws NotFoundException {
258
// name = removeNamespace(name);
259
// for (int i = 0; i < nodes.getLength(); ++i) {
260
// Node n = nodes.item(i);
261
// String localNodeName = n.getLocalName();
262
// if (localNodeName == null) {
263
// localNodeName = removeNamespace(n.getNodeName());
264
// }
265
// if (name.equals(localNodeName))
266
// return (Element) n;
267
// }
268
//
269
//
270
// throw new NotFoundException(MessageFormat.format(NbBundle.getMessage(Util.class,
271
// "MSG_UnableToFindNode"),
272
// new Object[] {name}), name);
273
// }
274
//
275
// public static Element findFirstNodeByNames(Element node, String[] names) throws NotFoundException {
276
// for (int i = 0; i < names.length; ++i)
277
// node = findFirstNodeByName(node, names[i]);
278
// return node;
279
// }
280
//
281
// public static Element findFirstNodeByName(Document doc, String name) throws NotFoundException {
282
// return findFirstNodeByName(doc.getChildNodes(), name);
283
// }
284
//
285
// //Following methods were copied from com.sun.forte4j.webdesigner.xmlcomponent.Util
286
// //because the wsdl module cannot be dependent on jwd.
287
//
288
// public static Class getClass(String className) {
289
// Class cls;
290
// if (className == null)
291
// return null;
292
// if (isClassArray(className)) {
293
// // Recursively figure out what our type is.
294
// // Is this the best way for getting an Array type?
295
// cls = getClass(className.substring(0, className.length()-2));
296
// Object arrayObject = java.lang.reflect.Array.newInstance(cls, 0);
297
// return arrayObject.getClass();
298
// }
299
// //ClassLoader cl = Top Manager.getDefault().currentClassLoader();
300
// //
301
// // Removing Top Manager calls (see Issuezilla 31753). Replace above with:
302
// ClassLoader cl = ClassPath.getClassPath(null, ClassPath.EXECUTE).getClassLoader(false);
303
//
304
// // BEGIN_NOI18N
305
// if ("int".equals(className))
306
// cls = Integer.TYPE;
307
// else if ("long".equals(className))
308
// cls = Long.TYPE;
309
// else if ("char".equals(className))
310
// cls = Character.TYPE;
311
// else if ("short".equals(className))
312
// cls = Short.TYPE;
313
// else if ("double".equals(className))
314
// cls = Double.TYPE;
315
// else if ("float".equals(className))
316
// cls = Float.TYPE;
317
// else if ("byte".equals(className))
318
// cls = Byte.TYPE;
319
// else if ("boolean".equals(className))
320
// cls = Boolean.TYPE;
321
// else if ("void".equals(className))
322
// cls= Void.TYPE;
323
// else {
324
// try {
325
// cls = cl.loadClass(className);
326
// } catch (java.lang.ClassNotFoundException e) {
327
// e.printStackTrace();
328
// cls = null;
329
// }
330
// }
331
// // END_NOI18N
332
// return cls;
333
// }
334
//
335
// public static boolean compile(DataObject[] dataObjects) {
336
// CompilerJob job = new CompilerJob(Compiler.DEPTH_ONE);
337
// for(int i = 0; i < dataObjects.length; i++) {
338
// CompilerCookie comp = (CompilerCookie) dataObjects[i].
339
// getCookie(CompilerCookie.Compile.class);
340
// if (comp != null) {
341
// comp.addToJob(job, Compiler.DEPTH_ONE);
342
// }
343
// }
344
// CompilerTask task = job.start();
345
// return task.isSuccessful();
346
// }
347
//
348
// static public boolean isClassArray(String returnClassName) {
349
// return returnClassName.endsWith("[]"); // NOI18N
350
// }
351
//
352
// static public boolean isCollectionType(String bindingType) {
353
// boolean isCollection = false;
354
// Class c = getClass(bindingType);
355
// if(c != null){
356
// if (java.util.Collection.class.isAssignableFrom(c))
357
// isCollection = true;
358
// }
359
//
360
// return isCollection;
361
// }
362

363     // change all occurrences of oldSubString to newSubString
364
public static String JavaDoc changeString(String JavaDoc inString, String JavaDoc oldSubString, String JavaDoc newSubString) {
365         if (oldSubString.trim().equals(""))
366             return inString;
367         
368         int start = 0;
369         int end = 0;
370         StringBuffer JavaDoc changedString = new StringBuffer JavaDoc("");
371         
372         end = inString.indexOf(oldSubString, start);
373         while(end != -1) {
374             //add substring before oldSubString and append newSubString
375
changedString.append(inString.substring(start, end) + newSubString);
376             
377             //recompute starting index to end of changed substring
378
start = end + oldSubString.length();
379             //recompute end index
380
end = inString.indexOf(oldSubString, start);
381         }
382         
383         //append remainder of the String, if any
384
changedString.append(inString.substring(start));
385         
386         return changedString.toString();
387     }
388     
389     /**
390      * This method will create a jar file for a given WebServiceData object.
391      *
392      * @param inWSData - the WebServiceData to create the jar file for.
393      * @returns jarfileName - the file name (without path) of the jar file created. null if not created.
394      *
395      *
396      */

397     public static boolean createWSJar(WebServiceData inWSData, OutputStream inOutputStream, String JavaDoc inJarFileName) {
398         
399         String JavaDoc jarFileName = null;
400         
401         OutputStream outputStream = null;
402         
403         FileOutputStream fileOutputStream = null;
404         Date date = new Date();
405         File tmpOutputDir = null;
406         File errorFile = null;
407         try{
408             File tempFile = File.createTempFile("wstemp","ws");
409             tmpOutputDir = new File(tempFile.getParentFile(), "wstemp" + date.getTime());
410             if (!tmpOutputDir.exists()) tmpOutputDir.mkdirs();
411             errorFile = File.createTempFile("wscompile","error",tempFile.getParentFile());
412             
413             /**
414              * if the outputstream is null, create a temporary directory for the wscompile.
415              */

416             if(null == inOutputStream) {
417                 fileOutputStream = new FileOutputStream(errorFile);
418                 outputStream = fileOutputStream;
419             } else {
420                 outputStream = inOutputStream;
421             }
422         }catch (IOException ioe){
423             ErrorManager.getDefault().notify(ioe);
424 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
425
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
426             return false;
427         }
428         
429         /**
430          * Now create the class to do the WSDL to Java conversion
431          */

432         Wsdl2Java wsdl2Java = new Wsdl2Java();
433         /**
434          * Set the output directory to the temp one we just created.
435          */

436         wsdl2Java.setOutputDirectory(tmpOutputDir.getAbsolutePath());
437         
438         /**
439          * Set the package.
440          */

441         wsdl2Java.setPackageName(inWSData.getPackageName());
442         
443         /**
444          * Set the WSDL to use.
445          */

446         try {
447             wsdl2Java.setWsdlUrl(new URL JavaDoc(inWSData.getURL()));
448         } catch(MalformedURLException JavaDoc mfue) {
449             ErrorManager.getDefault().notify(mfue);
450 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "WS_NOJAR_ERROR"),2);
451
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "WS_NOJAR_ERROR"));
452             return false;
453         }
454         
455         /**
456          * Set the proxy information.
457          */

458         System.setProperty("http.proxyHost", WebProxySetter.getInstance().getProxyHost());
459         System.setProperty("http.proxyPort", WebProxySetter.getInstance().getProxyPort());
460         
461         
462         /**
463          * Do it!
464          */

465         
466         /**
467          * Let the user know we'return doing some time consuming process.
468          */

469         StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "WS_CREATING_JARFILE"));
470         
471         if(!wsdl2Java.execute(inWSData,outputStream)) {
472             ErrorManager.getDefault().log("Util.createWSJar:" + NbBundle.getMessage(Util.class, "WS_WSDL2JAVA_ERROR"));
473 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "WS_WSDL2JAVA_ERROR"),2);
474
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "WS_WSDL2JAVA_ERROR"));
475             return false;
476         }
477         StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "WS_CREATING_JARFILE_FINISHED"));
478         
479         /**
480          * Now we need to compile the wrapper client java files.
481          *
482          */

483         
484         ArrayList argList = new ArrayList();
485         
486         argList.add("-d");
487         argList.add(tmpOutputDir.getAbsolutePath());
488         argList.add("-classpath");
489         String JavaDoc classPath = tmpOutputDir.getAbsolutePath() + File.pathSeparator + Util.getRuntimeClassPath();
490         argList.add(classPath);
491         argList.add("-g");
492         
493         /**
494          * Now add the files to be compiled
495          */

496         File wrapperFile = wsdl2Java.getWebserviceClient();
497         argList.add(wrapperFile.getAbsolutePath());
498         File wrapperBeanInfoFile = wsdl2Java.getWebserviceClientBeanInfo();
499         argList.add(wrapperBeanInfoFile.getAbsolutePath());
500         
501         String JavaDoc [] args = (String JavaDoc [])argList.toArray(new String JavaDoc[0]);
502         
503         // ByteArrayOutputStream javacOutput = new ByteArrayOutputStream();
504

505         /**
506          * Define a temp file for the compile results.
507          */

508 // String outputDir = System.getProperty("user.home");
509
// File outputDirFile = new File(outputDir);
510
File tempFile = null;
511         try{
512 // tempFile = File.createTempFile("wstemp","compile_errors",outputDirFile);
513
tempFile = File.createTempFile("wstemp","compile_errors");
514         }catch (IOException ioe){
515             ErrorManager.getDefault().notify(ioe);
516 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
517
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
518             return false;
519         }
520         
521         FileOutputStream out = null;
522         
523         try {
524             out = new FileOutputStream(tempFile);
525         } catch(FileNotFoundException fnfe) {
526             
527             ErrorManager.getDefault().notify(fnfe);
528 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
529
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
530             return false;
531         }
532         
533         JavaCompilerHelper compilerHelper = new JavaCompilerHelper(out);
534         
535         StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "WS_CLIENTWRAPPER_COMPILING"));
536         
537         boolean result = compilerHelper.compile(args);
538         if (!result) {
539             ErrorManager.getDefault().log("Util.createWSJar: " + NbBundle.getMessage(Util.class, "WS_CLIENTWRAPPER_COMPILE_ERROR") + tempFile == null ? "" : tempFile.getAbsolutePath());
540 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"),2);
541
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR"));
542             return false;
543         } else {
544             StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "WS_CLIENTWRAPPER_COMPILE_OK"));
545             /**
546              * clean up the output file since the compile was successful
547              */

548             tempFile.delete();
549         }
550         
551         
552         /**
553          * Now copy the web service icon image to the folder that will be jarred up.
554          */

555         try {
556             // Copy the Image contents from the URL into the new file craeted in the backing folder.
557
URL JavaDoc imageUrl = Util.class.getResource("/org/netbeans/modules/websvc/registry/resources/webservice.png");
558             DataInputStream in = new DataInputStream(imageUrl.openStream());
559             String JavaDoc iconImagePath = tmpOutputDir.getAbsolutePath() + File.separator + inWSData.getPackageName().replace('.', File.separatorChar);
560             File outputFile = new File(iconImagePath,WrapperClientBeanInfoWriter.WEBSERVICE_ICON_FILENAME);
561             DataOutputStream outImage = new DataOutputStream(new FileOutputStream(outputFile));
562             
563             byte[] bytes = new byte[1024];
564             int byteCount = in.read(bytes);
565             
566             while ( byteCount > -1 ) {
567                 outImage.write( bytes );
568                 byteCount = in.read(bytes);
569             }
570             outImage.flush();
571             outImage.close();
572             in.close();
573         } catch (IOException ioe) {
574             ErrorManager.getDefault().notify(ioe);
575 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(Util.class, "IMAGE_COPY_ERROR"),2);
576
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "IMAGE_COPY_ERROR"));
577             return false;
578         }
579         
580         
581         /**
582          * Now create the jar file from the output.
583          */

584         File wsJarFile = new File(inJarFileName);
585         JarUtil jarUtil = new JarUtil(wsJarFile);
586         jarUtil.addDirectory(new File(wsdl2Java.getOutputDirectory()));
587         
588         
589         
590         return true;
591     }
592     
593     public static String JavaDoc upperCaseFirstChar(String JavaDoc inString) {
594         if(null == inString) {
595             throw new IllegalArgumentException JavaDoc("Null string passed!");
596         }
597         String JavaDoc returnString = new String JavaDoc(inString);
598         String JavaDoc firstCharacter = returnString.substring(0,1);
599         returnString = firstCharacter.toUpperCase() + returnString.substring(1);
600         
601         return returnString;
602     }
603     /**
604      * This method will take a WSDL port name like "threat.cfc" and change it to
605      * "ThreatCfc"
606      */

607     public static String JavaDoc getProperPortName(String JavaDoc inPortName) {
608         String JavaDoc returnString = "";
609         /**
610          * parse the string by ".", uppercase each piece, and put them back together
611          */

612         
613         /**
614          * If we don't have a "." in the string, simply uppercase the first char and
615          * return it.
616          */

617         if(inPortName.indexOf(".") == -1) {
618             returnString = Util.upperCaseFirstChar(inPortName);
619             return returnString;
620         }
621         
622         StringTokenizer tokenizer = new StringTokenizer(inPortName,".");
623         while(tokenizer.hasMoreTokens()) {
624             String JavaDoc currentToken = tokenizer.nextToken();
625             returnString += Util.upperCaseFirstChar(currentToken);
626             
627         }
628         
629         return returnString;
630         
631     }
632     
633     public static String JavaDoc [] getRunTimeJarFiles() throws WebServiceException {
634         /**
635          * Read in the Runtime Jar file Names
636          */

637         
638         ArrayList returnJarFileNames = new ArrayList();
639         
640         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
641         DocumentBuilder JavaDoc builder = null;
642         
643         try {
644             builder = factory.newDocumentBuilder();
645             
646         } catch(ParserConfigurationException JavaDoc pe) {
647             ErrorManager.getDefault().notify(pe);
648             ErrorManager.getDefault().log("Util.getRunTimeJarFiles: ParserConfigurationException=" + pe);
649             throw new WebServiceException(NbBundle.getMessage(Util.class, "ERROR_READING_RUNTIMEJARS"),pe);
650         }
651         
652         Document document = null;
653         try {
654             File runtimeJarsFile = InstalledFileLocator.getDefault().locate(
655                     "config" + File.separator + "WebServices" + File.separator +
656                     "websvc_runtimejars.xml", null, false);
657             document = builder.parse(runtimeJarsFile);
658         } catch(SAXException JavaDoc se) {
659             ErrorManager.getDefault().notify(se);
660             ErrorManager.getDefault().log("Util.getRunTimeJarFiles: SAXException=" + se);
661             throw new WebServiceException(NbBundle.getMessage(Util.class, "ERROR_READING_RUNTIMEJARS"),se);
662         } catch(IOException ioe) {
663             ErrorManager.getDefault().notify(ioe);
664             ErrorManager.getDefault().log("Util.getRunTimeJarFiles: IOException=" + ioe);
665             throw new WebServiceException(NbBundle.getMessage(Util.class, "ERROR_READING_RUNTIMEJARS"),ioe);
666         }
667         
668         NodeList list = document.getElementsByTagName("Jar");
669         
670         String JavaDoc serverInstanceIDs[] = Deployment.getDefault().getServerInstanceIDs ();
671         J2eePlatform platform = null;
672         for (int i = 0; i < serverInstanceIDs.length; i++) {
673             J2eePlatform p = Deployment.getDefault().getJ2eePlatform (serverInstanceIDs [i]);
674             if (p != null && p.isToolSupported ("wscompile")) {
675                 platform = p;
676                 break;
677             }
678         }
679         File appserverRoot = platform == null ? null : platform.getPlatformRoots () [0];
680         String JavaDoc asRootPath = (appserverRoot != null) ? appserverRoot.getAbsolutePath() : "";
681         asRootPath = asRootPath.replace('\\', '/');
682             
683         Node currentNode = null;
684         for (int ii=0; ii < list.getLength(); ii++) {
685             currentNode = list.item(ii);
686             String JavaDoc name = currentNode.getNodeName();
687             String JavaDoc localName = currentNode.getLocalName();
688             String JavaDoc value = currentNode.getNodeValue();
689             NamedNodeMap nodeMap = currentNode.getAttributes();
690             Node fileNode = nodeMap.getNamedItem("file");
691             String JavaDoc jarString = "";
692             try {
693                 jarString = fileNode.getNodeValue();
694             } catch (DOMException de) {
695                 ErrorManager.getDefault().notify(de);
696                 ErrorManager.getDefault().log("Util.getRunTimeJarFiles: DOMException=" + de);
697                 throw new WebServiceException(NbBundle.getMessage(Util.class, "ERROR_READING_RUNTIMEJARS"),de);
698             }
699             if (jarString.indexOf("\\{appserv\\.home\\}") > -1) {
700                 jarString = jarString.replaceAll("\\{appserv\\.home\\}", asRootPath);
701             } else {
702                 File f = InstalledFileLocator.getDefault().locate(jarString, null, false);
703                 if (f != null) {
704                     jarString = f.getPath();
705                 }
706             }
707             returnJarFileNames.add(jarString);
708         }
709         
710         return (String JavaDoc []) returnJarFileNames.toArray(new String JavaDoc[0]);
711         
712     }
713     
714     /**
715      * This method will construct the default classpath to be used for running the "wscompile" tool.
716      */

717     public static String JavaDoc getRuntimeClassPath() {
718         String JavaDoc [] jarFileNames = null;
719         String JavaDoc returnClassPath = "";
720         try {
721             jarFileNames = Util.getRunTimeJarFiles();
722         } catch(WebServiceException wse) {
723 // StatusDisplayer.getDefault().displayError(wse.getMessage(),2);
724
StatusDisplayer.getDefault().setStatusText(wse.getMessage());
725             return returnClassPath;
726         }
727         
728         for(int ii=0; null != jarFileNames && ii < jarFileNames.length; ii++) {
729             returnClassPath += jarFileNames[ii];
730             /**
731              * If there's another path, add the path separator
732              */

733             if(ii+1 < jarFileNames.length) {
734                 returnClassPath += File.pathSeparator;
735             }
736         }
737         
738         return returnClassPath;
739         
740     }
741     /**
742      * This method will determine if a package name is valid. For each qualification of the package name (part between
743      * the ".", the first character will be checked against Character.isJavaIdentifierStart() then each character will
744      * be checked against Character.isJavaIdentifierPart().
745      */

746     public static boolean isValidPackageName(String JavaDoc inPackageName) {
747         if(null == inPackageName || inPackageName.length() == 0) return false;
748         
749         /**
750          * A "$" seems to be a valid part but messes with the package name so we need to check for it
751          * explicitly.
752          */

753         if(inPackageName.indexOf("$") != -1) {
754             return false;
755         }
756         
757         
758         /**
759          * If we have more than one qualification ("."), we need to check the start of each
760          * one for validity then each character of each one.
761          */

762         
763         StringTokenizer tokenizer = new StringTokenizer(inPackageName,".");
764         
765         if(tokenizer.hasMoreTokens()) {
766             String JavaDoc currentLevel = null;
767             while(tokenizer.hasMoreTokens()) {
768                 currentLevel = (String JavaDoc)tokenizer.nextToken();
769                 if(!Character.isJavaIdentifierStart(currentLevel.charAt(0))) {
770                     return false;
771                 }
772                 for(int ii=0; ii < currentLevel.length(); ii++) {
773                     if(!Character.isJavaIdentifierPart(currentLevel.charAt(ii))) {
774                         return false;
775                     }
776                 }
777             }
778         } else {
779             /**
780              * we only have a single qualification for the package name.
781              */

782             if(!Character.isJavaIdentifierStart(inPackageName.charAt(0))) {
783                 return false;
784             }
785             for(int ii=0; ii < inPackageName.length(); ii++) {
786                 if(!Character.isJavaIdentifierPart(inPackageName.charAt(ii))) {
787                     return false;
788                 }
789             }
790         }
791         return true;
792     }
793     /**
794      * This method will determine if an identifier is valid. The first character will be checked against Character.isJavaIdentifierStart() then each character will
795      * be checked against Character.isJavaIdentifierPart().
796      */

797     public static boolean isValidIdentifier(String JavaDoc inIdentifier) {
798         if(null == inIdentifier || inIdentifier.length() == 0) return false;
799         
800         
801         if(!Character.isJavaIdentifierStart(inIdentifier.charAt(0))) {
802             return false;
803         }
804         for(int ii=0; ii < inIdentifier.length(); ii++) {
805             if(!Character.isJavaIdentifierPart(inIdentifier.charAt(ii))) {
806                 return false;
807             }
808         }
809         
810         return true;
811     }
812    
813     /**
814      * Bug fix: 5059732
815      * This method will return the correct type for the parameter. If the JavaParameter is considered a "Holder"
816      * the holder class name will be used.
817      *
818      *TODO: include in JAX-RPC API
819      *
820      * If the parameter is a "Holder" we need the holder type and not the JavaType. This is
821      * typically the case when there is no return type and the parameter's meant to be mutable, pass-by-reference
822      * type parameters. I took the code below directly from the JAX-RPC class:
823      * "com.sun.xml.rpc.processor.generator.StubGenerator" except that I'm not checking the Operation for an Array type.
824      * - David Botterill 6/8/2004
825      *
826      */

827     public static String JavaDoc getParameterType(Port inPort, JavaParameter inParameter) {
828         
829         String JavaDoc parameterType = "";
830         ClientProcessorEnvironment env = new ClientProcessorEnvironment(new ByteArrayOutputStream(), null, null);
831         
832         if (inParameter.isHolder()) {
833             if (inParameter.getHolderName() == null) {
834                 parameterType = env.getNames().holderClassName(inPort, inParameter.getType());
835             } else {
836                 parameterType = inParameter.getHolderName();
837             }
838         } else {
839             parameterType =inParameter.getType().getName();
840         }
841         
842         return parameterType;
843         
844     }
845 }
846 // END_NOI18N
847
Popular Tags