Skip to content

Commit

Permalink
Revert formatting churn
Browse files Browse the repository at this point in the history
  • Loading branch information
holly-cummins committed Dec 19, 2024
1 parent cba74bf commit 289c8c4
Showing 1 changed file with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,30 @@ public void close() throws Exception {
@Override
public <T> Optional<T> getConfigValue(String key, Class<T> type) {

if (false && new Exception().getStackTrace().length > 100) {
// TODO expensive, awkward
System.out.println("HOLLY averting infinite loop " + key);
new Exception().printStackTrace();
return Optional.empty();
} else {
ClassLoader old = Thread.currentThread()
.getContextClassLoader();
try {
// TODO this infinite loops, check that assumption
// we are assuming here that the the classloader has been initialised with some kind of different provider that does not infinite loop.
Thread.currentThread()
.setContextClassLoader(classLoader);
if (classLoader == ConfigProvider.class.getClassLoader()) {
return ConfigProvider.getConfig(classLoader)
.getOptionalValue(key, type);
} else {
//the config is in an isolated CL
//we need to extract it via reflection
//this is pretty yuck, but I don't really see a solution
Class<?> configProviderClass = classLoader.loadClass(ConfigProvider.class.getName());
Method getConfig = configProviderClass.getMethod("getConfig", ClassLoader.class);
Object config = getConfig.invoke(null, classLoader);
return (Optional<T>) getConfig.getReturnType()
.getMethod("getOptionalValue", String.class, Class.class)
.invoke(config, key, type);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
Thread.currentThread()
.setContextClassLoader(old);
ClassLoader old = Thread.currentThread()
.getContextClassLoader();
try {
// we are assuming here that the the classloader has been initialised with some kind of different provider that does not infinite loop.
Thread.currentThread()
.setContextClassLoader(classLoader);
if (classLoader == ConfigProvider.class.getClassLoader()) {
return ConfigProvider.getConfig(classLoader)
.getOptionalValue(key, type);
} else {
//the config is in an isolated CL
//we need to extract it via reflection
//this is pretty yuck, but I don't really see a solution
Class<?> configProviderClass = classLoader.loadClass(ConfigProvider.class.getName());
Method getConfig = configProviderClass.getMethod("getConfig", ClassLoader.class);
Object config = getConfig.invoke(null, classLoader);
return (Optional<T>) getConfig.getReturnType()
.getMethod("getOptionalValue", String.class, Class.class)
.invoke(config, key, type);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}

Expand Down Expand Up @@ -113,14 +104,10 @@ public Object instance(Class<?> clazz, Annotation... qualifiers) {
}

Class<?> cdi = classLoader.loadClass("jakarta.enterprise.inject.spi.CDI");
Object instance = cdi.getMethod("current")
.invoke(null);
Object instance = cdi.getMethod("current").invoke(null);
Method selectMethod = cdi.getMethod("select", Class.class, Annotation[].class);
Object cdiInstance = selectMethod.invoke(instance, actualClass, qualifiers);
return selectMethod.getReturnType()
.getMethod("get")
.invoke(cdiInstance);

return selectMethod.getReturnType().getMethod("get").invoke(cdiInstance);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 289c8c4

Please sign in to comment.