From 6d20251875181d3f814e55e5a473d15c0baf0301 Mon Sep 17 00:00:00 2001 From: mloufra Date: Fri, 28 Oct 2022 04:21:22 +0000 Subject: [PATCH 1/6] move extensionSetting to BaseExtension Signed-off-by: mloufra --- .../org/opensearch/sdk/BaseExtension.java | 33 ++++++++++++++++++- .../helloworld/HelloWorldExtension.java | 32 +----------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/BaseExtension.java b/src/main/java/org/opensearch/sdk/BaseExtension.java index dc863fef..9d22f198 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,40 @@ public abstract class BaseExtension implements Extension { protected ThreadPool threadPool; /** - * Empty constructor to fulfill abstract class requirements + * Optional classpath-relative path to a yml file containing extension settings. + */ + protected static final String EXTENSION_SETTINGS_PATH = "/sample/helloworld-settings.yml"; + + /** + * The extension settings include a name, host address, and port. + */ + protected ExtensionSettings settings; + + /** + * Instantiate this extension, initializing the connection settings and REST actions. */ protected BaseExtension() { + try { + this.settings = initializeSettings(); + } catch (IOException e) { + throw new ExceptionInInitializerError(e); + } + } + /** + * 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. + */ + protected 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; } /** 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..4f0e5bb5 100644 --- a/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java +++ b/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java @@ -30,26 +30,12 @@ */ public class HelloWorldExtension extends BaseExtension { - /** - * Optional classpath-relative path to a yml file containing extension settings. - */ - 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. */ public HelloWorldExtension() { super(); - try { - this.settings = initializeSettings(); - } catch (IOException e) { - throw new ExceptionInInitializerError(e); - } + } @Override @@ -62,22 +48,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. * From 36f56ae58ae5fca1a02be6637114b57ff0b207cc Mon Sep 17 00:00:00 2001 From: mloufra Date: Mon, 31 Oct 2022 17:02:27 +0000 Subject: [PATCH 2/6] address comment Signed-off-by: mloufra --- .../org/opensearch/sdk/BaseExtension.java | 35 ++++++++----------- .../helloworld/HelloWorldExtension.java | 20 ++++++----- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/BaseExtension.java b/src/main/java/org/opensearch/sdk/BaseExtension.java index 9d22f198..d3456dac 100644 --- a/src/main/java/org/opensearch/sdk/BaseExtension.java +++ b/src/main/java/org/opensearch/sdk/BaseExtension.java @@ -36,40 +36,35 @@ public abstract class BaseExtension implements Extension { protected ThreadPool threadPool; /** - * Optional classpath-relative path to a yml file containing extension settings. + * The extension settings include a name, host address, and port. */ - protected static final String EXTENSION_SETTINGS_PATH = "/sample/helloworld-settings.yml"; + private ExtensionSettings settings; /** - * The extension settings include a name, host address, and port. + * Empty constructor to fulfill abastract class requirement */ - protected ExtensionSettings settings; + protected BaseExtension() { + + } /** * Instantiate this extension, initializing the connection settings and REST actions. + * @throws IOException on failure to load settings. */ - protected BaseExtension() { + protected BaseExtension(String path) throws IOException { try { - this.settings = initializeSettings(); + 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); } } - /** - * 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. - */ - protected 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; + @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 4f0e5bb5..adcf47d5 100644 --- a/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java +++ b/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java @@ -31,16 +31,20 @@ public class HelloWorldExtension extends BaseExtension { /** - * Instantiate this extension, initializing the connection settings and REST actions. + * Optional classpath-relative path to a yml file containing extension settings. */ - public HelloWorldExtension() { - super(); + private static final String EXTENSION_SETTINGS_PATH = "/sample/helloworld-settings.yml"; - } - - @Override - public ExtensionSettings getExtensionSettings() { - return this.settings; + /** + * + * 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. + * + * @throws IOException on failure to load settings. + */ + public HelloWorldExtension() throws IOException { + super(EXTENSION_SETTINGS_PATH); } @Override From 97add3253b59a8cec0297d7393152daf156009cf Mon Sep 17 00:00:00 2001 From: mloufra Date: Mon, 31 Oct 2022 17:07:24 +0000 Subject: [PATCH 3/6] address comment Signed-off-by: mloufra --- src/main/java/org/opensearch/sdk/BaseExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/sdk/BaseExtension.java b/src/main/java/org/opensearch/sdk/BaseExtension.java index d3456dac..0feda599 100644 --- a/src/main/java/org/opensearch/sdk/BaseExtension.java +++ b/src/main/java/org/opensearch/sdk/BaseExtension.java @@ -53,10 +53,10 @@ protected BaseExtension() { */ protected BaseExtension(String path) throws IOException { 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."); } + this.settings = Extension.readSettingsFromYaml(path); } catch (IOException e) { throw new ExceptionInInitializerError(e); } From 426aef4ae4d89485b9d5345b2fa20ffd7fd09118 Mon Sep 17 00:00:00 2001 From: mloufra Date: Mon, 31 Oct 2022 17:30:16 +0000 Subject: [PATCH 4/6] fix spotless error Signed-off-by: mloufra --- src/main/java/org/opensearch/sdk/BaseExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/sdk/BaseExtension.java b/src/main/java/org/opensearch/sdk/BaseExtension.java index 0feda599..33586c4d 100644 --- a/src/main/java/org/opensearch/sdk/BaseExtension.java +++ b/src/main/java/org/opensearch/sdk/BaseExtension.java @@ -56,7 +56,7 @@ protected BaseExtension(String path) throws IOException { if (settings == null || settings.getHostAddress() == null || settings.getHostPort() == null) { throw new IOException("Failed to initialize Extension settings. No port bound."); } - this.settings = Extension.readSettingsFromYaml(path); + this.settings = Extension.readSettingsFromYaml(path); } catch (IOException e) { throw new ExceptionInInitializerError(e); } From 3c8ea4f52cc9b51d6e86cb6dd1b9c5c4466aa586 Mon Sep 17 00:00:00 2001 From: mloufra Date: Mon, 31 Oct 2022 17:49:38 +0000 Subject: [PATCH 5/6] fix setting initialization error Signed-off-by: mloufra --- src/main/java/org/opensearch/sdk/BaseExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/sdk/BaseExtension.java b/src/main/java/org/opensearch/sdk/BaseExtension.java index 33586c4d..d3456dac 100644 --- a/src/main/java/org/opensearch/sdk/BaseExtension.java +++ b/src/main/java/org/opensearch/sdk/BaseExtension.java @@ -53,10 +53,10 @@ protected BaseExtension() { */ protected BaseExtension(String path) throws IOException { 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."); } - this.settings = Extension.readSettingsFromYaml(path); } catch (IOException e) { throw new ExceptionInInitializerError(e); } From 51a0e2317fdeebd2f7eaf3751e0f8e9a621d1595 Mon Sep 17 00:00:00 2001 From: mloufra Date: Wed, 2 Nov 2022 18:12:00 +0000 Subject: [PATCH 6/6] add new constructor in BaseExtensions Signed-off-by: mloufra --- .../org/opensearch/sdk/BaseExtension.java | 19 +++++++++---------- .../helloworld/HelloWorldExtension.java | 5 ++--- .../sdk/ExtensionsRunnerForTest.java | 6 +----- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/BaseExtension.java b/src/main/java/org/opensearch/sdk/BaseExtension.java index d3456dac..9e50eb8f 100644 --- a/src/main/java/org/opensearch/sdk/BaseExtension.java +++ b/src/main/java/org/opensearch/sdk/BaseExtension.java @@ -38,20 +38,12 @@ public abstract class BaseExtension implements Extension { /** * The extension settings include a name, host address, and port. */ - private ExtensionSettings settings; - - /** - * Empty constructor to fulfill abastract class requirement - */ - protected BaseExtension() { - - } + private final ExtensionSettings settings; /** * Instantiate this extension, initializing the connection settings and REST actions. - * @throws IOException on failure to load settings. */ - protected BaseExtension(String path) throws IOException { + protected BaseExtension(String path) { try { this.settings = Extension.readSettingsFromYaml(path); if (settings == null || settings.getHostAddress() == null || settings.getHostPort() == null) { @@ -62,6 +54,13 @@ protected BaseExtension(String path) throws IOException { } } + /** + * 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 adcf47d5..270767d0 100644 --- a/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java +++ b/src/main/java/org/opensearch/sdk/sample/helloworld/HelloWorldExtension.java @@ -36,14 +36,13 @@ public class HelloWorldExtension extends BaseExtension { private static final String EXTENSION_SETTINGS_PATH = "/sample/helloworld-settings.yml"; /** - * + * 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. * - * @throws IOException on failure to load settings. */ - public HelloWorldExtension() throws IOException { + public HelloWorldExtension() { super(EXTENSION_SETTINGS_PATH); } 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() {