Skip to content

Commit

Permalink
Merge pull request #41544 from gsmet/curate-application-getOrCreate
Browse files Browse the repository at this point in the history
Rename confusing methods in CuratedApplication
  • Loading branch information
geoand authored Jul 1, 2024
2 parents 819095f + b4bbb8a commit 322d112
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private synchronized void firstStart() {
//this is a bit yuck, but we need replace the default
//exit handler in the runtime class loader
//TODO: look at implementing a common core classloader, that removes the need for this sort of crappy hack
curatedApplication.getBaseRuntimeClassLoader().loadClass(ApplicationLifecycleManager.class.getName())
curatedApplication.getOrCreateBaseRuntimeClassLoader().loadClass(ApplicationLifecycleManager.class.getName())
.getMethod("setDefaultExitCodeHandler", Consumer.class)
.invoke(null, new Consumer<Integer>() {
@Override
Expand Down Expand Up @@ -139,7 +139,8 @@ public void accept(Integer integer) {
ApplicationStateNotification.notifyStartupFailed(t);

if (RuntimeUpdatesProcessor.INSTANCE != null) {
Thread.currentThread().setContextClassLoader(curatedApplication.getBaseRuntimeClassLoader());
Thread.currentThread()
.setContextClassLoader(curatedApplication.getOrCreateBaseRuntimeClassLoader());
try {
if (!InitialConfigurator.DELAYED_HANDLER.isActivated()) {
Class<?> cl = Thread.currentThread().getContextClassLoader()
Expand Down Expand Up @@ -184,7 +185,7 @@ public synchronized void restartApp(Set<String> changedResources, ClassChangeInf
consoleContext.reset();
}
stop();
Timing.restart(curatedApplication.getAugmentClassLoader());
Timing.restart(curatedApplication.getOrCreateAugmentClassLoader());
deploymentProblem = null;
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Expand All @@ -206,7 +207,7 @@ public synchronized void restartApp(Set<String> changedResources, ClassChangeInf
}
if (!(rootCause instanceof BindException)) {
log.error("Failed to start quarkus", t);
Thread.currentThread().setContextClassLoader(curatedApplication.getAugmentClassLoader());
Thread.currentThread().setContextClassLoader(curatedApplication.getOrCreateAugmentClassLoader());
LoggingSetupRecorder.handleFailedStart();
}
}
Expand Down Expand Up @@ -255,19 +256,19 @@ public byte[] apply(String s, byte[] bytes) {
}, testSupport);

for (HotReplacementSetup service : ServiceLoader.load(HotReplacementSetup.class,
curatedApplication.getBaseRuntimeClassLoader())) {
curatedApplication.getOrCreateBaseRuntimeClassLoader())) {
hotReplacementSetups.add(service);
service.setupHotDeployment(processor);
processor.addHotReplacementSetup(service);
}
for (DeploymentFailedStartHandler service : ServiceLoader.load(DeploymentFailedStartHandler.class,
curatedApplication.getAugmentClassLoader())) {
curatedApplication.getOrCreateAugmentClassLoader())) {
processor.addDeploymentFailedStartHandler(new Runnable() {
@Override
public void run() {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(curatedApplication.getAugmentClassLoader());
Thread.currentThread().setContextClassLoader(curatedApplication.getOrCreateAugmentClassLoader());
service.handleFailedInitialStart();
} finally {
Thread.currentThread().setContextClassLoader(old);
Expand Down Expand Up @@ -365,7 +366,7 @@ public void accept(CuratedApplication o, Map<String, Object> params) {
//setup the dev mode thread pool for NIO
System.setProperty("java.nio.channels.DefaultThreadPool.threadFactory",
"io.quarkus.dev.io.NioThreadPoolThreadFactory");
Timing.staticInitStarted(o.getBaseRuntimeClassLoader(), false);
Timing.staticInitStarted(o.getOrCreateBaseRuntimeClassLoader(), false);
//https://github.com/quarkusio/quarkus/issues/9748
//if you have an app with all daemon threads then the app thread
//may be the only thread keeping the JVM alive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class IsolatedRemoteDevModeMain implements BiConsumer<CuratedApplication,

static RemoteDevClient createClient(CuratedApplication curatedApplication) {
ServiceLoader<RemoteDevClientProvider> providers = ServiceLoader.load(RemoteDevClientProvider.class,
curatedApplication.getAugmentClassLoader());
curatedApplication.getOrCreateAugmentClassLoader());
RemoteDevClient client = null;
for (RemoteDevClientProvider provider : providers) {
Optional<RemoteDevClient> opt = provider.getClient();
Expand Down Expand Up @@ -140,19 +140,19 @@ public byte[] apply(String s, byte[] bytes) {
}, null);

for (HotReplacementSetup service : ServiceLoader.load(HotReplacementSetup.class,
curatedApplication.getBaseRuntimeClassLoader())) {
curatedApplication.getOrCreateBaseRuntimeClassLoader())) {
hotReplacementSetups.add(service);
service.setupHotDeployment(processor);
processor.addHotReplacementSetup(service);
}
for (DeploymentFailedStartHandler service : ServiceLoader.load(DeploymentFailedStartHandler.class,
curatedApplication.getAugmentClassLoader())) {
curatedApplication.getOrCreateAugmentClassLoader())) {
processor.addDeploymentFailedStartHandler(new Runnable() {
@Override
public void run() {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(curatedApplication.getAugmentClassLoader());
Thread.currentThread().setContextClassLoader(curatedApplication.getOrCreateAugmentClassLoader());
service.handleFailedInitialStart();
} finally {
Thread.currentThread().setContextClassLoader(old);
Expand Down Expand Up @@ -198,7 +198,7 @@ public void close() {
@Override
public void accept(CuratedApplication o, Map<String, Object> o2) {
LoggingSetupRecorder.handleFailedStart(); //we are not going to actually run an app
Timing.staticInitStarted(o.getBaseRuntimeClassLoader(), false);
Timing.staticInitStarted(o.getOrCreateBaseRuntimeClassLoader(), false);
try {
curatedApplication = o;
Object potentialContext = o2.get(DevModeContext.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public byte[] apply(String s, byte[] bytes) {
}, testSupport);

for (HotReplacementSetup service : ServiceLoader.load(HotReplacementSetup.class,
curatedApplication.getBaseRuntimeClassLoader())) {
curatedApplication.getOrCreateBaseRuntimeClassLoader())) {
hotReplacementSetups.add(service);
service.setupHotDeployment(processor);
processor.addHotReplacementSetup(service);
Expand Down Expand Up @@ -107,7 +107,7 @@ public void close() {
public void accept(CuratedApplication o, Map<String, Object> params) {
System.setProperty("java.nio.channels.DefaultThreadPool.threadFactory",
"io.quarkus.dev.io.NioThreadPoolThreadFactory");
Timing.staticInitStarted(o.getBaseRuntimeClassLoader(), false);
Timing.staticInitStarted(o.getOrCreateBaseRuntimeClassLoader(), false);
try {
curatedApplication = o;
Object potentialContext = params.get(DevModeContext.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public synchronized void abort() {
Runnable prepare(ClassScanResult classScanResult, boolean reRunFailures, long runId, TestRunListener listener) {

var old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(testApplication.getAugmentClassLoader());
Thread.currentThread().setContextClassLoader(testApplication.getOrCreateAugmentClassLoader());
try {
synchronized (this) {
if (runner != null) {
Expand Down Expand Up @@ -84,7 +84,7 @@ public FilterResult apply(TestDescriptor testDescriptor) {
@Override
public void run() {
var old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(testApplication.getAugmentClassLoader());
Thread.currentThread().setContextClassLoader(testApplication.getOrCreateAugmentClassLoader());
try {
prepared.run();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class JBangAugmentorImpl implements BiConsumer<CuratedApplication, Map<St
@Override
public void accept(CuratedApplication curatedApplication, Map<String, Object> resultMap) {

QuarkusClassLoader classLoader = curatedApplication.getAugmentClassLoader();
QuarkusClassLoader classLoader = curatedApplication.getOrCreateAugmentClassLoader();

try (QuarkusClassLoader deploymentClassLoader = curatedApplication.createDeploymentClassLoader()) {
QuarkusBootstrap quarkusBootstrap = curatedApplication.getQuarkusBootstrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private BuildResult runAugment(boolean firstRun, Set<String> changedResources,
Class<? extends BuildItem>... finalOutputs) {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
QuarkusClassLoader classLoader = curatedApplication.getAugmentClassLoader();
QuarkusClassLoader classLoader = curatedApplication.getOrCreateAugmentClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
LaunchMode.set(launchMode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public StartupActionImpl(CuratedApplication curatedApplication, BuildResult buil
this.runtimeApplicationShutdownBuildItems = buildResult.consumeMulti(RuntimeApplicationShutdownBuildItem.class);

Map<String, byte[]> transformedClasses = extractTransformedClasses(buildResult);
QuarkusClassLoader baseClassLoader = curatedApplication.getBaseRuntimeClassLoader();
QuarkusClassLoader baseClassLoader = curatedApplication.getOrCreateBaseRuntimeClassLoader();
QuarkusClassLoader runtimeClassLoader;

//so we have some differences between dev and test mode here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public QuarkusBootstrap getQuarkusBootstrap() {
}

public Object runInAugmentClassLoader(String consumerName, Map<String, Object> params) {
return runInCl(consumerName, params, getAugmentClassLoader());
return runInCl(consumerName, params, getOrCreateAugmentClassLoader());
}

public CurationResult getCurationResult() {
Expand All @@ -99,7 +99,7 @@ public CurationResult getCurationResult() {

public AugmentAction createAugmentor() {
try {
Class<?> augmentor = getAugmentClassLoader().loadClass(AUGMENTOR);
Class<?> augmentor = getOrCreateAugmentClassLoader().loadClass(AUGMENTOR);
return (AugmentAction) augmentor.getConstructor(CuratedApplication.class).newInstance(this);
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -115,8 +115,8 @@ public AugmentAction createAugmentor() {
*/
public AugmentAction createAugmentor(String functionName, Map<String, Object> props) {
try {
Class<?> augmentor = getAugmentClassLoader().loadClass(AUGMENTOR);
Function<Object, List<?>> function = (Function<Object, List<?>>) getAugmentClassLoader()
Class<?> augmentor = getOrCreateAugmentClassLoader().loadClass(AUGMENTOR);
Function<Object, List<?>> function = (Function<Object, List<?>>) getOrCreateAugmentClassLoader()
.loadClass(functionName)
.getDeclaredConstructor()
.newInstance();
Expand Down Expand Up @@ -189,7 +189,7 @@ private void addCpElement(QuarkusClassLoader.Builder builder, ResolvedDependency
builder.addElement(element);
}

public synchronized QuarkusClassLoader getAugmentClassLoader() {
public synchronized QuarkusClassLoader getOrCreateAugmentClassLoader() {
if (augmentClassLoader == null) {
//first run, we need to build all the class loaders
QuarkusClassLoader.Builder builder = QuarkusClassLoader.builder(
Expand Down Expand Up @@ -226,6 +226,14 @@ public synchronized QuarkusClassLoader getAugmentClassLoader() {
return augmentClassLoader;
}

/**
* In most cases {@link #getOrCreateAugmentClassLoader()} should be used but this can be useful if you want to be able to
* get this instance without creating it (and so potentially get null if it doesn't exist).
*/
public QuarkusClassLoader getAugmentClassLoader() {
return augmentClassLoader;
}

/**
* creates the base runtime class loader.
*
Expand All @@ -235,7 +243,7 @@ public synchronized QuarkusClassLoader getAugmentClassLoader() {
* as each startup can generate new resources.
*
*/
public synchronized QuarkusClassLoader getBaseRuntimeClassLoader() {
public synchronized QuarkusClassLoader getOrCreateBaseRuntimeClassLoader() {
if (baseRuntimeClassLoader == null) {
QuarkusClassLoader.Builder builder = QuarkusClassLoader.builder(
"Quarkus Base Runtime ClassLoader: " + quarkusBootstrap.getMode() + getClassLoaderNameSuffix(),
Expand Down Expand Up @@ -304,6 +312,14 @@ public synchronized QuarkusClassLoader getBaseRuntimeClassLoader() {
return baseRuntimeClassLoader;
}

/**
* In most cases {@link #getOrCreateBaseRuntimeClassLoader()} should be used but this can be useful if you want to be able
* to get this instance without creating it (and so potentially get null if it doesn't exist).
*/
public QuarkusClassLoader getBaseRuntimeClassLoader() {
return baseRuntimeClassLoader;
}

private static boolean isHotReloadable(ResolvedDependency a, Set<Path> hotReloadPaths) {
for (Path p : a.getContentTree().getRoots()) {
if (hotReloadPaths.contains(p)) {
Expand All @@ -317,7 +333,7 @@ public QuarkusClassLoader createDeploymentClassLoader() {
//first run, we need to build all the class loaders
QuarkusClassLoader.Builder builder = QuarkusClassLoader
.builder("Deployment Class Loader: " + quarkusBootstrap.getMode() + getClassLoaderNameSuffix(),
getAugmentClassLoader(), false)
getOrCreateAugmentClassLoader(), false)
.addClassLoaderEventListeners(quarkusBootstrap.getClassLoaderEventListeners())
.setAssertionsEnabled(quarkusBootstrap.isAssertionsEnabled())
.setAggregateParentResources(true);
Expand Down Expand Up @@ -355,7 +371,7 @@ public String getClassLoaderNameSuffix() {
}

public QuarkusClassLoader createRuntimeClassLoader(Map<String, byte[]> resources, Map<String, byte[]> transformedClasses) {
return createRuntimeClassLoader(getBaseRuntimeClassLoader(), resources, transformedClasses);
return createRuntimeClassLoader(getOrCreateBaseRuntimeClassLoader(), resources, transformedClasses);
}

public QuarkusClassLoader createRuntimeClassLoader(ClassLoader base, Map<String, byte[]> resources,
Expand All @@ -366,7 +382,7 @@ public QuarkusClassLoader createRuntimeClassLoader(ClassLoader base, Map<String,
+ getClassLoaderNameSuffix()
+ " restart no:"
+ runtimeClassLoaderCount.getAndIncrement(),
getBaseRuntimeClassLoader(), false)
getOrCreateBaseRuntimeClassLoader(), false)
.setAssertionsEnabled(quarkusBootstrap.isAssertionsEnabled())
.setAggregateParentResources(true);
builder.setTransformedClasses(transformedClasses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class<? extend
// we need to write the Index to make it reusable from other parts of the testing infrastructure that run in different ClassLoaders
TestClassIndexer.writeIndex(testClassesIndex, testClassLocation, requiredTestClass);

Timing.staticInitStarted(curatedApplication.getBaseRuntimeClassLoader(),
Timing.staticInitStarted(curatedApplication.getOrCreateBaseRuntimeClassLoader(),
curatedApplication.getQuarkusBootstrap().isAuxiliaryApplication());
final Map<String, Object> props = new HashMap<>();
props.put(TEST_LOCATION, testClassLocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void accept(String s, String s2) {
// obtain the ID of the shared network - this needs to be done after the augmentation has been run
// or else we run into various ClassLoader problems
try {
Class<?> networkClass = curatedApplication.getAugmentClassLoader()
Class<?> networkClass = curatedApplication.getOrCreateAugmentClassLoader()
.loadClass("org.testcontainers.containers.Network");
Object sharedNetwork = networkClass.getField("SHARED").get(null);
networkId = (String) networkClass.getMethod("getId").invoke(sharedNetwork);
Expand Down

0 comments on commit 322d112

Please sign in to comment.