[jboss-cvs] JBossAS SVN: r100318 - in branches/JBPAPP_5_0: testsuite/src/main/org/jboss/test/naming/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Feb 3 00:10:14 EST 2010


Author: bstansberry at jboss.com
Date: 2010-02-03 00:10:13 -0500 (Wed, 03 Feb 2010)
New Revision: 100318

Added:
   branches/JBPAPP_5_0/testsuite/src/main/org/jboss/test/naming/test/NamingProviderURLWriterUnitTestCase.java
Modified:
   branches/JBPAPP_5_0/server/src/main/org/jboss/naming/NamingProviderURLWriter.java
Log:
[JBPAPP-3364] Avoid windows file locking and deal w/ it if it happens

Modified: branches/JBPAPP_5_0/server/src/main/org/jboss/naming/NamingProviderURLWriter.java
===================================================================
--- branches/JBPAPP_5_0/server/src/main/org/jboss/naming/NamingProviderURLWriter.java	2010-02-03 04:26:33 UTC (rev 100317)
+++ branches/JBPAPP_5_0/server/src/main/org/jboss/naming/NamingProviderURLWriter.java	2010-02-03 05:10:13 UTC (rev 100318)
@@ -22,7 +22,9 @@
 
 package org.jboss.naming;
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.Inet6Address;
@@ -54,7 +56,6 @@
    private Server server;
    private URI outputDir;
    private String filename = DEFAULT_PERSIST_FILE_NAME;
-   private File outputFile;
 
    public URI getOutputDirURI()
    {
@@ -135,29 +136,36 @@
          {
             base.mkdirs();
             
-            outputFile = new File(base, getOutputFileName());
-            if (outputFile.exists())
+            File file = new File(base, getOutputFileName());
+            if (file.exists())
             {
-               outputFile.delete();
+               file.delete();
             }
-            if (log.isTraceEnabled())
-            {
-               log.trace("Creating file " + outputFile);
-            }
-            outputFile.createNewFile();            
-            PrintWriter writer = new PrintWriter(outputFile);
+            PrintWriter writer = null;
             try
-            {
+            {               
+               if (log.isTraceEnabled())
+               {
+                  log.trace("Creating file " + file);
+               }
+               file.createNewFile();
+               file.deleteOnExit();
+               
+               writer = new PrintWriter(file);
                writer.println(bootstrapUrl);
                writer.flush();
             }
+            catch (Exception e)
+            {
+               handleOutputFileCreationException(file.toURI(), e);
+            }
             finally
             {
-               outputFile.deleteOnExit();
-               writer.close();
+               if (writer != null)
+               {
+                  writer.close();
+               }
             }
-            
-            outputFile.deleteOnExit();
          }
          else
          {
@@ -172,11 +180,69 @@
       }
    }
 
+   /** JBAS-7674 Try to intelligently deal with case where Windows has the file locked */
+   private void handleOutputFileCreationException(URI outputFile, Exception e)
+   {
+      boolean handled = false;
+      FileReader fr = null;
+      try
+      {
+         File f = new File(outputFile);
+         if (f.exists())
+         {
+            fr = new FileReader(f);
+            String existing = new BufferedReader(fr).readLine();
+            if (existing != null && this.bootstrapUrl.equals(existing.trim()))
+            {
+               log.info("Experienced a problem (" + e + 
+                        ") updating the naming service URL file " + outputFile + 
+                        " but the current contents are correct.");
+            }
+            else
+            {
+               log.error("Cannot update naming service URL file " + outputFile, e);
+            }
+            handled = true;
+         }
+      }
+      catch (Exception ignored)
+      {
+         ;
+      }
+      finally
+      {
+         if (fr != null)
+         {
+            try
+            {
+               fr.close();
+            }
+            catch (Exception ignored) {}
+         }
+      }
+      
+      if (!handled)
+      {
+         log.error("Cannot create a naming service URL file at " + outputFile, e);
+      }
+      
+   }
+
    public void stop() throws Exception
    {
-      if (outputFile != null)
+      if (outputDir != null)
       {
-         outputFile.delete();
+         String outputFile = "";
+         try
+         {
+            File f = new File(new File(outputDir), getOutputFileName());
+            outputFile = f.getAbsolutePath();
+            f.delete();
+         }
+         catch (Exception e)
+         {
+            log.warn("Failed to delete JNP URL file " + outputFile + " due to " + e);
+         }
       }
    }
 

Copied: branches/JBPAPP_5_0/testsuite/src/main/org/jboss/test/naming/test/NamingProviderURLWriterUnitTestCase.java (from rev 100316, trunk/testsuite/src/main/org/jboss/test/naming/test/NamingProviderURLWriterUnitTestCase.java)
===================================================================
--- branches/JBPAPP_5_0/testsuite/src/main/org/jboss/test/naming/test/NamingProviderURLWriterUnitTestCase.java	                        (rev 0)
+++ branches/JBPAPP_5_0/testsuite/src/main/org/jboss/test/naming/test/NamingProviderURLWriterUnitTestCase.java	2010-02-03 05:10:13 UTC (rev 100318)
@@ -0,0 +1,164 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.test.naming.test;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.net.URI;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+
+import org.jboss.naming.NamingProviderURLWriter;
+import org.jboss.util.file.Files;
+
+/**
+ * Unit tests of {@link NamingProviderURLWriter}
+ *
+ * @author Brian Stansberry
+ * 
+ * @version $Revision$
+ */
+public class NamingProviderURLWriterUnitTestCase extends TestCase
+{
+   private static long count = System.currentTimeMillis();
+   private URI tempDir;
+   private CountDownLatch tearDownLatch;
+   
+   /**
+    * Create a new NamingProviderURLWriterUnitTestCase.
+    * 
+    * @param name
+    */
+   public NamingProviderURLWriterUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      
+      File tmp = new File(System.getProperty("java.io.tmpdir"));
+      File dir = new File(tmp, getClass().getSimpleName());
+      tempDir = dir.toURI();
+      dir.mkdirs();
+      dir.deleteOnExit();
+      
+      count++;
+   }
+
+   protected void tearDown() throws Exception
+   {
+      try
+      {
+         super.tearDown();
+      }
+      finally
+      {
+         if (tearDownLatch != null)
+         {
+            tearDownLatch.countDown();
+         }
+         if (tempDir != null)
+         {
+            Files.delete(new File(tempDir));
+         }
+      }
+   }
+   
+   /**
+    * JBAS-7674 -- try to lock the file that will be written and check
+    * that the NamingProviderURLWriter start/stop methods don't blow up.
+    * Note this test is only meaningful on windows.
+    * 
+    * @throws Exception
+    */
+   public void testLifecycleWithLockedFile() throws Exception
+   {
+      String fileName = "jnp-service-" + count + ".url";
+      tearDownLatch = new CountDownLatch(1);
+      CountDownLatch lockedLatch = new CountDownLatch(1);
+      FileLocker locker = new FileLocker(tempDir, fileName, null, tearDownLatch, lockedLatch);
+      
+      NamingProviderURLWriter testee = new NamingProviderURLWriter();
+      testee.setOutputDirURL(tempDir);
+      testee.setOutputFileName(fileName);
+      testee.setBootstrapAddress("localhost");
+      testee.setBootstrapPort(9999);
+      
+      Thread t = new Thread(locker);
+      t.setDaemon(true);
+      t.start();
+      assertTrue("FileLocker locked file", lockedLatch.await(10, TimeUnit.SECONDS));
+      testee.start();
+      testee.stop();
+   }
+   
+   /** Hacky attempt to "lock" a file. */
+   private static class FileLocker implements Runnable
+   {
+      private final File file;
+      private final String serviceURL;
+      private final CountDownLatch tearDownLatch;
+      private final CountDownLatch lockedLatch;
+      
+      private FileLocker(URI tempDir, String fileName, String serviceURL, CountDownLatch tearDownLatch, CountDownLatch lockedLatch)
+      {
+         this.file = new File(new File(tempDir), fileName);
+         this.tearDownLatch = tearDownLatch;
+         this.lockedLatch = lockedLatch;
+         this.serviceURL = serviceURL == null ? "jnp://localhost:1099" : serviceURL;
+      }
+
+      public void run()
+      {
+         PrintWriter writer = null;
+         try
+         {
+            writer = new PrintWriter(file);
+            writer.println(serviceURL);
+            writer.flush();
+            // Tell driver thread we're ready
+            lockedLatch.countDown();
+            // Hold lock until test completion
+            tearDownLatch.await();
+         }
+         catch (Exception e)
+         {
+            e.printStackTrace();
+         }
+         finally
+         {
+            if (writer != null)
+            {
+               writer.close();
+            }
+         }
+         
+      }
+      
+   }
+
+}




More information about the jboss-cvs-commits mailing list