Author: mturk Date: Fri Apr 17 10:17:34 2009 New Revision: 765931 URL: http://svn.apache.org/viewvc?rev=765931&view=rev Log: Add Descriptor classes that encapsulate the OS object handles
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java (with props) commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java (with props) commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h (with props) commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (with props) Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h commons/sandbox/runtime/trunk/src/main/native/shared/error.c commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c commons/sandbox/runtime/trunk/src/main/native/test/testcase.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java?rev=765931&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java Fri Apr 17 10:17:34 2009 @@ -0,0 +1,97 @@ +/* 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.commons.runtime; + +import java.io.Closeable; +import java.io.IOException; +import java.io.SyncFailedException; + +/** Represents the Operating System object descriptor. + * <p> + * Object descriptors represent the allocated system resource that + * can be of any type supported. Such objects are {...@code files} , + * {...@code sockets} , {...@code processes} etc... + * </p> + * <p> + * Descriptor is Operating system dependent usually encapsulating + * the {...@code int} on unixes and {...@code HANDLE} on Microsoft Windows. + * </p> + * @since Runtime 1.0 + */ +public abstract class Descriptor implements Closeable { + + /* + * Descriptor can be only created from native code. + * Suppress any instantiation except form internal classes. + */ + protected Descriptor() + { + // No Instance + } + + private native void close0() + throws IOException; + + private native void sync0() + throws SyncFailedException, IOException; + + /** + * Called by the garbage collector when the object is destroyed. + * The class will free internal resources allocated by the Operating system. + * @see Object#finalize() + * @throws Throwable the {...@code Exception} raised by this method. + */ + protected final void finalize() + throws Throwable + { + close0(); + } + + /** + * Free the allocated resource by the Operating system. + * <p> + * Note that {...@code Object.finalize()} method will call + * this function. However if the native code can block for + * long time explicit {...@code close()} should be called. + * </p> + * @see java.io.Closeable#close() + * @throws IOException if an I/O error occurs. + */ + public final void close() + throws IOException + { + close0(); + } + + /** + * Force the underlying object to synchonize any buffered data. + * @throws SyncFailedException when the object cannot be flushed. + * @throws IOException if an I/O error occurs. + */ + public final void sync() + throws SyncFailedException, IOException + { + sync0(); + } + + /** + * Check if the underlying Operating system descriptor is closed. + * @return {...@code true} if descriptor is closed {...@code false} otherwise. + */ + public abstract boolean isClosed(); + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java?rev=765931&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java Fri Apr 17 10:17:34 2009 @@ -0,0 +1,52 @@ +/* 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.commons.runtime; + +/** Represents the Operating System 32-bit object descriptor. + * <p> + * IHANDLE always represents the posix int descriptor. + * LHANDLE represents the pointer if descriptor is pointer based + * </p> + * @since Runtime 1.0 + */ +class Descriptor32 extends Descriptor { + + private int IHANDLE; + private int PHANDLE; + private int CLEANUP; + + /* + * Descriptor can be only created from native code. + * Suppress any instantiation except form JNI. + */ + protected Descriptor32(int i, int l, int c) + { + IHANDLE = i; + PHANDLE = l; + CLEANUP = c; + } + + public boolean isClosed() + { + // true if both int is negative and pointer is NULL + if (IHANDLE < 0 && PHANDLE == 0) + return true; + else + return false; + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java?rev=765931&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java Fri Apr 17 10:17:34 2009 @@ -0,0 +1,52 @@ +/* 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.commons.runtime; + +/** Represents the Operating System 64-bit object descriptor. + * <p> + * IHANDLE always represents the posix int descriptor. + * LHANDLE represents the pointer if descriptor is pointer based + * </p> + * @since Runtime 1.0 + */ +class Descriptor64 extends Descriptor { + + private int IHANDLE; + private long PHANDLE; + private long CLEANUP; + + /* + * Descriptor can be only created from native code. + * Suppress any instantiation except form JNI. + */ + private Descriptor64(int i, long l, long c) + { + IHANDLE = i; + PHANDLE = l; + CLEANUP = c; + } + + public boolean isClosed() + { + // true if both int is negative and pointer is NULL + if (IHANDLE < 0 && PHANDLE == 0L) + return true; + else + return false; + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Fri Apr 17 10:17:34 2009 @@ -66,6 +66,7 @@ COMMON_OBJS=\ $(SRCDIR)/shared/buildmark.$(OBJ) \ $(SRCDIR)/shared/clazz.$(OBJ) \ + $(SRCDIR)/shared/descriptor.$(OBJ) \ $(SRCDIR)/shared/dbb.$(OBJ) \ $(SRCDIR)/shared/error.$(OBJ) \ $(SRCDIR)/shared/memory.$(OBJ) \ Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Fri Apr 17 10:17:34 2009 @@ -60,6 +60,7 @@ COMMON_OBJS=\ $(SRCDIR)/shared/buildmark.$(OBJ) \ $(SRCDIR)/shared/clazz.$(OBJ) \ + $(SRCDIR)/shared/descriptor.$(OBJ) \ $(SRCDIR)/shared/dbb.$(OBJ) \ $(SRCDIR)/shared/error.$(OBJ) \ $(SRCDIR)/shared/memory.$(OBJ) \ Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h?rev=765931&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h (added) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h Fri Apr 17 10:17:34 2009 @@ -0,0 +1,85 @@ +/* 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. + */ + +#ifndef _ACR_DESCRIPTOR_H +#define _ACR_DESCRIPTOR_H + +#include "acr.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file acr_descriptor.h + * @brief + * + * ACR Pointer class functions + * + */ + +typedef enum { + ACR_DESC_CLOSE, + ACR_DESC_SYNC +} acr_descriptor_cb_type_e; + +/** + * Descriptor callback function prototype. + * The callback function must check for data validity. + * Consecutive invocations will always contain NULL data. + */ +typedef int (acr_descriptor_callback_fn_t)(acr_descriptor_cb_type_e, int, void *); + +/** + * Create new Descriptor class instance + * @param env Current JNI environment + * @param i Native integer descriptor to wrap into Descriptor class + * @param p Native pointer descriptor to wrap into Descriptor class + * @param cb callback function to use + */ +ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *env, int i, void *p, + acr_descriptor_callback_fn_t *cb); + +/** + * Call descriptor cleanup and clear Java object. + * @param env Current JNI environment + * @param obj Java Descriptor object to clean. The function will + * clear the callback and data inside Java object. + * @return ACR_INCOMPLETE if already cleared. + */ +ACR_DECLARE(int) ACR_DescriptorCleanup(JNIEnv *env, jobject obj); + +/** + * Get the native pointer from the Pointer object. + * @param env Current JNI environment + * @param obj Java Descriptor object use. + * @return Underlying wrapped integer descriptor. + */ +ACR_DECLARE(int) ACR_DescriptorGetInt(JNIEnv *env, jobject obj); + +/** + * Get the native pointer from the Pointer object. + * @param env Current JNI environment + * @param obj Java Descriptor object use. + * @return Underlying wrapped pointer descriptor. + */ +ACR_DECLARE(void *) ACR_DescriptorGetPtr(JNIEnv *env, jobject obj); + +#ifdef __cplusplus +} +#endif + +#endif /* _ACR_DESCRIPTOR_H */ Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h Fri Apr 17 10:17:34 2009 @@ -405,6 +405,7 @@ #define ACR_EISNULL (ACR_OS_START_ERROR + 100) #define ACR_EINVALSIZ (ACR_OS_START_ERROR + 101) #define ACR_ERANGE (ACR_OS_START_ERROR + 102) +#define ACR_ECLASSNOTFOUND (ACR_OS_START_ERROR + 103) /** * @defgroup ACR_STATUS_IS Status Value Tests Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h Fri Apr 17 10:17:34 2009 @@ -36,7 +36,7 @@ * The cleanup function must chech for data validity. * Consecutive invocations will always contain NULL data. */ -typedef int (acr_pointer_callback_fn_t)(void *); +typedef int (acr_pointer_cleanup_fn_t)(void *); /** * Create new Pointer class instance @@ -45,10 +45,10 @@ * @param cb callback function to use on Pointer.finalize() */ ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *env, void *p, - acr_pointer_callback_fn_t *cb); + acr_pointer_cleanup_fn_t *cb); /** - * Call pointer cleanup. + * Call pointer cleanup and clear Java object. * @param env Current JNI environment * @param ptr Java Pointer object to clean. The function will * clear the callback and pointer inside Java object. Added: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c?rev=765931&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (added) +++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Fri Apr 17 10:17:34 2009 @@ -0,0 +1,260 @@ +/* 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. + */ + +/* + * + * @author Mladen Turk + */ + +#include "acr.h" +#include "acr_private.h" +#include "acr_error.h" +#include "acr_clazz.h" +#include "acr_descriptor.h" + +/** + * Descriptor class utilities + */ +#if CC_SIZEOF_VOIDP == 8 + +J_DECLARE_CLAZZ = { + NULL, + NULL, + ACR_CLASS_PATH "Descriptor64" +}; + +J_DECLARE_F_ID(0000) = { + NULL, + "IHANDLE", + "I" +}; + +J_DECLARE_F_ID(0001) = { + NULL, + "PHANDLE", + "J" +}; + +J_DECLARE_F_ID(0002) = { + NULL, + "CLEANUP", + "J" +}; + +J_DECLARE_M_ID(0000) = { + NULL, + "<init>", + "(IJJ)V" +}; + +#else + +J_DECLARE_CLAZZ = { + NULL, + NULL, + ACR_CLASS_PATH "Descriptor32" +}; + +J_DECLARE_F_ID(0000) = { + NULL, + "IHANDLE", + "I" +}; + +J_DECLARE_F_ID(0001) = { + NULL, + "PHANDLE", + "I" +}; + +J_DECLARE_F_ID(0002) = { + NULL, + "CLEANUP", + "I" +}; + +J_DECLARE_M_ID(0000) = { + NULL, + "<init>", + "(III)V" +}; + +#endif + +ACR_CLASS_LDEF(Descriptor) +{ + int rv; + + if ((rv = ACR_LoadClass(_E, &_clazzn)) != ACR_SUCCESS) + return rv; + J_LOAD_IFIELD(0000); + J_LOAD_IFIELD(0001); + J_LOAD_IFIELD(0002); + J_LOAD_METHOD(0000); + + return ACR_SUCCESS; +} + +ACR_CLASS_UDEF(Descriptor) +{ + ACR_UnloadClass(_E, &_clazzn); +} + + +ACR_JNI_EXPORT_DECLARE(void, Descriptor, close0)(ACR_JNISTDARGS) +{ + acr_descriptor_callback_fn_t *callback; + jint i = GET_IFIELD_I(0000, _O); +#if CC_SIZEOF_VOIDP == 8 + jlong p = GET_IFIELD_J(0001, _O); + jlong c = GET_IFIELD_J(0002, _O); + + if (p) { + SET_IFIELD_J(0001, _O, 0); + } +#else + jlong p = GET_IFIELD_I(0001, _O); + jlong c = GET_IFIELD_I(0002, _O); + + if (p) { + SET_IFIELD_I(0001, _O, 0); + } +#endif + if (i >= 0) { + SET_IFIELD_I(0000, _O, -1); + } + + callback = (acr_descriptor_callback_fn_t *)((acr_ptr_t)c); + if (callback) { + int rc = (*callback)(ACR_DESC_CLOSE, i, (void *)((acr_ptr_t)p)); + if (rc) { + /* Throw IOException with errno message */ + ACR_ThrowException(_E, THROW_FMARK, ACR_EX_EIO, rc); + } + } +} + +ACR_JNI_EXPORT_DECLARE(void, Descriptor, sync0)(ACR_JNISTDARGS) +{ + acr_descriptor_callback_fn_t *callback; + jint i = GET_IFIELD_I(0000, _O); +#if CC_SIZEOF_VOIDP == 8 + jlong p = GET_IFIELD_J(0001, _O); + jlong c = GET_IFIELD_J(0002, _O); + +#else + jlong p = GET_IFIELD_I(0001, _O); + jlong c = GET_IFIELD_I(0002, _O); + +#endif + callback = (acr_descriptor_callback_fn_t *)((acr_ptr_t)c); + if (callback) { + int rc = (*callback)(ACR_DESC_SYNC, i, (void *)((acr_ptr_t)p)); + if (rc) { + if (rc == ACR_EINVAL) + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESYNC, 0); + else + ACR_ThrowException(_E, THROW_FMARK, ACR_EX_EIO, rc); + } + } +} + +ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *_E, int i, void *p, + acr_descriptor_callback_fn_t *cb) +{ + if (_clazzn.i && _m0000n.i) + return (*_E)->NewObject(_E, _clazzn.i, _m0000n.i, + (jint)i, (acr_ptr_t)p, (acr_ptr_t)cb); + else { + ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); + return NULL; + } +} + +ACR_DECLARE(int) ACR_DescriptorCleanup(ACR_JNISTDARGS) +{ + if (_clazzn.i && _m0000n.i) { + acr_descriptor_callback_fn_t *callback; + jint i = GET_IFIELD_I(0000, _O); +#if CC_SIZEOF_VOIDP == 8 + jlong p = GET_IFIELD_J(0001, _O); + jlong c = GET_IFIELD_J(0002, _O); + + if (p) { + SET_IFIELD_J(0001, _O, 0); + } + if (c) { + SET_IFIELD_J(0002, _O, 0); + } +#else + jlong p = GET_IFIELD_I(0001, _O); + jlong c = GET_IFIELD_I(0002, _O); + + if (p) { + SET_IFIELD_I(0001, _O, 0); + } + if (c) { + SET_IFIELD_I(0002, _O, 0); + } +#endif + if (i >= 0) { + SET_IFIELD_I(0000, _O, -1); + } + + callback = (acr_descriptor_callback_fn_t *)((acr_ptr_t)c); + if (callback) { + int rc = (*callback)(ACR_DESC_CLOSE, i, (void *)((acr_ptr_t)p)); + if (rc) { + /* Throw IOException with errno message */ + ACR_ThrowException(_E, THROW_FMARK, ACR_EX_EIO, rc); + } + return rc; + } + else { + /* Already cleared */ + return ACR_INCOMPLETE; + } + } + else + return ACR_ECLASSNOTFOUND; +} + +ACR_DECLARE(void *) ACR_DescriptorGetPtr(ACR_JNISTDARGS) +{ + if (_clazzn.i && _m0000n.i) { +#if CC_SIZEOF_VOIDP == 8 + jlong p = GET_IFIELD_J(0001, _O); +#else + jint p = GET_IFIELD_I(0001, _O); +#endif + return (void *)((acr_ptr_t)p); + } + else { + ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); + return NULL; + } +} + +ACR_DECLARE(int) ACR_DescriptorGetInt(ACR_JNISTDARGS) +{ + if (_clazzn.i && _m0000n.i) { + return GET_IFIELD_I(0000, _O); + } + else { + ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); + return -1; + } +} Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Fri Apr 17 10:17:34 2009 @@ -232,6 +232,8 @@ return "The given argument size is invalid"; case ACR_ERANGE: return "The given argument is out of range"; + case ACR_ECLASSNOTFOUND: + return "The specified Java Class was not found"; default: return "Error string not specified yet"; } Modified: commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Fri Apr 17 10:17:34 2009 @@ -103,7 +103,7 @@ ACR_JNI_EXPORT_DECLARE(void, Pointer, cleanup0)(ACR_JNISTDARGS) { - acr_pointer_callback_fn_t *cleanup; + acr_pointer_cleanup_fn_t *cleanup; #if CC_SIZEOF_VOIDP == 8 jlong h = GET_IFIELD_J(0000, _O); jlong c = GET_IFIELD_J(0001, _O); @@ -119,7 +119,7 @@ SET_IFIELD_I(0000, _O, 0); } #endif - cleanup = (acr_pointer_callback_fn_t *)((acr_ptr_t)c); + cleanup = (acr_pointer_cleanup_fn_t *)((acr_ptr_t)c); if (cleanup) { int rc = (*cleanup)((void *)((acr_ptr_t)h)); if (rc) { @@ -130,19 +130,21 @@ } ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *_E, void *p, - acr_pointer_callback_fn_t *cb) + acr_pointer_cleanup_fn_t *cb) { if (_clazzn.i && _m0000n.i) return (*_E)->NewObject(_E, _clazzn.i, _m0000n.i, (acr_ptr_t)p, (acr_ptr_t)cb); - else + else { + ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); return NULL; + } } ACR_DECLARE(int) ACR_PointerCleanup(ACR_JNISTDARGS) { if (_clazzn.i && _m0000n.i) { - acr_pointer_callback_fn_t *cleanup; + acr_pointer_cleanup_fn_t *cleanup; #if CC_SIZEOF_VOIDP == 8 jlong h = GET_IFIELD_J(0000, _O); jlong c = GET_IFIELD_J(0001, _O); @@ -164,7 +166,7 @@ SET_IFIELD_I(0001, _O, 0); } #endif - cleanup = (acr_pointer_callback_fn_t *)((acr_ptr_t)c); + cleanup = (acr_pointer_cleanup_fn_t *)((acr_ptr_t)c); if (cleanup) { int rc = (*cleanup)((void *)((acr_ptr_t)h)); if (rc) { @@ -179,7 +181,7 @@ } } else - return ACR_ESYMNOTFOUND; + return ACR_ECLASSNOTFOUND; } ACR_DECLARE(void *) ACR_PointerGet(ACR_JNISTDARGS) @@ -192,6 +194,8 @@ #endif return (void *)((acr_ptr_t)h); } - else + else { + ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); return NULL; + } } Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Fri Apr 17 10:17:34 2009 @@ -24,6 +24,7 @@ #include "acr_vm.h" #include "acr_clazz.h" #include "acr_pointer.h" +#include "acr_descriptor.h" /** @@ -234,3 +235,41 @@ void *v = ACR_PointerGet(_E, p); return v != NULL; } + +ACR_CLASS_LDEC(Descriptor); + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test020)(ACR_JNISTDARGS, jint d) +{ + ACR_CLASS_LRUN(Descriptor); + return 0; +} + +static int dcallback(acr_descriptor_cb_type_e t, int i, void *p) +{ + fprintf(stderr, "[native] Descriptor callback(%d) called: %d/%p\n", t, i, p); + fflush(stderr); + return 0; +} + +ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test021)(ACR_JNISTDARGS, jint i, jint d) +{ + + return ACR_DescriptorCreate(_E, i, (void *)d, dcallback); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test022)(ACR_JNISTDARGS, jobject d) +{ + + return ACR_DescriptorCleanup(_E, d); +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test023)(ACR_JNISTDARGS, jobject d) +{ + void *v = ACR_DescriptorGetPtr(_E, d); + return v != NULL; +} + +ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test024)(ACR_JNISTDARGS, jobject d) +{ + return ACR_DescriptorGetInt(_E, d); +} Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=765931&r1=765930&r2=765931&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Fri Apr 17 10:17:34 2009 @@ -56,11 +56,19 @@ private static native int test013(int d); private static native Class test014(int d); private static native String[] test015(int d); + // Pointer private static native int test016(int d); private static native Pointer test017(int d); private static native int test018(Pointer p); private static native int test019(Pointer p); + // Descriptor + private static native int test020(int d); + private static native Descriptor test021(int i, int d); + private static native int test022(Descriptor p); + private static native int test023(Descriptor p); + private static native int test024(Descriptor p); + protected void setUp() throws Exception @@ -235,7 +243,8 @@ throws Throwable { Pointer p = test017(0xcafebabe); - assertNotNull("Pointer",p); + assertNotNull("Pointer", p); + assertFalse("IsNull", p.IsNull()); p.free(); p = null; System.gc(); @@ -262,7 +271,7 @@ throws Throwable { Pointer p = test017(0xcafebabe); - assertNotNull("Pointer",p); + assertNotNull("Pointer", p); int r = test018(p); assertEquals("Result ", 0, r); int c = test018(p); @@ -281,7 +290,7 @@ throws Throwable { Pointer p = test017(0xcafebabe); - assertNotNull("Pointer",p); + assertNotNull("Pointer", p); int r = test019(p); assertEquals("Result ", 1, r); p.free(); @@ -296,4 +305,128 @@ Thread.sleep(200); } + public void testDescClassLoad() + throws Exception + { + int i = test020(0); + assertEquals("Value", 0, i); + } + + public void testDescriptorCb() + throws Throwable + { + Descriptor d = test021(2303, 0xcafebabe); + assertNotNull("Descriptor", d); + assertFalse("Closed", d.isClosed()); + d.close(); + d = null; + System.gc(); + // This should be enough for a gc + // from Pointer.finalize() + Thread.sleep(200); + } + + public void testDescriptorInt() + throws Throwable + { + Descriptor d = test021(2303, 0); + assertNotNull("Descriptor", d); + assertFalse("Closed", d.isClosed()); + d.close(); + assertTrue("Closed", d.isClosed()); + d = null; + System.gc(); + // This should be enough for a gc + // from Pointer.finalize() + Thread.sleep(200); + } + + public void testDescriptorPtr() + throws Throwable + { + Descriptor d = test021(-1, 0xcafebabe); + assertNotNull("Descriptor", d); + assertFalse("Closed", d.isClosed()); + d.close(); + assertTrue("Closed", d.isClosed()); + d = null; + System.gc(); + // This should be enough for a gc + // from Pointer.finalize() + Thread.sleep(200); + } + + public void testDescriptorGc() + throws Throwable + { + Descriptor d = test021(2303, 0xcafebabe); + assertNotNull("Descriptor", d); + assertFalse("Closed", d.isClosed()); + d = null; + System.gc(); + // This should be enough for a first invocation + // from Pointer.finalize() + Thread.sleep(200); + } + + public void testDescriptorNativeClr() + throws Throwable + { + Descriptor d = test021(2303, 0xcafebabe); + assertNotNull("Descriptor", d); + assertFalse("Closed", d.isClosed()); + int r = test022(d); + assertEquals("Result ", 0, r); + int c = test022(d); + // 70008 is ACR_INCLOMPLETE */ + assertEquals("Result ", 70008, c); + + d.close(); + d = null; + System.gc(); + // This should be enough for a gc + // from Pointer.finalize() + Thread.sleep(200); + } + + public void testDescriptorNativePtrGet() + throws Throwable + { + Descriptor d = test021(-1, 0xcafebabe); + assertNotNull("Descriptor", d); + assertFalse("Closed", d.isClosed()); + int r = test023(d); + assertEquals("Result ", 1, r); + d.close(); + int c = test023(d); + // Second call must return NULL + assertEquals("Result ", 0, c); + + d = null; + System.gc(); + // This should be enough for a gc + // from Pointer.finalize() + Thread.sleep(200); + } + + public void testDescriptorNativeIntGet() + throws Throwable + { + Descriptor d = test021(2303, 0); + assertNotNull("Descriptor", d); + assertFalse("Closed", d.isClosed()); + int r = test024(d); + assertEquals("Result ", 2303, r); + d.close(); + int c = test024(d); + // Second call must return -1 + assertEquals("Result ", -1, c); + + d = null; + System.gc(); + // This should be enough for a gc + // from Pointer.finalize() + Thread.sleep(200); + } + }