107 lines
3.2 KiB
Java
107 lines
3.2 KiB
Java
package ru.kirillius.pf.sdn;
|
|
|
|
import lombok.Getter;
|
|
import org.eclipse.jetty.server.Server;
|
|
import ru.kirillius.pf.sdn.External.API.HEInfoProvider;
|
|
import ru.kirillius.pf.sdn.core.Auth.AuthManager;
|
|
import ru.kirillius.pf.sdn.core.Config;
|
|
import ru.kirillius.pf.sdn.core.Context;
|
|
import ru.kirillius.pf.sdn.core.ContextEventsHandler;
|
|
import ru.kirillius.pf.sdn.core.Networking.ASInfoService;
|
|
import ru.kirillius.pf.sdn.core.Networking.NetworkManager;
|
|
import ru.kirillius.pf.sdn.core.Plugin;
|
|
import ru.kirillius.pf.sdn.core.Subscription.SubscriptionManager;
|
|
import ru.kirillius.utils.logging.SystemLogger;
|
|
|
|
import java.io.Closeable;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class AppContext implements Context, Closeable {
|
|
|
|
protected final static String CTX = AppContext.class.getSimpleName();
|
|
|
|
public AppContext(File configFile) {
|
|
try {
|
|
config = Config.load(configFile);
|
|
} catch (IOException e) {
|
|
config = new Config();
|
|
try {
|
|
Config.store(config, configFile);
|
|
} catch (IOException ex) {
|
|
throw new RuntimeException(ex);
|
|
}
|
|
}
|
|
|
|
authManager = new AuthManager(this);
|
|
server = new Server();
|
|
ASInfoService = new ASInfoService();
|
|
ASInfoService.setProvider(new HEInfoProvider(this));
|
|
networkManager = new NetworkManager(this);
|
|
networkManager.getInputResources().add(config.getCustomResources());
|
|
subscriptionManager = new SubscriptionManager(this);
|
|
subscribe();
|
|
initPlugins();
|
|
}
|
|
|
|
private void initPlugins() {
|
|
config.getEnabledPlugins().forEach(pluginClass -> {
|
|
var plugin = Plugin.loadPlugin(pluginClass, this);
|
|
loadedPlugins.add(plugin);
|
|
});
|
|
}
|
|
|
|
private final List<Plugin<?>> loadedPlugins = new ArrayList<>();
|
|
|
|
private void subscribe() {
|
|
var eventsHandler = getEventsHandler();
|
|
eventsHandler.getSubscriptionsUpdateEvent().add(bundle -> {
|
|
var manager = getNetworkManager();
|
|
var inputResources = getNetworkManager().getInputResources();
|
|
inputResources.clear();
|
|
inputResources.add(config.getCustomResources());
|
|
inputResources.add(bundle);
|
|
manager.triggerUpdate();
|
|
});
|
|
}
|
|
|
|
|
|
@Getter
|
|
private final NetworkManager networkManager;
|
|
@Getter
|
|
private Config config;
|
|
@Getter
|
|
private final AuthManager authManager;
|
|
@Getter
|
|
private final Server server;
|
|
@Getter
|
|
private final ASInfoService ASInfoService;
|
|
@Getter
|
|
private final SubscriptionManager subscriptionManager;
|
|
|
|
@Override
|
|
public void close() throws IOException {
|
|
loadedPlugins.forEach(plugin -> {
|
|
try {
|
|
plugin.close();
|
|
} catch (IOException e) {
|
|
SystemLogger.error("Error closing plugin", CTX, e);
|
|
}
|
|
});
|
|
ASInfoService.close();
|
|
networkManager.close();
|
|
try {
|
|
server.stop();
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
@Getter
|
|
private final ContextEventsHandler EventsHandler = new ContextEventsHandler();
|
|
|
|
|
|
}
|