1 19 20 package org.netbeans.modules.websvc.registry.util; 21 22 import java.util.*; 23 import java.io.*; 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 import org.w3c.dom.*; 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import org.xml.sax.SAXException ; 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 51 public class Util { 52 363 public static String changeString(String inString, String oldSubString, String newSubString) { 365 if (oldSubString.trim().equals("")) 366 return inString; 367 368 int start = 0; 369 int end = 0; 370 StringBuffer changedString = new StringBuffer (""); 371 372 end = inString.indexOf(oldSubString, start); 373 while(end != -1) { 374 changedString.append(inString.substring(start, end) + newSubString); 376 377 start = end + oldSubString.length(); 379 end = inString.indexOf(oldSubString, start); 381 } 382 383 changedString.append(inString.substring(start)); 385 386 return changedString.toString(); 387 } 388 389 397 public static boolean createWSJar(WebServiceData inWSData, OutputStream inOutputStream, String inJarFileName) { 398 399 String 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 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().setStatusText(NbBundle.getMessage(Util.class, "PROXY_GEN_ERROR")); 426 return false; 427 } 428 429 432 Wsdl2Java wsdl2Java = new Wsdl2Java(); 433 436 wsdl2Java.setOutputDirectory(tmpOutputDir.getAbsolutePath()); 437 438 441 wsdl2Java.setPackageName(inWSData.getPackageName()); 442 443 446 try { 447 wsdl2Java.setWsdlUrl(new URL (inWSData.getURL())); 448 } catch(MalformedURLException mfue) { 449 ErrorManager.getDefault().notify(mfue); 450 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(Util.class, "WS_NOJAR_ERROR")); 452 return false; 453 } 454 455 458 System.setProperty("http.proxyHost", WebProxySetter.getInstance().getProxyHost()); 459 System.setProperty("http.proxyPort", WebProxySetter.getInstance().getProxyPort()); 460 461 462 465 466 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().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 483 484 ArrayList argList = new ArrayList(); 485 486 argList.add("-d"); 487 argList.add(tmpOutputDir.getAbsolutePath()); 488 argList.add("-classpath"); 489 String classPath = tmpOutputDir.getAbsolutePath() + File.pathSeparator + Util.getRuntimeClassPath(); 490 argList.add(classPath); 491 argList.add("-g"); 492 493 496 File wrapperFile = wsdl2Java.getWebserviceClient(); 497 argList.add(wrapperFile.getAbsolutePath()); 498 File wrapperBeanInfoFile = wsdl2Java.getWebserviceClientBeanInfo(); 499 argList.add(wrapperBeanInfoFile.getAbsolutePath()); 500 501 String [] args = (String [])argList.toArray(new String [0]); 502 503 505 508 File tempFile = null; 511 try{ 512 tempFile = File.createTempFile("wstemp","compile_errors"); 514 }catch (IOException ioe){ 515 ErrorManager.getDefault().notify(ioe); 516 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().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().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 548 tempFile.delete(); 549 } 550 551 552 555 try { 556 URL imageUrl = Util.class.getResource("/org/netbeans/modules/websvc/registry/resources/webservice.png"); 558 DataInputStream in = new DataInputStream(imageUrl.openStream()); 559 String 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().setStatusText(NbBundle.getMessage(Util.class, "IMAGE_COPY_ERROR")); 577 return false; 578 } 579 580 581 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 upperCaseFirstChar(String inString) { 594 if(null == inString) { 595 throw new IllegalArgumentException ("Null string passed!"); 596 } 597 String returnString = new String (inString); 598 String firstCharacter = returnString.substring(0,1); 599 returnString = firstCharacter.toUpperCase() + returnString.substring(1); 600 601 return returnString; 602 } 603 607 public static String getProperPortName(String inPortName) { 608 String returnString = ""; 609 612 613 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 currentToken = tokenizer.nextToken(); 625 returnString += Util.upperCaseFirstChar(currentToken); 626 627 } 628 629 return returnString; 630 631 } 632 633 public static String [] getRunTimeJarFiles() throws WebServiceException { 634 637 638 ArrayList returnJarFileNames = new ArrayList(); 639 640 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 641 DocumentBuilder builder = null; 642 643 try { 644 builder = factory.newDocumentBuilder(); 645 646 } catch(ParserConfigurationException 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 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 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 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 name = currentNode.getNodeName(); 687 String localName = currentNode.getLocalName(); 688 String value = currentNode.getNodeValue(); 689 NamedNodeMap nodeMap = currentNode.getAttributes(); 690 Node fileNode = nodeMap.getNamedItem("file"); 691 String 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 []) returnJarFileNames.toArray(new String [0]); 711 712 } 713 714 717 public static String getRuntimeClassPath() { 718 String [] jarFileNames = null; 719 String returnClassPath = ""; 720 try { 721 jarFileNames = Util.getRunTimeJarFiles(); 722 } catch(WebServiceException wse) { 723 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 733 if(ii+1 < jarFileNames.length) { 734 returnClassPath += File.pathSeparator; 735 } 736 } 737 738 return returnClassPath; 739 740 } 741 746 public static boolean isValidPackageName(String inPackageName) { 747 if(null == inPackageName || inPackageName.length() == 0) return false; 748 749 753 if(inPackageName.indexOf("$") != -1) { 754 return false; 755 } 756 757 758 762 763 StringTokenizer tokenizer = new StringTokenizer(inPackageName,"."); 764 765 if(tokenizer.hasMoreTokens()) { 766 String currentLevel = null; 767 while(tokenizer.hasMoreTokens()) { 768 currentLevel = (String )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 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 797 public static boolean isValidIdentifier(String 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 827 public static String getParameterType(Port inPort, JavaParameter inParameter) { 828 829 String 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 | Popular Tags |