Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -82,6 +84,7 @@ final class Builder {
private SharedQueueHandler sharedQueueHandler;
private SharedDataHandler sharedDataHandler;
private boolean shared;
private HashMap<List<String>, byte[]> properties = new HashMap<>();

/**
* Private constructor for the Builder.
Expand All @@ -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<List<String>, 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<String> 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<String> 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
Expand Down Expand Up @@ -362,6 +401,15 @@ public PluginFactory build() {
SharedDataHandler sharedDataHandler = this.sharedDataHandler;
boolean shared = this.shared;

HashMap<List<String>, byte[]> properties = new HashMap<>();
if (this.properties != null) {
for (Map.Entry<List<String>, 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
Expand Down Expand Up @@ -389,7 +437,8 @@ public Plugin create() throws Exception {
pluginConfig,
metricsHandler,
sharedQueueHandler,
sharedDataHandler);
sharedDataHandler,
properties);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,8 @@ public Plugin(
byte[] pluginConfig,
MetricsHandler metricsHandler,
SharedQueueHandler sharedQueueHandler,
SharedDataHandler sharedDataHandler)
SharedDataHandler sharedDataHandler,
HashMap<List<String>, byte[]> properties)
throws StartException {
Objects.requireNonNull(proxyWasm);
this.name = Objects.requireNonNullElse(name, "default");
Expand All @@ -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());
Expand Down Expand Up @@ -138,7 +140,7 @@ public void close() {
private Runnable cancelTick;
private final HashMap<String, ForeignFunction> foreignFunctions;
private byte[] funcCallData = new byte[0];
private final HashMap<List<String>, byte[]> properties = new HashMap<>();
private final HashMap<List<String>, byte[]> properties;

class HandlerImpl extends ChainedHandler {

Expand Down Expand Up @@ -170,6 +172,9 @@ public byte[] getProperty(List<String> 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);
}
Expand Down
Loading