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-1677 - Use try-with-resources to ensure that resources are closed
Date Thu, 13 Dec 2018 21:32:09 GMT
Repository: knox
Updated Branches:
  refs/heads/master ecbbbd00a -> c00066050


KNOX-1677 - Use try-with-resources to ensure that resources are 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/c0006605
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/c0006605
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/c0006605

Branch: refs/heads/master
Commit: c00066050bfaa7409f64eb112e8e52f82d66c253
Parents: ecbbbd0
Author: Kevin Risden <krisden@apache.org>
Authored: Tue Dec 11 16:42:12 2018 -0500
Committer: Kevin Risden <krisden@apache.org>
Committed: Thu Dec 13 16:05:56 2018 -0500

----------------------------------------------------------------------
 .../security/ldap/SimpleLdapServerTest.java     | 17 ++-------
 .../ha/provider/HaServletContextListener.java   | 13 ++-----
 .../impl/AtlasZookeeperURLManagerTest.java      | 18 +++++-----
 .../impl/HS2ZookeeperURLManagerTest.java        | 36 ++++++++++----------
 .../impl/SOLRZookeeperURLManagerTest.java       | 18 +++++-----
 .../impl/HostmapDeploymentContributor.java      | 16 ++++-----
 .../api/UrlRewriteServletContextListener.java   | 16 ++-------
 .../rewrite/impl/UrlRewriteResponseStream.java  |  9 +++--
 .../org/apache/knox/gateway/GatewayServer.java  | 12 +++----
 .../knox/gateway/deploy/DeploymentFactory.java  |  3 +-
 .../org/apache/knox/gateway/util/KnoxCLI.java   | 34 +++++++-----------
 .../service/test/ServiceTestResource.java       | 13 ++++---
 .../dispatch/AbstractGatewayDispatch.java       |  6 ++--
 .../java/org/apache/knox/test/TestUtils.java    |  7 ++--
 .../correlation/Log4jCorrelationService.java    | 11 +++---
 15 files changed, 90 insertions(+), 139 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-demo-ldap/src/test/java/org/apache/knox/gateway/security/ldap/SimpleLdapServerTest.java
----------------------------------------------------------------------
diff --git a/gateway-demo-ldap/src/test/java/org/apache/knox/gateway/security/ldap/SimpleLdapServerTest.java
b/gateway-demo-ldap/src/test/java/org/apache/knox/gateway/security/ldap/SimpleLdapServerTest.java
index 2d1528f..f7bd4d6 100644
--- a/gateway-demo-ldap/src/test/java/org/apache/knox/gateway/security/ldap/SimpleLdapServerTest.java
+++ b/gateway-demo-ldap/src/test/java/org/apache/knox/gateway/security/ldap/SimpleLdapServerTest.java
@@ -57,17 +57,11 @@ public class SimpleLdapServerTest {
 
   @Test
   public void testBind() throws LdapException, IOException {
-    LdapConnection connection;
-
-    connection = new LdapNetworkConnection( "localhost", port );
-    try {
+    try(LdapConnection connection = new LdapNetworkConnection( "localhost", port )) {
       connection.bind( "uid=guest,ou=people,dc=hadoop,dc=apache,dc=org", "guest-password"
);
-    } finally {
-      connection.close();
     }
 
-    connection = new LdapNetworkConnection( "localhost", port );
-    try {
+    try(LdapConnection connection = new LdapNetworkConnection( "localhost", port )) {
       connection.bind( "uid=nobody,ou=people,dc=hadoop,dc=apache,dc=org", "guest-password"
);
       fail( "Expected LdapAuthenticationException" );
     } catch ( LdapAuthenticationException e ) {
@@ -75,12 +69,9 @@ public class SimpleLdapServerTest {
       assertEquals("INVALID_CREDENTIALS: Bind failed: ERR_229 " +
                        "Cannot authenticate user uid=nobody,ou=people,dc=hadoop,dc=apache,dc=org",
           e.getMessage());
-    } finally {
-      connection.close();
     }
 
-    connection = new LdapNetworkConnection( "localhost", port );
-    try {
+    try(LdapConnection connection = new LdapNetworkConnection( "localhost", port )) {
       connection.bind( "uid=guest,ou=people,dc=hadoop,dc=apache,dc=org", "wrong-password"
);
       fail( "Expected LdapAuthenticationException" );
     } catch ( LdapAuthenticationException e ) {
@@ -88,8 +79,6 @@ public class SimpleLdapServerTest {
       assertEquals("INVALID_CREDENTIALS: Bind failed: ERR_229 " +
                        "Cannot authenticate user uid=guest,ou=people,dc=hadoop,dc=apache,dc=org",
           e.getMessage());
-    } finally {
-      connection.close();
     }
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/HaServletContextListener.java
----------------------------------------------------------------------
diff --git a/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/HaServletContextListener.java
b/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/HaServletContextListener.java
index 37424ae..62d21aa 100644
--- a/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/HaServletContextListener.java
+++ b/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/HaServletContextListener.java
@@ -19,8 +19,6 @@ package org.apache.knox.gateway.ha.provider;
 
 import org.apache.knox.gateway.ha.provider.impl.DefaultHaProvider;
 import org.apache.knox.gateway.ha.provider.impl.HaDescriptorManager;
-import org.apache.knox.gateway.ha.provider.impl.i18n.HaMessages;
-import org.apache.knox.gateway.i18n.messages.MessagesFactory;
 import org.apache.knox.gateway.services.GatewayServices;
 import org.apache.knox.gateway.services.registry.ServiceRegistry;
 
@@ -39,8 +37,6 @@ public class HaServletContextListener implements ServletContextListener
{
    public static final String DESCRIPTOR_DEFAULT_FILE_NAME = "ha.xml";
    public static final String DESCRIPTOR_DEFAULT_LOCATION = "/WEB-INF/" + DESCRIPTOR_DEFAULT_FILE_NAME;
 
-   private static final HaMessages LOG = MessagesFactory.get(HaMessages.class);
-
    @Override
    public void contextInitialized(ServletContextEvent event) {
       HaDescriptor descriptor;
@@ -94,13 +90,8 @@ public class HaServletContextListener implements ServletContextListener
{
    }
 
    private static HaDescriptor loadDescriptor(URL url) throws IOException {
-      InputStream stream = url.openStream();
-      HaDescriptor descriptor = HaDescriptorManager.load(stream);
-      try {
-         stream.close();
-      } catch (IOException e) {
-         LOG.failedToLoadHaDescriptor(e);
+      try(InputStream stream = url.openStream()) {
+        return HaDescriptorManager.load(stream);
       }
-      return descriptor;
    }
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/AtlasZookeeperURLManagerTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/AtlasZookeeperURLManagerTest.java
b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/AtlasZookeeperURLManagerTest.java
index 2cb00ce..2ee6cad 100644
--- a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/AtlasZookeeperURLManagerTest.java
+++ b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/AtlasZookeeperURLManagerTest.java
@@ -50,19 +50,19 @@ public class AtlasZookeeperURLManagerTest {
         cluster = new TestingCluster(3);
         cluster.start();
 
-        CuratorFramework zooKeeperClient =
+        try(CuratorFramework zooKeeperClient =
                 CuratorFrameworkFactory.builder().connectString(cluster.getConnectString())
                                                  .retryPolicy(new ExponentialBackoffRetry(1000,
3))
-                                                 .build();
+                                                 .build()) {
 
-        zooKeeperClient.start();
-        assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
+          zooKeeperClient.start();
+          assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
 
-        zooKeeperClient.create().forPath("/apache_atlas");
-        zooKeeperClient.create().forPath("/apache_atlas/active_server_info");
-        zooKeeperClient.setData().forPath("/apache_atlas/active_server_info",
-                                          atlasNode1.getBytes(StandardCharsets.UTF_8));
-        zooKeeperClient.close();
+          zooKeeperClient.create().forPath("/apache_atlas");
+          zooKeeperClient.create().forPath("/apache_atlas/active_server_info");
+          zooKeeperClient.setData().forPath("/apache_atlas/active_server_info",
+              atlasNode1.getBytes(StandardCharsets.UTF_8));
+        }
         setAtlasActiveHostURLInZookeeper(atlasNode1);
 
         manager = new AtlasZookeeperURLManager();

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/HS2ZookeeperURLManagerTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/HS2ZookeeperURLManagerTest.java
b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/HS2ZookeeperURLManagerTest.java
index 77b17b0..2500e4b 100644
--- a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/HS2ZookeeperURLManagerTest.java
+++ b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/HS2ZookeeperURLManagerTest.java
@@ -46,26 +46,26 @@ public class HS2ZookeeperURLManagerTest {
     cluster = new TestingCluster(3);
     cluster.start();
 
-    CuratorFramework zooKeeperClient =
+    try(CuratorFramework zooKeeperClient =
         CuratorFrameworkFactory.builder().connectString(cluster.getConnectString())
-            .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
+            .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build()) {
 
-    String host1 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice;"
+
-        "hive.server2.thrift.http.port=10001;hive.server2.thrift.bind.host=host1;hive.server2.use.SSL=true";
-    String host2 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=foobar;"
+
-        "hive.server2.thrift.http.port=10002;hive.server2.thrift.bind.host=host2;hive.server2.use.SSL=false";
-    String host3 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice;"
+
-        "hive.server2.thrift.http.port=10003;hive.server2.thrift.bind.host=host3;hive.server2.use.SSL=false";
-    String host4 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice;"
+
-        "hive.server2.thrift.http.port=10004;hive.server2.thrift.bind.host=host4;hive.server2.use.SSL=true";
-    zooKeeperClient.start();
-    assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
-    zooKeeperClient.create().forPath("/hiveServer2");
-    zooKeeperClient.create().forPath("/hiveServer2/host1", host1.getBytes(StandardCharsets.UTF_8));
-    zooKeeperClient.create().forPath("/hiveServer2/host2", host2.getBytes(StandardCharsets.UTF_8));
-    zooKeeperClient.create().forPath("/hiveServer2/host3", host3.getBytes(StandardCharsets.UTF_8));
-    zooKeeperClient.create().forPath("/hiveServer2/host4", host4.getBytes(StandardCharsets.UTF_8));
-    zooKeeperClient.close();
+      String host1 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice;"
+
+                         "hive.server2.thrift.http.port=10001;hive.server2.thrift.bind.host=host1;hive.server2.use.SSL=true";
+      String host2 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=foobar;"
+
+                         "hive.server2.thrift.http.port=10002;hive.server2.thrift.bind.host=host2;hive.server2.use.SSL=false";
+      String host3 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice;"
+
+                         "hive.server2.thrift.http.port=10003;hive.server2.thrift.bind.host=host3;hive.server2.use.SSL=false";
+      String host4 = "hive.server2.authentication=NONE;hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice;"
+
+                         "hive.server2.thrift.http.port=10004;hive.server2.thrift.bind.host=host4;hive.server2.use.SSL=true";
+      zooKeeperClient.start();
+      assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
+      zooKeeperClient.create().forPath("/hiveServer2");
+      zooKeeperClient.create().forPath("/hiveServer2/host1", host1.getBytes(StandardCharsets.UTF_8));
+      zooKeeperClient.create().forPath("/hiveServer2/host2", host2.getBytes(StandardCharsets.UTF_8));
+      zooKeeperClient.create().forPath("/hiveServer2/host3", host3.getBytes(StandardCharsets.UTF_8));
+      zooKeeperClient.create().forPath("/hiveServer2/host4", host4.getBytes(StandardCharsets.UTF_8));
+    }
     manager = new HS2ZookeeperURLManager();
     HaServiceConfig config = new DefaultHaServiceConfig("HIVE");
     config.setEnabled(true);

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/SOLRZookeeperURLManagerTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/SOLRZookeeperURLManagerTest.java
b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/SOLRZookeeperURLManagerTest.java
index aea7acd..2e7e476 100644
--- a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/SOLRZookeeperURLManagerTest.java
+++ b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/SOLRZookeeperURLManagerTest.java
@@ -51,17 +51,17 @@ public class SOLRZookeeperURLManagerTest {
     cluster = new TestingCluster(3);
     cluster.start();
 
-    CuratorFramework zooKeeperClient =
+    try(CuratorFramework zooKeeperClient =
         CuratorFrameworkFactory.builder().connectString(cluster.getConnectString())
-            .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
+            .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build()) {
 
-    zooKeeperClient.start();
-    assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
-    zooKeeperClient.create().forPath("/live_nodes");
-    zooKeeperClient.create().forPath("/live_nodes/host1:8983_solr");
-    zooKeeperClient.create().forPath("/live_nodes/host2:8983_solr");
-    zooKeeperClient.create().forPath("/live_nodes/host3:8983_solr");
-    zooKeeperClient.close();
+      zooKeeperClient.start();
+      assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
+      zooKeeperClient.create().forPath("/live_nodes");
+      zooKeeperClient.create().forPath("/live_nodes/host1:8983_solr");
+      zooKeeperClient.create().forPath("/live_nodes/host2:8983_solr");
+      zooKeeperClient.create().forPath("/live_nodes/host3:8983_solr");
+    }
     manager = new SOLRZookeeperURLManager();
     HaServiceConfig config = new DefaultHaServiceConfig("SOLR");
     config.setEnabled(true);

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java
b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java
index b4d7fee..8b2175a 100644
--- a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java
+++ b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java
@@ -72,15 +72,15 @@ public class HostmapDeploymentContributor
 
   private Asset createAsset( Provider provider ) {
     StringWriter buffer = new StringWriter();
-    PrintWriter writer = new PrintWriter( buffer );
-    for( Map.Entry<String, String> entry : provider.getParams().entrySet() ) {
-      String externalHosts = entry.getKey();
-      String internalHosts = entry.getValue();
-      writer.print( externalHosts );
-      writer.print( "=" );
-      writer.println( internalHosts );
+    try(PrintWriter writer = new PrintWriter( buffer )) {
+      for (Map.Entry<String, String> entry : provider.getParams().entrySet()) {
+        String externalHosts = entry.getKey();
+        String internalHosts = entry.getValue();
+        writer.print(externalHosts);
+        writer.print("=");
+        writer.println(internalHosts);
+      }
     }
-    writer.close();
     String string = buffer.toString();
     return new StringAsset( string );
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletContextListener.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletContextListener.java
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletContextListener.java
index aa98ace..80e85eb 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletContextListener.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletContextListener.java
@@ -21,9 +21,6 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 
-import org.apache.knox.gateway.filter.rewrite.i18n.UrlRewriteMessages;
-import org.apache.knox.gateway.i18n.messages.MessagesFactory;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -38,7 +35,6 @@ public class UrlRewriteServletContextListener implements ServletContextListener
   public static final String DESCRIPTOR_LOCATION_INIT_PARAM_NAME = "rewriteDescriptorLocation";
   public static final String DESCRIPTOR_DEFAULT_FILE_NAME = "rewrite.xml";
   public static final String DESCRIPTOR_DEFAULT_LOCATION = "/WEB-INF/" + DESCRIPTOR_DEFAULT_FILE_NAME;
-  private static final UrlRewriteMessages LOG = MessagesFactory.get( UrlRewriteMessages.class
);
 
   @Override
   public void contextInitialized( ServletContextEvent event ) {
@@ -89,15 +85,9 @@ public class UrlRewriteServletContextListener implements ServletContextListener
   }
 
   private static UrlRewriteRulesDescriptor loadDescriptor( URL url ) throws IOException {
-    InputStream stream = url.openStream();
-    Reader reader = new InputStreamReader( stream, StandardCharsets.UTF_8 );
-    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.load( "xml",
reader );
-    try {
-      reader.close();
-    } catch( IOException closeException ) {
-      LOG.failedToLoadRewriteRulesDescriptor( closeException );
+    try (InputStream stream = url.openStream();
+         Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)){
+      return UrlRewriteRulesDescriptorFactory.load( "xml", reader );
     }
-    return descriptor;
   }
-
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseStream.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseStream.java
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseStream.java
index 49523e5..8611fb9 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseStream.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseStream.java
@@ -40,15 +40,14 @@ public class UrlRewriteResponseStream extends
   }
 
   @Override
-  public void write( int b ) throws IOException {
+  public void write( int b ) {
     buffer.write( b );
   }
 
   @Override
   public void close() throws IOException {
-    InputStream stream = new ByteArrayInputStream( buffer.toByteArray() );
-    response.streamResponse( stream ) ;
-    stream.close();
+    try(InputStream stream = new ByteArrayInputStream(buffer.toByteArray())) {
+      response.streamResponse(stream);
+    }
   }
-
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/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 023956c..7dba62a 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
@@ -251,14 +251,10 @@ public class GatewayServer {
 
   private static Properties loadBuildProperties() {
     Properties properties = new Properties();
-    InputStream inputStream = GatewayServer.class.getClassLoader().getResourceAsStream( "build.properties"
);
-    if( inputStream != null ) {
-      try {
-        properties.load( inputStream );
-        inputStream.close();
-      } catch( IOException e ) {
-        // Ignore.
-      }
+    try(InputStream inputStream = GatewayServer.class.getClassLoader().getResourceAsStream(
"build.properties" )) {
+      properties.load( inputStream );
+    } catch( IOException e ) {
+      // Ignore.
     }
     return properties;
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
b/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
index ae6fbb1..d6d54da 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/deploy/DeploymentFactory.java
@@ -211,9 +211,8 @@ public abstract class DeploymentFactory {
       Marshaller marshaller = jaxbContext.createMarshaller();
       marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
       marshaller.marshal( topology, writer );
-      writer.close();
       xml = writer.toString();
-    } catch (IOException | JAXBException e) {
+    } catch (JAXBException e) {
       throw new DeploymentException( "Failed to marshall topology.", e );
     }
     return new StringAsset( xml );

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java b/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java
index cc3a574..4a68365 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java
@@ -1357,12 +1357,11 @@ public class KnoxCLI extends Configured implements Tool {
         Console c = System.console();
         if( c != null) {
           this.username = c.readLine("Username: ");
-        }else{
-          try {
-            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
+        } else {
+          try(InputStreamReader inputStreamReader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
+              BufferedReader reader = new BufferedReader(inputStreamReader)) {
             out.println("Username: ");
             this.username = reader.readLine();
-            reader.close();
           } catch (IOException e){
             out.println(e.toString());
             this.username = "";
@@ -1375,8 +1374,8 @@ public class KnoxCLI extends Configured implements Tool {
         if( c != null) {
           this.password = c.readPassword("Password: ");
         }else{
-          try {
-            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
+          try(InputStreamReader inputStreamReader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
+              BufferedReader reader = new BufferedReader(inputStreamReader)) {
             out.println("Password: ");
             String pw = reader.readLine();
             if(pw != null){
@@ -1384,7 +1383,6 @@ public class KnoxCLI extends Configured implements Tool {
             } else {
               this.password = new char[0];
             }
-            reader.close();
           } catch (IOException e){
             out.println(e.toString());
             this.password = new char[0];
@@ -1678,14 +1676,13 @@ public class KnoxCLI extends Configured implements Tool {
         request = new HttpGet(httpServiceTestURL);
       }
 
-
       request.setHeader("Authorization", authString);
       request.setHeader("Accept", MediaType.APPLICATION_JSON.getMediaType());
       try {
         out.println(request.toString());
-        CloseableHttpResponse response = client.execute(request);
+        try(CloseableHttpResponse response = client.execute(request)) {
 
-        switch (response.getStatusLine().getStatusCode()) {
+          switch (response.getStatusLine().getStatusCode()) {
 
           case 200:
             response.getEntity().writeTo(out);
@@ -1703,11 +1700,9 @@ public class KnoxCLI extends Configured implements Tool {
             out.println(response.getStatusLine().toString());
             response.getEntity().writeTo(out);
             break;
+          }
         }
-
-        response.close();
         request.releaseConnection();
-
       } catch (ClientProtocolException e) {
         out.println(e.toString());
         if (debug) {
@@ -1729,7 +1724,6 @@ public class KnoxCLI extends Configured implements Tool {
           out.println(e.toString());
         }
       }
-
     }
 
     public void retryRequest(){
@@ -2046,14 +2040,10 @@ public class KnoxCLI extends Configured implements Tool {
 
   private static Properties loadBuildProperties() {
     Properties properties = new Properties();
-    InputStream inputStream = KnoxCLI.class.getClassLoader().getResourceAsStream( "build.properties"
);
-    if( inputStream != null ) {
-      try {
-        properties.load( inputStream );
-        inputStream.close();
-      } catch( IOException e ) {
-        // Ignore.
-      }
+    try(InputStream inputStream = KnoxCLI.class.getClassLoader().getResourceAsStream( "build.properties"
)) {
+      properties.load(inputStream);
+    } catch( IOException e ) {
+      // Ignore.
     }
     return properties;
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/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 c91cf9e..1d39ff0 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
@@ -121,13 +121,12 @@ public class ServiceTestResource {
 
             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();
-
+              try(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());
+              }
             } catch (IOException e) {
               messages.add("Exception: " + e.getMessage());
               test.setMessage(e.getMessage());

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
----------------------------------------------------------------------
diff --git a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
index ab5f3fe..a973dcc 100644
--- a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
+++ b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
@@ -55,9 +55,9 @@ public abstract class AbstractGatewayDispatch implements Dispatch {
     if (response instanceof GatewayResponse) {
       ((GatewayResponse) response).streamResponse(stream);
     } else {
-      OutputStream output = response.getOutputStream();
-      IOUtils.copy(stream, output);
-      output.close();
+      try(OutputStream output = response.getOutputStream()) {
+        IOUtils.copy(stream, output);
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/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 b7f204c..88e1030 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
@@ -182,10 +182,9 @@ public class TestUtils {
   }
 
   public static int findFreePort() throws IOException {
-    ServerSocket socket = new ServerSocket(0);
-    int port = socket.getLocalPort();
-    socket.close();
-    return port;
+    try(ServerSocket socket = new ServerSocket(0)) {
+      return socket.getLocalPort();
+    }
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/knox/blob/c0006605/gateway-util-common/src/main/java/org/apache/knox/gateway/audit/log4j/correlation/Log4jCorrelationService.java
----------------------------------------------------------------------
diff --git a/gateway-util-common/src/main/java/org/apache/knox/gateway/audit/log4j/correlation/Log4jCorrelationService.java
b/gateway-util-common/src/main/java/org/apache/knox/gateway/audit/log4j/correlation/Log4jCorrelationService.java
index 5b4d9d4..8c7def4 100644
--- a/gateway-util-common/src/main/java/org/apache/knox/gateway/audit/log4j/correlation/Log4jCorrelationService.java
+++ b/gateway-util-common/src/main/java/org/apache/knox/gateway/audit/log4j/correlation/Log4jCorrelationService.java
@@ -98,15 +98,14 @@ public class Log4jCorrelationService implements CorrelationService {
 
   @Override
   public byte[] getExternalizedContext() {
-    ByteArrayOutputStream baos = new ByteArrayOutputStream();
-    try {
-    ObjectOutputStream oos = new ObjectOutputStream( baos );
-    oos.writeObject( getContext() );
-    oos.close();
+    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+      try(ObjectOutputStream oos = new ObjectOutputStream( baos )) {
+        oos.writeObject( getContext() );
+      }
+      return baos.toByteArray();
     } catch ( IOException e ) {
       throw new RuntimeException( e );
     }
-    return baos.toByteArray();
   }
 }
 


Mime
View raw message