add java jni bindings with gradle and maven packaging support#30
Merged
MuriloChianfa merged 6 commits intomainfrom Jan 31, 2026
Merged
add java jni bindings with gradle and maven packaging support#30MuriloChianfa merged 6 commits intomainfrom
MuriloChianfa merged 6 commits intomainfrom
Conversation
Resolved conflicts in: - .github/workflows/ci.yml: Added Java, C#, Lua, Perl, PHP, and Python bindings tests - CMakeLists.txt: Combined Java, C#, Lua, Perl, PHP, and Python wrapper options - docker/README.md: Documented all language binding containers - scripts/docker-build.sh: Added support for all binding images This merge brings in the C# (#31), Lua (#29), Perl (#28), Python (#27), and PHP (#26) bindings alongside the existing Java bindings work.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds comprehensive Java bindings for the liblpm C library using JNI (Java Native Interface), providing high-performance longest prefix match (LPM) routing table operations for Java applications. The bindings deliver near-native C performance with modern Java idioms, type safety, automatic resource management, and bundled native libraries for easy deployment.
Type of Change
Related Issues
Closes #
Motivation and Context
Java is widely used in enterprise networking, cloud infrastructure, microservices, API gateways, and high-performance applications where efficient IP routing lookups are essential. While Java has extensive networking capabilities, high-performance LPM implementations with clean APIs are rare. This implementation provides:
LpmTableIPv4andLpmTableIPv6classes for compile-time safetyAutoCloseableinterface with finalizer safety netThis enables Java developers to leverage liblpm's high-performance routing capabilities in Spring Boot applications, Apache Kafka processors, microservices, API gateways (Kong, Zuul), and enterprise network tools.
Changes Made
Core Implementation
Java API (
src/main/java/): 9 classes, ~1,800 linesLpmTable.java: Abstract base class with common functionality and native method declarationsLpmTableIPv4.java(423 lines): IPv4-specific table with optimized int-based APILpmTableIPv6.java: IPv6-specific table with 16-byte address handlingAlgorithm.java: Type-safe enum for algorithm selection (DIR24, STRIDE8, WIDE16)NextHop.java: Utility class for next hop value validation and conversionLpmException: Base exception for LPM operationsInvalidPrefixException: Invalid address or prefix formatNativeLibraryException: Native library loading failuresNativeLibraryLoader.java: Automatic native library extraction and loadingJNI C Bridge (
src/main/native/liblpm_jni.c): 540 linesJNI_OnLoad,JNI_OnUnload)GetPrimitiveArrayCriticalAPI Features:
create(),create(Algorithm)close(), try-with-resources supportgetVersion(),NextHopvalidationBuild System
Gradle Build (
build.gradle): 218 linesCMakeLists.txt: CMake integration for JNI library
Maven POM Generation: Automatic pom.xml generation with:
Testing & Quality
Test Suite (
src/test/java/): 5 test classes, comprehensive coverageLpmTableIPv4Test.java: IPv4 operations, edge cases, longest prefix matchLpmTableIPv6Test.java: IPv6 operations, address format variationsAlgorithmTest.java: Algorithm selection validationNextHopTest.java: Next hop utility method testingMemoryManagementTest.java: Resource cleanup, try-with-resources, finalizersExamples (
examples/): 3 comprehensive examplesBasicExample.java(119 lines): Getting started, basic operationsBatchLookupExample.java: High-performance batch processingAlgorithmSelectionExample.java: Algorithm comparison and selectionDocumentation
README.md (325 lines):
Javadoc: Comprehensive API documentation
Testing
Test Environment
Tests Performed
./gradlew test)./gradlew javadoc)./gradlew runExamples)Test Output
Performance Impact
Performance Characteristics
JNI Overhead: JNI calls add approximately 15-40ns per call. To minimize overhead:
Use byte[] or int API: Avoid String parsing in hot paths
Batch Operations: Use batch lookups for multiple addresses
Zero-Copy with GetPrimitiveArrayCritical:
Pre-allocated Arrays: Reuse result arrays to avoid GC pressure
Benchmark Results
Key Takeaway:
intAPI for IPv4 hot paths (fastest)Documentation
Javadoc Highlights:
Code Quality
Java Code Quality:
C Code Quality:
Breaking Changes
This is a new feature addition with no impact on existing C library or other language bindings.
Additional Notes
Key Features
LpmTableIPv4andLpmTableIPv6classes prevent runtime errorsGetPrimitiveArrayCriticalfor batch operationsPackage Structure
Files Changed
Maven Artifact
Ready for Maven Central publication:
Includes:
Platform Support
Java Versions:
Use Cases
Thread Safety
lookup,lookupBatch): Thread-safe, concurrent reads allowedinsert,delete): NOT thread-safeExample with
ReadWriteLock:Checklist
Reviewer Notes
Areas for Review
JNI Code Safety: Memory management and zero-copy operations
GetPrimitiveArrayCriticalusage correctnessJava API Design: Modern Java best practices
Performance Optimizations: JNI overhead minimization
Build System: Gradle and CMake integration
Documentation: Completeness and accuracy
Testing: Coverage and edge cases
Testing Instructions
Development Workflow
Performance Testing
Known Limitations
Future Enhancements (Out of Scope)
Maven Central Publication
Once approved, the artifact can be published to Maven Central:
After release, users can add to their projects:
Gradle:
Maven: