This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 1ae3e9aac2 GROOVY-11459: configurable hashing algorithm (port for
4_0_X)
1ae3e9aac2 is described below
commit 1ae3e9aac20873056eec210530205a342f436ef1
Author: Paul King <[email protected]>
AuthorDate: Mon Nov 4 15:25:39 2024 +1000
GROOVY-11459: configurable hashing algorithm (port for 4_0_X)
---
src/main/java/groovy/lang/GroovyClassLoader.java | 42 +++++++++++++++---------
1 file changed, 27 insertions(+), 15 deletions(-)
diff --git a/src/main/java/groovy/lang/GroovyClassLoader.java
b/src/main/java/groovy/lang/GroovyClassLoader.java
index 397eafd9e7..64a4b33571 100644
--- a/src/main/java/groovy/lang/GroovyClassLoader.java
+++ b/src/main/java/groovy/lang/GroovyClassLoader.java
@@ -106,6 +106,7 @@ public class GroovyClassLoader extends URLClassLoader {
private String sourceEncoding;
private Boolean recompile;
private static final AtomicInteger scriptNameCounter = new
AtomicInteger(1_000_000); // 1,000,000 avoids conflicts with names from the
GroovyShell
+ private static final String HASH_ALGORITHM =
System.getProperty("groovy.cache.hashing.algorithm", "md5");
static {
registerAsParallelCapable();
@@ -274,11 +275,7 @@ public class GroovyClassLoader extends URLClassLoader {
* @return the main class defined in the given script
*/
public Class parseClass(String text) throws CompilationFailedException {
- try {
- return parseClass(text, "Script_" +
EncodingGroovyMethods.md5(text) + ".groovy");
- } catch (NoSuchAlgorithmException e) {
- throw new GroovyBugError("Failed to generate md5", e); // should
never happen
- }
+ return parseClass(text, "Script_" + genEncodingString(text) +
".groovy");
}
public synchronized String generateScriptName() {
@@ -314,11 +311,7 @@ public class GroovyClassLoader extends URLClassLoader {
// and avoid occupying Permanent Area/Metaspace repeatedly
String cacheKey = genSourceCacheKey(codeSource);
- return sourceCache.getAndPut(
- cacheKey,
- key -> doParseClass(codeSource),
- shouldCacheSource
- );
+ return sourceCache.getAndPut(cacheKey, key ->
doParseClass(codeSource), shouldCacheSource);
}
private String genSourceCacheKey(GroovyCodeSource codeSource) {
@@ -342,11 +335,7 @@ public class GroovyClassLoader extends URLClassLoader {
strToDigest.append("name:").append(codeSource.getName());
}
- try {
- return EncodingGroovyMethods.md5(strToDigest);
- } catch (NoSuchAlgorithmException e) {
- throw new GroovyRuntimeException(e); // should never reach here!
- }
+ return genEncodingString(strToDigest);
}
private Class doParseClass(GroovyCodeSource codeSource) {
@@ -1233,4 +1222,27 @@ public class GroovyClassLoader extends URLClassLoader {
}
}
}
+
+ /**
+ * Generates an encoded string based on the specified characters and the
defined encoding algorithm.
+ * Supported algorithms currently are "md5" and sha256".
+ * An exception is throw for an unknown algorithm or if the JVM doesn't
support the algorithm.
+ *
+ * @param chars The characters to encode.
+ * @return The encoded string.
+ */
+ public String genEncodingString(CharSequence chars) {
+ try {
+ switch(HASH_ALGORITHM) {
+ case "md5":
+ return EncodingGroovyMethods.md5(chars);
+ case "sha256":
+ return EncodingGroovyMethods.sha256(chars);
+ default:
+ throw new IllegalStateException("Unknown hash algorithm");
+ }
+ } catch (NoSuchAlgorithmException e) {
+ throw new GroovyRuntimeException(e);
+ }
+ }
}