[ofbiz-framework] branch trunk updated: Improved: Enforce noninstantiability to GroovyUtil Class. (#180)

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: Improved: Enforce noninstantiability to GroovyUtil Class. (#180)

surajk
This is an automated email from the ASF dual-hosted git repository.

surajk 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 846e5f0  Improved: Enforce noninstantiability to GroovyUtil Class. (#180)
846e5f0 is described below

commit 846e5f0dedcb4a723b0d0e37b1bf2d9102fcb0b3
Author: Suraj Khurana <[hidden email]>
AuthorDate: Sat Jun 6 10:03:33 2020 +0530

    Improved: Enforce noninstantiability to GroovyUtil Class. (#180)
   
    (OFBIZ-11778)
   
    Made class as final, moved default constructor and renamed private data members as per naming convention best practices.
---
 .../java/org/apache/ofbiz/base/util/GroovyUtil.java   | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java
index 5355c68..b9cefd5 100644
--- a/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java
+++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java
@@ -41,13 +41,14 @@ import groovy.lang.Script;
  * Groovy Utilities.
  *
  */
-public class GroovyUtil {
+public final class GroovyUtil {
 
     private static final String MODULE = GroovyUtil.class.getName();
+    private static final UtilCache<String, Class<?>> PARSED_SCRIPTS = UtilCache.createUtilCache("script.GroovyLocationParsedCache", 0, 0, false);
+    private static final GroovyClassLoader GROOVY_CLASS_LOADER;
 
-    private static final UtilCache<String, Class<?>> parsedScripts = UtilCache.createUtilCache("script.GroovyLocationParsedCache", 0, 0, false);
+    private GroovyUtil() { }
 
-    private static final GroovyClassLoader groovyScriptClassLoader;
     static {
         GroovyClassLoader groovyClassLoader = null;
         String scriptBaseClass = UtilProperties.getPropertyValue("groovy", "scriptBaseClass");
@@ -56,7 +57,7 @@ public class GroovyUtil {
             conf.setScriptBaseClass(scriptBaseClass);
             groovyClassLoader = new GroovyClassLoader(GroovyUtil.class.getClassLoader(), conf);
         }
-        groovyScriptClassLoader = groovyClassLoader;
+        GROOVY_CLASS_LOADER = groovyClassLoader;
     }
 
     /**
@@ -139,14 +140,14 @@ public class GroovyUtil {
 
     public static Class<?> getScriptClassFromLocation(String location) throws GeneralException {
         try {
-            Class<?> scriptClass = parsedScripts.get(location);
+            Class<?> scriptClass = PARSED_SCRIPTS.get(location);
             if (scriptClass == null) {
                 URL scriptUrl = FlexibleLocation.resolveLocation(location);
                 if (scriptUrl == null) {
                     throw new GeneralException("Script not found at location [" + location + "]");
                 }
                 scriptClass = parseClass(scriptUrl.openStream(), location);
-                Class<?> scriptClassCached = parsedScripts.putIfAbsent(location, scriptClass);
+                Class<?> scriptClassCached = PARSED_SCRIPTS.putIfAbsent(location, scriptClass);
                 if (scriptClassCached == null) { // putIfAbsent returns null if the class is added to the cache
                     if (Debug.verboseOn()) {
                         Debug.logVerbose("Cached Groovy script at: " + location, MODULE);
@@ -177,8 +178,8 @@ public class GroovyUtil {
      */
     private static Class<?> parseClass(InputStream in, String location) throws IOException {
         String classText = UtilIO.readString(in);
-        if (groovyScriptClassLoader != null) {
-            return groovyScriptClassLoader.parseClass(classText, location);
+        if (GROOVY_CLASS_LOADER != null) {
+            return GROOVY_CLASS_LOADER.parseClass(classText, location);
         } else {
             GroovyClassLoader classLoader = new GroovyClassLoader();
             Class<?> klass = classLoader.parseClass(classText, location);
@@ -214,6 +215,4 @@ public class GroovyUtil {
                 ? script.run()
                 : script.invokeMethod(methodName, new Object[] { context });
     }
-
-    private GroovyUtil() {}
 }