[exo-jcr-commits] exo-jcr SVN: r4614 - in jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl: util/io and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Jul 8 04:01:54 EDT 2011


Author: areshetnyak
Date: 2011-07-08 04:01:53 -0400 (Fri, 08 Jul 2011)
New Revision: 4614

Modified:
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/config/RepositoryServiceConfigurationImpl.java
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/DirectoryHelper.java
Log:
EXOJCR-1132 : The problem with backup configuration in RepositoryServiceConfigurationImpl.retain() on OS Windows was fixed.

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/config/RepositoryServiceConfigurationImpl.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/config/RepositoryServiceConfigurationImpl.java	2011-07-08 04:01:54 UTC (rev 4613)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/config/RepositoryServiceConfigurationImpl.java	2011-07-08 08:01:53 UTC (rev 4614)
@@ -26,6 +26,7 @@
 import org.exoplatform.services.jcr.config.ConfigurationPersister;
 import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
 import org.exoplatform.services.jcr.config.RepositoryServiceConfiguration;
+import org.exoplatform.services.jcr.impl.util.io.DirectoryHelper;
 import org.exoplatform.services.naming.InitialContextInitializer;
 import org.jibx.runtime.BindingDirectory;
 import org.jibx.runtime.IBindingFactory;
@@ -175,9 +176,16 @@
             File sourceConfig = new File(filePath.toURI());
             SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
             File backUp = new File(sourceConfig.getAbsoluteFile() + "_" + format.format(new Date()));
-            if (!PrivilegedFileHelper.renameTo(sourceConfig, backUp))
+            try
+            {
+               DirectoryHelper.renameFile(sourceConfig, backUp);
+            }
+            catch (IOException ioe)
+            {
                throw new RepositoryException("Can't back up configuration on path "
-                  + PrivilegedFileHelper.getAbsolutePath(sourceConfig));
+                        + PrivilegedFileHelper.getAbsolutePath(sourceConfig), ioe);
+            }
+
             saveStream = PrivilegedFileHelper.fileOutputStream(sourceConfig);
          }
 

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/DirectoryHelper.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/DirectoryHelper.java	2011-07-08 04:01:54 UTC (rev 4613)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/DirectoryHelper.java	2011-07-08 08:01:53 UTC (rev 4614)
@@ -288,4 +288,73 @@
          }
       }
    }
+
+   /**
+    * Rename file.
+    * 
+    * @param srcFile
+    *          source file
+    * @param dstFile
+    *          destination file 
+    * @throws IOException
+    *          if any exception occurred 
+    */
+   public static synchronized void renameFile(File srcFile, File dstFile) throws IOException
+   {
+      /* This is not atomic.  If the program crashes between the call to
+         delete() and the call to renameTo() then we're screwed, but I've
+         been unable to figure out how else to do this... */
+
+      if (PrivilegedFileHelper.exists(dstFile))
+         if (!PrivilegedFileHelper.delete(dstFile))
+            throw new IOException("Cannot delete " + dstFile);
+
+      // Rename the srcFile file to the new one. Unfortunately, the renameTo()
+      // method does not work reliably under some JVMs.  Therefore, if the
+      // rename fails, we manually rename by copying the srcFile file to the new one
+      if (!PrivilegedFileHelper.renameTo(srcFile, dstFile))
+      {
+         java.io.InputStream in = null;
+         java.io.OutputStream out = null;
+         byte buffer[] = null;
+         try
+         {
+            in = PrivilegedFileHelper.fileInputStream(srcFile);
+            out = PrivilegedFileHelper.fileOutputStream(dstFile);
+            // see if the buffer needs to be initialized. Initialization is
+            // only done on-demand since many VM's will never run into the renameTo
+            // bug and hence shouldn't waste 1K of mem for no reason.
+            if (buffer == null)
+            {
+               buffer = new byte[1024];
+            }
+            int len;
+            while ((len = in.read(buffer)) >= 0)
+            {
+               out.write(buffer, 0, len);
+            }
+
+            // delete the srcFile file.
+            PrivilegedFileHelper.delete(srcFile);
+         }
+         catch (IOException ioe)
+         {
+            IOException newExc = new IOException("Cannot rename " + srcFile + " to " + dstFile);
+            newExc.initCause(ioe);
+            throw newExc;
+         }
+         finally
+         {
+            if (in != null)
+            {
+               in.close();
+            }
+
+            if (out != null)
+            {
+               out.close();
+            }
+         }
+      }
+   }
 }



More information about the exo-jcr-commits mailing list