Correctly nest locks and unlocks to resolve FindBugs warning
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 7 Aug 2011 10:47:38 +0000 (10:47 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 7 Aug 2011 10:47:38 +0000 (10:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1154691 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/juli/FileHandler.java

index d5fd6e2..6569fb0 100644 (file)
@@ -172,29 +172,29 @@ public class FileHandler
         String tsString = ts.toString().substring(0, 19);
         String tsDate = tsString.substring(0, 10);
 
-        writerLock.readLock().lock();
         try {
+            writerLock.readLock().lock();
             // If the date has changed, switch log files
             if (rotatable && !date.equals(tsDate)) {
-                // Update to writeLock before we switch
-                writerLock.readLock().unlock();
-                writerLock.writeLock().lock();
-
-                // Make sure another thread hasn't already done this
-                if (!date.equals(tsDate)) {
-                    closeWriter();
-                    date = tsDate;
-                    openWriter();
+                try {
+                    // Update to writeLock before we switch
+                    writerLock.readLock().unlock();
+                    writerLock.writeLock().lock();
+    
+                    // Make sure another thread hasn't already done this
+                    if (!date.equals(tsDate)) {
+                        closeWriter();
+                        date = tsDate;
+                        openWriter();
+                    }
+                } finally {
+                    writerLock.writeLock().unlock();
+                    // Down grade to read-lock. This ensures the writer remains valid
+                    // until the log message is written
+                    writerLock.readLock().lock();
                 }
-                // Down grade to read-lock. This ensures the writer remains valid
-                // until the log message is written
-                writerLock.readLock().lock();
             }
-        } finally {
-            writerLock.writeLock().unlock();
-        }
 
-        try {
             String result = null;
             try {
                 result = getFormatter().format(record);