mirror of https://github.com/halo-dev/halo
Restore context ClassLoader management during plugin lifecycle
parent
6992f1788c
commit
81fc98f8b0
|
@ -70,11 +70,6 @@ public class DefaultPluginApplicationContextFactory implements PluginApplication
|
||||||
var pluginWrapper = pluginManager.getPlugin(pluginId);
|
var pluginWrapper = pluginManager.getPlugin(pluginId);
|
||||||
var classLoader = pluginWrapper.getPluginClassLoader();
|
var classLoader = pluginWrapper.getPluginClassLoader();
|
||||||
|
|
||||||
// Set the context ClassLoader to the plugin ClassLoader to ensure that
|
|
||||||
// any class loading operations performed by the context (e.g., initializing
|
|
||||||
// bean definitions, loading class resources during static initialization)
|
|
||||||
// use the correct ClassLoader.
|
|
||||||
Thread.currentThread().setContextClassLoader(classLoader);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Manually creating a BeanFactory and setting the plugin's ClassLoader is necessary
|
* Manually creating a BeanFactory and setting the plugin's ClassLoader is necessary
|
||||||
|
@ -189,7 +184,18 @@ public class DefaultPluginApplicationContextFactory implements PluginApplication
|
||||||
log.debug("Refreshing application context for plugin {}", pluginId);
|
log.debug("Refreshing application context for plugin {}", pluginId);
|
||||||
sw.start("Refresh");
|
sw.start("Refresh");
|
||||||
|
|
||||||
context.refresh();
|
// Set the context ClassLoader to the plugin ClassLoader to ensure that
|
||||||
|
// any class loading operations performed by the context (e.g., initializing
|
||||||
|
// bean definitions, loading class resources during static initialization)
|
||||||
|
// use the correct ClassLoader.
|
||||||
|
var previous = Thread.currentThread().getContextClassLoader();
|
||||||
|
try {
|
||||||
|
Thread.currentThread().setContextClassLoader(classLoader);
|
||||||
|
context.refresh();
|
||||||
|
} finally {
|
||||||
|
// reset the class loader to previous one to prevent resource leak
|
||||||
|
Thread.currentThread().setContextClassLoader(previous);
|
||||||
|
}
|
||||||
sw.stop();
|
sw.stop();
|
||||||
log.debug("Refreshed application context for plugin {}", pluginId);
|
log.debug("Refreshed application context for plugin {}", pluginId);
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
|
|
|
@ -27,9 +27,11 @@ public class SpringPlugin extends Plugin {
|
||||||
public void start() {
|
public void start() {
|
||||||
log.info("Preparing starting plugin {}", pluginContext.getName());
|
log.info("Preparing starting plugin {}", pluginContext.getName());
|
||||||
var pluginId = pluginContext.getName();
|
var pluginId = pluginContext.getName();
|
||||||
|
var previous = Thread.currentThread().getContextClassLoader();
|
||||||
try {
|
try {
|
||||||
// initialize context
|
// initialize context
|
||||||
this.context = contextFactory.create(pluginId);
|
this.context = contextFactory.create(pluginId);
|
||||||
|
Thread.currentThread().setContextClassLoader(this.context.getClassLoader());
|
||||||
log.info("Application context {} for plugin {} is created", this.context, pluginId);
|
log.info("Application context {} for plugin {} is created", this.context, pluginId);
|
||||||
|
|
||||||
var pluginOpt = context.getBeanProvider(Plugin.class)
|
var pluginOpt = context.getBeanProvider(Plugin.class)
|
||||||
|
@ -55,13 +57,17 @@ public class SpringPlugin extends Plugin {
|
||||||
this.stop();
|
this.stop();
|
||||||
// propagate exception to invoker.
|
// propagate exception to invoker.
|
||||||
throw t;
|
throw t;
|
||||||
|
} finally {
|
||||||
|
Thread.currentThread().setContextClassLoader(previous);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
var previous = Thread.currentThread().getContextClassLoader();
|
||||||
try {
|
try {
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
|
Thread.currentThread().setContextClassLoader(context.getClassLoader());
|
||||||
log.info("Before publishing plugin stopping event for plugin {}",
|
log.info("Before publishing plugin stopping event for plugin {}",
|
||||||
pluginContext.getName());
|
pluginContext.getName());
|
||||||
context.publishEvent(new SpringPluginStoppingEvent(this, this));
|
context.publishEvent(new SpringPluginStoppingEvent(this, this));
|
||||||
|
@ -74,6 +80,7 @@ public class SpringPlugin extends Plugin {
|
||||||
log.info("Stopped {} for plugin {}", this.delegate, pluginContext.getName());
|
log.info("Stopped {} for plugin {}", this.delegate, pluginContext.getName());
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
Thread.currentThread().setContextClassLoader(previous);
|
||||||
if (context instanceof ConfigurableApplicationContext configurableContext) {
|
if (context instanceof ConfigurableApplicationContext configurableContext) {
|
||||||
log.info("Closing plugin context for plugin {}", pluginContext.getName());
|
log.info("Closing plugin context for plugin {}", pluginContext.getName());
|
||||||
configurableContext.close();
|
configurableContext.close();
|
||||||
|
|
Loading…
Reference in New Issue