-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34722 from cescoffier/snappy-conditional-loading-jvm
Make Snappy optional in JVM mode
- Loading branch information
Showing
4 changed files
with
87 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...kafka-client/deployment/src/main/java/io/quarkus/kafka/client/deployment/SnappyUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.kafka.client.deployment; | ||
|
||
import org.xerial.snappy.OSInfo; | ||
|
||
/** | ||
* This class should only be used if Snappy is available on the classpath. | ||
*/ | ||
public class SnappyUtils { | ||
|
||
private SnappyUtils() { | ||
// Avoid direct instantiation | ||
} | ||
|
||
public static String getNativeLibFolderPathForCurrentOS() { | ||
return OSInfo.getNativeLibFolderPathForCurrentOS(); | ||
} | ||
} |
54 changes: 0 additions & 54 deletions
54
...ons/kafka-client/runtime/src/main/java/io/quarkus/kafka/client/runtime/KafkaRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ns/kafka-client/runtime/src/main/java/io/quarkus/kafka/client/runtime/SnappyRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.kafka.client.runtime; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.URL; | ||
|
||
import org.xerial.snappy.OSInfo; | ||
import org.xerial.snappy.SnappyLoader; | ||
|
||
import io.quarkus.runtime.annotations.Recorder; | ||
|
||
@Recorder | ||
public class SnappyRecorder { | ||
|
||
public void loadSnappy() { | ||
// Resolve the library file name with a suffix (e.g., dll, .so, etc.) | ||
String snappyNativeLibraryName = System.mapLibraryName("snappyjava"); | ||
String snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); | ||
boolean hasNativeLib = hasResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName); | ||
|
||
if (!hasNativeLib) { | ||
String errorMessage = String.format("no native library is found for os.name=%s and os.arch=%s", OSInfo.getOSName(), | ||
OSInfo.getArchName()); | ||
throw new RuntimeException(errorMessage); | ||
} | ||
|
||
File out = extractLibraryFile( | ||
SnappyLoader.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName), | ||
snappyNativeLibraryName); | ||
|
||
System.load(out.getAbsolutePath()); | ||
} | ||
|
||
private static boolean hasResource(String path) { | ||
return SnappyLoader.class.getResource(path) != null; | ||
} | ||
|
||
private static File extractLibraryFile(URL library, String name) { | ||
String tmp = System.getProperty("java.io.tmpdir"); | ||
File extractedLibFile = new File(tmp, name); | ||
|
||
try (BufferedInputStream inputStream = new BufferedInputStream(library.openStream()); | ||
FileOutputStream fileOS = new FileOutputStream(extractedLibFile)) { | ||
byte[] data = new byte[8192]; | ||
int byteContent; | ||
while ((byteContent = inputStream.read(data, 0, 8192)) != -1) { | ||
fileOS.write(data, 0, byteContent); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException( | ||
"Unable to extract native library " + name + " to " + extractedLibFile.getAbsolutePath(), e); | ||
} | ||
|
||
extractedLibFile.deleteOnExit(); | ||
|
||
return extractedLibFile; | ||
} | ||
} |