|
17 | 17 | import dev.openfeature.sdk.ErrorCode; |
18 | 18 | import dev.openfeature.sdk.Reason; |
19 | 19 | import java.io.File; |
| 20 | +import java.io.FileOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
20 | 23 | import java.net.URL; |
21 | 24 | import java.nio.charset.StandardCharsets; |
22 | 25 | import java.nio.file.Files; |
|
29 | 32 | * EvaluationWasm is a class that represents the evaluation of a feature flag |
30 | 33 | * it calls an external WASM module to evaluate the feature flag. |
31 | 34 | */ |
32 | | -public final class EvaluationWasm { |
| 35 | +public class EvaluationWasm { |
33 | 36 | private final Instance instance; |
34 | 37 | private final ExportFunction evaluate; |
35 | 38 | private final ExportFunction malloc; |
@@ -58,27 +61,39 @@ public EvaluationWasm() throws WasmFileNotFound { |
58 | 61 | /** |
59 | 62 | * getWasmFile is a function that returns the path to the WASM file. |
60 | 63 | * It looks for the file in the classpath under the directory "wasm". |
| 64 | + * This method handles both file system resources and JAR-packaged resources. |
61 | 65 | * |
62 | 66 | * @return the path to the WASM file |
63 | 67 | * @throws WasmFileNotFound - if the file is not found |
64 | 68 | */ |
65 | | - private File getWasmFile() throws WasmFileNotFound { |
| 69 | + private InputStream getWasmFile() throws WasmFileNotFound { |
66 | 70 | try { |
67 | 71 | ClassLoader classLoader = EvaluationWasm.class.getClassLoader(); |
68 | 72 | URL directoryUrl = classLoader.getResource("wasm"); |
69 | 73 | if (directoryUrl == null) { |
70 | 74 | throw new RuntimeException("Directory not found"); |
71 | 75 | } |
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")); |
| 76 | + |
| 77 | + val wasmResources = classLoader.getResources("wasm"); |
| 78 | + while (wasmResources.hasMoreElements()) { |
| 79 | + URL resourceUrl = wasmResources.nextElement(); |
| 80 | + if ("jar".equals(resourceUrl.getProtocol()) && resourceUrl.getPath().contains("!")) { |
| 81 | + val jarPath = resourceUrl.getPath(); |
| 82 | + val path = jarPath.substring(jarPath.indexOf("!") + 1); |
| 83 | + val resourceName = path + "/gofeatureflag-evaluation.wasi"; |
| 84 | + val inputStream = classLoader.getResourceAsStream(resourceName); |
| 85 | + if (inputStream != null) { |
| 86 | + return inputStream; |
| 87 | + } |
| 88 | + } |
| 89 | + val inputStream = classLoader.getResourceAsStream("wasm/gofeatureflag-evaluation.wasi"); |
| 90 | + if (inputStream != null) { |
| 91 | + return inputStream; |
| 92 | + } |
81 | 93 | } |
| 94 | + throw new WasmFileNotFound("impossible to find the wasm file"); |
| 95 | + } catch(WasmFileNotFound e) { |
| 96 | + throw e; |
82 | 97 | } catch (Exception e) { |
83 | 98 | throw new WasmFileNotFound(e); |
84 | 99 | } |
|
0 commit comments