Author: hashutosh
Date: Thu Mar 27 00:06:29 2014
New Revision: 1582135

URL: http://svn.apache.org/r1582135
Log:
HIVE-6750 : Hive printing debug information in stdout after the end of CLI 
session (Vaibhav Gumashta via Ashutosh Chauhan)

Modified:
    
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/common/JavaUtils.java

Modified: 
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/common/JavaUtils.java
URL: 
http://svn.apache.org/viewvc/hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/common/JavaUtils.java?rev=1582135&r1=1582134&r2=1582135&view=diff
==============================================================================
--- 
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/common/JavaUtils.java
 (original)
+++ 
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/common/JavaUtils.java
 Thu Mar 27 00:06:29 2014
@@ -18,16 +18,18 @@
 
 package org.apache.hadoop.hive.common;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
+import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.IOException;
+import java.io.PrintStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URLClassLoader;
 import java.util.Arrays;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * Collection of Java class loading/reflection related utilities common across
  * Hive.
@@ -98,8 +100,17 @@ public final class JavaUtils {
     if (loader instanceof Closeable) {
       ((Closeable)loader).close();
     } else if (SUN_MISC_UTIL_RELEASE != null && loader instanceof 
URLClassLoader) {
+      PrintStream outputStream = System.out;
+      ByteArrayOutputStream byteArrayOutputStream = new 
ByteArrayOutputStream();
+      PrintStream newOutputStream = new PrintStream(byteArrayOutputStream);
       try {
+        // SUN_MISC_UTIL_RELEASE.invoke prints to System.out
+        // So we're changing the outputstream for that call,
+        // and setting it back to original System.out when we're done
+        System.setOut(newOutputStream);
         SUN_MISC_UTIL_RELEASE.invoke(null, loader);
+        String output = byteArrayOutputStream.toString("UTF8");
+        LOG.debug(output);
       } catch (InvocationTargetException e) {
         if (e.getTargetException() instanceof IOException) {
           throw (IOException)e.getTargetException();
@@ -108,6 +119,10 @@ public final class JavaUtils {
       } catch (Exception e) {
         throw new IOException(e);
       }
+      finally {
+        System.setOut(outputStream);
+        newOutputStream.close();
+      }
     }
   }
 


Reply via email to