Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Commit 931458a

Browse files
committed
kludge WriteTimeoutSocket to work properly on Android - bug 7513
1 parent 11d5352 commit 931458a

4 files changed

Lines changed: 74 additions & 3 deletions

File tree

doc/release/CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ K 7471 InternetAddress.getLocalAddress should use
3333
InetAddress.getCanonicalHostName
3434
K 7472 Store finalizers should not talk to server
3535
K 7512 NullPointerException if SASL is enabled on Android
36+
K 7513 write timeouts don't work with SSL on Android
3637

3738

3839
CHANGES IN THE 1.5.5 RELEASE

mail/src/main/java/com/sun/mail/util/WriteTimeoutSocket.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -44,6 +44,7 @@
4444
import java.net.*;
4545
import java.util.concurrent.*;
4646
import java.nio.channels.SocketChannel;
47+
import java.lang.reflect.*;
4748

4849
/**
4950
* A special Socket that uses a ScheduledExecutorService to
@@ -117,6 +118,22 @@ public void bind(SocketAddress local) throws IOException {
117118
socket.bind(local);
118119
}
119120

121+
@Override
122+
public SocketAddress getRemoteSocketAddress() {
123+
return socket.getRemoteSocketAddress();
124+
}
125+
126+
@Override
127+
public SocketAddress getLocalSocketAddress() {
128+
return socket.getLocalSocketAddress();
129+
}
130+
131+
@Override
132+
public void setPerformancePreferences(int connectionTime, int latency,
133+
int bandwidth) {
134+
socket.setPerformancePreferences(connectionTime, latency, bandwidth);
135+
}
136+
120137
@Override
121138
public SocketChannel getChannel() {
122139
return socket.getChannel();
@@ -296,6 +313,18 @@ public boolean isInputShutdown() {
296313
public boolean isOutputShutdown() {
297314
return socket.isOutputShutdown();
298315
}
316+
317+
/**
318+
* KLUDGE for Android, which has this illegal non-Java Compatible method.
319+
*/
320+
public FileDescriptor getFileDescriptor$() {
321+
try {
322+
Method m = Socket.class.getDeclaredMethod("getFileDescriptor$");
323+
return (FileDescriptor)m.invoke(socket);
324+
} catch (Exception ex) {
325+
return null;
326+
}
327+
}
299328
}
300329

301330

mail/src/test/java/com/sun/mail/test/ProtocolHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2009-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -48,6 +48,7 @@
4848
import java.net.SocketException;
4949
import java.util.logging.Logger;
5050
import java.util.logging.Level;
51+
import javax.net.ssl.SSLException;
5152

5253
/**
5354
* Handle protocol connection.
@@ -113,6 +114,8 @@ public final void run() {
113114
//clientSocket.close();
114115
} catch (SocketException sex) {
115116
// ignore it, often get "connection reset" when client closes
117+
} catch (SSLException sex) {
118+
// ignore it, often occurs when testing SSL
116119
} catch (Exception e) {
117120
LOGGER.log(Level.SEVERE, "Error", e);
118121
} finally {

mail/src/test/java/com/sun/mail/util/WriteTimeoutSocketTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2009-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2009-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -40,11 +40,15 @@
4040

4141
package com.sun.mail.util;
4242

43+
import java.lang.reflect.*;
44+
4345
import java.io.IOException;
4446
import java.io.InterruptedIOException;
4547
import java.util.Properties;
4648
import java.util.List;
4749
import java.util.ArrayList;
50+
import java.util.Set;
51+
import java.util.HashSet;
4852

4953
import javax.mail.*;
5054
import javax.mail.internet.MimeMessage;
@@ -142,6 +146,40 @@ public void testSSLSocketFactory() throws Exception {
142146
assertTrue(sf.getSocketWrapped() || sf.getSocketCreated());
143147
}
144148

149+
/**
150+
* Test that WriteTimeoutSocket overrides all methods from Socket.
151+
* XXX - this is kind of hacky since it depends on Method.toString
152+
*/
153+
@Test
154+
public void testOverrides() throws Exception {
155+
Set<String> socketMethods = new HashSet<String>();
156+
Method[] m = java.net.Socket.class.getDeclaredMethods();
157+
String className = java.net.Socket.class.getName() + ".";
158+
for (int i = 0; i < m.length; i++) {
159+
if (Modifier.isPublic(m[i].getModifiers()) &&
160+
!Modifier.isStatic(m[i].getModifiers())) {
161+
String name = m[i].toString().
162+
replace("synchronized ", "").
163+
replace(className, "");
164+
socketMethods.add(name);
165+
}
166+
}
167+
Set<String> wtsocketMethods = new HashSet<String>();
168+
m = WriteTimeoutSocket.class.getDeclaredMethods();
169+
className = WriteTimeoutSocket.class.getName() + ".";
170+
for (int i = 0; i < m.length; i++) {
171+
if (Modifier.isPublic(m[i].getModifiers())) {
172+
String name = m[i].toString().
173+
replace("synchronized ", "").
174+
replace(className, "");
175+
socketMethods.remove(name);
176+
}
177+
}
178+
for (String s : socketMethods)
179+
System.out.println("WriteTimeoutSocket did not override: " + s);
180+
assertTrue(socketMethods.isEmpty());
181+
}
182+
145183
private static String[] getAnonCipherSuitesArray() {
146184
SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
147185
List<String> anon = new ArrayList<String>();

0 commit comments

Comments
 (0)