From 859f5e47008c87f4a860382314354750dc0f6829 Mon Sep 17 00:00:00 2001 From: David Li
This breaks a dependency chain in compilation. If we directly used AdbcException, then we
+ * would need to compile the Java code before compiling JNI code, but we need the compiled JNI code
+ * to compile the Java code.
+ */
+public class NativeAdbcException extends Exception {
+ public NativeAdbcException(String message) {
+ super(message);
+ }
+}
diff --git a/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeHandle.java b/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeHandle.java
new file mode 100644
index 0000000000..a634023cd6
--- /dev/null
+++ b/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeHandle.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.adbc.driver.jni.impl;
+
+import java.lang.ref.Cleaner;
+
+public class NativeHandle implements AutoCloseable {
+ static final Cleaner cleaner = Cleaner.create();
+
+ // TODO: this could store an enum to identify the type of handle as additional checking
+ private final State state;
+ private final Cleaner.Cleanable cleanable;
+
+ NativeHandle(NativeHandleType type, long nativeHandle) {
+ this.state = new State(type, nativeHandle);
+ this.cleanable = cleaner.register(this, state);
+ }
+
+ @Override
+ public void close() {
+ cleanable.clean();
+ }
+
+ public NativeHandleType getHandleType() {
+ return state.type;
+ }
+
+ private static class State implements Runnable {
+ private final NativeHandleType type;
+ long nativeHandle;
+
+ State(NativeHandleType type, long nativeHandle) {
+ this.type = type;
+ this.nativeHandle = nativeHandle;
+ }
+
+ @Override
+ public void run() {
+ if (nativeHandle == 0) return;
+ final long handle = nativeHandle;
+ nativeHandle = 0;
+ try {
+ switch (type) {
+ case DATABASE:
+ NativeAdbc.closeDatabase(handle);
+ break;
+ case CONNECTION:
+ NativeAdbc.closeConnection(handle);
+ break;
+ case STATEMENT:
+ NativeAdbc.closeStatement(handle);
+ break;
+ }
+ } catch (NativeAdbcException e) {
+ // TODO: convert to ADBC exception first
+ throw new RuntimeException(e);
+ }
+ }
+ }
+}
diff --git a/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeHandleType.java b/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeHandleType.java
new file mode 100644
index 0000000000..dbf6ca63b1
--- /dev/null
+++ b/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeHandleType.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.adbc.driver.jni.impl;
+
+public enum NativeHandleType {
+ DATABASE,
+ CONNECTION,
+ STATEMENT,
+ ;
+}
diff --git a/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/package-info.java b/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/package-info.java
new file mode 100644
index 0000000000..ed2ba096a8
--- /dev/null
+++ b/java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/package-info.java
@@ -0,0 +1,18 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.adbc.driver.jni;
diff --git a/java/driver/jni/src/main/resources/META-INF/services/org.apache.arrow.adbc.drivermanager.AdbcDriverFactory b/java/driver/jni/src/main/resources/META-INF/services/org.apache.arrow.adbc.drivermanager.AdbcDriverFactory
new file mode 100644
index 0000000000..c592768f18
--- /dev/null
+++ b/java/driver/jni/src/main/resources/META-INF/services/org.apache.arrow.adbc.drivermanager.AdbcDriverFactory
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+org.apache.arrow.adbc.driver.jni.NativeDriverFactory
diff --git a/java/driver/jni/src/test/java/org/apache/arrow/adbc/driver/jni/JniDriverTest.java b/java/driver/jni/src/test/java/org/apache/arrow/adbc/driver/jni/JniDriverTest.java
new file mode 100644
index 0000000000..4c75729d7a
--- /dev/null
+++ b/java/driver/jni/src/test/java/org/apache/arrow/adbc/driver/jni/JniDriverTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.adbc.driver.jni;
+
+import org.apache.arrow.adbc.core.AdbcConnection;
+import org.apache.arrow.adbc.core.AdbcDatabase;
+import org.apache.arrow.adbc.core.AdbcStatement;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class JniDriverTest {
+ @Test
+ void minimal() throws Exception {
+ try (final BufferAllocator allocator = new RootAllocator()) {
+ JniDriver driver = new JniDriver(allocator);
+ Map