You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ie...@apache.org on 2012/10/26 03:32:40 UTC

svn commit: r1402370 - in /sling/trunk/bundles/commons/log/src: main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerWriter.java test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java

Author: ieb
Date: Fri Oct 26 01:32:39 2012
New Revision: 1402370

URL: http://svn.apache.org/viewvc?rev=1402370&view=rev
Log:
SLING-2149 Fixed. Added fallback to Stdout on permission denied and added unit tests to simulate the issue.

Modified:
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerWriter.java
    sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerWriter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerWriter.java?rev=1402370&r1=1402369&r2=1402370&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerWriter.java (original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerWriter.java Fri Oct 26 01:32:39 2012
@@ -19,6 +19,7 @@
 package org.apache.sling.commons.log.internal.slf4j;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
@@ -350,20 +351,30 @@ class SlingLoggerWriter extends Writer {
     }
 
     private Writer createWriter() throws IOException {
-        if (file == null) {
-            return new OutputStreamWriter(System.out) {
-                @Override
-                public void close() {
-                    // not really !!
-                }
-            };
-        }
-
-        // ensure parent path of the file to create
-        file.getParentFile().mkdirs();
-
-        // open the file in append mode to not overwrite an existing
-        // log file from a previous instance running
-        return new OutputStreamWriter(new FileOutputStream(file, true));
+        if ( file != null ) {
+            try {
+                // ensure parent path of the file to create
+                file.getParentFile().mkdirs();
+        
+                // open the file in append mode to not overwrite an existing
+                // log file from a previous instance running
+                return new OutputStreamWriter(new FileOutputStream(file, true));
+            } catch ( FileNotFoundException e) {
+                System.out.println("Unable to open "+file.getAbsolutePath()+" due to "+e.getMessage());
+                System.out.println("Defaulting to stdout");
+            } catch ( SecurityException e) {
+                System.out.println("Unable to open "+file.getAbsolutePath()+" due to "+e.getMessage());
+                System.out.println("Defaulting to stdout");
+            }
+            file = null;
+            path = null;
+        }
+        
+        return new OutputStreamWriter(System.out) {
+            @Override
+            public void close() {
+                // not really !!
+            }
+        };        
     }
 }

Modified: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java?rev=1402370&r1=1402369&r2=1402370&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java (original)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java Fri Oct 26 01:32:39 2012
@@ -229,6 +229,54 @@ public class SlingLogWriterTest extends 
         assertTime("'.'yyyy-MM-dd", "");
         assertTime("'.'yyyy-MM-dd-mm", "'.'yyyy-MM-dd-mm");
     }
+    
+    public void test_create_denied_parent() throws IOException {
+        File baseFile = getBaseFile();
+        File protectedParent = new File(baseFile,"protected");
+        protectedParent.mkdirs();
+        File loggingParent = new File(protectedParent,"logging");
+        protectedParent.setExecutable(false);
+        protectedParent.setWritable(false);
+        try{
+            assertFalse(protectedParent.canWrite());
+            SlingLoggerWriter writer = createLogWriter(loggingParent.getAbsolutePath(), -1, 10);
+            assertNotNull(writer);
+            assertNull(writer.getFile());
+            assertNull(writer.getPath());
+            writer.append("Testing Stdout");
+        } finally {
+            try {
+                protectedParent.setExecutable(true);
+                protectedParent.setWritable(true);
+            } catch ( Exception e ) {
+                // no need.
+            }
+        }
+    }
+
+    public void test_create_denied() throws IOException {
+        File baseFile = getBaseFile();
+        File protectedParent = new File(baseFile,"protected");
+        File loggingParent = new File(protectedParent,"logging");
+        loggingParent.mkdirs();
+        loggingParent.setWritable(false);
+        protectedParent.setExecutable(false);
+        try {
+            assertFalse(loggingParent.canWrite());
+            SlingLoggerWriter writer = createLogWriter(loggingParent.getAbsolutePath(), -1, 10);
+            assertNotNull(writer);
+            assertNull(writer.getFile());
+            assertNull(writer.getPath());
+            writer.append("Testing Stdout");
+        } finally {
+            try {
+                protectedParent.setExecutable(true);
+                protectedParent.setWritable(true);
+            } catch ( Exception e ) {
+                // no need.
+            }
+        }
+    }
 
     private SlingLoggerWriter createLogWriter(String file, int numFiles,
             long size) throws IOException {