You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2015/02/23 12:23:15 UTC

cxf git commit: More try with resources refactoring

Repository: cxf
Updated Branches:
  refs/heads/master db18a965f -> 2cfa9011a


More try with resources refactoring


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/2cfa9011
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/2cfa9011
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/2cfa9011

Branch: refs/heads/master
Commit: 2cfa9011aaada9fe68bd0d9aad7ec86991ede43c
Parents: db18a96
Author: Colm O hEigeartaigh <co...@apache.org>
Authored: Mon Feb 23 11:22:58 2015 +0000
Committer: Colm O hEigeartaigh <co...@apache.org>
Committed: Mon Feb 23 11:22:58 2015 +0000

----------------------------------------------------------------------
 .../cxf/testutil/common/ServerLauncher.java     | 19 ++++++++++---------
 .../org/apache/cxf/tools/common/ClassUtils.java | 20 +++++---------------
 .../cxf/tools/common/CommandInterfaceUtils.java | 12 ++++--------
 .../cxf/tools/common/ProcessorTestBase.java     | 11 ++---------
 .../cxf/tools/corba/common/SchemaFactory.java   |  8 +-------
 .../tools/corba/common/WSDLCorbaFactory.java    |  8 +-------
 .../processors/idl/IDLToWSDLProcessor.java      |  8 +-------
 .../generator/wsdl11/WSDL11Generator.java       |  8 +-------
 .../cxf/tools/validator/internal/Stax2DOM.java  | 13 +------------
 .../cxf/tools/wadlto/jaxrs/SourceGenerator.java | 10 ++--------
 10 files changed, 28 insertions(+), 89 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/testutils/src/main/java/org/apache/cxf/testutil/common/ServerLauncher.java
----------------------------------------------------------------------
diff --git a/testutils/src/main/java/org/apache/cxf/testutil/common/ServerLauncher.java b/testutils/src/main/java/org/apache/cxf/testutil/common/ServerLauncher.java
index 6c85c15..f442d9d 100644
--- a/testutils/src/main/java/org/apache/cxf/testutil/common/ServerLauncher.java
+++ b/testutils/src/main/java/org/apache/cxf/testutil/common/ServerLauncher.java
@@ -281,10 +281,9 @@ public class ServerLauncher {
         }
 
         public void run() {
-            PrintStream ps = null;
+            String outputDir = System.getProperty("server.output.dir", "target/surefire-reports/");
+            FileOutputStream fos = null;
             try {
-                String outputDir = System.getProperty("server.output.dir", "target/surefire-reports/");
-                FileOutputStream fos;
                 try {
                     fos = new FileOutputStream(outputDir + className + ".out");
                 } catch (FileNotFoundException fex) {
@@ -294,12 +293,18 @@ public class ServerLauncher {
                     } else {
                         outputDir += "/target/surefire-reports/";
                     }
-                    
+    
                     File file = new File(outputDir);
                     file.mkdirs();
                     fos = new FileOutputStream(outputDir + className + ".out");
                 }
-                ps = new PrintStream(fos);
+            } catch (IOException ex) {
+                if (!ex.getMessage().contains("Stream closed")) {
+                    ex.printStackTrace();
+                }
+            }
+            
+            try (PrintStream ps = new PrintStream(fos)) {
                 boolean running = true;
                 StringBuilder serverOutput = new StringBuilder();
                 for (int ch = in.read(); ch != -1; ch = in.read()) {
@@ -335,10 +340,6 @@ public class ServerLauncher {
                 if (!ex.getMessage().contains("Stream closed")) {
                     ex.printStackTrace();
                 }
-            } finally {
-                if (ps != null) {
-                    ps.close();
-                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java
----------------------------------------------------------------------
diff --git a/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java b/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java
index 1dff016..6085c34 100644
--- a/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java
+++ b/tools/common/src/main/java/org/apache/cxf/tools/common/ClassUtils.java
@@ -127,15 +127,12 @@ public class ClassUtils {
     
     private void copyXmlFile(File from, File to) throws ToolException, IOException {
 
-        FileInputStream input = null;
-        FileOutputStream output = null;
-        try {
-            String dir = to.getCanonicalPath()
+        String dir = to.getCanonicalPath()
                 .substring(0, to.getCanonicalPath().lastIndexOf(File.separator));
-            File dirFile = new File(dir);
-            dirFile.mkdirs();
-            input = new FileInputStream(from);
-            output = new FileOutputStream(to);
+        File dirFile = new File(dir);
+        dirFile.mkdirs();
+        try (FileInputStream input = new FileInputStream(from);
+            FileOutputStream output = new FileOutputStream(to)) {
             byte[] b = new byte[1024 * 3];
             int len = 0;
             while (len != -1) {
@@ -148,13 +145,6 @@ public class ClassUtils {
         } catch (Exception e) {
             Message msg = new Message("FAIL_TO_COPY_GENERATED_RESOURCE_FILE", LOG);
             throw new ToolException(msg, e);
-        } finally {
-            if (output != null) {
-                output.close();
-            }
-            if (input != null) {
-                input.close();
-            }
         }
     }    
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/common/src/main/java/org/apache/cxf/tools/common/CommandInterfaceUtils.java
----------------------------------------------------------------------
diff --git a/tools/common/src/main/java/org/apache/cxf/tools/common/CommandInterfaceUtils.java b/tools/common/src/main/java/org/apache/cxf/tools/common/CommandInterfaceUtils.java
index 8884b34..4e8a531 100644
--- a/tools/common/src/main/java/org/apache/cxf/tools/common/CommandInterfaceUtils.java
+++ b/tools/common/src/main/java/org/apache/cxf/tools/common/CommandInterfaceUtils.java
@@ -41,14 +41,10 @@ public final class CommandInterfaceUtils {
             // configure it.
             System.setProperty("org.apache.commons.logging.Log",
                                "org.apache.commons.logging.impl.Jdk14Logger");
-            InputStream commandConfig = CommandInterfaceUtils.class
-                .getResourceAsStream("commandLogging.properties");
-            try {
-                try {
-                    LogManager.getLogManager().readConfiguration(commandConfig);
-                } finally {
-                    commandConfig.close();
-                }
+
+            try (InputStream commandConfig = CommandInterfaceUtils.class
+                .getResourceAsStream("commandLogging.properties")) {
+                LogManager.getLogManager().readConfiguration(commandConfig);
             } catch (IOException ioe) {
                 throw new RuntimeException(ioe);
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/common/src/main/java/org/apache/cxf/tools/common/ProcessorTestBase.java
----------------------------------------------------------------------
diff --git a/tools/common/src/main/java/org/apache/cxf/tools/common/ProcessorTestBase.java b/tools/common/src/main/java/org/apache/cxf/tools/common/ProcessorTestBase.java
index fc21ed9..40558f2 100644
--- a/tools/common/src/main/java/org/apache/cxf/tools/common/ProcessorTestBase.java
+++ b/tools/common/src/main/java/org/apache/cxf/tools/common/ProcessorTestBase.java
@@ -112,8 +112,7 @@ public class ProcessorTestBase extends Assert {
         StringBuilder classPath = new StringBuilder();
         if (loader instanceof URLClassLoader) {
             for (URL url : ((URLClassLoader)loader).getURLs()) {
-                File file;
-                file = new File(url.toURI());
+                File file = new File(url.toURI());
                 String filename = file.getAbsolutePath();
                 if (filename.indexOf("junit") == -1) {
                     classPath.append(filename);
@@ -121,9 +120,7 @@ public class ProcessorTestBase extends Assert {
                 }
                 if (filename.indexOf("surefirebooter") != -1) {
                     //surefire 2.4 uses a MANIFEST classpath that javac doesn't like
-                    JarFile jar = null;
-                    try {
-                        jar = new JarFile(filename);
+                    try (JarFile jar = new JarFile(filename)) {
                         Attributes attr = jar.getManifest().getMainAttributes();
                         if (attr != null) {
                             String cp = attr.getValue("Class-Path");
@@ -144,10 +141,6 @@ public class ProcessorTestBase extends Assert {
                                 }
                             }
                         }
-                    } finally {
-                        if (jar != null) {
-                            jar.close();
-                        }
                     }
                 }
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/SchemaFactory.java
----------------------------------------------------------------------
diff --git a/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/SchemaFactory.java b/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/SchemaFactory.java
index 5296f96..cfea144 100644
--- a/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/SchemaFactory.java
+++ b/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/SchemaFactory.java
@@ -127,14 +127,8 @@ public abstract class SchemaFactory {
             try {
                 Properties properties = new Properties();
                 File propFile = new File(propFileName);
-                FileInputStream fis = null;
-                try {
-                    fis = new FileInputStream(propFile);
+                try (FileInputStream fis = new FileInputStream(propFile)) {
                     properties.load(fis);
-                } finally {
-                    if (fis != null) {
-                        fis.close();
-                    }
                 }
 
                 factoryImplName = properties.getProperty(PROPERTY_NAME);

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/WSDLCorbaFactory.java
----------------------------------------------------------------------
diff --git a/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/WSDLCorbaFactory.java b/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/WSDLCorbaFactory.java
index 0c1ed11..04737a1 100644
--- a/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/WSDLCorbaFactory.java
+++ b/tools/corba/src/main/java/org/apache/cxf/tools/corba/common/WSDLCorbaFactory.java
@@ -139,14 +139,8 @@ public abstract class WSDLCorbaFactory {
             try {
                 Properties properties = new Properties();
                 File propFile = new File(propFileName);
-                FileInputStream fis = null;
-                try {
-                    fis = new FileInputStream(propFile);
+                try (FileInputStream fis = new FileInputStream(propFile)) {
                     properties.load(fis);
-                } finally {
-                    if (fis != null) {
-                        fis.close();
-                    }
                 }
 
                 factoryImplName = properties.getProperty(PROPERTY_NAME);

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/corba/src/main/java/org/apache/cxf/tools/corba/processors/idl/IDLToWSDLProcessor.java
----------------------------------------------------------------------
diff --git a/tools/corba/src/main/java/org/apache/cxf/tools/corba/processors/idl/IDLToWSDLProcessor.java b/tools/corba/src/main/java/org/apache/cxf/tools/corba/processors/idl/IDLToWSDLProcessor.java
index f56b167..186f3b5 100644
--- a/tools/corba/src/main/java/org/apache/cxf/tools/corba/processors/idl/IDLToWSDLProcessor.java
+++ b/tools/corba/src/main/java/org/apache/cxf/tools/corba/processors/idl/IDLToWSDLProcessor.java
@@ -636,9 +636,7 @@ public class IDLToWSDLProcessor extends IDLProcessor {
                 }
             } else if (mapping.startsWith(":")) {
                 mapping = mapping.substring(1);
-                BufferedReader reader = null;
-                try {
-                    reader = new BufferedReader(new FileReader(mapping));
+                try (BufferedReader reader = new BufferedReader(new FileReader(mapping))) {
                     String token = reader.readLine();
                     while (token != null) {
                         int pos = token.indexOf("=");
@@ -657,10 +655,6 @@ public class IDLToWSDLProcessor extends IDLProcessor {
                 } catch (Exception ex) {
                     throw new RuntimeException("Incorrect properties file for mns mapping - " + mapping
                                                + ". Cause: " + ex.getMessage());
-                } finally {
-                    if (reader != null) {
-                        reader.close();
-                    }
                 }
             } else {
                 throw new RuntimeException("Option mns should have a start([) & close(]) bracket"

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
----------------------------------------------------------------------
diff --git a/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java b/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
index b515211..257889d 100644
--- a/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
+++ b/tools/javato/ws/src/main/java/org/apache/cxf/tools/java2wsdl/generator/wsdl11/WSDL11Generator.java
@@ -95,14 +95,8 @@ public class WSDL11Generator extends AbstractGenerator<Definition> {
                     } else {
                         wsdlFile = new File(outputdir, wsdlDef.getQName().getLocalPart() + ".wsdl");
                     }
-                    OutputStream wsdlOs = null;
-                    try {
-                        wsdlOs = new BufferedOutputStream(new FileOutputStream(wsdlFile));
+                    try (OutputStream wsdlOs = new BufferedOutputStream(new FileOutputStream(wsdlFile))) {
                         wsdlWriter.writeWSDL(wsdlDef, wsdlOs);
-                    } finally {
-                        if (wsdlOs != null) {
-                            wsdlOs.close();
-                        }
                     }
                 }
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
----------------------------------------------------------------------
diff --git a/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java b/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
index a9c4797..b26a780 100644
--- a/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
+++ b/tools/validator/src/main/java/org/apache/cxf/tools/validator/internal/Stax2DOM.java
@@ -20,7 +20,6 @@
 package org.apache.cxf.tools.validator.internal;
 
 import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
@@ -55,10 +54,8 @@ public class Stax2DOM {
     }
 
     public Document getDocument(URL url) throws ToolException {
-        InputStream input = null;
         XMLStreamReader reader = null;
-        try {
-            input = url.openStream();
+        try (InputStream input = url.openStream()) {
             StreamSource src = new StreamSource(input, url.toExternalForm());
             reader = StaxUtils.createXMLStreamReader(src);
             return StaxUtils.read(reader, true);
@@ -70,14 +67,6 @@ public class Stax2DOM {
             } catch (XMLStreamException e1) {
                 throw new ToolException(e1);
             }
-            if (input != null) {
-                try {
-                    input.close();
-                } catch (IOException e) {
-                    // throw or change do nothing.
-                    throw new ToolException(e);
-                }
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/2cfa9011/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
index 59d08e6..a584ec1 100644
--- a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
+++ b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
@@ -1446,16 +1446,10 @@ public class SourceGenerator {
         
         try {
             file.createNewFile();
-            Writer writer = null;
-            try {
-                writer = new OutputStreamWriter(new FileOutputStream(file), 
-                                                encoding == null ? "UTF-8" : encoding);
+            try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), 
+                                                encoding == null ? "UTF-8" : encoding)) {
                 writer.write(content);
                 writer.flush();
-            } finally {
-                if (writer != null) {
-                    writer.close();
-                }
             }
         } catch (FileNotFoundException ex) {
             LOG.warning(file.getAbsolutePath() + " is not found");