commons-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=1100174&view=rev
Log:
Derive local address directly from endpoint instead from socket address.

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/LocalSocketAddress.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
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java

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=1100174&r1=1100173&r2=1100174&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 11:39:58 2011
@@ -37,6 +37,7 @@ public abstract class EndpointAddress ex
     private static native int     family0(long sa);
     private static native void    free0(long sa);
     private static native boolean hasnext0(long sa);
+    private static native String  name0(long sa);
 
     static {
         init0();
@@ -77,6 +78,19 @@ public abstract class EndpointAddress ex
     }
 
     /**
+     * Returns a string representation of this endpoint.
+     */
+    @Override
+    public String toString()
+    {
+        String name = name0(sa);
+        if (name == null)
+            return super.toString();
+        else
+            return name;
+    }
+
+    /**
      * Called by the garbage collector when the object is destroyed.
      * The class will free internal resources allocated by the Operating system.
      * @see Object#finalize()

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java?rev=1100174&r1=1100173&r2=1100174&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java
Fri May  6 11:39:58 2011
@@ -19,9 +19,8 @@
 package org.apache.commons.runtime.net;
 
 import java.io.File;
-import java.net.SocketException;
 import java.net.UnknownHostException;
-import org.apache.commons.runtime.Memory;
+import org.apache.commons.runtime.Errno;
 import org.apache.commons.runtime.Status;
 import org.apache.commons.runtime.InvalidArgumentException;
 
@@ -30,9 +29,11 @@ import org.apache.commons.runtime.Invali
  * <p>
  * </p>
  */
-public final class LocalSocketAddress extends SocketAddress
+public final class LocalSocketAddress extends EndpointAddress
 {
 
+    private native int sockaddr0(String name);
+    
     private LocalSocketAddress()
     {
         // No instance
@@ -44,7 +45,16 @@ public final class LocalSocketAddress ex
     public LocalSocketAddress(String name)
         throws NetworkException, InvalidArgumentException
     {
-        super(name, AddressFamily.LOCAL);
+        super(AddressFamily.LOCAL);
+        if (name == null || name.length() == 0)
+            throw new InvalidArgumentException();
+        int rc = sockaddr0(name);
+        if (rc != 0) {
+            if (rc == Errno.ENOMEM)
+                throw new OutOfMemoryError();
+            else
+                throw new NetworkException(Status.describe(rc));
+        }
     }
 
     /**
@@ -53,7 +63,17 @@ public final class LocalSocketAddress ex
     public LocalSocketAddress(File path)
         throws NetworkException, InvalidArgumentException
     {
-        super( path.getPath(), AddressFamily.LOCAL);
+        super(AddressFamily.LOCAL);
+        String name = path.getPath();
+        if (name == null || name.length() == 0)
+            throw new InvalidArgumentException();
+        int rc = sockaddr0(name);
+        if (rc != 0) {
+            if (rc == Errno.ENOMEM)
+                throw new OutOfMemoryError();
+            else
+                throw new NetworkException(Status.describe(rc));
+        }
     }
 
 }

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=1100174&r1=1100173&r2=1100174&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 11:39:58 2011
@@ -78,8 +78,6 @@ public abstract class SocketAddress exte
                NetworkException
     {
         super(family);
-        if (host == null && family == AddressFamily.LOCAL)
-            throw new InvalidArgumentException();
         int rc = sockaddr0(host, family.valueOf(), port, flags);
         if (rc != 0) {
             if (rc == Errno.ENOMEM)
@@ -94,8 +92,6 @@ public abstract class SocketAddress exte
                NetworkException
     {
         super(family);
-        if (host == null && family == AddressFamily.LOCAL)
-            throw new InvalidArgumentException();
         int rc = sockaddr0(host, family.valueOf(), 0, 0);
         if (rc != 0) {
             if (rc == Errno.ENOMEM)

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=1100174&r1=1100173&r2=1100174&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 11:39:58 2011
@@ -583,9 +583,9 @@ call_resolver(acr_sockaddr_t **sa, const
     return 0;
 }
 
-static int
-find_addresses(acr_sockaddr_t **sa, const char *hostname,
-               int family, int port, int flags)
+int
+AcrFindAddresses(acr_sockaddr_t **sa, const char *hostname,
+                 int family, int port, int flags)
 {
     if (flags & ACR_IPV4_ADDR_OK) {
         int rc = call_resolver(sa, hostname, AF_INET, port, flags);
@@ -604,93 +604,7 @@ find_addresses(acr_sockaddr_t **sa, cons
     return call_resolver(sa, hostname, family, port, flags);
 }
 
-int
-AcrGetSockaddrInfo(acr_sockaddr_t **sa, const char *hostname,
-                   int family, int port, int flags)
-{
-    int masked;
-    char *np;
-    *sa = 0;
-
-    /* TODO: Move the param checks to java code
-     */
-    if ((masked = flags & (ACR_IPV4_ADDR_OK | ACR_IPV6_ADDR_OK))) {
-        if (hostname == 0 || family != AF_UNSPEC ||
-            masked == (ACR_IPV4_ADDR_OK | ACR_IPV6_ADDR_OK)) {
-            return ACR_EINVAL;
-        }
-    }
-    if (family == AF_UNSPEC && hostname != 0 && *hostname == '/')
-        family = AF_LOCAL;
-    if (family == AF_LOCAL) {
-        if (hostname != 0) {
-#if HAVE_SYS_UN_H
-            *sa = calloc(1, sizeof(acr_sockaddr_t));
-            if (*sa == 0)
-                return ACR_ENOMEM;
-            if (*hostname != '/') {
-                size_t i = (int)strlcpy((*sa)->hostname, VAR_RUN_PATH "/", sizeof((*sa)->hostname));
-                strlcat((*sa)->hostname, hostname, sizeof((*sa)->hostname));
-                np = (*sa)->hostname + i;
-                while (*np != '\0') {
-                    if (*np == '/' || *np == ' ')
-                        *np = '_';
-                    np++;
-                }
-            }
-            else
-                strlcpy((*sa)->hostname, 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;
-            return 0;
-#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, 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;
-            return 0;
-#else
-            *sa = 0;
-            return ACR_ENOTIMPL;
-#endif
-        }
-        else {
-            *sa = 0;
-            return ACR_EINVAL;
-        }
-    }
-    return find_addresses(sa, hostname, family, port, flags);
-}
-
-void
-AcrFreeSockaddr(acr_sockaddr_t *sa)
-{
-    acr_sockaddr_t *sp = sa;
-    while (sp != 0) {
-        sa = sp->next;
-        AcrFree(sp);
-        sp = sa;
-    }
-}
-
 #define GETHOSTBYNAME_BUFLEN 512
-
 int
 AcrGetNameInfo(char **hostname, acr_sockaddr_t *sockaddr, int flags)
 {
@@ -769,7 +683,13 @@ ACR_NET_EXPORT(void, EndpointAddress, in
 
 ACR_NET_EXPORT(void, EndpointAddress, free0)(JNI_STDARGS, jlong sa)
 {
-    AcrFreeSockaddr(J2P(sa, acr_sockaddr_t *));
+    acr_sockaddr_t *np;
+    acr_sockaddr_t *sp = J2P(sa, acr_sockaddr_t *);
+    while (sp != 0) {
+        np = sp->next;
+        AcrFree(sp);
+        sp = np;
+    }
 }
 
 ACR_NET_EXPORT(jint, EndpointAddress, family0)(JNI_STDARGS, jlong sa)
@@ -800,6 +720,25 @@ ACR_NET_EXPORT(jboolean, EndpointAddress
         return JNI_FALSE;
 }
 
+ACR_NET_EXPORT(jstring, EndpointAddress, name0)(JNI_STDARGS, jlong sa)
+{
+    char buf[256];
+    const char *name = 0;
+    acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
+
+    if (a == 0)
+        return 0;
+    if (a->hostname[0] == '\0') {
+        /* Return IP address representation
+         */
+        if (AcrGetSockaddrIp(buf, 256, a) == 0)
+            name = buf;
+    }
+    else
+        name = a->hostname;
+    return AcrNewJavaStringA(env, name);
+}
+
 acr_sockaddr_t *
 AcrGetSockaddr(JNI_STDARGS)
 {
@@ -889,6 +828,7 @@ ACR_NET_EXPORT(jint, SocketAddress, sock
     acr_sockaddr_t *sa = 0;
     int ffamily = AF_UNSPEC;
     int rc = 0;
+    int masked;
 
     switch (family) {
         case 1:
@@ -901,8 +841,17 @@ ACR_NET_EXPORT(jint, SocketAddress, sock
             ffamily = AF_LOCAL;
         break;
     }
+    /* TODO: Move the param checks to java code
+     */
+    if ((masked = flags & (ACR_IPV4_ADDR_OK | ACR_IPV6_ADDR_OK))) {
+        if (hostname == 0 || family != AF_UNSPEC ||
+            masked == (ACR_IPV4_ADDR_OK | ACR_IPV6_ADDR_OK)) {
+            return ACR_EINVAL;
+        }
+    }
+    
     WITH_CSTR(hostname) {
-        rc = AcrGetSockaddrInfo(&sa, J2S(hostname), ffamily, port, flags);
+        rc = AcrFindAddresses(&sa, J2S(hostname), ffamily, port, flags);
     } DONE_WITH_STR(hostname);
 
     if (rc == 0)
@@ -964,3 +913,60 @@ ACR_NET_EXPORT(jint, SocketAddress, next
         sa->next = 0;
     return rc;
 }
+
+ACR_NET_EXPORT(jint, LocalSocketAddress, sockaddr0)(JNI_STDARGS, jstring hostname)
+{
+    acr_sockaddr_t *sa = 0;
+    char *np;
+    int   rc = 0;
+
+    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;
+            while (*np != '\0') {
+                if (*np == '/' || *np == ' ')
+                    *np = '_';
+                np++;
+            }
+        }
+        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;
+#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;
+        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;
+#else
+        sa = 0;
+        rc = ACR_ENOTIMPL;
+#endif
+    } DONE_WITH_STR(hostname);
+
+    if (rc == 0)
+        rc = AcrSetSockaddr(env, obj, sa);
+    return rc;
+}

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java?rev=1100174&r1=1100173&r2=1100174&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
Fri May  6 11:39:58 2011
@@ -32,9 +32,8 @@ public class TestAddress extends Assert
     {
         LocalSocketAddress la = new LocalSocketAddress("foo/bar");
         assertNotNull(la);
-        assertEquals(la.getHostName(), "/var/run/foo_bar");
+        assertEquals(la.toString(), "/var/run/foo_bar");
         assertEquals(la.getFamily(), AddressFamily.LOCAL);
-        assertNull(la.next());
         la = null;
         System.gc();
     }
@@ -45,8 +44,7 @@ public class TestAddress extends Assert
     {
         LocalSocketAddress la = new LocalSocketAddress("foo/bar");
         assertNotNull(la);
-        assertEquals(la.getHostName(), "\\\\.\\pipe\\foo_bar");
-        assertNull(la.next());
+        assertEquals(la.toString(), "\\\\.\\pipe\\foo_bar");
         la = null;
         System.gc();
     }



Mime
View raw message