Remove InjectionFeature loading.

This commit is contained in:
Jeremy Wood 2023-03-25 00:02:48 -04:00
parent 376fc8db18
commit cbc3bace58
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
3 changed files with 4 additions and 55 deletions

View File

@ -165,7 +165,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
* Function to Register all the Events needed.
*/
private void registerEvents() {
// TODO add automatic listener registration through hk2
PluginManager pluginManager = getServer().getPluginManager();
getAllServices(Listener.class).forEach(listener -> pluginManager.registerEvents(listener, this));
}
@ -174,7 +173,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
* Register Multiverse-Core commands to Command Manager.
*/
private void registerCommands() {
// TODO add automatic command registration through hk2
var commandManager = serviceLocator.getService(MVCommandManager.class);
serviceLocator.getAllServices(MultiverseCommand.class).forEach(commandManager::registerCommand);
}
@ -194,7 +192,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
* Register all the destinations.
*/
private void registerDestinations() {
// TODO add automatic destination registration through hk2
var destinationsProvider = serviceLocator.getService(DestinationsProvider.class);
serviceLocator.getAllServices(Destination.class).forEach(destinationsProvider::registerDestination);
}

View File

@ -1,33 +0,0 @@
package com.onarandombox.MultiverseCore.inject;
import org.glassfish.hk2.api.ServiceLocator;
import org.jvnet.hk2.annotations.Contract;
/**
* Marker interface for injection features.
* <br/>
* Injection features are used to extend the functionality of the {@link PluginInjection} class. They are only used
* internally and should not be implemented by plugins.
*/
@Contract
public interface InjectionFeature {
/**
* Called prior to the eager loading of {@link AutoLoadedService}s.
* <br/>
* It's possible that performing injection in feature related services will cause {@link AutoLoadedService} instances to
* be created.
*
* @param pluginServiceLocator The service locator for the plugin.
*/
default void preServicesCreation(ServiceLocator pluginServiceLocator) {}
/**
* Called after the eager loading of {@link AutoLoadedService}s.
* <br/>
* All {@link AutoLoadedService} instances should be created by this point.
*
* @param pluginServiceLocator The service locator for the plugin.
*/
default void postServicesCreation(ServiceLocator pluginServiceLocator) {}
}

View File

@ -6,7 +6,6 @@ import com.onarandombox.MultiverseCore.inject.binder.ServerBinder;
import io.vavr.control.Try;
import org.bukkit.plugin.Plugin;
import org.glassfish.hk2.api.DynamicConfigurationService;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.hk2.internal.ServiceLocatorFactoryImpl;
@ -14,8 +13,6 @@ import org.glassfish.hk2.utilities.ClasspathDescriptorFileFinder;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* Provides methods to set up dependency injection for plugins.
* <br/>
@ -39,15 +36,9 @@ public final class PluginInjection {
public static Try<ServiceLocator> createServiceLocator(@NotNull PluginBinder<?> pluginBinder) {
var factory = new ServiceLocatorFactoryImpl();
var systemServiceLocator = createSystemServiceLocator(factory);
var features = systemServiceLocator
.mapTry(locator -> locator.getAllServices(InjectionFeature.class));
return systemServiceLocator
return createSystemServiceLocator(factory)
.flatMap(systemLocator -> createServerServiceLocator(factory, systemLocator))
.map(serverLocator -> new PluginInjection(pluginBinder, factory, serverLocator))
.flatMap(pluginInjection -> features.flatMap(pluginInjection::load));
.flatMap(serverLocator -> new PluginInjection(pluginBinder, factory, serverLocator).load());
}
/**
@ -90,16 +81,10 @@ public final class PluginInjection {
pluginServiceLocator = serviceLocatorFactory.create(plugin.getName(), serverServiceLocator);
}
private Try<ServiceLocator> load(List<InjectionFeature> features) {
private Try<ServiceLocator> load() {
return Try.runRunnable(() -> ServiceLocatorUtilities.bind(pluginServiceLocator, pluginBinder))
.flatMap(ignored -> populatePluginServiceLocator(pluginServiceLocator, plugin))
.andThenTry(() -> loadAncillaryServices(features));
}
private void loadAncillaryServices(List<InjectionFeature> features) throws MultiException {
features.forEach(feature -> feature.preServicesCreation(pluginServiceLocator));
pluginServiceLocator.getAllServices(AutoLoadedService.class);
features.forEach(feature -> feature.postServicesCreation(pluginServiceLocator));
.andThenTry(locator -> locator.getAllServices(AutoLoadedService.class));
}
@NotNull