[ofbiz-framework] branch trunk 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 trunk 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 trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


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

commit 1be480b68c52b8c8403b7c935b35981e943f5057
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 47d690f..ed7529d 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
@@ -106,7 +109,9 @@ if (fileType) {
             } catch (Exception e) {
                 logError(e, "error deleting existing file (not neccessarily a problem)")
             }
-            file.renameTo(file1)
+            
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/config/EditProductConfigItemContent.groovy b/applications/product/groovyScripts/catalog/config/EditProductConfigItemContent.groovy
index 034be19..0d284d9 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
 
 // make the image file formats
@@ -116,7 +118,8 @@ if (fileType) {
             } catch (Exception e) {
                 logError(e, "error deleting existing file (not neccessarily a problem)")
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/imagemanagement/ImageUpload.groovy b/applications/product/groovyScripts/catalog/imagemanagement/ImageUpload.groovy
index 6a18c67..602b997 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.*
 
 context.nowTimestampString = UtilDateTime.nowTimestamp().toString()
 
@@ -119,7 +120,8 @@ if (fileType) {
             } catch (Exception e) {
                 logError(e, "error deleting existing file (not neccessarily a problem)")
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/imagemanagement/SetDefaultImage.groovy b/applications/product/groovyScripts/catalog/imagemanagement/SetDefaultImage.groovy
index 446e74c..2dfb38b 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
 
 context.nowTimestampString = UtilDateTime.nowTimestamp().toString()
@@ -146,7 +144,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()
@@ -157,7 +155,8 @@ if (fileType) {
             } catch (Exception e) {
                 logError(e, "error deleting existing file (not neccessarily a problem)")
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             logError(e, module)
         }
diff --git a/applications/product/groovyScripts/catalog/product/EditProductContent.groovy b/applications/product/groovyScripts/catalog/product/EditProductContent.groovy
index 71f3c0c..f4b1fe8 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
@@ -125,7 +127,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()
@@ -136,7 +138,8 @@ if (fileType) {
             } catch (Exception e) {
                 logError(e, "error deleting existing file (not neccessarily a problem)")
             }
-            file.renameTo(file1)
+            Path source = file.toPath()
+            Files.move(source, source.resolveSibling(filenameToUse))
         } catch (Exception e) {
             logError(e, module)
         }
@@ -149,7 +152,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 ->