forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for Snappy in the Kafka client extension.
- Loading branch information
1 parent
4868b64
commit 05e5e8b
Showing
26 changed files
with
1,178 additions
and
47 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
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
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
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
74 changes: 74 additions & 0 deletions
74
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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.SnappyError; | ||
import org.xerial.snappy.SnappyErrorCode; | ||
import org.xerial.snappy.SnappyLoader; | ||
|
||
import io.quarkus.runtime.annotations.Recorder; | ||
|
||
@Recorder | ||
public class KafkaRecorder { | ||
|
||
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) { | ||
if (OSInfo.getOSName().equals("Mac")) { | ||
// Fix for openjdk7 for Mac | ||
String altName = "libsnappyjava.jnilib"; | ||
if (hasResource(snappyNativeLibraryPath + "/" + altName)) { | ||
snappyNativeLibraryName = altName; | ||
hasNativeLib = true; | ||
} | ||
} | ||
} | ||
|
||
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 SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, 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; | ||
} | ||
|
||
} |
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
Oops, something went wrong.