commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1100295 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/shared/
Date Fri, 06 May 2011 17:28:49 GMT
Author: mturk
Date: Fri May  6 17:28:49 2011
New Revision: 1100295

URL: http://svn.apache.org/viewvc?rev=1100295&view=rev
Log:
Use java byte array for acr_sockaddr_t storage since we cannot be sure that finalize will
be called

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpointAddress.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java?rev=1100295&r1=1100294&r2=1100295&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java
Fri May  6 17:28:49 2011
@@ -28,15 +28,22 @@ public abstract class EndpointAddress
      * This is pointer to Operating System sockaddr structure and is used
      * used internally by socket functions.
      */
-    protected long sa;
+    protected byte[] sa;
 
     private int    family;
-    private static native int     family0(long sa);
-    private static native void    free0(long sa);
-    private static native boolean hasnext0(long sa);
-    private static native boolean equals0(long sa1, long sa2);
-    private static native String  name0(long sa);
+    private static final  int     sasize;
+    private static native int     init0();
+    private static native int     family0(byte[] sa);
+    private static native void    free0(byte[] sa);
+    private static native boolean equals0(byte[] sa1, byte[] sa2);
+    private static native String  name0(byte[] sa);
+
+    static {
+        // Gets the size of internal structure
+        //
+        sasize = init0();
 
+    }
     /**
      * Creates a new EndpointAddress object.
      */
@@ -59,7 +66,7 @@ public abstract class EndpointAddress
      */
     public final AddressFamily getFamily()
     {
-        if (family == -1 && sa != 0L)
+        if (family == -1 && sa != null)
             family = family0(sa);
         return AddressFamily.valueOf(family);
     }
@@ -69,7 +76,7 @@ public abstract class EndpointAddress
      */
     public final boolean hasNext()
     {
-        return hasnext0(sa);
+        return sa.length > sasize;
     }
 
     /**
@@ -105,21 +112,4 @@ public abstract class EndpointAddress
            return false;
     }
 
-    /**
-     * 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.
-     */
-    @Override
-    protected final void finalize()
-        throws Throwable
-    {
-        try {
-            free0(sa);
-        } finally {
-            sa = 0L;
-        }
-    }
-    
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpointAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpointAddress.java?rev=1100295&r1=1100294&r2=1100295&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpointAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalEndpointAddress.java
Fri May  6 17:28:49 2011
@@ -34,8 +34,8 @@ import org.apache.commons.runtime.Invali
 public final class LocalEndpointAddress extends EndpointAddress
 {
 
-    private long sa;
-    private native long sockaddr0(String name)
+    private byte[] sa;
+    private native byte[] sockaddr0(String name)
         throws OutOfMemoryError, NetworkException;
     
     private LocalEndpointAddress()

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java?rev=1100295&r1=1100294&r2=1100295&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
Fri May  6 17:28:49 2011
@@ -32,7 +32,7 @@ import org.apache.commons.runtime.Invali
 public abstract class SocketAddress extends EndpointAddress
 {
     // Hide parent field.
-    // private long sa;
+    // private byte[] sa;
     /**
      * First query for IPv4 addresses; only look
      * for IPv6 addresses if the first query failed;
@@ -49,15 +49,15 @@ public abstract class SocketAddress exte
     private static final int IPV6_ADDR_OK  = 2;
 
     /* Structure members */
-    private static native String  hostname0(long sa);
-    private static native String  hostname1(long sa);
-    private static native String  servname0(long sa);
-    private static native int     port0(long sa);
+    private static native String  hostname0(byte[] sa);
+    private static native String  hostname1(byte[] sa);
+    private static native String  servname0(byte[] sa);
+    private static native int     port0(byte[] sa);
 
-    private static native String  ipaddr0(long sa);
-    private static native long    sockaddr0(String hostname, int family, int port, int flags)
+    private static native String  ipaddr0(byte[] sa);
+    private static native byte[]  sockaddr0(String hostname, int family, int port, int flags)
         throws OutOfMemoryError, NetworkException;
-    private static native int     next0(long sa);
+    private static native int     next0(byte[] sa);
 
     private String fqdn = null;
     /**

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c?rev=1100295&r1=1100294&r2=1100295&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Fri May  6 17:28:49 2011
@@ -719,97 +719,129 @@ AcrGetServInfo(const char **servname, ac
     return 0;
 }
 
-ACR_NET_EXPORT(void, EndpointAddress, free0)(JNI_STDARGS, jlong sa)
+static jbyteArray
+sockaddr_to_byte_array(JNI_STDENV, acr_sockaddr_t *sa)
 {
-    acr_sockaddr_t *np;
-    acr_sockaddr_t *sp = J2P(sa, acr_sockaddr_t *);
-    while (sp != 0) {
-        np = sp->next;
-        AcrFree(sp);
-        sp = np;
+    jbyteArray ba;
+    acr_sockaddr_t *ai;
+    int ai_len = ISIZEOF(acr_sockaddr_t);
+    int ai_cnt = 0;
+    int ai_pos = 0;
+
+    ai = sa;
+    while (ai != 0) {
+        ai_cnt++;
+        ai = ai->next;
     }
+    if (ai_cnt == 0)
+        return 0;
+    ba = (*env)->NewByteArray(env, ai_len * ai_cnt);
+    if (ba == 0)
+        goto cleanup;
+    ai = sa;
+    while (ai != 0) {
+        (*env)->SetByteArrayRegion(env, ba, ai_pos, ai_len, (jbyte *)ai);
+        ai = ai->next;
+        ai_pos += ai_len;
+    }
+cleanup:
+    ai = sa;
+    while (sa != 0) {
+        ai = sa->next;
+        AcrFree(sa);
+        sa = ai;
+    }
+    return ba;
 }
 
-ACR_NET_EXPORT(jint, EndpointAddress, family0)(JNI_STDARGS, jlong sa)
-{
-    acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
+#define JBA2SA(BA) \
+    (acr_sockaddr_t *)(*env)->GetPrimitiveArrayCritical(env, (BA), 0)
+#define JSA_RP(BA, SA) \
+    (*env)->ReleasePrimitiveArrayCritical(env, (BA), (SA), 0)
 
-    if (a == 0)
-        return 0;
-    switch (a->family) {
-        case AF_INET:
-            return 1;
-        case AF_INET6:
-            return 2;
-        case AF_LOCAL:
-            return 3;
-        default:
-        break;
-    }
-    return 0;
+ACR_NET_EXPORT(jint, EndpointAddress, init0)(JNI_STDARGS)
+{
+    return ISIZEOF(acr_sockaddr_t);
 }
 
-ACR_NET_EXPORT(jboolean, EndpointAddress, hasnext0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(jint, EndpointAddress, family0)(JNI_STDARGS, jbyteArray a)
 {
-    acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
-    if (a != 0 && a->next != 0)
-        return JNI_TRUE;
-    else
-        return JNI_FALSE;
+    jint rv = -1;
+    acr_sockaddr_t *sa = JBA2SA(a);
+
+    if (sa != 0) {
+        switch (sa->family) {
+            case AF_INET:
+                rv = 1;
+            case AF_INET6:
+                rv = 2;
+            case AF_LOCAL:
+                rv = 3;
+            default:
+                rv = 0;
+            break;
+        }
+    }
+    JSA_RP(a, sa);
+    return rv;
 }
 
-ACR_NET_EXPORT(jstring, EndpointAddress, name0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(jstring, EndpointAddress, name0)(JNI_STDARGS, jbyteArray a)
 {
     char buf[256];
     const char *name = "(null)";
-    acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
+    acr_sockaddr_t *sa = JBA2SA(a);
 
-    if (a != 0) {
-        if (a->hostname[0] == '\0') {
+    if (sa != 0) {
+        if (sa->hostname[0] == '\0') {
             /* Return IP address representation
              */
-            if (AcrGetSockaddrIp(buf, 256, a) == 0)
+            if (AcrGetSockaddrIp(buf, 256, sa) == 0)
                 name = buf;
         }
         else
-            name = a->hostname;
+            name = sa->hostname;
     }
+    JSA_RP(a, sa);
     return AcrNewJavaStringA(env, name);
 }
 
-ACR_NET_EXPORT(jboolean, EndpointAddress, equals0)(JNI_STDARGS, jlong sa1, jlong sa2)
-{
-    acr_sockaddr_t *addr1 = J2P(sa1, acr_sockaddr_t *);
-    acr_sockaddr_t *addr2 = J2P(sa2, acr_sockaddr_t *);
-
-    if (addr1 == 0 || addr2 == 0)
-        return JNI_FALSE;
-    if (addr1->iplen == addr2->iplen &&
-        memcmp(addr1->ipaddr, addr2->ipaddr, addr1->iplen) == 0) {
-        return JNI_TRUE;
+ACR_NET_EXPORT(jboolean, EndpointAddress, equals0)(JNI_STDARGS,
+                                                   jbyteArray sa1,
+                                                   jbyteArray sa2)
+{
+    jboolean rv = JNI_FALSE;
+    acr_sockaddr_t *addr1 = JBA2SA(sa1);
+    acr_sockaddr_t *addr2 = JBA2SA(sa2);
+
+    if (addr1 != 0 && addr2 != 0) {
+        if (addr1->iplen == addr2->iplen &&
+            memcmp(addr1->ipaddr, addr2->ipaddr, addr1->iplen) == 0)
+            rv = JNI_TRUE;
+        else if (V4MAPPED_EQUAL(addr1, addr2))
+            rv = JNI_TRUE;
+        else if (V4MAPPED_EQUAL(addr2, addr1))
+            rv = JNI_TRUE;
     }
-    if (V4MAPPED_EQUAL(addr1, addr2))
-        return 1;
-    if (V4MAPPED_EQUAL(addr2, addr1))
-        return JNI_TRUE;
-    return JNI_FALSE; /* not equal */
+    JSA_RP(sa1, addr1);
+    JSA_RP(sa2, addr2);
+    return rv;
 }
 
-ACR_NET_EXPORT(jlong, LocalEndpointAddress, sockaddr0)(JNI_STDARGS, jstring hostname)
+ACR_NET_EXPORT(jbyteArray, LocalEndpointAddress, sockaddr0)(JNI_STDARGS, jstring hostname)
 {
-    acr_sockaddr_t *sa = 0;
+    jbyteArray     ba;
+    acr_sockaddr_t sa;
     char *np;
     int   rc = 0;
 
+    memset(&sa, 0, sizeof(acr_sockaddr_t));
     WITH_CSTR(hostname) {
 #if HAVE_SYS_UN_H
-        sa = calloc(1, sizeof(acr_sockaddr_t));
-        if (sa == 0)
-            return ACR_ENOMEM;
         if (*J2S(hostname) != '/') {
-            size_t i = (int)strlcpy(sa->hostname, VAR_RUN_PATH "/", sizeof(sa->hostname));
-            strlcat(sa->hostname, J2S(hostname), sizeof(sa->hostname));
-            np = sa->hostname + i;
+            size_t i = (int)strlcpy(sa.hostname, VAR_RUN_PATH "/", sizeof(sa.hostname));
+            strlcat(sa.hostname, J2S(hostname), sizeof(sa.hostname));
+            np = sa.hostname + i;
             while (*np != '\0') {
                 if (*np == '/' || *np == ' ')
                     *np = '_';
@@ -817,114 +849,131 @@ ACR_NET_EXPORT(jlong, LocalEndpointAddre
             }
         }
         else
-            strlcpy(sa->hostname, J2S(hostname), sizeof(sa->hostname));
-        strlcpy(sa->sa.unx.sun_path, sa->hostname, sizeof(sa->sa.unx.sun_path));
-        sa->sa.unx.sun_family = AF_LOCAL;
-        sa->family  = AF_LOCAL;
-        sa->salen   = ISIZEOF(struct sockaddr_un);
-        sa->addrlen = ISIZEOF(sa->sa.unx.sun_path);
-        sa->ipaddr  = &(sa->sa.unx.sun_path);
-        sa->iplen   = sa->addrlen;
+            strlcpy(sa.hostname, J2S(hostname), sizeof(sa.hostname));
+        strlcpy(sa.sa.unx.sun_path, sa.hostname, sizeof(sa.sa.unx.sun_path));
+        sa.sa.unx.sun_family = AF_LOCAL;
+        sa.family  = AF_LOCAL;
+        sa.salen   = ISIZEOF(struct sockaddr_un);
+        sa.addrlen = ISIZEOF(sa.sa.unx.sun_path);
+        sa.ipaddr  = &(sa.sa.unx.sun_path);
+        sa.iplen   = sa.addrlen;
 #elif defined(WINDOWS)
         sa = calloc(1, sizeof(acr_sockaddr_t));
         if (sa == 0)
             return ACR_ENOMEM;
-        strlcpy(sa->hostname, "\\\\.\\pipe\\", sizeof(sa->hostname));
-        strlcat(sa->hostname, J2S(hostname), sizeof(sa->hostname));
-        np = sa->hostname + 9;
+        strlcpy(sa.hostname, "\\\\.\\pipe\\", sizeof(sa.hostname));
+        strlcat(sa.hostname, J2S(hostname), sizeof(sa.hostname));
+        np = sa.hostname + 9;
         while (*np != '\0') {
             if (*np == '/' || *np == '\\'|| *np == ' ')
                 *np = '_';
             np++;
         }
-        sa->family  = AF_LOCAL;
-        sa->addrlen = ISIZEOF(sa->hostname);
-        sa->ipaddr  = &(sa->hostname);
-        sa->iplen   = sa->addrlen;
+        sa.family  = AF_LOCAL;
+        sa.addrlen = ISIZEOF(sa.hostname);
+        sa.ipaddr  = &(sa.hostname);
+        sa.iplen   = sa.addrlen;
 #else
-        sa = 0;
         rc = ACR_ENOTIMPL;
 #endif
     } DONE_WITH_STR(hostname);
 
-    if (rc != 0)
+    if (rc != 0) {
         ACR_THROW_NET_ERROR(rc);
-    return P2J(sa);;
-}
-
-ACR_NET_EXPORT(jstring, SocketAddress, hostname0)(JNI_STDARGS, jlong a)
-{
-    acr_sockaddr_t *sa = J2P(a, acr_sockaddr_t *);
-    if (sa != 0 && sa->hostname != '\0')
-        return AcrNewJavaStringA(env, sa->hostname);
-    else {
-        char buf[256];
-        if (AcrGetSockaddrIp(buf, 256, sa) == 0)
-            return AcrNewJavaStringA(env, buf);
+        return 0;
     }
-    return 0;
+    ba = (*env)->NewByteArray(env, ISIZEOF(acr_sockaddr_t));
+    if (ba != 0)
+        (*env)->SetByteArrayRegion(env, ba, 0, ISIZEOF(acr_sockaddr_t), (jbyte *)&sa);
+    return ba;
+}
+
+ACR_NET_EXPORT(jstring, SocketAddress, hostname0)(JNI_STDARGS, jbyteArray a)
+{
+    jstring rv = 0;
+    acr_sockaddr_t *sa = JBA2SA(a);
+
+    if (sa != 0) {
+        if (sa->hostname != '\0')
+            rv = AcrNewJavaStringA(env, sa->hostname);
+        else {
+            char buf[256];
+            if (AcrGetSockaddrIp(buf, 256, sa) == 0)
+                rv = AcrNewJavaStringA(env, buf);
+        }
+    }
+    JSA_RP(a, sa);
+    return rv;
 }
 
-ACR_NET_EXPORT(jstring, SocketAddress, hostname1)(JNI_STDARGS, jlong a)
+ACR_NET_EXPORT(jstring, SocketAddress, hostname1)(JNI_STDARGS, jbyteArray a)
 {
     const char *host = 0;
     int   rc;
-    acr_sockaddr_t *sa = J2P(a, acr_sockaddr_t *);
+    jstring rv = 0;
+    acr_sockaddr_t *sa = JBA2SA(a);
 
-    if (sa == 0)
-        return 0;
-    rc = AcrGetNameInfo(&host, sa, 0);
-    if (rc == 0)
-        return AcrNewJavaStringA(env, host);
-    else {
-        char buf[256];
-        if (AcrGetSockaddrIp(buf, 256, sa) == 0)
-            return AcrNewJavaStringA(env, buf);
+    if (sa != 0) {
+        rc = AcrGetNameInfo(&host, sa, 0);
+        if (rc == 0)
+            rv = AcrNewJavaStringA(env, host);
+        else {
+            char buf[256];
+            if (AcrGetSockaddrIp(buf, 256, sa) == 0)
+                rv = AcrNewJavaStringA(env, buf);
+        }
     }
-    return 0;
+    JSA_RP(a, sa);
+    return rv;
 }
 
-ACR_NET_EXPORT(jstring, SocketAddress, servname0)(JNI_STDARGS, jlong a)
+ACR_NET_EXPORT(jstring, SocketAddress, servname0)(JNI_STDARGS, jbyteArray a)
 {
     const char *serv = 0;
     int   rc;
-    acr_sockaddr_t *sa = J2P(a, acr_sockaddr_t *);
+    jstring rv = 0;
+    acr_sockaddr_t *sa = JBA2SA(a);
 
-    if (sa != 0 && sa->servname != '\0')
-        return AcrNewJavaStringA(env, sa->servname);
-    else {
-        rc = AcrGetServInfo(&serv, sa, 0);
-        if (rc == 0)
-            return AcrNewJavaStringA(env, serv);
-        else
-            return 0;
+    if (sa != 0) {
+        if (sa->servname != '\0')
+            rv = AcrNewJavaStringA(env, sa->servname);
+        else {
+            rc = AcrGetServInfo(&serv, sa, 0);
+            if (rc == 0)
+                rv = AcrNewJavaStringA(env, serv);
+        }
     }
+    JSA_RP(a, sa);
+    return rv;
 }
 
-ACR_NET_EXPORT(jstring, SocketAddress, ipaddr0)(JNI_STDARGS, jlong a)
+ACR_NET_EXPORT(jstring, SocketAddress, ipaddr0)(JNI_STDARGS, jbyteArray a)
 {
     char buf[256];
-    acr_sockaddr_t *sa = J2P(a, acr_sockaddr_t *);
+    jstring rv = 0;
+    acr_sockaddr_t *sa = JBA2SA(a);
 
-    if (sa == 0)
-        return 0;
-    if (AcrGetSockaddrIp(buf, 256, sa) == 0)
-        return AcrNewJavaStringA(env, buf);
-    else
-        return 0;
+    if (sa != 0) {
+        if (AcrGetSockaddrIp(buf, 256, sa) == 0)
+            rv = AcrNewJavaStringA(env, buf);
+    }
+    JSA_RP(a, sa);
+    return rv;
 }
 
-ACR_NET_EXPORT(jint, SocketAddress, port0)(JNI_STDARGS, jlong a)
+ACR_NET_EXPORT(jint, SocketAddress, port0)(JNI_STDARGS,jbyteArray a)
 {
-    acr_sockaddr_t *sa = J2P(a, acr_sockaddr_t *);
+    jint rv = 0;
+    acr_sockaddr_t *sa = JBA2SA(a);
+
     if (sa != 0)
-        return sa->port;
-    else
-        return 0;
+        rv = sa->port;
+    JSA_RP(a, sa);
+    return rv;
 }
 
-ACR_NET_EXPORT(jlong, SocketAddress, sockaddr0)(JNI_STDARGS, jstring hostname,
-                                                jint family, jint port, jint flags)
+ACR_NET_EXPORT(jbyteArray, SocketAddress, sockaddr0)(JNI_STDARGS, jstring hostname,
+                                                     jint family, jint port, jint flags)
 {
     acr_sockaddr_t *sa = 0;
     int ffamily = AF_UNSPEC;
@@ -956,28 +1005,10 @@ ACR_NET_EXPORT(jlong, SocketAddress, soc
         rc = AcrFindAddresses(&sa, J2S(hostname), ffamily, port, flags);
     } DONE_WITH_STR(hostname);
 
-    if (rc != 0)
+    if (rc != 0) {
         ACR_THROW_NET_ERROR(rc);
-    return P2J(sa);
-}
-
-ACR_NET_EXPORT(jint, SocketAddress, next0)(JNI_STDARGS, jlong a)
-{
-    acr_sockaddr_t *sa = J2P(a, acr_sockaddr_t *);
-
-    if (sa == 0 || sa->next == 0)
-        return -1;
-    switch (sa->next->family) {
-        case AF_INET:
-            return 1;
-        case AF_INET6:
-            return 2;
-        case AF_LOCAL:
-            /* XXX: This can never happen (TM)
-             */
-            return 3;
-        default:
-        break;
+        return 0;
     }
-    return 0;
+    else
+        return sockaddr_to_byte_array(env, sa);
 }



Mime
View raw message