KNOX-876 - Fix FindBugs "Experimental" issues (Colm O hEigeartaigh via lmccay)
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ApplicationDeploymentContributor.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ApplicationDeploymentContributor.java
index f93c38a..ce642d1 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ApplicationDeploymentContributor.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ApplicationDeploymentContributor.java
@@ -60,7 +60,7 @@
private UrlRewriteRulesDescriptor serviceRules;
- private static ServiceDefinition loadServiceDefinition( Application application, File file ) throws JAXBException, FileNotFoundException {
+ private static ServiceDefinition loadServiceDefinition( Application application, File file ) throws JAXBException, FileNotFoundException, IOException {
ServiceDefinition definition;
if( !file.exists() ) {
definition = new ServiceDefinition();
@@ -77,8 +77,9 @@
} else {
JAXBContext context = JAXBContext.newInstance( ServiceDefinition.class );
Unmarshaller unmarshaller = context.createUnmarshaller();
- FileInputStream inputStream = new FileInputStream( file );
- definition = (ServiceDefinition) unmarshaller.unmarshal( inputStream );
+ try( FileInputStream inputStream = new FileInputStream( file ) ) {
+ definition = (ServiceDefinition) unmarshaller.unmarshal( inputStream );
+ }
}
return definition;
}
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
index 0e2a14d..15ad535 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
@@ -186,18 +186,14 @@
System.out.println("knoxinit successful!");
displayTokenDetails(json);
- FileOutputStream fos = null;
- try {
- File tokenfile = new File(System.getProperty("user.home"), ".knoxtokencache");
- fos = new FileOutputStream(tokenfile);
+ File tokenfile = new File(System.getProperty("user.home"), ".knoxtokencache");
+ try( FileOutputStream fos = new FileOutputStream(tokenfile) ) {
fos.write(text.getBytes("UTF-8"));
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
//add owners permission only
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(Paths.get(System.getProperty("user.home") + "/.knoxtokencache"), perms);
- } finally {
- fos.close();
}
}
catch(HadoopException he) {
diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/BaseKeystoreService.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/BaseKeystoreService.java
index 055cbe3..70e5689 100644
--- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/BaseKeystoreService.java
+++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/BaseKeystoreService.java
@@ -212,26 +212,19 @@
protected void writeCertificateToFile( Certificate cert, final File file ) throws CertificateEncodingException, IOException {
byte[] bytes = cert.getEncoded();
- final FileOutputStream out = new FileOutputStream( file );
Base64 encoder = new Base64( 76, "\n".getBytes( "ASCII" ) );
- try {
+ try( final FileOutputStream out = new FileOutputStream( file ) ) {
out.write( "-----BEGIN CERTIFICATE-----\n".getBytes( "ASCII" ) );
out.write( encoder.encodeToString( bytes ).getBytes( "ASCII" ) );
out.write( "-----END CERTIFICATE-----\n".getBytes( "ASCII" ) );
- } finally {
- out.close();
}
}
protected void writeKeystoreToFile(final KeyStore keyStore, final File file)
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
// TODO: backup the keystore on disk before attempting a write and restore on failure
- final FileOutputStream out = new FileOutputStream(file);
- try {
- keyStore.store( out, masterService.getMasterSecret());
- }
- finally {
- out.close();
+ try( final FileOutputStream out = new FileOutputStream(file) ) {
+ keyStore.store( out, masterService.getMasterSecret() );
}
}
diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/X509CertificateUtil.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/X509CertificateUtil.java
index f6a7ecd..87bb78c 100644
--- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/X509CertificateUtil.java
+++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/security/impl/X509CertificateUtil.java
@@ -276,14 +276,11 @@
public static void writeCertificateToFile(Certificate cert, final File file)
throws CertificateEncodingException, IOException {
byte[] bytes = cert.getEncoded();
- final FileOutputStream out = new FileOutputStream( file );
Base64 encoder = new Base64( 76, "\n".getBytes( "ASCII" ) );
- try {
+ try( final FileOutputStream out = new FileOutputStream( file ) ) {
out.write( "-----BEGIN CERTIFICATE-----\n".getBytes( "ASCII" ) );
out.write( encoder.encodeToString( bytes ).getBytes( "ASCII" ) );
out.write( "-----END CERTIFICATE-----\n".getBytes( "ASCII" ) );
- } finally {
- out.close();
}
}