Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move extension settings reading into the BaseExtension #205

Merged
merged 6 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/main/java/org/opensearch/sdk/BaseExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.opensearch.sdk;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

Expand All @@ -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);
}
}

dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
/**
* take an ExtensionSettings object and set it directly
*/
protected BaseExtension(ExtensionSettings settings) {
this.settings = settings;
}

@Override
public ExtensionSettings getExtensionSettings() {
return this.settings;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,22 @@ 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.
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
* 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
public List<ExtensionRestHandler> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExtensionRestHandler> getExtensionRestHandlers() {
Expand Down