Author: mturk Date: Mon May 25 11:29:34 2009 New Revision: 778391 URL: http://svn.apache.org/viewvc?rev=778391&view=rev Log: Add DescriptorType object and Descriptor flags final field
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DescriptorType.java (with props) Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c commons/sandbox/runtime/trunk/src/main/native/test/testcase.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Modified: 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=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor.java Mon May 25 11:29:34 2009 @@ -39,7 +39,12 @@ * If the operation is recoverable the clearerr() should be * used after the recovery. */ - private int IERRNUM; + private int IERRNUM; + + /* Immutable Descriptor flags. + * Set by the native on Descriptor creation. + */ + private final int IDFLAGS; /* * Descriptor can be only created from native code. @@ -48,11 +53,13 @@ private Descriptor() { // No Instance + IDFLAGS = 0; } - protected Descriptor(int e) + protected Descriptor(int v) { - IERRNUM = e; + IERRNUM = 0; + IDFLAGS = v; } private native void close0() @@ -143,6 +150,16 @@ } /** + * Get the descriptory type. + * This method is public, but it is intended for the internal use only. + */ + public final DescriptorType getType() + { + // Lower IDFLAGS byte is Descriptor type + return DescriptorType.valueOf(IDFLAGS & 0x000000FF); + } + + /** * Check if the underlying Operating system descriptor is closed. * @return {...@code true} if descriptor is closed {...@code false} otherwise. */ Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DescriptorType.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DescriptorType.java?rev=778391&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DescriptorType.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DescriptorType.java Mon May 25 11:29:34 2009 @@ -0,0 +1,87 @@ +/* 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; + +/** + * Determines the underlying OS descriptor type. + */ +public enum DescriptorType +{ + /** Unknown {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 0} + */ + UNKNOWN( 0), + /** File {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 1} + */ + FILE( 1), + /** User {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 2} + */ + USER( 2), + /** Group {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 3} + */ + GROUP( 3), + /** Global Mutex {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 4} + */ + MUTEX( 4), + /** Shared memory {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 4} + */ + SHM( 5), + /** Pipe {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 4} + */ + PIPE( 6), + /** Socket {...@code Descriptor} type. + * <br/>Defined integer value is: {...@code 4} + */ + SOCKET( 7); + + + private int value; + private DescriptorType(int v) + { + value = v; + } + + /** Integer representing the value of this Enum. + */ + public int valueOf() + { + return value; + } + + /** Return the {...@code DescriptorType} matching the specified {...@code value}. + * @param value Integer value matching one of the + * {...@code DescriptorType} enums. + * @throws IllegalArgumentException if {...@code value} does not match + * to and of the {...@code DescriptorType} enums. + */ + public static DescriptorType valueOf(int value) + throws IllegalArgumentException + { + for (DescriptorType e : values()) { + if (e.value == value) + return e; + } + throw new IllegalArgumentException("Invalid initializer: " + value); + } + +} Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DescriptorType.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: 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=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h Mon May 25 11:29:34 2009 @@ -31,6 +31,16 @@ * */ +#define ACR_DT_UNKNOWN 0x00000000 +#define ACR_DT_FILE 0x00000001 +#define ACR_DT_USER 0x00000002 +#define ACR_DT_GROUP 0x00000003 +#define ACR_DT_MUTEX 0x00000004 +#define ACR_DT_SHM 0x00000005 +#define ACR_DT_PIPE 0x00000006 +#define ACR_DT_SOCKET 0x00000007 +#define ACR_DT_MASK 0x000000FF + typedef enum { ACR_DESC_CLOSE, ACR_DESC_SYNC @@ -55,13 +65,13 @@ /** * Create new Descriptor class instance * @param env Current JNI environment - * @param e Initial errno code for the descriptor object. + * @param f Descriptor flags. * @param i Native integer descriptor to wrap into Descriptor class * @param p Native pointer descriptor to wrap into Descriptor class * @param cb handler function to use * @param ho handler function to use */ -ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *env, int e, int i, void *p, +ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *env, int f, int i, void *p, acr_descriptor_handler_fn_t *cb); /** @@ -84,11 +94,19 @@ * Get the native errno number from the Descriptor object. * @param env Current JNI environment * @param obj Java Descriptor object use. - * @return Underlying errno. + * @return Underlying descriptor IERRNUM field value. */ ACR_DECLARE(int) ACR_DescriptorGetErr(JNIEnv *env, jobject obj); /** + * Get the Descriptor flags. + * @param env Current JNI environment + * @param obj Java Descriptor object use. + * @return Underlying descriptor IDFLAGS field value. + */ +ACR_DECLARE(int) ACR_DescriptorFlags(ACR_JNISTDARGS); + +/** * Get the native int descriptor from the Descriptor object. * @param env Current JNI environment * @param obj Java Descriptor object use. Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c?rev=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c Mon May 25 11:29:34 2009 @@ -115,7 +115,7 @@ if (!gr) return NULL; - gid = ACR_DescriptorCreate(_E, 0, gr->gr_gid, NULL, NULL); + gid = ACR_DescriptorCreate(_E, ACR_DT_GROUP, gr->gr_gid, NULL, NULL); if (!gid) { return NULL; @@ -159,7 +159,8 @@ if (!gr) return NULL; - gid = ACR_DescriptorCreate(_E, 0, gr->gr_gid, NULL, NULL); + gid = ACR_DescriptorCreate(_E, ACR_DT_GROUP, + gr->gr_gid, NULL, NULL); if (!gid) { return NULL; Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c?rev=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/user.c Mon May 25 11:29:34 2009 @@ -131,7 +131,7 @@ if (!pw) return NULL; - uid = ACR_DescriptorCreate(_E, 0, pw->pw_uid, NULL, NULL); + uid = ACR_DescriptorCreate(_E, ACR_DT_USER, pw->pw_uid, NULL, NULL); if (!uid) { return NULL; @@ -181,7 +181,7 @@ if (!pw) return NULL; - uid = ACR_DescriptorCreate(_E, 0, pw->pw_uid, NULL, NULL); + uid = ACR_DescriptorCreate(_E, ACR_DT_USER, pw->pw_uid, NULL, NULL); if (!uid) { return NULL; Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c?rev=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c Mon May 25 11:29:34 2009 @@ -133,7 +133,8 @@ return NULL; } - gid = ACR_DescriptorCreate(_E, 0, -1, sid, sid_descriptor_handler); + gid = ACR_DescriptorCreate(_E, ACR_DT_GROUP, -1, + sid, sid_descriptor_handler); if (!gid) { free(sid); return NULL; @@ -198,7 +199,8 @@ free(sid); return NULL; } - gid = ACR_DescriptorCreate(_E, 0, -1, sid, sid_descriptor_handler); + gid = ACR_DescriptorCreate(_E, ACR_DT_GROUP, -1, + sid, sid_descriptor_handler); if (!gid) { free(sid); return NULL; Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c?rev=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c Mon May 25 11:29:34 2009 @@ -147,7 +147,8 @@ return NULL; } - uid = ACR_DescriptorCreate(_E, 0, -1, sid, sid_descriptor_handler); + uid = ACR_DescriptorCreate(_E, ACR_DT_USER, -1, + sid, sid_descriptor_handler); if (!uid) { free(sid); return NULL; @@ -226,7 +227,8 @@ return NULL; } - uid = ACR_DescriptorCreate(_E, 0, -1, sid, sid_descriptor_handler); + uid = ACR_DescriptorCreate(_E, ACR_DT_USER, -1, + sid, sid_descriptor_handler); if (!uid) { free(sid); return NULL; Modified: 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=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Mon May 25 11:29:34 2009 @@ -83,6 +83,12 @@ "I" }; +J_DECLARE_F_ID(0004) = { + NULL, + "IDFLAGS", + "I" +}; + ACR_CLASS_LDEF(Descriptor) { int rv; @@ -93,6 +99,7 @@ J_LOAD_IFIELD(0001); J_LOAD_IFIELD(0002); J_LOAD_IFIELD(0003); + J_LOAD_IFIELD(0004); J_LOAD_METHOD(0000); return ACR_SUCCESS; @@ -124,8 +131,10 @@ if (!_clazzn.i) { initd0(_E); } - nd = ACR_DescriptorCreate(_E, ACR_EBADF, -1, NULL, NULL); - + nd = ACR_DescriptorCreate(_E, 0, -1, NULL, NULL); + if (nd && _clazzn.i && J4MID(0000)) { + SET_IFIELD_I(0003, nd, ACR_EBADF); + } return nd; } @@ -201,12 +210,12 @@ } -ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *_E, int e, int i, void *p, +ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *_E, int f, int i, void *p, acr_descriptor_handler_fn_t *cb) { if (_clazzn.i && J4MID(0000)) return (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), - (jint)e, (jint)i, P2N(p), P2N(cb)); + (jint)f, (jint)i, P2N(p), P2N(cb)); else { ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); return NULL; @@ -286,6 +295,17 @@ } } +ACR_DECLARE(int) ACR_DescriptorFlags(ACR_JNISTDARGS) +{ + if (_clazzn.i && J4MID(0000)) { + return GET_IFIELD_I(0004, _O); + } + else { + ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND); + return -1; + } +} + ACR_DECLARE(int) ACR_DescriptorSetPtr(ACR_JNISTDARGS, void *p) { if (_clazzn.i && J4MID(0000)) { 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=778391&r1=778390&r2=778391&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Mon May 25 11:29:34 2009 @@ -255,7 +255,7 @@ ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test021)(ACR_JNISTDARGS, jint i, jint d) { - return ACR_DescriptorCreate(_E, 0, i, I2P(d, void *), dhandler); + return ACR_DescriptorCreate(_E, ACR_DT_SOCKET, i, I2P(d, void *), dhandler); } ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test022)(ACR_JNISTDARGS, jobject 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=778391&r1=778390&r2=778391&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 Mon May 25 11:29:34 2009 @@ -551,6 +551,20 @@ Thread.sleep(200); } + public void testDescriptorType() + throws Throwable + { + Descriptor d = test021(2303, 0); + assertNotNull("Descriptor", d); + assertEquals("Type", d.getType(), DescriptorType.SOCKET); + d.close(); + d = null; + System.gc(); + // This should be enough for a gc + // from Pointer.finalize() + Thread.sleep(200); + } + public void testUtf16String() throws Throwable {