This is an automated email from the ASF dual-hosted git repository.

zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new b45d0e65cc [ISSUE #4612] init shenyu-plugin-wasm-api module (#5424)
b45d0e65cc is described below

commit b45d0e65ccaf1fdd49643b52a1026ad1f22b7e39
Author: dragon-zhang <[email protected]>
AuthorDate: Sat Jan 27 18:44:11 2024 +0800

    [ISSUE #4612] init shenyu-plugin-wasm-api module (#5424)
    
    * [ISSUE #4612] init shenyu-plugin-wasm-api module
    
    * simplify dep
---
 shenyu-plugin/pom.xml                              |   1 +
 .../pom.xml                                        |   4 +-
 .../plugin/wasm/api}/AbstractWasmPlugin.java       |   5 +-
 .../wasm/api}/exception/ShenyuWasmException.java   |   2 +-
 .../api}/exception/ShenyuWasmInitException.java    |   2 +-
 .../shenyu/plugin/wasm/api/loader}/WasmLoader.java |   4 +-
 .../shenyu-plugin-wasm-api/src/test/README.md      |  19 +++
 .../plugin/wasm/api}/AbstractWasmPluginTest.java   |   2 +-
 ...api.AbstractWasmPluginTest$RustWasmPlugin.wasm} | Bin
 .../src/test/rust-wasm-plugin/Cargo.toml           |   0
 .../src/test/rust-wasm-plugin/src/lib.rs           |   0
 shenyu-plugin/shenyu-plugin-wasm-base/pom.xml      |   7 +-
 .../plugin/wasm/base/AbstractShenyuWasmPlugin.java |   3 +-
 .../base/handler/AbstractWasmDiscoveryHandler.java |  56 +++++++++
 .../base/handler/AbstractWasmMetaDataHandler.java  |  77 ++++++++++++
 .../handler/AbstractWasmPluginDataHandler.java     | 138 +++++++++++++++++++++
 .../shenyu-plugin-wasm-base/src/test/README.md     |  19 ---
 17 files changed, 307 insertions(+), 32 deletions(-)

diff --git a/shenyu-plugin/pom.xml b/shenyu-plugin/pom.xml
index 17e406f8bb..540393acbd 100644
--- a/shenyu-plugin/pom.xml
+++ b/shenyu-plugin/pom.xml
@@ -28,6 +28,7 @@
 
     <modules>
         <module>shenyu-plugin-api</module>
+        <module>shenyu-plugin-wasm-api</module>
         <module>shenyu-plugin-base</module>
         <module>shenyu-plugin-wasm-base</module>
         <module>shenyu-plugin-httpclient</module>
diff --git a/shenyu-plugin/shenyu-plugin-wasm-base/pom.xml 
b/shenyu-plugin/shenyu-plugin-wasm-api/pom.xml
similarity index 94%
copy from shenyu-plugin/shenyu-plugin-wasm-base/pom.xml
copy to shenyu-plugin/shenyu-plugin-wasm-api/pom.xml
index d64e2de340..8e90f828dd 100644
--- a/shenyu-plugin/shenyu-plugin-wasm-base/pom.xml
+++ b/shenyu-plugin/shenyu-plugin-wasm-api/pom.xml
@@ -23,12 +23,12 @@
         <version>2.6.2-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
-    <artifactId>shenyu-plugin-wasm-base</artifactId>
+    <artifactId>shenyu-plugin-wasm-api</artifactId>
 
     <dependencies>
         <dependency>
             <groupId>org.apache.shenyu</groupId>
-            <artifactId>shenyu-plugin-base</artifactId>
+            <artifactId>shenyu-plugin-api</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/AbstractWasmPlugin.java
 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/AbstractWasmPlugin.java
similarity index 97%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/AbstractWasmPlugin.java
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/AbstractWasmPlugin.java
index ce3a58a059..cc90b37182 100644
--- 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/AbstractWasmPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/AbstractWasmPlugin.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.plugin.wasm.base;
+package org.apache.shenyu.plugin.wasm.api;
 
 import io.github.kawamuray.wasmtime.Extern;
 import io.github.kawamuray.wasmtime.WasmFunctions;
@@ -25,6 +25,7 @@ import org.apache.shenyu.plugin.api.ShenyuPluginChain;
 import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
 import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
 import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
+import org.apache.shenyu.plugin.wasm.api.loader.WasmLoader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
@@ -41,7 +42,7 @@ import java.util.concurrent.ConcurrentHashMap;
  *
  * @see org.apache.shenyu.plugin.api.ShenyuPlugin
  * @see io.github.kawamuray.wasmtime.WasmValType
- * @see org.apache.shenyu.plugin.wasm.base.WasmLoader
+ * @see WasmLoader
  */
 public abstract class AbstractWasmPlugin extends WasmLoader implements 
ShenyuPlugin {
     
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/exception/ShenyuWasmException.java
 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/exception/ShenyuWasmException.java
similarity index 96%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/exception/ShenyuWasmException.java
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/exception/ShenyuWasmException.java
index 52b524bd71..b53b6b3b62 100644
--- 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/exception/ShenyuWasmException.java
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/exception/ShenyuWasmException.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.plugin.wasm.base.exception;
+package org.apache.shenyu.plugin.wasm.api.exception;
 
 import org.apache.shenyu.common.exception.ShenyuException;
 
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/exception/ShenyuWasmInitException.java
 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/exception/ShenyuWasmInitException.java
similarity index 96%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/exception/ShenyuWasmInitException.java
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/exception/ShenyuWasmInitException.java
index 230e93676a..5ac88bfd1b 100644
--- 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/exception/ShenyuWasmInitException.java
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/exception/ShenyuWasmInitException.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.plugin.wasm.base.exception;
+package org.apache.shenyu.plugin.wasm.api.exception;
 
 /**
  * Shenyu WASM init Exception.
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/WasmLoader.java
 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/loader/WasmLoader.java
similarity index 98%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/WasmLoader.java
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/loader/WasmLoader.java
index 706d55f503..c089344f6b 100644
--- 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/WasmLoader.java
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/loader/WasmLoader.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.plugin.wasm.base;
+package org.apache.shenyu.plugin.wasm.api.loader;
 
 import io.github.kawamuray.wasmtime.Extern;
 import io.github.kawamuray.wasmtime.Func;
@@ -25,7 +25,7 @@ import io.github.kawamuray.wasmtime.Module;
 import io.github.kawamuray.wasmtime.Store;
 import io.github.kawamuray.wasmtime.wasi.WasiCtx;
 import io.github.kawamuray.wasmtime.wasi.WasiCtxBuilder;
-import org.apache.shenyu.plugin.wasm.base.exception.ShenyuWasmInitException;
+import org.apache.shenyu.plugin.wasm.api.exception.ShenyuWasmInitException;
 
 import java.io.IOException;
 import java.net.URISyntaxException;
diff --git a/shenyu-plugin/shenyu-plugin-wasm-api/src/test/README.md 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/test/README.md
new file mode 100644
index 0000000000..1da92def00
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-wasm-api/src/test/README.md
@@ -0,0 +1,19 @@
+# How to build the wasm file
+
+1. install rustup
+
+2. install rust
+
+3. generate the wasm file
+
+```shell
+cd {shenyu}/shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-filter
+cargo build --target wasm32-wasi --release
+```
+
+then you will see the wasm file
+in 
`{shenyu}/shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-filter/target/wasm32-wasi/release/rust_filter.wasm`
+
+4. rename the wasm file
+
+rename the file to 
`org.apache.shenyu.plugin.wasm.api.AbstractWasmPluginTest$RustWasmPlugin.wasm`
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/test/java/org/apache/shenyu/plugin/wasm/base/AbstractWasmPluginTest.java
 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/test/java/org/apache/shenyu/plugin/wasm/api/AbstractWasmPluginTest.java
similarity index 99%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/test/java/org/apache/shenyu/plugin/wasm/base/AbstractWasmPluginTest.java
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/test/java/org/apache/shenyu/plugin/wasm/api/AbstractWasmPluginTest.java
index e626c784ff..07a1fb645f 100644
--- 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/test/java/org/apache/shenyu/plugin/wasm/base/AbstractWasmPluginTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/test/java/org/apache/shenyu/plugin/wasm/api/AbstractWasmPluginTest.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.plugin.wasm.base;
+package org.apache.shenyu.plugin.wasm.api;
 
 import io.github.kawamuray.wasmtime.Func;
 import io.github.kawamuray.wasmtime.Store;
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/test/resources/org.apache.shenyu.plugin.wasm.base.AbstractWasmPluginTest$RustWasmPlugin.wasm
 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/test/resources/org.apache.shenyu.plugin.wasm.api.AbstractWasmPluginTest$RustWasmPlugin.wasm
similarity index 100%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/test/resources/org.apache.shenyu.plugin.wasm.base.AbstractWasmPluginTest$RustWasmPlugin.wasm
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/test/resources/org.apache.shenyu.plugin.wasm.api.AbstractWasmPluginTest$RustWasmPlugin.wasm
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/test/rust-wasm-plugin/Cargo.toml 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-plugin/Cargo.toml
similarity index 100%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/test/rust-wasm-plugin/Cargo.toml
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-plugin/Cargo.toml
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/test/rust-wasm-plugin/src/lib.rs 
b/shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-plugin/src/lib.rs
similarity index 100%
rename from 
shenyu-plugin/shenyu-plugin-wasm-base/src/test/rust-wasm-plugin/src/lib.rs
rename to 
shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-plugin/src/lib.rs
diff --git a/shenyu-plugin/shenyu-plugin-wasm-base/pom.xml 
b/shenyu-plugin/shenyu-plugin-wasm-base/pom.xml
index d64e2de340..17735b1c7a 100644
--- a/shenyu-plugin/shenyu-plugin-wasm-base/pom.xml
+++ b/shenyu-plugin/shenyu-plugin-wasm-base/pom.xml
@@ -28,12 +28,13 @@
     <dependencies>
         <dependency>
             <groupId>org.apache.shenyu</groupId>
-            <artifactId>shenyu-plugin-base</artifactId>
+            <artifactId>shenyu-plugin-wasm-api</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
-            <groupId>io.github.kawamuray.wasmtime</groupId>
-            <artifactId>wasmtime-java</artifactId>
+            <groupId>org.apache.shenyu</groupId>
+            <artifactId>shenyu-plugin-base</artifactId>
+            <version>${project.version}</version>
         </dependency>
         <dependency>
             <groupId>ch.qos.logback</groupId>
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/AbstractShenyuWasmPlugin.java
 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/AbstractShenyuWasmPlugin.java
index c44da42e74..ae8198ebb0 100644
--- 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/AbstractShenyuWasmPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/AbstractShenyuWasmPlugin.java
@@ -29,6 +29,7 @@ import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
 import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
 import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
 import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
+import org.apache.shenyu.plugin.wasm.api.loader.WasmLoader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
@@ -45,7 +46,7 @@ import java.util.concurrent.ConcurrentHashMap;
  *
  * @see org.apache.shenyu.plugin.base.AbstractShenyuPlugin
  * @see io.github.kawamuray.wasmtime.WasmValType
- * @see org.apache.shenyu.plugin.wasm.base.WasmLoader
+ * @see org.apache.shenyu.plugin.wasm.api.loader.WasmLoader
  */
 public abstract class AbstractShenyuWasmPlugin extends AbstractShenyuPlugin {
     
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmDiscoveryHandler.java
 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmDiscoveryHandler.java
new file mode 100644
index 0000000000..2021c9cf52
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmDiscoveryHandler.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.wasm.base.handler;
+
+import io.github.kawamuray.wasmtime.WasmFunctions;
+import io.github.kawamuray.wasmtime.WasmValType;
+import org.apache.shenyu.common.dto.DiscoverySyncData;
+import org.apache.shenyu.plugin.base.handler.DiscoveryUpstreamDataHandler;
+import org.apache.shenyu.plugin.wasm.api.exception.ShenyuWasmInitException;
+import org.apache.shenyu.plugin.wasm.api.loader.WasmLoader;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * upstreamList data change.
+ */
+public abstract class AbstractWasmDiscoveryHandler extends WasmLoader 
implements DiscoveryUpstreamDataHandler {
+    
+    protected static final Map<Long, DiscoverySyncData> ARGUMENTS = new 
ConcurrentHashMap<>();
+    
+    protected static final String METHOD_NAME = "handlerDiscoveryUpstreamData";
+    
+    @Override
+    public void handlerDiscoveryUpstreamData(final DiscoverySyncData 
discoverySyncData) {
+        super.getWasmExtern(METHOD_NAME)
+                .map(handlerDiscoveryUpstreamData -> {
+                    // WASI cannot easily pass Java objects like JNI, here we 
pass Long as arg
+                    // then we can get the argument by Long
+                    final Long argumentId = getArgumentId(discoverySyncData);
+                    ARGUMENTS.put(argumentId, discoverySyncData);
+                    // call WASI function
+                    WasmFunctions.consumer(super.getStore(), 
handlerDiscoveryUpstreamData.func(), WasmValType.I64)
+                            .accept(argumentId);
+                    ARGUMENTS.remove(argumentId);
+                    return argumentId;
+                }).orElseThrow(() -> new ShenyuWasmInitException(METHOD_NAME + 
" function not find in wasm file: " + getWasmName()));
+    }
+    
+    protected abstract Long getArgumentId(DiscoverySyncData discoverySyncData);
+}
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmMetaDataHandler.java
 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmMetaDataHandler.java
new file mode 100644
index 0000000000..ee56c83e2c
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmMetaDataHandler.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.wasm.base.handler;
+
+import io.github.kawamuray.wasmtime.Extern;
+import io.github.kawamuray.wasmtime.WasmFunctions;
+import io.github.kawamuray.wasmtime.WasmValType;
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
+import org.apache.shenyu.plugin.wasm.api.exception.ShenyuWasmInitException;
+import org.apache.shenyu.plugin.wasm.api.loader.WasmLoader;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * The type abstract wasm metadata subscriber.
+ */
+public abstract class AbstractWasmMetaDataHandler extends WasmLoader 
implements MetaDataHandler {
+    
+    protected static final Map<Long, MetaData> ARGUMENTS = new 
ConcurrentHashMap<>();
+    
+    protected static final String HANDLE_METHOD_NAME = "handle";
+    
+    protected static final String REMOVE_METHOD_NAME = "remove";
+    
+    protected static final String REFRESH_METHOD_NAME = "refresh";
+    
+    @Override
+    public void handle(final MetaData metaData) {
+        super.getWasmExtern(HANDLE_METHOD_NAME)
+                .map(handle -> callWASI(metaData, handle))
+                .orElseThrow(() -> new 
ShenyuWasmInitException(HANDLE_METHOD_NAME + " function not find in wasm file: 
" + getWasmName()));
+    }
+    
+    @Override
+    public void remove(final MetaData metaData) {
+        super.getWasmExtern(REMOVE_METHOD_NAME)
+                .map(remove -> callWASI(metaData, remove))
+                .orElseThrow(() -> new 
ShenyuWasmInitException(REMOVE_METHOD_NAME + " function not find in wasm file: 
" + getWasmName()));
+    }
+    
+    private Long callWASI(final MetaData metaData, final Extern execute) {
+        // WASI cannot easily pass Java objects like JNI, here we pass Long as 
arg
+        // then we can get the argument by Long
+        final Long argumentId = getArgumentId(metaData);
+        ARGUMENTS.put(argumentId, metaData);
+        // call WASI function
+        WasmFunctions.consumer(super.getStore(), execute.func(), 
WasmValType.I64)
+                .accept(argumentId);
+        ARGUMENTS.remove(argumentId);
+        return argumentId;
+    }
+    
+    protected abstract Long getArgumentId(MetaData metaData);
+    
+    @Override
+    public void refresh() {
+        super.getWasmExtern(REFRESH_METHOD_NAME)
+                .ifPresent(refresh -> WasmFunctions.consumer(super.getStore(), 
refresh.func()).accept());
+    }
+}
diff --git 
a/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmPluginDataHandler.java
 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmPluginDataHandler.java
new file mode 100644
index 0000000000..350f331589
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmPluginDataHandler.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.wasm.base.handler;
+
+import io.github.kawamuray.wasmtime.Extern;
+import io.github.kawamuray.wasmtime.WasmFunctions;
+import io.github.kawamuray.wasmtime.WasmValType;
+import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
+import org.apache.shenyu.plugin.wasm.api.loader.WasmLoader;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * AbstractWasmPluginDataHandler.
+ */
+public abstract class AbstractWasmPluginDataHandler extends WasmLoader 
implements PluginDataHandler {
+    
+    protected static final Map<Long, PluginData> PLUGIN_ARGUMENTS = new 
ConcurrentHashMap<>();
+    
+    protected static final String HANDLER_PLUGIN_METHOD_NAME = "handlerPlugin";
+    
+    protected static final String REMOVE_PLUGIN_METHOD_NAME = "removePlugin";
+    
+    protected static final Map<Long, SelectorData> SELECTOR_ARGUMENTS = new 
ConcurrentHashMap<>();
+    
+    protected static final String HANDLER_SELECTOR_METHOD_NAME = 
"handlerSelector";
+    
+    protected static final String REMOVE_SELECTOR_METHOD_NAME = 
"removeSelector";
+    
+    protected static final Map<Long, RuleData> RULE_ARGUMENTS = new 
ConcurrentHashMap<>();
+    
+    protected static final String HANDLER_RULE_METHOD_NAME = "handlerRule";
+    
+    protected static final String REMOVE_RULE_METHOD_NAME = "removeRule";
+    
+    @Override
+    public void handlerPlugin(final PluginData pluginData) {
+        super.getWasmExtern(HANDLER_PLUGIN_METHOD_NAME)
+                .ifPresent(handlerPlugin -> callWASI(pluginData, 
handlerPlugin));
+    }
+    
+    @Override
+    public void removePlugin(final PluginData pluginData) {
+        super.getWasmExtern(REMOVE_PLUGIN_METHOD_NAME)
+                .ifPresent(handlerPlugin -> callWASI(pluginData, 
handlerPlugin));
+    }
+    
+    @Override
+    public void handlerSelector(final SelectorData selectorData) {
+        super.getWasmExtern(HANDLER_SELECTOR_METHOD_NAME)
+                .ifPresent(handlerPlugin -> callWASI(selectorData, 
handlerPlugin));
+    }
+    
+    @Override
+    public void removeSelector(final SelectorData selectorData) {
+        super.getWasmExtern(REMOVE_SELECTOR_METHOD_NAME)
+                .ifPresent(handlerPlugin -> callWASI(selectorData, 
handlerPlugin));
+    }
+    
+    @Override
+    public void handlerRule(final RuleData ruleData) {
+        super.getWasmExtern(HANDLER_RULE_METHOD_NAME)
+                .ifPresent(handlerPlugin -> callWASI(ruleData, handlerPlugin));
+    }
+    
+    @Override
+    public void removeRule(final RuleData ruleData) {
+        super.getWasmExtern(REMOVE_RULE_METHOD_NAME)
+                .ifPresent(handlerPlugin -> callWASI(ruleData, handlerPlugin));
+    }
+    
+    private Long callWASI(final PluginData pluginData, final Extern execute) {
+        // WASI cannot easily pass Java objects like JNI, here we pass Long as 
arg
+        // then we can get the argument by Long
+        final Long argumentId = getPluginArgumentId(pluginData);
+        PLUGIN_ARGUMENTS.put(argumentId, pluginData);
+        // call WASI function
+        WasmFunctions.consumer(super.getStore(), execute.func(), 
WasmValType.I64)
+                .accept(argumentId);
+        PLUGIN_ARGUMENTS.remove(argumentId);
+        return argumentId;
+    }
+    
+    private Long callWASI(final RuleData ruleData, final Extern execute) {
+        // WASI cannot easily pass Java objects like JNI, here we pass Long as 
arg
+        // then we can get the argument by Long
+        final Long argumentId = getRuleArgumentId(ruleData);
+        RULE_ARGUMENTS.put(argumentId, ruleData);
+        // call WASI function
+        WasmFunctions.consumer(super.getStore(), execute.func(), 
WasmValType.I64)
+                .accept(argumentId);
+        RULE_ARGUMENTS.remove(argumentId);
+        return argumentId;
+    }
+    
+    private Long callWASI(final SelectorData selectorData, final Extern 
execute) {
+        // WASI cannot easily pass Java objects like JNI, here we pass Long as 
arg
+        // then we can get the argument by Long
+        final Long argumentId = getSelectorArgumentId(selectorData);
+        SELECTOR_ARGUMENTS.put(argumentId, selectorData);
+        // call WASI function
+        WasmFunctions.consumer(super.getStore(), execute.func(), 
WasmValType.I64)
+                .accept(argumentId);
+        SELECTOR_ARGUMENTS.remove(argumentId);
+        return argumentId;
+    }
+    
+    protected Long getPluginArgumentId(final PluginData pluginData) {
+        return 0L;
+    }
+    
+    protected Long getSelectorArgumentId(final SelectorData selectorData) {
+        return 0L;
+    }
+    
+    protected Long getRuleArgumentId(final RuleData ruleData) {
+        return 0L;
+    }
+}
diff --git a/shenyu-plugin/shenyu-plugin-wasm-base/src/test/README.md 
b/shenyu-plugin/shenyu-plugin-wasm-base/src/test/README.md
deleted file mode 100644
index 82bb92adb6..0000000000
--- a/shenyu-plugin/shenyu-plugin-wasm-base/src/test/README.md
+++ /dev/null
@@ -1,19 +0,0 @@
-# How to build the wasm file
-
-1. install rustup
-
-2. install rust
-
-3. generate the wasm file
-
-```shell
-cd {shenyu}/shenyu-plugin/shenyu-plugin-wasm-base/src/test/rust-wasm-filter
-cargo build --target wasm32-wasi --release
-```
-
-then you will see the wasm file
-in 
`{shenyu}/shenyu-plugin/shenyu-plugin-wasm-base/src/test/rust-wasm-filter/target/wasm32-wasi/release/rust_filter.wasm`
-
-4. rename the wasm file
-
-rename the file to 
`org.apache.shenyu.plugin.wasm.base.AbstractWasmPluginTest$RustWasmPlugin.wasm`

Reply via email to