From ffcfef265e95056c2480a7b8c8ad83d645d19a9b Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Thu, 17 Jul 2025 13:57:31 -0400 Subject: [PATCH] feat: enhance PluginFactory with properties management - Added a properties field to the PluginFactory.Builder for managing plugin properties. - Introduced methods `withProperties` and `withProperty` to set multiple or single properties respectively. - Updated the Plugin constructor to accept properties and ensure they are properly initialized. - return a value for the pluign_root_id property This commit enhances the PluginFactory by allowing users to define and manage properties for plugin instances. The new methods facilitate easier configuration of properties, improving the flexibility and usability of the PluginFactory. This change is expected to streamline the process of setting up plugins with specific configurations, ultimately leading to a more robust implementation of the proxy-wasm functionality. Signed-off-by: Hiram Chirino --- .../roastedroot/proxywasm/PluginFactory.java | 51 ++++++++++++++++++- .../proxywasm/internal/Plugin.java | 11 ++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java index d1cdc16..00fdee0 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java @@ -8,7 +8,9 @@ import com.dylibso.chicory.wasm.WasmModule; import io.roastedroot.proxywasm.internal.ProxyWasm; import java.net.URI; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.function.Function; @@ -82,6 +84,7 @@ final class Builder { private SharedQueueHandler sharedQueueHandler; private SharedDataHandler sharedDataHandler; private boolean shared; + private HashMap, byte[]> properties = new HashMap<>(); /** * Private constructor for the Builder. @@ -105,6 +108,42 @@ public PluginFactory.Builder withName(String name) { return this; } + /** + * Sets the properties for this plugin instance. + * + * @param properties A map where keys are the property names expected by the WASM module, + * and values are the corresponding byte arrays. + * @return this {@code Builder} instance for method chaining. + */ + public PluginFactory.Builder withProperties(Map, byte[]> properties) { + this.properties = new HashMap<>(properties); + return this; + } + + /** + * Sets a single property for this plugin instance. + * + * @param key The key of the property to set. + * @param value The value of the property to set. + * @return this {@code Builder} instance for method chaining. + */ + public PluginFactory.Builder withProperty(List key, byte[] value) { + this.properties.put(key, value); + return this; + } + + /** + * Sets a single property for this plugin instance. + * + * @param key The key of the property to set. + * @param value The value of the property to set. + * @return this {@code Builder} instance for method chaining. + */ + public PluginFactory.Builder withProperty(List key, String value) { + this.properties.put(key, bytes(value)); + return this; + } + /** * Registers foreign (host-provided) functions that can be called by the WASM plugin. * These functions allow the plugin to interact with the host environment beyond the standard @@ -362,6 +401,15 @@ public PluginFactory build() { SharedDataHandler sharedDataHandler = this.sharedDataHandler; boolean shared = this.shared; + HashMap, byte[]> properties = new HashMap<>(); + if (this.properties != null) { + for (Map.Entry, byte[]> listEntry : this.properties.entrySet()) { + byte[] value = listEntry.getValue(); + value = Arrays.copyOf(value, value.length); + this.properties.put(List.copyOf(listEntry.getKey()), value); + } + } + return new PluginFactory() { @Override @@ -389,7 +437,8 @@ public Plugin create() throws Exception { pluginConfig, metricsHandler, sharedQueueHandler, - sharedDataHandler); + sharedDataHandler, + properties); } }; } diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java index f693d60..c345a68 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java @@ -6,6 +6,7 @@ import static io.roastedroot.proxywasm.internal.WellKnownHeaders.PATH; import static io.roastedroot.proxywasm.internal.WellKnownHeaders.SCHEME; import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_NAME; +import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_ROOT_ID; import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_VM_ID; import io.roastedroot.proxywasm.ForeignFunction; @@ -50,7 +51,8 @@ public Plugin( byte[] pluginConfig, MetricsHandler metricsHandler, SharedQueueHandler sharedQueueHandler, - SharedDataHandler sharedDataHandler) + SharedDataHandler sharedDataHandler, + HashMap, byte[]> properties) throws StartException { Objects.requireNonNull(proxyWasm); this.name = Objects.requireNonNullElse(name, "default"); @@ -61,12 +63,12 @@ public Plugin( this.vmConfig = vmConfig; this.pluginConfig = pluginConfig; this.logger = Objects.requireNonNullElse(logger, LogHandler.DEFAULT); - ; this.metricsHandler = Objects.requireNonNullElse(metricsHandler, MetricsHandler.DEFAULT); this.sharedQueueHandler = Objects.requireNonNullElse(sharedQueueHandler, SharedQueueHandler.DEFAULT); this.sharedDataHandler = Objects.requireNonNullElse(sharedDataHandler, SharedDataHandler.DEFAULT); + this.properties = Objects.requireNonNull(properties); this.wasm = proxyWasm; this.wasm.setPluginHandler(new HandlerImpl()); @@ -138,7 +140,7 @@ public void close() { private Runnable cancelTick; private final HashMap foreignFunctions; private byte[] funcCallData = new byte[0]; - private final HashMap, byte[]> properties = new HashMap<>(); + private final HashMap, byte[]> properties; class HandlerImpl extends ChainedHandler { @@ -170,6 +172,9 @@ public byte[] getProperty(List path) throws WasmException { if (PLUGIN_VM_ID.equals(path)) { return bytes(name); } + if (PLUGIN_ROOT_ID.equals(path)) { + return bytes(name); + } if (PLUGIN_NAME.equals(path)) { return bytes(name); }