|
16 | 16 | import dev.openfeature.contrib.providers.gofeatureflag.wasm.bean.WasmInput; |
17 | 17 | import dev.openfeature.sdk.ErrorCode; |
18 | 18 | import dev.openfeature.sdk.Reason; |
19 | | -import java.io.File; |
20 | | -import java.net.URL; |
| 19 | +import java.io.InputStream; |
21 | 20 | import java.nio.charset.StandardCharsets; |
22 | | -import java.nio.file.Files; |
23 | | -import java.nio.file.Path; |
24 | | -import java.nio.file.Paths; |
25 | 21 | import java.util.Collections; |
26 | 22 | import lombok.val; |
27 | 23 |
|
@@ -58,27 +54,21 @@ public EvaluationWasm() throws WasmFileNotFound { |
58 | 54 | /** |
59 | 55 | * getWasmFile is a function that returns the path to the WASM file. |
60 | 56 | * It looks for the file in the classpath under the directory "wasm". |
| 57 | + * This method handles both file system resources and JAR-packaged resources. |
61 | 58 | * |
62 | 59 | * @return the path to the WASM file |
63 | 60 | * @throws WasmFileNotFound - if the file is not found |
64 | 61 | */ |
65 | | - private File getWasmFile() throws WasmFileNotFound { |
| 62 | + private InputStream getWasmFile() throws WasmFileNotFound { |
66 | 63 | try { |
67 | | - ClassLoader classLoader = EvaluationWasm.class.getClassLoader(); |
68 | | - URL directoryUrl = classLoader.getResource("wasm"); |
69 | | - if (directoryUrl == null) { |
70 | | - throw new RuntimeException("Directory not found"); |
71 | | - } |
72 | | - Path dirPath = Paths.get(directoryUrl.toURI()); |
73 | | - try (val files = Files.list(dirPath)) { |
74 | | - return files.filter(path -> path.getFileName().toString().startsWith("gofeatureflag-evaluation") |
75 | | - && (path.getFileName().toString().endsWith(".wasi") |
76 | | - || path.getFileName().toString().endsWith(".wasm"))) |
77 | | - .findFirst() |
78 | | - .map(Path::toFile) |
79 | | - .orElseThrow( |
80 | | - () -> new RuntimeException("No file starting with 'gofeatureflag-evaluation' found")); |
| 64 | + final String wasmResourcePath = "wasm/gofeatureflag-evaluation.wasi"; |
| 65 | + InputStream inputStream = EvaluationWasm.class.getClassLoader().getResourceAsStream(wasmResourcePath); |
| 66 | + if (inputStream == null) { |
| 67 | + throw new WasmFileNotFound("WASM resource not found in classpath: " + wasmResourcePath); |
81 | 68 | } |
| 69 | + return inputStream; |
| 70 | + } catch (WasmFileNotFound e) { |
| 71 | + throw e; |
82 | 72 | } catch (Exception e) { |
83 | 73 | throw new WasmFileNotFound(e); |
84 | 74 | } |
|
0 commit comments