knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kris...@apache.org
Subject knox git commit: KNOX-1603 - Ensure resources are properly closed
Date Thu, 15 Nov 2018 17:30:31 GMT
Repository: knox
Updated Branches:
  refs/heads/master b34c0eb81 -> 7c22f02ad


KNOX-1603 - Ensure resources are properly closed

Signed-off-by: Kevin Risden <krisden@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/7c22f02a
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/7c22f02a
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/7c22f02a

Branch: refs/heads/master
Commit: 7c22f02ad8fe1df99b184d906077e6028b7f65a1
Parents: b34c0eb
Author: Kevin Risden <krisden@apache.org>
Authored: Thu Nov 15 09:15:37 2018 -0500
Committer: Kevin Risden <krisden@apache.org>
Committed: Thu Nov 15 12:30:03 2018 -0500

----------------------------------------------------------------------
 .../org/apache/knox/gateway/GatewayServer.java  |   6 +-
 .../gateway/util/ServiceDefinitionsLoader.java  |  12 +--
 .../service/test/ServiceTestResource.java       | 106 ++++++++++---------
 .../services/hostmap/FileBasedHostMapper.java   |  35 +++---
 .../java/org/apache/knox/test/TestUtils.java    |   4 +-
 5 files changed, 80 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/7c22f02a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java
index 9f9bc1e..385f9cb 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java
@@ -960,9 +960,9 @@ public class GatewayServer {
   }
 
   private static void checkAddressAvailability( InetSocketAddress address ) throws IOException
{
-    ServerSocket socket = new ServerSocket();
-    socket.bind( address );
-    socket.close();
+    try( ServerSocket socket = new ServerSocket() ) {
+      socket.bind(address);
+    }
   }
 
   private static class RegexFilenameFilter implements FilenameFilter {

http://git-wip-us.apache.org/repos/asf/knox/blob/7c22f02a/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
b/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
index cd6b7d2..d2687ec 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceDefinitionsLoader.java
@@ -118,15 +118,9 @@ public class ServiceDefinitionsLoader {
   public static UrlRewriteRulesDescriptor loadRewriteRules(File servicesDir) {
     File rewriteFile = new File(servicesDir, REWRITE_FILE);
     if ( rewriteFile.exists() ) {
-      InputStream stream;
-      try {
-        stream = new FileInputStream(rewriteFile);
-        Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
-        UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.load(
-            "xml", reader);
-        reader.close();
-        stream.close();
-        return rules;
+      try (InputStream stream = new FileInputStream(rewriteFile);
+           Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
+        return UrlRewriteRulesDescriptorFactory.load("xml", reader);
       } catch ( FileNotFoundException e ) {
         log.failedToFindRewriteFile(rewriteFile.getAbsolutePath(), e);
       } catch ( IOException e ) {

http://git-wip-us.apache.org/repos/asf/knox/blob/7c22f02a/gateway-service-test/src/main/java/org/apache/knox/gateway/service/test/ServiceTestResource.java
----------------------------------------------------------------------
diff --git a/gateway-service-test/src/main/java/org/apache/knox/gateway/service/test/ServiceTestResource.java
b/gateway-service-test/src/main/java/org/apache/knox/gateway/service/test/ServiceTestResource.java
index 2ab6a14..9e8554e 100644
--- a/gateway-service-test/src/main/java/org/apache/knox/gateway/service/test/ServiceTestResource.java
+++ b/gateway-service-test/src/main/java/org/apache/knox/gateway/service/test/ServiceTestResource.java
@@ -70,7 +70,7 @@ public class ServiceTestResource {
     String authString;
     GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
     SSLContext ctx = null;
-    CloseableHttpClient client;
+    CloseableHttpClient client = null;
     String id = getTopologyName();
 
     Topology topology = getTopology(id);
@@ -92,63 +92,67 @@ public class ServiceTestResource {
     }
 
 //    Initialize the HTTP client
-    if(ctx == null) {
-      client = HttpClients.createDefault();
-    } else {
-      client = HttpClients.custom().setSslcontext(ctx).build();
-    }
-
-    if(topology != null) {
-      for (Service s : topology.getServices()) {
-        List<String> urls = getServiceTestURLs(config, s.getRole(), topology);
-
-//          Make sure we handle a case where no URLs are found.
-        if(urls.size() <= 0) {
-          ServiceTest test = new ServiceTest(s);
-          test.setMessage("This service did not contain any test URLs");
-        }
+    try {
+      if (ctx == null) {
+        client = HttpClients.createDefault();
+      } else {
+        client = HttpClients.custom().setSSLContext(ctx).build();
+      }
 
-        for (String url : urls) {
-          HttpGet req = new HttpGet();
-          ServiceTest test = new ServiceTest(s, url);
+      if (topology != null) {
+        for (Service s : topology.getServices()) {
+          List<String> urls = getServiceTestURLs(config, s.getRole(), topology);
 
-          if(authString != null) {
-            req.setHeader("Authorization", authString);
-          } else {
-            messages.add("No credentials provided. Expect HTTP 401 responses.");
+          //          Make sure we handle a case where no URLs are found.
+          if (urls.size() <= 0) {
+            ServiceTest test = new ServiceTest(s);
+            test.setMessage("This service did not contain any test URLs");
           }
 
-          try {
-            req.setURI(new URIBuilder(url).build());
-            CloseableHttpResponse res = client.execute(req);
-            String contentLength = "Content-Length:" + res.getEntity().getContentLength();
-            String contentType = (res.getEntity().getContentType() != null) ? res.getEntity().getContentType().toString()
: "No-contenttype";
-            test.setResponseContent( contentLength + "," + contentType );
-            test.setHttpCode(res.getStatusLine().getStatusCode());
-            res.close();
-
-          } catch (IOException e) {
-            messages.add("Exception: " + e.getMessage());
-            test.setMessage(e.getMessage());
-          } catch (URISyntaxException e) {
-            test.setMessage(e.getMessage());
-          } catch (Exception e) {
-            messages.add(e.getMessage());
-            test.setMessage(e.getMessage());
-          } finally {
-            req.releaseConnection();
-            tests.add(test);
+          for (String url : urls) {
+            HttpGet req = new HttpGet();
+            ServiceTest test = new ServiceTest(s, url);
+
+            if (authString != null) {
+              req.setHeader("Authorization", authString);
+            } else {
+              messages.add("No credentials provided. Expect HTTP 401 responses.");
+            }
+
+            try {
+              req.setURI(new URIBuilder(url).build());
+              CloseableHttpResponse res = client.execute(req);
+              String contentLength = "Content-Length:" + res.getEntity().getContentLength();
+              String contentType = (res.getEntity().getContentType() != null) ? res.getEntity().getContentType().toString()
: "No-contenttype";
+              test.setResponseContent(contentLength + "," + contentType);
+              test.setHttpCode(res.getStatusLine().getStatusCode());
+              res.close();
+
+            } catch (IOException e) {
+              messages.add("Exception: " + e.getMessage());
+              test.setMessage(e.getMessage());
+            } catch (URISyntaxException e) {
+              test.setMessage(e.getMessage());
+            } catch (Exception e) {
+              messages.add(e.getMessage());
+              test.setMessage(e.getMessage());
+            } finally {
+              req.releaseConnection();
+              tests.add(test);
+            }
           }
         }
+      } else {
+        messages.add("Topology " + id + " not found");
+      }
+    } finally {
+      if(client != null) {
+        try {
+          client.close();
+        } catch (IOException e) {
+          e.printStackTrace();
+        }
       }
-    } else {
-      messages.add("Topology " + id + " not found");
-    }
-
-    try {
-      client.close();
-    } catch (IOException e) {
-      e.printStackTrace();
     }
 
     ServiceTestWrapper stw = new ServiceTestWrapper();

http://git-wip-us.apache.org/repos/asf/knox/blob/7c22f02a/gateway-spi/src/main/java/org/apache/knox/gateway/services/hostmap/FileBasedHostMapper.java
----------------------------------------------------------------------
diff --git a/gateway-spi/src/main/java/org/apache/knox/gateway/services/hostmap/FileBasedHostMapper.java
b/gateway-spi/src/main/java/org/apache/knox/gateway/services/hostmap/FileBasedHostMapper.java
index 2055e53..1d73078 100644
--- a/gateway-spi/src/main/java/org/apache/knox/gateway/services/hostmap/FileBasedHostMapper.java
+++ b/gateway-spi/src/main/java/org/apache/knox/gateway/services/hostmap/FileBasedHostMapper.java
@@ -32,27 +32,28 @@ public class FileBasedHostMapper implements HostMapper {
   private Map<String, String> outbound = new HashMap<>();
 
   public FileBasedHostMapper( URL url ) throws IOException {
-    if( url != null ) {
-      InputStream stream = url.openStream();
-      BufferedReader reader = new BufferedReader( new InputStreamReader( stream, StandardCharsets.UTF_8
) );
-      String line = reader.readLine();
-      while( line != null ) {
-        String[] lineSplit = line.split( "=" );
-        if( lineSplit.length >= 2 ) {
-          String[] externalSplit = lineSplit[ 0 ].split( "," );
-          String[] internalSplit = lineSplit[ 1 ].split( "," );
-          if( externalSplit.length >= 1 && internalSplit.length >= 1 ) {
-            for( String external : externalSplit ) {
-              inbound.put( external.trim(), internalSplit[ 0 ].trim() );
-            }
-            for( String internal : internalSplit ) {
-              outbound.put( internal.trim(), externalSplit[ 0 ].trim() );
+    if (url != null) {
+      try (InputStream stream = url.openStream();
+           InputStreamReader inputStreamReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
+           BufferedReader reader = new BufferedReader(inputStreamReader)) {
+        String line = reader.readLine();
+        while (line != null) {
+          String[] lineSplit = line.split("=");
+          if (lineSplit.length >= 2) {
+            String[] externalSplit = lineSplit[0].split(",");
+            String[] internalSplit = lineSplit[1].split(",");
+            if (externalSplit.length >= 1 && internalSplit.length >= 1) {
+              for (String external : externalSplit) {
+                inbound.put(external.trim(), internalSplit[0].trim());
+              }
+              for (String internal : internalSplit) {
+                outbound.put(internal.trim(), externalSplit[0].trim());
+              }
             }
           }
+          line = reader.readLine();
         }
-        line = reader.readLine();
       }
-      reader.close();
     }
   }
 

http://git-wip-us.apache.org/repos/asf/knox/blob/7c22f02a/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
----------------------------------------------------------------------
diff --git a/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java b/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
index 5a28238..23e9380 100644
--- a/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
+++ b/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
@@ -127,10 +127,8 @@ public class TestUtils {
   public static void awaitPortOpen( InetSocketAddress address, int timeout, int delay ) throws
InterruptedException {
     long maxTime = System.currentTimeMillis() + timeout;
     do {
-      try {
-        Socket socket = new Socket();
+      try (Socket socket = new Socket()) {
         socket.connect( address, delay );
-        socket.close();
         return;
       } catch ( IOException e ) {
         //e.printStackTrace();


Mime
View raw message