Author: veithen
Date: Sun May 29 12:56:14 2016
New Revision: 1746001

URL: http://svn.apache.org/viewvc?rev=1746001&view=rev
Log:
Prevent LogAspect from causing a NullPointerException when invoked outside of 
ManagedTestSuite (e.g. in a regular unit test). This problem has been observed 
in SYNAPSE-1027.

Modified:
    
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/axis2/LogAspect.java
    
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/util/LogManager.java

Modified: 
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/axis2/LogAspect.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/axis2/LogAspect.java?rev=1746001&r1=1746000&r2=1746001&view=diff
==============================================================================
--- 
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/axis2/LogAspect.java
 (original)
+++ 
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/axis2/LogAspect.java
 Sun May 29 12:56:14 2016
@@ -48,12 +48,16 @@ public class LogAspect {
     public void aroundWriteTo(ProceedingJoinPoint proceedingJoinPoint,
             MessageContext msgContext, OMOutputFormat format, OutputStream 
out, boolean preserve)
             throws Throwable {
-        OutputStream log = LogManager.INSTANCE.createLog("formatter");
-        try {
-            OutputStream tee = new TeeOutputStream(out, log);
-            proceedingJoinPoint.proceed(new Object[] { msgContext, format, 
tee, preserve });
-        } finally {
-            log.close();
+        if (LogManager.INSTANCE.isLoggingEnabled()) {
+            OutputStream log = LogManager.INSTANCE.createLog("formatter");
+            try {
+                OutputStream tee = new TeeOutputStream(out, log);
+                proceedingJoinPoint.proceed(new Object[] { msgContext, format, 
tee, preserve });
+            } finally {
+                log.close();
+            }
+        } else {
+            proceedingJoinPoint.proceed();
         }
     }
     
@@ -61,20 +65,22 @@ public class LogAspect {
         pointcut="call(javax.activation.DataSource 
org.apache.axis2.format.MessageFormatterEx.getDataSource(..))",
         returning="dataSource")
     public void afterGetDataSource(DataSource dataSource) {
-        try {
-            OutputStream out = LogManager.INSTANCE.createLog("formatter");
+        if (LogManager.INSTANCE.isLoggingEnabled()) {
             try {
-                InputStream in = dataSource.getInputStream();
+                OutputStream out = LogManager.INSTANCE.createLog("formatter");
                 try {
-                    IOUtils.copy(in, out);
+                    InputStream in = dataSource.getInputStream();
+                    try {
+                        IOUtils.copy(in, out);
+                    } finally {
+                        in.close();
+                    }
                 } finally {
-                    in.close();
+                    out.close();
                 }
-            } finally {
-                out.close();
+            } catch (Throwable ex) {
+                log.error("Unable to dump message", ex);
             }
-        } catch (Throwable ex) {
-            log.error("Unable to dump message", ex);
         }
     }
     
@@ -83,16 +89,20 @@ public class LogAspect {
             " && args(in, contentType, msgContext)")
     public Object aroundProcessDocument(ProceedingJoinPoint 
proceedingJoinPoint,
             InputStream in, String contentType, MessageContext msgContext) 
throws Throwable {
-        InputStream tee;
-        if (in == null) {
-            tee = null;
+        if (LogManager.INSTANCE.isLoggingEnabled()) {
+            InputStream tee;
+            if (in == null) {
+                tee = null;
+            } else {
+                OutputStream log = LogManager.INSTANCE.createLog("builder");
+                // Note: We can't close the log right after the method 
execution because the
+                //       message builder may use streaming. LogManager will 
take care of closing the
+                //       log for us if anything goes wrong.
+                tee = new TeeInputStream(in, log, true);
+            }
+            return proceedingJoinPoint.proceed(new Object[] { tee, 
contentType, msgContext });
         } else {
-            OutputStream log = LogManager.INSTANCE.createLog("builder");
-            // Note: We can't close the log right after the method execution 
because the
-            //       message builder may use streaming. LogManager will take 
care of closing the
-            //       log for us if anything goes wrong.
-            tee = new TeeInputStream(in, log, true);
+            return proceedingJoinPoint.proceed();
         }
-        return proceedingJoinPoint.proceed(new Object[] { tee, contentType, 
msgContext });
     }
 }

Modified: 
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/util/LogManager.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/util/LogManager.java?rev=1746001&r1=1746000&r2=1746001&view=diff
==============================================================================
--- 
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/util/LogManager.java
 (original)
+++ 
axis/axis2/java/core/trunk/modules/transport/testkit/src/main/java/org/apache/axis2/transport/testkit/util/LogManager.java
 Sun May 29 12:56:14 2016
@@ -70,6 +70,10 @@ public class LogManager {
         }
     }
     
+    public synchronized boolean isLoggingEnabled() {
+        return testCaseDir != null;
+    }
+    
     public synchronized OutputStream createLog(String name) throws IOException 
{
         testCaseDir.mkdirs();
         OutputStream log = new FileOutputStream(new File(testCaseDir, 
StringUtils.leftPad(String.valueOf(sequence++), 2, '0') + "-" + name + ".log"));


Reply via email to