harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r433944 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/org/apache/harmony/luni/net/PlainSocketImpl.java test/java/tests/api/java/net/ServerSocketTest.java
Date Wed, 23 Aug 2006 06:53:01 GMT
Author: pyang
Date: Tue Aug 22 23:53:00 2006
New Revision: 433944

URL: http://svn.apache.org/viewvc?rev=433944&view=rev
Log:
Patch applied for HARMONY-1235 ([classlib][net] unexpected ClassCastException for ServerSocket.implAccept(Socket))

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ServerSocketTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java?rev=433944&r1=433943&r2=433944&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
Tue Aug 22 23:53:00 2006
@@ -20,6 +20,7 @@
 import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.io.OutputStream;
+import java.lang.reflect.Field;
 import java.net.ConnectException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -30,6 +31,8 @@
 import java.net.SocketOptions;
 import java.net.SocketTimeoutException;
 import java.net.UnknownHostException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 
 import org.apache.harmony.luni.platform.INetworkSystem;
 import org.apache.harmony.luni.platform.Platform;
@@ -58,8 +61,6 @@
 
 	private boolean tcpNoDelay = true;
 
-	private Object connectLock = new Object();
-
 	// used to store the trafficClass value which is simply returned
 	// as the value that was set. We also need it to pass it to methods
 	// that specify an address packets are going to be sent to
@@ -74,6 +75,10 @@
 	public boolean shutdownInput = false;
 	
 	Proxy proxy = null;
+	
+	private static Field fdField = null;
+	
+	private static Field localportField = null;
 
 	/**
 	 * Accepts a connection on the provided socket, by calling the IP stack.
@@ -91,14 +96,50 @@
 		}
 
 		try {
-			netImpl.acceptStreamSocket(fd, newImpl, ((PlainSocketImpl) newImpl)
-					.getFD(), receiveTimeout);
+			if (newImpl instanceof PlainSocketImpl) {
+				PlainSocketImpl newPlainSocketImpl = (PlainSocketImpl) newImpl;
+				netImpl.acceptStreamSocket(fd, newImpl, newPlainSocketImpl
+						.getFileDescriptor(), receiveTimeout);
+				newPlainSocketImpl.setLocalport(getLocalPort());
+			} else {
+				// if newImpl is not an instance of PlainSocketImpl, use
+				// reflection to get/set protected fields.
+				if (null == fdField) {
+					fdField = getSocketImplField("fd"); // $NON-NLS-1$
+				}
+				FileDescriptor newFd = (FileDescriptor) fdField.get(newImpl);
+				netImpl.acceptStreamSocket(fd, newImpl, newFd, receiveTimeout);
+
+				if (null == localportField) {
+					localportField = getSocketImplField("localport"); // $NON-NLS-1$
+				}
+				localportField.setInt(newImpl, getLocalPort());
+			}
 		} catch (InterruptedIOException e) {
 			throw new SocketTimeoutException(e.getMessage());
-		}
-		((PlainSocketImpl) newImpl).setLocalport(getLocalPort());
+		} catch (IllegalAccessException e) {
+			// empty
+		}	
 	}
 
+	/*
+	 * gets SocketImpl field by reflection.
+	 */
+	private Field getSocketImplField(final String fieldName) {
+		return AccessController.doPrivileged(new PrivilegedAction<Field>() {
+			public Field run() {
+				Field field = null;
+				try {
+					field = SocketImpl.class.getDeclaredField(fieldName); //$NON-NLS-1$
+					field.setAccessible(true);
+				} catch (NoSuchFieldException e) {
+				    throw new Error(e);
+				}
+				return field;
+			}
+		});
+	}
+	
 	/**
 	 * Answer the number of bytes that may be read from this socket without
 	 * blocking. This call does not block.

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ServerSocketTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ServerSocketTest.java?rev=433944&r1=433943&r2=433944&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ServerSocketTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ServerSocketTest.java
Tue Aug 22 23:53:00 2006
@@ -16,7 +16,9 @@
 package tests.api.java.net;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InterruptedIOException;
+import java.io.OutputStream;
 import java.net.ConnectException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -24,6 +26,7 @@
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketException;
+import java.net.SocketImpl;
 import java.util.Date;
 import java.util.Properties;
 
@@ -946,6 +949,93 @@
 			Thread.sleep(1000);
 		} catch (java.lang.InterruptedException e) {
 			System.out.println("Exception during startClinet()" + e.toString());
+		}
+	}
+	
+	/**
+	 * @tests java.net.ServerSocket#implAccept
+	 */
+	public void test_implAcceptLjava_net_Socket() throws Exception {
+		// regression test for Harmony-1235
+		try {
+			new MockServerSocket().mockImplAccept(new MockSocket(
+					new MockSocketImpl()));
+		} catch (SocketException e) {
+			// expected
+		}
+	}
+	
+	static class MockSocketImpl extends SocketImpl {
+		protected void create(boolean arg0) throws IOException {
+			// empty
+		}
+
+		protected void connect(String arg0, int arg1) throws IOException {
+			// empty
+		}
+
+		protected void connect(InetAddress arg0, int arg1) throws IOException {
+			// empty
+		}
+
+		protected void connect(SocketAddress arg0, int arg1) throws IOException {
+			// empty
+		}
+
+		protected void bind(InetAddress arg0, int arg1) throws IOException {
+			// empty
+		}
+
+		protected void listen(int arg0) throws IOException {
+			// empty
+		}
+
+		protected void accept(SocketImpl arg0) throws IOException {
+			// empty
+		}
+
+		protected InputStream getInputStream() throws IOException {
+			return null;
+		}
+
+		protected OutputStream getOutputStream() throws IOException {
+			return null;
+		}
+
+		protected int available() throws IOException {
+			return 0;
+		}
+
+		protected void close() throws IOException {
+			// empty
+		}
+
+		protected void sendUrgentData(int arg0) throws IOException {
+			// empty
+		}
+
+		public void setOption(int arg0, Object arg1) throws SocketException {
+			// empty
+		}
+
+		public Object getOption(int arg0) throws SocketException {
+			return null;
+		}
+	}
+
+	static class MockSocket extends Socket {
+		public MockSocket(SocketImpl impl) throws SocketException {
+			super(impl);
+		}
+	}
+
+	static class MockServerSocket extends ServerSocket {
+		public MockServerSocket() throws Exception {
+			super();
+		}
+
+		public void mockImplAccept(Socket s) throws Exception {
+			super.implAccept(s);
 		}
 	}
 }



Mime
View raw message