ranger-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject ranger git commit: RANGER-1814 : Move the reader into a local variable, and so it can be ensured by static code analysis, that it's closed properly.
Date Mon, 02 Oct 2017 11:51:00 GMT
Repository: ranger
Updated Branches:
  refs/heads/master f0cb6223d -> 9cdc1144f


RANGER-1814 : Move the reader into a local variable, and so it can be ensured by static code
analysis, that it's closed properly.

Change-Id: I942079f5b95cdd6a5f1131be1ed18d0f7e6b076e

Signed-off-by: Colm O hEigeartaigh <coheigea@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/ranger/commit/9cdc1144
Tree: http://git-wip-us.apache.org/repos/asf/ranger/tree/9cdc1144
Diff: http://git-wip-us.apache.org/repos/asf/ranger/diff/9cdc1144

Branch: refs/heads/master
Commit: 9cdc1144f4938f666c80bd5cea6f2243492c71a3
Parents: f0cb622
Author: Zsombor Gegesy <gzsombor@gmail.com>
Authored: Thu Sep 28 13:02:37 2017 +0200
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Mon Oct 2 12:33:01 2017 +0100

----------------------------------------------------------------------
 .../audit/provider/LocalFileLogBuffer.java      | 59 ++++++++++----------
 1 file changed, 30 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ranger/blob/9cdc1144/agents-audit/src/main/java/org/apache/ranger/audit/provider/LocalFileLogBuffer.java
----------------------------------------------------------------------
diff --git a/agents-audit/src/main/java/org/apache/ranger/audit/provider/LocalFileLogBuffer.java
b/agents-audit/src/main/java/org/apache/ranger/audit/provider/LocalFileLogBuffer.java
index 56a24ed..8a14394 100644
--- a/agents-audit/src/main/java/org/apache/ranger/audit/provider/LocalFileLogBuffer.java
+++ b/agents-audit/src/main/java/org/apache/ranger/audit/provider/LocalFileLogBuffer.java
@@ -367,7 +367,6 @@ class DestinationDispatcherThread<T> extends Thread {
 	private DebugTracer            mLogger            = null;
 
 	private String         mCurrentLogfile = null;
-	private BufferedReader mReader         = null;
 
 	public DestinationDispatcherThread(LocalFileLogBuffer<T> fileLogBuffer, LogDestination<T>
destination, DebugTracer tracer) {
 		super(DestinationDispatcherThread.class.getSimpleName() + "-" + System.currentTimeMillis());
@@ -491,34 +490,35 @@ class DestinationDispatcherThread<T> extends Thread {
 
 		long destinationPollIntervalInMs = 1000L;
 
-		openCurrentFile();
-
-		 while(!mStopThread) {
-			String log = getNextStringifiedLog();
+		BufferedReader reader = openCurrentFile();
+		try {
+			while(!mStopThread) {
+				String log = getNextStringifiedLog(reader);
 
-			if(log == null) { // reached end-of-file
-				ret = true;
+				if(log == null) { // reached end-of-file
+					ret = true;
 
-				break;
-			}
+					break;
+				}
 
-			try {
-				// loop until log is sent successfully
-				while(!mStopThread && !mDestination.sendStringified(log)) {
-					try {
-						Thread.sleep(destinationPollIntervalInMs);
-					} catch(InterruptedException excp) {
-						throw new RuntimeException("LocalFileLogBuffer.sendCurrentFile(" + mCurrentLogfile
+ "): failed while waiting for destination to be available", excp);
+				try {
+					// loop until log is sent successfully
+					while(!mStopThread && !mDestination.sendStringified(log)) {
+						try {
+							Thread.sleep(destinationPollIntervalInMs);
+						} catch(InterruptedException excp) {
+							throw new RuntimeException("LocalFileLogBuffer.sendCurrentFile(" + mCurrentLogfile
+ "): failed while waiting for destination to be available", excp);
+						}
 					}
+				} catch ( AuditMessageException msgError) {
+					mLogger.error("Error in log message:" + log);
+					//If there is error in log message, then it will be skipped
 				}
-			} catch ( AuditMessageException msgError) {
-				mLogger.error("Error in log message:" + log);
-				//If there is error in log message, then it will be skipped
 			}
+		} finally {
+			closeCurrentFile(reader);
 		}
 
-		closeCurrentFile();
-
 		if(!mStopThread) {
 			mDestination.flush();
 			archiveCurrentFile();
@@ -529,7 +529,7 @@ class DestinationDispatcherThread<T> extends Thread {
 		return ret;
 	}
 
-	private String getNextStringifiedLog() {
+	private String getNextStringifiedLog(BufferedReader mReader) {
 		String log = null;
 
 		if(mReader != null) {
@@ -569,15 +569,16 @@ class DestinationDispatcherThread<T> extends Thread {
 		return log;
 	}
 
-	private void openCurrentFile() {
+	private BufferedReader openCurrentFile() {
 		mLogger.debug("==> openCurrentFile(" + mCurrentLogfile + ")");
+		BufferedReader mReader = null;
 
 		if(mCurrentLogfile != null) {
 			try {
 				FileInputStream inStr = new FileInputStream(mCurrentLogfile);
-				
+
 				InputStreamReader strReader = createReader(inStr);
-				
+
 				if(strReader != null) {
 					mReader = new BufferedReader(strReader);
 				}
@@ -587,9 +588,10 @@ class DestinationDispatcherThread<T> extends Thread {
 		}
 
 		mLogger.debug("<== openCurrentFile(" + mCurrentLogfile + ")");
+		return mReader;
 	}
-	
-	private void closeCurrentFile() {
+
+	private void closeCurrentFile(BufferedReader mReader) {
 		mLogger.debug("==> closeCurrentFile(" + mCurrentLogfile + ")");
 
 		if(mReader != null) {
@@ -599,7 +601,6 @@ class DestinationDispatcherThread<T> extends Thread {
 				// ignore
 			}
 		}
-		mReader = null;
 
 		mLogger.debug("<== closeCurrentFile(" + mCurrentLogfile + ")");
 	}
@@ -653,7 +654,7 @@ class DestinationDispatcherThread<T> extends Thread {
 		mCurrentLogfile = null;
 	}
 
-	public InputStreamReader createReader(InputStream iStr) {
+	private InputStreamReader createReader(InputStream iStr) {
 		InputStreamReader reader = null;
 
 	    if(iStr != null) {


Mime
View raw message