Skip to content

Commit

Permalink
WIP ClassLoader fix for SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
kstich committed Jul 26, 2023
1 parent 6b7d4d5 commit 92af1be
Showing 1 changed file with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,39 @@ public final class EndpointRuleSet implements FromSourceLocation, ToNode, ToSmit
private static final String PARAMETERS = "parameters";
private static final String RULES = "rules";

private static boolean loaded = false;
private static final Map<String, Parameter> BUILT_INS = new HashMap<>();
private static final Map<String, FunctionDefinition> FUNCTIONS = new HashMap<>();
private static final List<AuthSchemeValidator> AUTH_SCHEME_VALIDATORS = new ArrayList<>();

static {
// Provide an explicit classloader, as the thread's classloader may be different in cases
// where a model is loaded in extraneous contexts, like a build tool.
// TODO Change how the classloader is configured.
for (EndpointRuleSetExtension extension : ServiceLoader.load(EndpointRuleSetExtension.class,
EndpointRuleSet.class.getClassLoader())
) {
System.out.println(extension.getClass().getCanonicalName());
String name;
for (Parameter builtIn : extension.getBuiltIns()) {
name = builtIn.getBuiltIn().get();
if (BUILT_INS.containsKey(name)) {
throw new RuntimeException("Attempted to load a duplicate built-in parameter: " + name);
}
BUILT_INS.put(name, builtIn);
}

for (FunctionDefinition functionDefinition : extension.getLibraryFunctions()) {
name = functionDefinition.getId();
if (FUNCTIONS.containsKey(name)) {
throw new RuntimeException("Attempted to load a duplicate library function: " + name);
}
FUNCTIONS.put(name, functionDefinition);
}

AUTH_SCHEME_VALIDATORS.addAll(extension.getAuthSchemeValidators());
}
}

private final Parameters parameters;
private final List<Rule> rules;
private final SourceLocation sourceLocation;
Expand Down Expand Up @@ -84,7 +112,6 @@ public static Builder builder() {
* @return the created EndpointRuleSet.
*/
public static EndpointRuleSet fromNode(Node node) throws RuleError {
loadExtensions();
return RuleError.context("when parsing endpoint ruleset", () -> {
ObjectNode objectNode = node.expectObjectNode("The root of a ruleset must be an object");

Expand Down Expand Up @@ -195,35 +222,6 @@ public String toString() {
return builder.toString();
}

private static void loadExtensions() {
if (loaded) {
return;
}
loaded = true;

for (EndpointRuleSetExtension extension : ServiceLoader.load(EndpointRuleSetExtension.class)) {
String name;
for (Parameter builtIn : extension.getBuiltIns()) {
name = builtIn.getBuiltIn().get();
if (BUILT_INS.containsKey(name)) {
throw new RuntimeException("Attempted to load a duplicate built-in parameter: " + name);
}
BUILT_INS.put(name, builtIn);
}

for (FunctionDefinition functionDefinition : extension.getLibraryFunctions()) {
name = functionDefinition.getId();
if (FUNCTIONS.containsKey(name)) {
throw new RuntimeException("Attempted to load a duplicate library function: " + name);
}
FUNCTIONS.put(name, functionDefinition);
}

AUTH_SCHEME_VALIDATORS.addAll(extension.getAuthSchemeValidators());
}
}


/**
* Returns true if a built-in of the provided name has been registered.
*
Expand Down

0 comments on commit 92af1be

Please sign in to comment.