diff --git a/src/main/java/org/opensearch/sdk/BaseExtension.java b/src/main/java/org/opensearch/sdk/BaseExtension.java index dc863fef..9e50eb8f 100644 --- a/src/main/java/org/opensearch/sdk/BaseExtension.java +++ b/src/main/java/org/opensearch/sdk/BaseExtension.java @@ -9,6 +9,7 @@ package org.opensearch.sdk; +import java.io.IOException; import java.util.Collection; import java.util.Collections; @@ -35,10 +36,34 @@ public abstract class BaseExtension implements Extension { protected ThreadPool threadPool; /** - * Empty constructor to fulfill abstract class requirements + * The extension settings include a name, host address, and port. */ - protected BaseExtension() { + private final ExtensionSettings settings; + /** + * Instantiate this extension, initializing the connection settings and REST actions. + */ + protected BaseExtension(String path) { + try { + this.settings = Extension.readSettingsFromYaml(path); + if (settings == null || settings.getHostAddress() == null || settings.getHostPort() == null) { + throw new IOException("Failed to initialize Extension settings. No port bound."); + } + } catch (IOException e) { + throw new ExceptionInInitializerError(e); + } + } + + /** + * take an ExtensionSettings object and set it directly + */ + protected BaseExtension(ExtensionSettings settings) { + this.settings = settings; + } + + @Override + public ExtensionSettings getExtensionSettings() { + return this.settings; } /** diff --git a/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java b/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java index c423aad5..270767d0 100644 --- a/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java +++ b/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java @@ -35,26 +35,15 @@ public class HelloWorldExtension extends BaseExtension { */ private static final String EXTENSION_SETTINGS_PATH = "/sample/helloworld-settings.yml"; - /** - * The extension settings include a name, host address, and port. - */ - private ExtensionSettings settings; - /** * Instantiate this extension, initializing the connection settings and REST actions. + * The Extension must provide its settings to the ExtensionsRunner. + * These may be optionally read from a YAML file on the class path. + * Or you may directly instantiate with the ExtensionSettings constructor. + * */ public HelloWorldExtension() { - super(); - try { - this.settings = initializeSettings(); - } catch (IOException e) { - throw new ExceptionInInitializerError(e); - } - } - - @Override - public ExtensionSettings getExtensionSettings() { - return this.settings; + super(EXTENSION_SETTINGS_PATH); } @Override @@ -62,22 +51,6 @@ public List getExtensionRestHandlers() { return List.of(new RestHelloAction()); } - /** - * The Extension must provide its settings to the ExtensionsRunner. - * These may be optionally read from a YAML file on the class path. - * Or you may directly instantiate with the ExtensionSettings constructor. - * - * @return This extension's settings. - * @throws IOException on failure to load settings. - */ - private static ExtensionSettings initializeSettings() throws IOException { - ExtensionSettings settings = Extension.readSettingsFromYaml(EXTENSION_SETTINGS_PATH); - if (settings == null || settings.getHostAddress() == null || settings.getHostPort() == null) { - throw new IOException("Failed to initialize Extension settings. No port bound."); - } - return settings; - } - /** * Entry point to execute an extension. * diff --git a/src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java b/src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java index 4e366996..5dd9695f 100644 --- a/src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java +++ b/src/test/java/org/opensearch/sdk/ExtensionsRunnerForTest.java @@ -15,11 +15,7 @@ public class ExtensionsRunnerForTest extends ExtensionsRunner { * @throws IOException if the runner failed to read settings or API. */ public ExtensionsRunnerForTest() throws IOException { - super(new BaseExtension() { - @Override - public ExtensionSettings getExtensionSettings() { - return new ExtensionSettings("sample-extension", "127.0.0.1", "4532"); - } + super(new BaseExtension(new ExtensionSettings("sample-extension", "127.0.0.1", "4532")) { @Override public List getExtensionRestHandlers() {