Skip to content

Commit

Permalink
Remove unused classes
Browse files Browse the repository at this point in the history
Signed-off-by: John Niang <[email protected]>
  • Loading branch information
JohnNiang committed Jan 25, 2024
1 parent cbb9b28 commit a382057
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.PluginRuntimeException;
Expand All @@ -22,6 +23,7 @@
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.Exceptions;
Expand Down Expand Up @@ -59,18 +61,26 @@ public ApplicationContext create(String pluginId) {
log.debug("Preparing to create application context for plugin {}", pluginId);
var pluginWrapper = pluginManager.getPlugin(pluginId);

var sw = new StopWatch("CreateApplicationContextFor" + pluginId);

sw.start("Create");
var context = new PluginApplicationContext(pluginId);
context.registerShutdownHook();
context.setParent(pluginManager.getSharedContext());

var classLoader = pluginWrapper.getPluginClassLoader();
var resourceLoader = new DefaultResourceLoader(classLoader);
context.setResourceLoader(resourceLoader);
sw.stop();

sw.start("LoadPropertySources");
var mutablePropertySources = context.getEnvironment().getPropertySources();

resolvePropertySources(pluginId, resourceLoader)
.forEach(mutablePropertySources::addLast);
sw.stop();

sw.start("RegisterBeans");
var beanFactory = context.getBeanFactory();
context.registerBean(AggregatedRouterFunction.class);
beanFactory.registerSingleton("pluginWrapper", pluginWrapper);
Expand All @@ -84,26 +94,13 @@ public ApplicationContext create(String pluginId) {
});

rootContext.getBeanProvider(ReactiveExtensionClient.class)
.ifAvailable(client -> {
.ifUnique(client -> {
var reactiveSettingFetcher = new DefaultReactiveSettingFetcher(client, pluginId);
var settingFetcher = new DefaultSettingFetcher(reactiveSettingFetcher);
beanFactory.registerSingleton("reactiveSettingFetcher", reactiveSettingFetcher);
beanFactory.registerSingleton("settingFetcher", settingFetcher);
});

var classNames = pluginManager.getExtensionClassNames(pluginId);
classNames.stream()
.map(className -> {
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
throw new PluginRuntimeException(String.format("""
Failed to load class %s for plugin %s.\
""", className, pluginId), e);
}
})
.forEach(context::register);

rootContext.getBeanProvider(PluginRequestMappingHandlerMapping.class)
.ifAvailable(handlerMapping -> {
var handlerMappingManager =
Expand All @@ -130,12 +127,32 @@ public ApplicationContext create(String pluginId) {
pluginRouterFunctionManager
);
});
sw.stop();

sw.start("LoadComponents");
var classNames = pluginManager.getExtensionClassNames(pluginId);
classNames.stream()
.map(className -> {
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
throw new PluginRuntimeException(String.format("""
Failed to load class %s for plugin %s.\
""", className, pluginId), e);
}
})
.forEach(clazzName -> context.registerBean(clazzName));
sw.stop();
log.debug("Created application context for plugin {}", pluginId);
log.debug("Refreshing application context for plugin {}", pluginId);

log.debug("Refreshing application context for plugin {}", pluginId);
sw.start("Refresh");
context.refresh();
sw.stop();
log.debug("Refreshed application context for plugin {}", pluginId);
if (log.isDebugEnabled()) {
log.debug("\n{}", sw.prettyPrint(TimeUnit.MILLISECONDS));
}
return context;
}

Expand Down Expand Up @@ -295,7 +312,7 @@ private List<PropertySource<?>> resolvePropertySources(String pluginId,
// resolve default config
Stream.of(
CLASSPATH_URL_PREFIX + "/config.yaml",
CLASSPATH_URL_PREFIX + "/config.yaml"
CLASSPATH_URL_PREFIX + "/config.yml"
)
.map(resourceLoader::getResource)
.forEach(resource -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@
import org.pf4j.PluginFactory;
import org.pf4j.PluginLoader;
import org.pf4j.PluginRepository;
import org.pf4j.PluginState;
import org.pf4j.PluginStateEvent;
import org.pf4j.PluginStateListener;
import org.pf4j.PluginStatusProvider;
import org.pf4j.PluginWrapper;
import org.springframework.context.ApplicationContext;
import org.springframework.data.util.Lazy;
import run.halo.app.infra.SystemVersionSupplier;
import run.halo.app.plugin.event.HaloPluginStartedEvent;
import run.halo.app.plugin.event.HaloPluginStoppedEvent;

/**
* PluginManager to hold the main ApplicationContext.
Expand Down Expand Up @@ -162,28 +157,4 @@ public ApplicationContext getSharedContext() {
return sharedContext.get();
}

private class PluginStoppedEventAdapter implements PluginStateListener {

@Override
public void pluginStateChanged(PluginStateEvent event) {
if (!PluginState.STOPPED.equals(event.getPluginState())) {
return;
}
var pluginWrapper = event.getPlugin();
rootContext.publishEvent(new HaloPluginStoppedEvent(event.getSource(), pluginWrapper));
}
}

private class PluginStartedEventAdapter implements PluginStateListener {

@Override
public void pluginStateChanged(PluginStateEvent event) {
if (!PluginState.STARTED.equals(event.getPluginState())) {
return;
}
// Indicate the state is started.
var pluginWrapper = event.getPlugin();
rootContext.publishEvent(new HaloPluginStartedEvent(event.getSource(), pluginWrapper));
}
}
}

0 comments on commit a382057

Please sign in to comment.