Skip to content

Commit 5243e53

Browse files
committed
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 <hiram@hiramchirino.com>
1 parent b5cc1f5 commit 5243e53

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/PluginFactory.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import com.dylibso.chicory.wasm.WasmModule;
99
import io.roastedroot.proxywasm.internal.ProxyWasm;
1010
import java.net.URI;
11+
import java.util.Arrays;
1112
import java.util.HashMap;
13+
import java.util.List;
1214
import java.util.Map;
1315
import java.util.function.Function;
1416

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

8689
/**
8790
* Private constructor for the Builder.
@@ -105,6 +108,42 @@ public PluginFactory.Builder withName(String name) {
105108
return this;
106109
}
107110

111+
/**
112+
* Sets the properties for this plugin instance.
113+
*
114+
* @param properties A map where keys are the property names expected by the WASM module,
115+
* and values are the corresponding byte arrays.
116+
* @return this {@code Builder} instance for method chaining.
117+
*/
118+
public PluginFactory.Builder withProperties(Map<List<String>, byte[]> properties) {
119+
this.properties = new HashMap<>(properties);
120+
return this;
121+
}
122+
123+
/**
124+
* Sets a single property for this plugin instance.
125+
*
126+
* @param key The key of the property to set.
127+
* @param value The value of the property to set.
128+
* @return this {@code Builder} instance for method chaining.
129+
*/
130+
public PluginFactory.Builder withProperty(List<String> key, byte[] value) {
131+
this.properties.put(key, value);
132+
return this;
133+
}
134+
135+
/**
136+
* Sets a single property for this plugin instance.
137+
*
138+
* @param key The key of the property to set.
139+
* @param value The value of the property to set.
140+
* @return this {@code Builder} instance for method chaining.
141+
*/
142+
public PluginFactory.Builder withProperty(List<String> key, String value) {
143+
this.properties.put(key, bytes(value));
144+
return this;
145+
}
146+
108147
/**
109148
* Registers foreign (host-provided) functions that can be called by the WASM plugin.
110149
* These functions allow the plugin to interact with the host environment beyond the standard
@@ -362,6 +401,15 @@ public PluginFactory build() {
362401
SharedDataHandler sharedDataHandler = this.sharedDataHandler;
363402
boolean shared = this.shared;
364403

404+
HashMap<List<String>, byte[]> properties = new HashMap<>();
405+
if (this.properties != null) {
406+
for (Map.Entry<List<String>, byte[]> listEntry : this.properties.entrySet()) {
407+
byte[] value = listEntry.getValue();
408+
value = Arrays.copyOf(value, value.length);
409+
this.properties.put(List.copyOf(listEntry.getKey()), value);
410+
}
411+
}
412+
365413
return new PluginFactory() {
366414

367415
@Override
@@ -389,7 +437,8 @@ public Plugin create() throws Exception {
389437
pluginConfig,
390438
metricsHandler,
391439
sharedQueueHandler,
392-
sharedDataHandler);
440+
sharedDataHandler,
441+
properties);
393442
}
394443
};
395444
}

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/Plugin.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static io.roastedroot.proxywasm.internal.WellKnownHeaders.PATH;
77
import static io.roastedroot.proxywasm.internal.WellKnownHeaders.SCHEME;
88
import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_NAME;
9+
import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_ROOT_ID;
910
import static io.roastedroot.proxywasm.internal.WellKnownProperties.PLUGIN_VM_ID;
1011

1112
import io.roastedroot.proxywasm.ForeignFunction;
@@ -50,7 +51,8 @@ public Plugin(
5051
byte[] pluginConfig,
5152
MetricsHandler metricsHandler,
5253
SharedQueueHandler sharedQueueHandler,
53-
SharedDataHandler sharedDataHandler)
54+
SharedDataHandler sharedDataHandler,
55+
HashMap<List<String>, byte[]> properties)
5456
throws StartException {
5557
Objects.requireNonNull(proxyWasm);
5658
this.name = Objects.requireNonNullElse(name, "default");
@@ -61,12 +63,12 @@ public Plugin(
6163
this.vmConfig = vmConfig;
6264
this.pluginConfig = pluginConfig;
6365
this.logger = Objects.requireNonNullElse(logger, LogHandler.DEFAULT);
64-
;
6566
this.metricsHandler = Objects.requireNonNullElse(metricsHandler, MetricsHandler.DEFAULT);
6667
this.sharedQueueHandler =
6768
Objects.requireNonNullElse(sharedQueueHandler, SharedQueueHandler.DEFAULT);
6869
this.sharedDataHandler =
6970
Objects.requireNonNullElse(sharedDataHandler, SharedDataHandler.DEFAULT);
71+
this.properties = Objects.requireNonNull(properties);
7072

7173
this.wasm = proxyWasm;
7274
this.wasm.setPluginHandler(new HandlerImpl());
@@ -138,7 +140,7 @@ public void close() {
138140
private Runnable cancelTick;
139141
private final HashMap<String, ForeignFunction> foreignFunctions;
140142
private byte[] funcCallData = new byte[0];
141-
private final HashMap<List<String>, byte[]> properties = new HashMap<>();
143+
private final HashMap<List<String>, byte[]> properties;
142144

143145
class HandlerImpl extends ChainedHandler {
144146

@@ -170,6 +172,9 @@ public byte[] getProperty(List<String> path) throws WasmException {
170172
if (PLUGIN_VM_ID.equals(path)) {
171173
return bytes(name);
172174
}
175+
if (PLUGIN_ROOT_ID.equals(path)) {
176+
return bytes(name);
177+
}
173178
if (PLUGIN_NAME.equals(path)) {
174179
return bytes(name);
175180
}

0 commit comments

Comments
 (0)