[ofbiz-framework] branch release17.12 updated: Fixed: Replace java.io.File::renameTo by java.nio.file.Files::move (OFBIZ-12063)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch release17.12 updated: Fixed: Replace java.io.File::renameTo by java.nio.file.Files::move (OFBIZ-12063)

jleroux@apache.org
This is an automated email from the ASF dual-hosted git repository.

jleroux pushed a commit to branch release17.12
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release17.12 by this push:
     new f6450a0  Fixed: Replace java.io.File::renameTo by java.nio.file.Files::move (OFBIZ-12063)
f6450a0 is described below

commit f6450a01c5573146077be8e73e55d1d782bddbde
Author: Jacques Le Roux <[hidden email]>
AuthorDate: Tue Nov 24 18:39:12 2020 +0100

    Fixed: Replace java.io.File::renameTo by java.nio.file.Files::move (OFBIZ-12063)
   
    This is a bug because the behaviour is not assured on Windows for instance. So
    it's impossible to correctly develop and test.
   
    As explained at
    https://docs.oracle.com/javase/8/docs/api/java/io/File.html#renameTo-java.io.File-
   
    <<Many aspects of the behavior of this method are inherently platform-dependent:
    The rename operation might not be able to move a file from one filesystem to
    another, it might not be atomic, and it might not succeed if a file with the
    destination abstract pathname already exists. The return value should always be
    checked to make sure that the rename operation was successful.
    Note that the Files class defines the move method to move or rename a file in a
    platform independent manner.>>
   
    So the expression
      file.renameTo(file1)
    used in 5 Application Product Groovy scripts can be easily replaced by
      Path source = file.toPath()
      Files.move(source, source.resolveSibling(filenameToUse))
    that is working on all platforms as explained at
    https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#
    move-java.nio.file.Path-java.nio.file.Path-java.nio.file.CopyOption...-
---
 .../groovyScripts/catalog/category/EditCategory.groovy      |  7 ++++++-
 .../catalog/config/EditProductConfigItemContent.groovy      |  7 +++++--
 .../catalog/imagemanagement/ImageUpload.groovy              |  8 +++++---
 .../catalog/imagemanagement/SetDefaultImage.groovy          | 13 ++++++-------
 .../groovyScripts/catalog/product/EditProductContent.groovy | 10 ++++++----
 5 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/applications/product/groovyScripts/catalog/category/EditCategory.groovy b/applications/product/groovyScripts/catalog/category/EditCategory.groovy
index 7d8edd0..6b78774 100644
--- a/applications/product/groovyScripts/catalog/category/EditCategory.groovy
+++ b/applications/product/groovyScripts/catalog/category/EditCategory.groovy
@@ -17,6 +17,9 @@
  * under the License.
  */
 
+import java.nio.file.Files
+import java.nio.file.Path
+
 import org.apache.ofbiz.base.util.*
 import org.apache.ofbiz.base.util.string.*
 import org.apache.ofbiz.entity.util.EntityUtilProperties
@@ -108,7 +111,9 @@ if (fileType) {
             } catch (Exception e) {
                 Debug.logError(e, "error deleting existing file (not neccessarily a problem)", module)
             }
-            file.renameTo(file1)
+            
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             Debug.logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/config/EditProductConfigItemContent.groovy b/applications/product/groovyScripts/catalog/config/EditProductConfigItemContent.groovy
index f2f9a02..608d077 100644
--- a/applications/product/groovyScripts/catalog/config/EditProductConfigItemContent.groovy
+++ b/applications/product/groovyScripts/catalog/config/EditProductConfigItemContent.groovy
@@ -17,9 +17,11 @@
  * under the License.
  */
 
+import java.nio.file.Files
+import java.nio.file.Path
+
 import org.apache.ofbiz.base.util.*
 import org.apache.ofbiz.base.util.string.*
-import org.apache.ofbiz.entity.*
 import org.apache.ofbiz.entity.util.EntityUtilProperties
 
 module = "EditProductConfigItemContent.groovy"
@@ -118,7 +120,8 @@ if (fileType) {
             } catch (Exception e) {
                 Debug.logError(e, "error deleting existing file (not neccessarily a problem)", module)
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             Debug.logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/imagemanagement/ImageUpload.groovy b/applications/product/groovyScripts/catalog/imagemanagement/ImageUpload.groovy
index 6a348fd..574864d 100644
--- a/applications/product/groovyScripts/catalog/imagemanagement/ImageUpload.groovy
+++ b/applications/product/groovyScripts/catalog/imagemanagement/ImageUpload.groovy
@@ -17,12 +17,13 @@
  * under the License.
  */
 
-import org.apache.ofbiz.entity.*
+import java.nio.file.Files
+import java.nio.file.Path
+
 import org.apache.ofbiz.base.util.*
 import org.apache.ofbiz.base.util.string.*
 import org.apache.ofbiz.entity.util.EntityUtilProperties
 import org.apache.ofbiz.product.image.ScaleImage
-import org.apache.ofbiz.entity.condition.*
 
 module = "ImageUpload.groovy"
 
@@ -121,7 +122,8 @@ if (fileType) {
             } catch (Exception e) {
                 Debug.logError(e, "error deleting existing file (not neccessarily a problem)", module)
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             Debug.logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/imagemanagement/SetDefaultImage.groovy b/applications/product/groovyScripts/catalog/imagemanagement/SetDefaultImage.groovy
index 85fda41..2dae8b8 100644
--- a/applications/product/groovyScripts/catalog/imagemanagement/SetDefaultImage.groovy
+++ b/applications/product/groovyScripts/catalog/imagemanagement/SetDefaultImage.groovy
@@ -19,16 +19,14 @@
 
 import java.awt.image.BufferedImage
 import java.awt.image.RenderedImage
-import java.io.File
-import java.util.List
+import java.nio.file.Files
+import java.nio.file.Path
 
 import javax.imageio.ImageIO
 
-import org.apache.ofbiz.entity.*
-import org.apache.ofbiz.entity.util.EntityUtil
-import org.apache.ofbiz.entity.util.EntityUtilProperties
 import org.apache.ofbiz.base.util.*
 import org.apache.ofbiz.base.util.string.*
+import org.apache.ofbiz.entity.util.EntityUtilProperties
 import org.apache.ofbiz.product.image.ScaleImage
 
 module = "SetDefaultImage.groovy"
@@ -148,7 +146,7 @@ if (fileType) {
                     File[] files = targetDir.listFiles()
                     for(File file : files) {
                         if (file.isFile() && !file.getName().equals(defaultFileName)) file.delete()
-                    }
+                    }
                 // Images aren't ordered by productId (${location}/${viewtype}/${sizetype}/${id}) !!! BE CAREFUL !!!
                 } else {
                     File[] files = targetDir.listFiles()
@@ -159,7 +157,8 @@ if (fileType) {
             } catch (Exception e) {
                 Debug.logError(e, "error deleting existing file (not neccessarily a problem)", module)
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             Debug.logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/product/EditProductContent.groovy b/applications/product/groovyScripts/catalog/product/EditProductContent.groovy
index 8c3873e..938687b 100644
--- a/applications/product/groovyScripts/catalog/product/EditProductContent.groovy
+++ b/applications/product/groovyScripts/catalog/product/EditProductContent.groovy
@@ -17,7 +17,9 @@
  * under the License.
  */
 
-import org.apache.ofbiz.entity.*
+import java.nio.file.Files
+import java.nio.file.Path
+
 import org.apache.ofbiz.base.util.*
 import org.apache.ofbiz.base.util.string.*
 import org.apache.ofbiz.entity.util.EntityUtilProperties
@@ -127,7 +129,7 @@ if (fileType) {
                         } else if(file.isFile() && "original".equals(fileType) && !file.getName().equals(defaultFileName)) {
                             file.delete()
                         }
-                    }
+                    }
                 // Images aren't ordered by productId (${location}/${viewtype}/${sizetype}/${id}) !!! BE CAREFUL !!!
                 } else {
                     File[] files = targetDir.listFiles()
@@ -138,7 +140,8 @@ if (fileType) {
             } catch (Exception e) {
                 Debug.logError(e, "error deleting existing file (not neccessarily a problem)", module)
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             Debug.logError(e, module)
         }
@@ -151,7 +154,6 @@ if (fileType) {
             if ("original".equals(fileType)) {
                 context.delegator = delegator
                 result = ScaleImage.scaleImageInAllSize(context, filenameToUse, "main", "0")
-
                 if (result.containsKey("responseMessage") && "success".equals(result.get("responseMessage"))) {
                     imgMap = result.get("imageUrlMap")
                     imgMap.each() { key, value ->