Skip to content

Commit aa3ffb5

Browse files
authored
fix(jans-auth-server): external libraries unavaiable from Java Interception Script #11377 (#11568)
* feat(jans-core): added diagnostic to java compiler Signed-off-by: YuriyZ <[email protected]> * feat(jans-core): made jit java compiler verbose Signed-off-by: YuriyZ <[email protected]> * fix(jans-core): fixed jit compiler classpath Signed-off-by: YuriyZ <[email protected]> * fix(jans-core): one more fix for jit compiler classpath Signed-off-by: YuriyZ <[email protected]> --------- Signed-off-by: YuriyZ <[email protected]>
1 parent 56d8ea8 commit aa3ffb5

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

jans-core/script/src/main/java/io/jans/service/custom/script/jit/SimpleJavaCompiler.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ public static String getClasspath() {
116116
sb.append(url.toString()).append(File.pathSeparator);
117117
}
118118

119-
classpath = sb.toString();
120-
119+
classpath = fixClasspath(sb.toString());
121120
}
122121
return classpath;
123122
}
@@ -132,14 +131,17 @@ private <T> Class<? extends T> compile0(Class<T> superClass, String source) {
132131
final List<JavaFileObject> compilationUnits = Collections.singletonList(new SourceFile(toUri("Generated"), source));
133132
final FileManager fileManager = new FileManager(standardJavaFileManager);
134133
final StringWriter output = new StringWriter();
135-
final JavaCompiler.CompilationTask task = compiler.getTask(output, fileManager, diagnostics, Arrays.asList("-g", "-proc:none", "-classpath", getClasspath()), null, compilationUnits);
134+
final String localClasspath = getClasspath();
135+
final JavaCompiler.CompilationTask task = compiler.getTask(output, fileManager, diagnostics, Arrays.asList("-g", "-verbose", "-proc:none", "-classpath", localClasspath), null, compilationUnits);
136136

137137
if (!task.call()) {
138138
log.error("Compilation diagnostics:");
139139
for (Diagnostic<? extends JavaFileObject> diag : diagnostics.getDiagnostics()) {
140140
log.error(diag.getMessage(Locale.getDefault()));
141141
}
142142

143+
log.error("Full classpath: {}", localClasspath);
144+
143145
throw new IllegalArgumentException("Compilation failed:\n" + output + "\n Source code: \n" + source);
144146
}
145147

@@ -152,6 +154,48 @@ private <T> Class<? extends T> compile0(Class<T> superClass, String source) {
152154
throw new IllegalArgumentException("Compilation yielded an unexpected number of classes: " + classCount);
153155
}
154156

157+
/**
158+
* Cleans a raw classpath string by:
159+
* 1. Removing any leading/trailing single quotes.
160+
* 2. Stripping out “jar:file:///” and “file:/” URI prefixes.
161+
* 3. Removing “!/” suffixes from JAR‐in‐WAR entries.
162+
* 4. Eliminating any literal newline characters.
163+
*
164+
* After this, each entry is a plain filesystem path, separated by File.pathSeparator.
165+
*
166+
* @param rawClasspath the unprocessed classpath (e.g. containing "jar:file://…!/:" or "file:/…" fragments)
167+
* @return a cleaned classpath suitable for passing to JavaCompiler or ClassLoader
168+
*/
169+
public static String fixClasspath(String rawClasspath) {
170+
if (rawClasspath == null) {
171+
return null;
172+
}
173+
174+
String cp = rawClasspath;
175+
176+
// Remove any newline or carriage‐return characters
177+
// (we only want one long line with proper path separators)
178+
cp = cp.replaceAll("[\\r\\n]", "");
179+
180+
// Remove “!/jar:file://”
181+
cp = cp.replaceAll("!/:jar", "");
182+
cp = cp.replaceAll(":jar:", ":");
183+
184+
// Remove “file:/” prefixes
185+
// This turns "file:/opt/jetty/…" into "/opt/jetty/…"
186+
cp = cp.replaceAll("file:/+", "/");
187+
188+
// If there are any accidental duplicate path‐separators,
189+
// collapse them. For Unix/Linux, File.pathSeparator is ":"
190+
String sep = File.pathSeparator;
191+
String doubleSep = sep + sep;
192+
while (cp.contains(doubleSep)) {
193+
cp = cp.replace(doubleSep, sep);
194+
}
195+
196+
return cp;
197+
}
198+
155199
/**
156200
* Represents a source file whose contents is loaded from a String
157201
*/

0 commit comments

Comments
 (0)