Skip to content

Commit

Permalink
GROOVY-11459: configurable hashing algorithm (port for 4_0_X)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulk-asert committed Nov 4, 2024
1 parent ba56432 commit 1ae3e9a
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/main/java/groovy/lang/GroovyClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -274,11 +275,7 @@ private GroovyCodeSource createCodeSource(PrivilegedAction<GroovyCodeSource> act
* @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() {
Expand Down Expand Up @@ -314,11 +311,7 @@ public Class parseClass(final GroovyCodeSource codeSource, boolean shouldCacheSo
// 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) {
Expand All @@ -342,11 +335,7 @@ private String genSourceCacheKey(GroovyCodeSource codeSource) {
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) {
Expand Down Expand Up @@ -1233,4 +1222,27 @@ public void call(final SourceUnit source, final GeneratorContext context, final
}
}
}

/**
* 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);
}
}
}

0 comments on commit 1ae3e9a

Please sign in to comment.