Yair Zaslavsky has uploaded a new change for review. Change subject: 3. [WIP] core: Introducing configuration loader ......................................................................
3. [WIP] core: Introducing configuration loader This class is responsible for loading , ordering and resolving conflicts of configurations Change-Id: I182904177ec088e62b35bde870ec79725fabc4e4 Signed-off-by: Yair Zaslvsky <yzasl...@redhat.com> --- A backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ConfigurationLoader.java A backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ExtensionManagerKeys.java 2 files changed, 140 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/65/24365/1 diff --git a/backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ConfigurationLoader.java b/backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ConfigurationLoader.java new file mode 100644 index 0000000..cef221e --- /dev/null +++ b/backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ConfigurationLoader.java @@ -0,0 +1,118 @@ +package org.ovirt.engine.core.extensions.mgr; + +import static java.util.Arrays.sort; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is responsible for loading the required {@code Configuration} in order to create an extension. It holds + * the logic of ordering and solving conflicts during loading the configuration + * + */ +public class ConfigurationLoader { + + private static final Logger log = LoggerFactory.getLogger(ConfigurationLoader.class); + private static volatile ConfigurationLoader instance = null; + private Set<String> configurationNames = new HashSet<String>(); + private List<Configuration> orderedConfigurations = new ArrayList<>(); + private List<String> orderedConfigurationNames = new ArrayList<>(); + private boolean firstDetected = false; + private boolean lastDetected = false; + + public static ConfigurationLoader getInstance() { + if (instance == null) { + synchronized (ConfigurationLoader.class) { + if (instance == null) { + instance = new ConfigurationLoader(); + } + } + } + return instance; + } + + public List<Configuration> loadFiles(File directory) throws ConfigurationException { + // Check that the folder that contains the configuration files exists: + if (!directory.exists()) { + throw new ConfigurationException( + "The directory \"" + directory.getAbsolutePath() + "\" containing the configuration files doesn't " + + + "exist."); + } + + // The order of the files inside the directory is relevant, as the objects are created in the same order that + // the files are processed, so it is better to sort them so that objects will always be created in the same + // order regardless of how the filesystem decides to store the entries of the directory: + File[] files = directory.listFiles(); + if (files != null) { + sort(files); + for (File file : files) { + if (file.getName().endsWith(".conf")) { + loadFile(file); + } + } + } + return orderedConfigurations; + } + + private void loadFile(File file) throws ConfigurationException { + // Load the configuration file: + Configuration config = null; + try { + config = Configuration.loadFile(file); + } catch (IOException exception) { + throw new ConfigurationException( + "Can't load object configuration file \"" + file.getAbsolutePath() + "\".", + exception); + } + + String name = config.getString(ExtensionManagerKeys.name.getKeyName()); + + // Check if the object has been explicitly disabled, if it is then return immediately: + Boolean enabled = config.getBoolean(ExtensionManagerKeys.enabled.getKeyName(), false); + if (!enabled.booleanValue()) { + log.debug("The extension \"{}\" is disabled", name); + return; + } + + // Each manager/extension has to have a unique name, in case there is an extension/manager loaded with the same + // name an error should be logged. + if (configurationNames.contains(name)) { + log.error("An extension with the name \"{}\" is already loaded", name); + return; + + } + configurationNames.add(name); + String position = config.getString(ExtensionManagerKeys.position.getKeyName()); + if (position != null) { + if (position.equalsIgnoreCase("first")) { + if (!firstDetected) { + orderedConfigurationNames.add(0, name); + orderedConfigurations.add(0, config); + firstDetected = true; + } else { + log.error("There is already an extension that is marked as the first. \"{}\" cannot be set as first", + name); + } + } else if (position.equals("last")) { + if (!lastDetected) { + orderedConfigurationNames.add(name); + orderedConfigurations.add(config); + lastDetected = true; + } else { + log.error("There is already an extension that is marked as the last. \"{}\" cannot be set as last", + name); + } + } + } + } + + +} diff --git a/backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ExtensionManagerKeys.java b/backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ExtensionManagerKeys.java new file mode 100644 index 0000000..cb58b9d --- /dev/null +++ b/backend/manager/modules/extension-manager/src/main/java/org/ovirt/engine/core/extensions/mgr/ExtensionManagerKeys.java @@ -0,0 +1,22 @@ +package org.ovirt.engine.core.extensions.mgr; + +public enum ExtensionManagerKeys { + + position("ovirt.engine.extension.position"), + name("ovirt.engine.extension.name"), + service("ovirt.engine.extension.service"), + module("ovirt.engine.extension.module"), + enabled("ovirt.engine.extension.enabled"); + + + private String keyName; + + private ExtensionManagerKeys(String keyName) { + this.keyName = keyName; + } + + public String getKeyName() { + return keyName; + } + +} -- To view, visit http://gerrit.ovirt.org/24365 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I182904177ec088e62b35bde870ec79725fabc4e4 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Yair Zaslavsky <yzasl...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches