Skip to content

Commit

Permalink
RANGER-331: inputstream is closed in the finally clause
Browse files Browse the repository at this point in the history
  • Loading branch information
sneethiraj committed Mar 24, 2015
1 parent 0ab4875 commit ce139e0
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,26 @@ public static void initResourceMap() {
if (in != null) {
try {
resourcemapProperties.load(in);
for (Map.Entry<Object, Object> entry : resourcemapProperties.entrySet() ) {
String key = (String)entry.getKey();
String value = (String)entry.getValue();
if (RANGER_SECTION_NAME.equals(value)) {
rangerInternalPropertyKeys.add(key);
}
}
for (Map.Entry<Object, Object> entry : resourcemapProperties.entrySet() ) {
String key = (String)entry.getKey();
String value = (String)entry.getValue();
if (RANGER_SECTION_NAME.equals(value)) {
rangerInternalPropertyKeys.add(key);
}
}
} catch (IOException e) {
throw new HadoopException("Unable to load resource map properties from [" + RESOURCEMAP_PROP_FILE + "]", e);
}
finally {
if (in != null) {
try {
in.close() ;
}
catch(IOException ioe) {
// Ignore IOException during close of stream
}
}
}
}
else {
throw new HadoopException("Unable to locate resource map properties from [" + RESOURCEMAP_PROP_FILE + "] in the class path.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,19 @@ private Element createNewElement(String propName, String val) {
private void loadInstallProperties() throws IOException {
if (propFile != null) {
FileInputStream in = new FileInputStream(propFile) ;
try {
installProperties.load(in);
}
finally {
if (in != null) {
try {
in.close();
}
catch(IOException ioe) {
// Ignore IOException during close of stream
}
}
}
}
// To support environment variable, we will add all environment variables to the Properties
installProperties.putAll(System.getenv());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ private void initConfig() {
LOG.severe("Unable to load config file [" + cfgFile + "]");
ioe.printStackTrace();
}
finally {
if (in != null) {
try {
in.close() ;
}
catch(IOException ioe) {
// Ignore IOE when the stream is closed.
}
}
}
serverConfigProperties.list(System.out);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.ranger.services.hdfs;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
Expand All @@ -39,7 +41,21 @@ public static void main(String[] args) throws Throwable {
String fileNameToMatch = (args.length == 3 ? null : args[3]) ;

Properties conf = new Properties() ;
conf.load(HdfsClientTester.class.getClassLoader().getResourceAsStream(propFile));

InputStream in = HdfsClientTester.class.getClassLoader().getResourceAsStream(propFile) ;
try {
conf.load(in);
}
finally {
if (in != null) {
try {
in.close() ;
}
catch(IOException ioe) {
// Ignore IOException created during close
}
}
}

HashMap<String,String> prop = new HashMap<String,String>() ;
for(Object key : conf.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.apache.ranger.services.hive.client;


import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
Expand All @@ -40,7 +42,21 @@ public static void main(String[] args) throws Throwable {
try {

Properties conf = new Properties() ;
conf.load(HiveClientTester.class.getClassLoader().getResourceAsStream(args[1]));

InputStream in = HiveClientTester.class.getClassLoader().getResourceAsStream(args[1]) ;
try {
conf.load(in);
}
finally {
if (in != null) {
try {
in.close();
}
catch(IOException ioe) {
// Ignore IOException when closing the stream
}
}
}

HashMap<String,String> prop = new HashMap<String,String>() ;
for(Object key : conf.keySet()) {
Expand Down
32 changes: 30 additions & 2 deletions jisql/src/main/java/org/apache/util/sql/Jisql.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

import org.apache.util.outputformatter.JisqlFormatter;

/**
Expand Down Expand Up @@ -291,8 +293,20 @@ public void run() throws Exception {
else {
if(connectString.toLowerCase().startsWith("jdbc:mysql") && inputFileName!=null){
MySQLPLRunner scriptRunner = new MySQLPLRunner(connection, false, true,printDebug);
scriptRunner.setDelimiter(commandTerminator,false);
scriptRunner.runScript(new FileReader(inputFileName));
scriptRunner.setDelimiter(commandTerminator,false);
FileReader reader = new FileReader(inputFileName) ;
try {
scriptRunner.runScript(reader);
}
finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
// Ignore error during closing of the reader stream
}
}
}
}else{
doIsql();
}
Expand Down Expand Up @@ -358,6 +372,9 @@ public void doIsql() throws SQLException {
statement = connection.createStatement();
connection.clearWarnings();
String trimmedLine=null;

try {

while (true) {
int linecount = 1;
query = new StringBuffer();
Expand Down Expand Up @@ -480,6 +497,17 @@ public void doIsql() throws SQLException {
if (inputQuery != null)
return;
}
}
finally {
if (reader != null) {
try {
reader.close();
}
catch(IOException ioe) {
// Ignore IOE when closing streams
}
}
}
}

/**
Expand Down
17 changes: 15 additions & 2 deletions jisql/src/main/java/org/apache/util/sql/MySQLPLRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -311,7 +310,21 @@ public static void main(String args[]){


// Executing SQL Script
scriptRunner.runScript(new FileReader(aSQLScriptFilePath));
FileReader reader = new FileReader(aSQLScriptFilePath) ;

try {
scriptRunner.runScript(reader);
}
finally {
if (reader != null) {
try {
reader.close();
}
catch(IOException ioe) {
// Ignore IOException when reader is getting closed
}
}
}


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public static KnoxClient getKnoxClient(String serviceName,
Map<String, String> configs) {
KnoxClient knoxClient = null;
LOG.debug("Getting knoxClient for ServiceName: " + serviceName
+ "configMap: " + configs.toString());
+ "configMap: " + configs);
String errMsg = " You can still save the repository and start creating "
+ "policies, but you would not be able to use autocomplete for "
+ "resource names. Check xa_portal.log for more info.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.ranger.hadoop.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
Expand All @@ -37,7 +39,20 @@ public static void main(String[] args) throws Throwable {
String fileNameToMatch = (args.length == 3 ? null : args[3]) ;

Properties conf = new Properties() ;
conf.load(HadoopFSTester.class.getClassLoader().getResourceAsStream(propFile));
InputStream in = HadoopFSTester.class.getClassLoader().getResourceAsStream(propFile) ;
try {
conf.load(in);
}
finally {
if (in != null) {
try {
in.close() ;
}
catch(IOException ioe) {
// Ignore IOE when closing stream
}
}
}

HashMap<String,String> prop = new HashMap<String,String>() ;
for(Object key : conf.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.ranger.hbase.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
Expand All @@ -42,8 +44,20 @@ public static void main(String[] args) throws Throwable {
LOG.info("Starting ...");

Properties conf = new Properties();

conf.load(HBaseClientTester.class.getClassLoader().getResourceAsStream(args[1]));
InputStream in = HBaseClientTester.class.getClassLoader().getResourceAsStream(args[1]) ;
try {
conf.load(in);
}
finally {
if (in != null) {
try {
in.close();
}
catch(IOException ioe) {
// Ignore IOE when closing stream
}
}
}

HashMap<String, String> prop = new HashMap<String, String>();
for (Object key : conf.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
Expand Down Expand Up @@ -169,7 +170,17 @@ private void init() {
try {
InputStream in = getFileInputStream(CONFIG_FILE) ;
if (in != null) {
prop.load(in) ;
try {
prop.load(in) ;
}
finally {
try {
in.close() ;
}
catch(IOException ioe) {
// Ignore IOE when closing stream
}
}
}
} catch (Throwable e) {
throw new RuntimeException("Unable to load configuration file [" + CONFIG_FILE + "]", e) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,18 @@ public void initialize(Subject subject, CallbackHandler callbackHandler, Map<Str
try {
in = getFileInputStream(val) ;
if (in != null) {
config = new Properties() ;
config.load(in);
try {
config = new Properties() ;
config.load(in);
}
finally {
try {
in.close();
}
catch(IOException ioe) {
// Ignore IOException when closing streams
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -125,9 +126,22 @@ private void startUnixUserGroupSyncProcess() {

//TODO: add more validation code
private void init() throws Throwable {
InputStream in = getFileInputStream("unixauthservice.properties") ;
Properties prop = new Properties() ;
prop.load(in);
InputStream in = getFileInputStream("unixauthservice.properties") ;

if (in != null) {
try {
prop.load(in);
}
finally {
try {
in.close();
}
catch(IOException ioe) {
// Ignore IOE when closing streams
}
}
}
keyStorePath = prop.getProperty(SSL_KEYSTORE_PATH_PARAM) ;
keyStorePathPassword = prop.getProperty(SSL_KEYSTORE_PATH_PASSWORD_PARAM) ;
trustStorePath = prop.getProperty(SSL_TRUSTSTORE_PATH_PARAM) ;
Expand Down

0 comments on commit ce139e0

Please sign in to comment.