src/share/jaxws_classes/com/sun/tools/internal/ws/util/WSDLFetcher.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -68,11 +68,11 @@
 
 
     /**
      *  Fetches the wsdls in the DOMForest to the options.destDir
      * @param forest
-     * @return
+     * @return location of fetched root WSDL document
      * @throws IOException
      * @throws XMLStreamException
      * @throws FileNotFoundException
      */
     public String fetchWsdls(MetadataFinder forest) throws IOException, XMLStreamException {

@@ -98,30 +98,38 @@
             public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
                 return null;
             }
         }, docLocator);
 
-        //XMLInputFactory readerFactory = XMLInputFactory.newInstance();
-        //XMLStreamReader xsr = readerFactory.createXMLStreamReader(new DOMSource(forest.get(rootWsdl)));
-
-        XMLStreamReader xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
-        XMLOutputFactory writerfactory = XMLOutputFactory.newInstance();
-        String resolvedRootWsdl = docLocator.getLocationFor(null, doc);
+        XMLStreamReader xsr = null;
+        XMLStreamWriter xsw = null;
+        OutputStream os = null;
+        String resolvedRootWsdl = null;
+        try {
+            XMLOutputFactory writerfactory;
+            xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
+            writerfactory = XMLOutputFactory.newInstance();
+            resolvedRootWsdl = docLocator.getLocationFor(null, doc);
         File outFile = new File(destDir, resolvedRootWsdl);
-        OutputStream os = new FileOutputStream(outFile);
+            os = new FileOutputStream(outFile);
         if(options.verbose) {
             listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
         }
-        XMLStreamWriter xsw = writerfactory.createXMLStreamWriter(os);
+            xsw = writerfactory.createXMLStreamWriter(os);
         //DOMForest eats away the whitespace loosing all the indentation, so write it through
         // indenting writer for better readability of fetched documents
         IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
         wsdlPatcher.bridge(xsr, indentingWriter);
-        xsr.close();
-        xsw.close();
-        os.close();
         options.addGeneratedFile(outFile);
+        } finally {
+            try {
+                if (xsr != null) {xsr.close();}
+                if (xsw != null) {xsw.close();}
+            } finally {
+                if (os != null) {os.close();}
+            }
+        }
         return resolvedRootWsdl;
 
 
     }
     private Map<String,String> createDocumentMap(MetadataFinder forest, File baseDir, final String rootWsdl, Set<String> externalReferences) {

@@ -134,13 +142,13 @@
             rootWsdlFileName = rootWsdl.substring(slashIndex+1);
         }
         if(!rootWsdlFileName.endsWith(WSDL_FILE_EXTENSION)) {
             Document rootWsdlDoc =  forest.get(rootWsdl);
             NodeList serviceNodes = rootWsdlDoc.getElementsByTagNameNS(WSDLConstants.QNAME_SERVICE.getNamespaceURI(),WSDLConstants.QNAME_SERVICE.getLocalPart());
-            if(serviceNodes.getLength() == 0)
+            if (serviceNodes.getLength() == 0) {
                 rootWsdlName = "Service";
-            else {
+            } else {
                 Node serviceNode = serviceNodes.item(0);
                 String serviceName = ((Element)serviceNode).getAttribute( WSDLConstants.ATTR_NAME);
                 rootWsdlName = serviceName;
             }
             rootWsdlFileName = rootWsdlName+ WSDL_FILE_EXTENSION;

@@ -175,12 +183,12 @@
         }
         return map;
     }
 
     private DocumentLocationResolver createDocResolver(final String baseWsdl, final DOMForest forest, final Map<String,String> documentMap) {
-
         return new DocumentLocationResolver() {
+            @Override
             public String getLocationFor(String namespaceURI, String systemId) {
                 try {
                     URL reference = new URL(new URL(baseWsdl),systemId);
                     systemId = reference.toExternalForm();
                 } catch (MalformedURLException e) {

@@ -196,11 +204,11 @@
         };
     }
 
     private String sanitize(String fileName) {
         fileName = fileName.replace('?', '.');
-        StringBuffer sb = new StringBuffer(fileName);
+        StringBuilder sb = new StringBuilder(fileName);
         for (int i = 0; i < sb.length(); i++) {
             char c = sb.charAt(i);
             if (Character.isLetterOrDigit(c) ||
                     (c == '/') ||
                     (c == '.') ||

@@ -214,12 +222,15 @@
         }
         return sb.toString();
     }
 
     private File getWSDLDownloadDir() {
-        File wsdlDir = new File(options.destDir,WSDL_PATH);
-        wsdlDir.mkdirs();
+        File wsdlDir = new File(options.destDir, WSDL_PATH);
+        boolean created = wsdlDir.mkdirs();
+        if (options.verbose && !created) {
+            listener.message(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(wsdlDir));
+        }
         return wsdlDir;
     }
 
     private static String WSDL_PATH="META-INF/wsdl";
     private static String WSDL_FILE_EXTENSION=".wsdl";