You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/04 23:23:59 UTC
svn commit: r1099610 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/net/ native/include/acr/ native/shared/
Author: mturk
Date: Wed May 4 21:23:58 2011
New Revision: 1099610
URL: http://svn.apache.org/viewvc?rev=1099610&view=rev
Log:
Use different class names for socket addresses
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java (with props)
Removed:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/AbstractSocketAddress.java
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java
commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
Added: 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=1099610&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java Wed May 4 21:23:58 2011
@@ -0,0 +1,78 @@
+/*
+ * 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.net;
+
+import java.io.File;
+import java.net.SocketException;
+import java.net.SocketAddress;
+import java.net.UnknownHostException;
+import org.apache.commons.runtime.Status;
+
+/**
+ * This class represents a socket endpoint described by a IP address and a port
+ * number. It is a concrete implementation of {@code SocketAddress} for IP.
+ */
+public abstract class EndpointAddress extends SocketAddress
+{
+
+ private AddressFamily family;
+ protected EndpointAddress()
+ {
+ // No instance
+ }
+
+ protected EndpointAddress(AddressFamily family)
+ {
+ this.family = family;
+ }
+
+ /**
+ * Gets the hostname of this socket.
+ *
+ * @return the socket endpoint hostname.
+ */
+ public abstract String getHostName();
+
+ /**
+ * Gets the service name of this socket.
+ *
+ * @return the socket endpoint service name.
+ */
+ public abstract String getServiceName();
+
+ /**
+ * Gets the port number of this socket.
+ *
+ * @return the socket endpoint port number.
+ */
+ public int getPort()
+ {
+ return 0;
+ }
+
+ /**
+ * Gets the {@code AddressFamily} of this socket.
+ *
+ * @return the socket AddressFamily.
+ */
+ public AddressFamily getFamily()
+ {
+ return family;
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointAddress.java
------------------------------------------------------------------------------
svn:eol-style = native
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=1099610&r1=1099609&r2=1099610&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 Wed May 4 21:23:58 2011
@@ -23,8 +23,14 @@ import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.runtime.Memory;
import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
-public final class LocalSocketAddress extends AbstractSocketAddress
+/**
+ * This class represents a local socket endpoint described by a name.
+ * <p>
+ * </p>
+ */
+public final class LocalSocketAddress extends SocketAddress
{
private LocalSocketAddress()
@@ -32,19 +38,22 @@ public final class LocalSocketAddress ex
// No instance
}
+ /**
+ * Create a new local socket adrress.
+ */
public LocalSocketAddress(String name)
- throws UnknownHostException
+ throws SystemException
{
- if (name == null)
- throw new IllegalArgumentException("name can't be null");
- create(name, AddressFamily.LOCAL, 0, 0);
+ super(name, AddressFamily.LOCAL);
}
+ /**
+ * Create a new local socket adrress.
+ */
public LocalSocketAddress(File path)
- throws UnknownHostException
+ throws SystemException
{
- if (path == null)
- throw new IllegalArgumentException("path can't be null");
- create(path.getPath(), AddressFamily.LOCAL, 0, 0);
+ super(path.getPath(), AddressFamily.LOCAL);
}
+
}
Added: 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=1099610&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java Wed May 4 21:23:58 2011
@@ -0,0 +1,204 @@
+/*
+ * 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.net;
+
+import java.io.File;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
+
+/**
+ * This class represents a socket endpoint described by a IP address and a port
+ * number. It is a concrete implementation of {@code java.net.SocketAddress} for IP.
+ */
+public abstract class SocketAddress extends EndpointAddress
+{
+ /**
+ * First query for IPv4 addresses; only look
+ * for IPv6 addresses if the first query failed;
+ * only valid if family is UNSPEC and hostname
+ * isn't null; mutually exclusive with IPV6_ADDR_OK
+ */
+ private static final int IPV4_ADDR_OK = 1;
+ /**
+ * First query for IPv6 addresses; only look
+ * for IPv4 addresses if the first query failed;
+ * only valid if family is UNSPEC and hostname
+ * isn't null; mutually exclusive with IPV4_ADDR_OK
+ */
+ private static final int IPV6_ADDR_OK = 2;
+
+ private static native void init0();
+ private static native void free0(long addr);
+ /* Structure members */
+ private static native String hostname0(long addr);
+ private static native String servname0(long addr);
+ private static native int port0(long addr);
+ private static native int family0(long addr);
+
+ private static native String ipaddr0(long addr);
+ private static native long geti0(String hostname, int family, int port, int flags)
+ throws SystemException;
+ private static native boolean equals0(long addr1, long addr2);
+
+ /**
+ * Native representation of @{code this} address.
+ * This is pointer to acr_sockaddr_t and is directly
+ * used by socket functions.
+ *
+ */
+ private long addr;
+ /**
+ * True if this address has been resolved.
+ */
+ private boolean resolved;
+
+ static {
+ init0();
+ }
+
+ /**
+ * Creates an new object
+ */
+ protected SocketAddress()
+ {
+ super(AddressFamily.UNSPEC);
+ }
+
+ /**
+ * Creates an new object
+ */
+ protected SocketAddress(String host, AddressFamily family, int port, int flags)
+ throws SystemException
+ {
+ super(family);
+ if (host == null && family == AddressFamily.LOCAL)
+ throw new IllegalArgumentException("host can't be null");
+ addr = geti0(host, family.valueOf(), port, flags);
+ }
+
+ protected SocketAddress(String host, AddressFamily family)
+ throws SystemException
+ {
+ super(family);
+ if (host == null && family == AddressFamily.LOCAL)
+ throw new IllegalArgumentException("host can't be null");
+ addr = geti0(host, family.valueOf(), 0, 0);
+ }
+
+ protected SocketAddress(String host, int port)
+ throws SystemException
+ {
+ super(AddressFamily.UNSPEC);
+ addr = geti0(host, 0, port, 0);
+ }
+
+ protected SocketAddress(int port)
+ throws SystemException
+ {
+ super(AddressFamily.UNSPEC);
+ addr = geti0(null, 0, port, 0);
+ }
+
+ /**
+ * Gets the hostname of this socket.
+ *
+ * @return the socket endpoint hostname.
+ */
+ @Override
+ public final String getHostName()
+ {
+ return hostname0(addr);
+ }
+
+ /**
+ * Gets the service name of this socket.
+ *
+ * @return the socket endpoint service name.
+ */
+ @Override
+ public final String getServiceName()
+ {
+ return servname0(addr);
+ }
+
+ /**
+ * Gets the port number of this socket.
+ *
+ * @return the socket endpoint port number.
+ */
+ @Override
+ public int getPort()
+ {
+ return port0(addr);
+ }
+
+ /**
+ * Gets the {@code AddressFamily} of this socket.
+ *
+ * @return the socket AddressFamily.
+ */
+ @Override
+ public final AddressFamily getFamily()
+ {
+ if (super.getFamily() == AddressFamily.LOCAL)
+ return AddressFamily.LOCAL;
+ else
+ return AddressFamily.valueOf(family0(addr));
+ }
+
+ /**
+ * Compares this {@code SocketAddress} to the specified object.
+ *
+ * @param other the reference {@code SocketAddress} with which to compare.
+ * @return {@code true} if the class of this {@code SocketAddress} object and the
+ * class of {@code other} are exactly equal; {@code false} otherwise.
+ */
+ @Override
+ public final boolean equals(Object other)
+ {
+ if (other == null)
+ return false;
+ if (other == this)
+ return true;
+ if (other instanceof SocketAddress)
+ return equals0(addr, ((SocketAddress)other).addr);
+ else
+ 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 void finalize()
+ throws Throwable
+ {
+ try {
+ free0(addr);
+ } finally {
+ addr = 0L;
+ }
+ }
+
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketAddress.java
------------------------------------------------------------------------------
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=1099610&r1=1099609&r2=1099610&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 Wed May 4 21:23:58 2011
@@ -1613,6 +1613,8 @@ ACR_INLINE(DWORD) AcrNetOsError()
#define ACR_THROW_MSG(CL, MS) AcrThrow(env, (CL), MS)
#define ACR_THROW_BY_ERRNO() AcrThrowByStatus(env, ACR_GET_OS_ERROR(), 0)
#define ACR_THROW_IO_ERRNO() AcrThrowIoStatus(env, ACR_GET_OS_ERROR(), 0)
+#define ACR_THROW_BY_ERROR(ER) AcrThrowByStatus(env, (ER), 0)
+#define ACR_THROW_IO_ERROR(ER) AcrThrowIoStatus(env, (ER), 0)
#ifdef __cplusplus
extern "C" {
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=1099610&r1=1099609&r2=1099610&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Wed May 4 21:23:58 2011
@@ -23,6 +23,7 @@
#include "acr/memory.h"
#include "acr/string.h"
#include "acr/port.h"
+#include "acr/unsafe.h"
#define V4MAPPED_EQUAL(a,b) \
((a)->sa.sin.sin_family == AF_INET && \
@@ -44,6 +45,22 @@
# define SET_H_ERRNO(newval)
#endif
+J_DECLARE_CLAZZ = {
+ INVALID_FIELD_OFFSET,
+ 0,
+ 0,
+ 0,
+ ACR_NET_CP "SocketAddress"
+};
+
+J_DECLARE_F_ID(0000) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "addr",
+ "J"
+};
+
/* const char *
* inet_ntop4(src, dst, size)
* format an IPv4 address, more or less like inet_ntoa()
@@ -587,9 +604,7 @@ AcrGetSockaddrInfo(acr_sockaddr_t **sa,
int family, int port, int flags)
{
int masked;
-#if defined(WINDOWS)
char *np;
-#endif
*sa = 0;
/* TODO: Move the param checks to java code
@@ -608,8 +623,19 @@ AcrGetSockaddrInfo(acr_sockaddr_t **sa,
*sa = calloc(1, sizeof(acr_sockaddr_t));
if (*sa == 0)
return ACR_ENOMEM;
- strlcpy((*sa)->sa.unx.sun_path, hostname, sizeof((*sa)->sa.unx.sun_path));
- strlcpy((*sa)->hostname, hostname, sizeof((*sa)->hostname));
+ 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);
@@ -658,21 +684,6 @@ AcrFreeSockaddr(acr_sockaddr_t *sa)
}
}
-int
-AcrIsSockaddrEqual(const acr_sockaddr_t *addr1,
- const acr_sockaddr_t *addr2)
-{
- if (addr1->iplen == addr2->iplen &&
- !memcmp(addr1->ipaddr, addr2->ipaddr, addr1->iplen)) {
- return 1;
- }
- if (V4MAPPED_EQUAL(addr1, addr2))
- return 1;
- if (V4MAPPED_EQUAL(addr2, addr1))
- return 1;
- return 0; /* not equal */
-}
-
#define GETHOSTBYNAME_BUFLEN 512
int
@@ -741,17 +752,22 @@ AcrGetNameInfo(char **hostname, acr_sock
return 0;
}
-ACR_NET_EXPORT(jlong, AbstractSocketAddress, alloc0)(JNI_STDARGS)
+ACR_NET_EXPORT(void, SocketAddress, init0)(JNI_STDARGS)
{
- return P2J(ACR_TALLOC(acr_sockaddr_t));
+ _clazzn.i = (jclass)(*env)->NewGlobalRef(env, obj);
+ if (_clazzn.i == 0)
+ return;
+ V_LOAD_IFIELD(0000);
+ UNSAFE_IFIELD(0000);
+ _clazzn.u = 1;
}
-ACR_NET_EXPORT(void, AbstractSocketAddress, free0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(void, SocketAddress, free0)(JNI_STDARGS, jlong sa)
{
AcrFreeSockaddr(J2P(sa, acr_sockaddr_t *));
}
-ACR_NET_EXPORT(jstring, AbstractSocketAddress, hostname0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(jstring, SocketAddress, hostname0)(JNI_STDARGS, jlong sa)
{
acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
if (a->hostname != '\0')
@@ -760,7 +776,7 @@ ACR_NET_EXPORT(jstring, AbstractSocketAd
return 0;
}
-ACR_NET_EXPORT(jstring, AbstractSocketAddress, servname0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(jstring, SocketAddress, servname0)(JNI_STDARGS, jlong sa)
{
acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
if (a->servname != '\0')
@@ -769,7 +785,7 @@ ACR_NET_EXPORT(jstring, AbstractSocketAd
return 0;
}
-ACR_NET_EXPORT(jstring, AbstractSocketAddress, ipaddr0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(jstring, SocketAddress, ipaddr0)(JNI_STDARGS, jlong sa)
{
char buf[256];
acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
@@ -782,13 +798,13 @@ ACR_NET_EXPORT(jstring, AbstractSocketAd
}
}
-ACR_NET_EXPORT(jint, AbstractSocketAddress, port0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(jint, SocketAddress, port0)(JNI_STDARGS, jlong sa)
{
acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
return a->port;
}
-ACR_NET_EXPORT(jint, AbstractSocketAddress, family0)(JNI_STDARGS, jlong sa)
+ACR_NET_EXPORT(jint, SocketAddress, family0)(JNI_STDARGS, jlong sa)
{
acr_sockaddr_t *a = J2P(sa, acr_sockaddr_t *);
switch (a->family) {
@@ -804,7 +820,7 @@ ACR_NET_EXPORT(jint, AbstractSocketAddre
return 0;
}
-ACR_NET_EXPORT(jlong, AbstractSocketAddress, geti0)(JNI_STDARGS, jstring hostname,
+ACR_NET_EXPORT(jlong, SocketAddress, geti0)(JNI_STDARGS, jstring hostname,
jint family, jint port, jint flags)
{
acr_sockaddr_t *sa = 0;
@@ -829,7 +845,22 @@ ACR_NET_EXPORT(jlong, AbstractSocketAddr
if (rc != 0) {
/* XXX: Throw UnknownHostException?
*/
- ACR_THROW(ACR_EX_EHOST, rc);
+ ACR_THROW_BY_ERROR(rc);
}
return P2J(sa);
}
+
+ACR_NET_EXPORT(jboolean, SocketAddress, equals0)(jlong sa1, jlong sa2)
+{
+ acr_sockaddr_t *addr1 = J2P(sa1, acr_sockaddr_t *);
+ acr_sockaddr_t *addr2 = J2P(sa2, acr_sockaddr_t *);
+ if (addr1->iplen == addr2->iplen &&
+ memcmp(addr1->ipaddr, addr2->ipaddr, addr1->iplen) == 0) {
+ return JNI_TRUE;
+ }
+ if (V4MAPPED_EQUAL(addr1, addr2))
+ return 1;
+ if (V4MAPPED_EQUAL(addr2, addr1))
+ return JNI_TRUE;
+ return JNI_FALSE; /* not equal */
+}