1717package one .nio .net ;
1818
1919import java .io .IOException ;
20- import java .io .RandomAccessFile ;
2120
2221class NativeSslSocket extends NativeSocket {
23- NativeSslContext context ;
22+ volatile NativeSslContext context ;
23+ private volatile NativeSslContext previousContext ;
2424 long ssl ;
2525
2626 private volatile boolean isEarlyDataAccepted = false ;
@@ -39,6 +39,11 @@ public synchronized void close() {
3939 sslFree (ssl );
4040 ssl = 0 ;
4141 }
42+ NativeSslContext prev = previousContext ;
43+ if (prev != null ) {
44+ prev .close ();
45+ previousContext = null ;
46+ }
4247 super .close ();
4348 }
4449
@@ -64,6 +69,22 @@ public SslContext getSslContext() {
6469 return context ;
6570 }
6671
72+ @ Override
73+ public void setSslContext (SslContext newContext ) throws IOException {
74+ NativeSslContext nativeCtx = (NativeSslContext ) newContext ;
75+
76+ // Close the context from the PREVIOUS reconfigure.
77+ // By now it is safe: at least one full volatile writeread cycle has passed
78+ NativeSslContext prev = this .previousContext ;
79+ if (prev != null ) {
80+ prev .close ();
81+ }
82+
83+ NativeSslContext old = this .context ;
84+ this .context = nativeCtx ;
85+ this .previousContext = old ; // defer context close to next reconfigure/socket close
86+ }
87+
6788 @ Override
6889 @ SuppressWarnings ("unchecked" )
6990 public Object getSslOption (SslOption option ) {
@@ -93,6 +114,7 @@ public Object getSslOption(SslOption option) {
93114 }
94115 return null ;
95116 }
117+
96118 @ Override
97119 public synchronized native void handshake (String sniHostName ) throws IOException ;
98120
@@ -128,16 +150,22 @@ private boolean sslHandshakeDone() {
128150 }
129151
130152 private synchronized native byte [] sslPeerCertificate ();
153+
131154 private synchronized native Object [] sslPeerCertificateChain ();
155+
132156 private synchronized native String sslCertName (int which );
157+
133158 private synchronized native String sslVerifyResult ();
134159
135160 private synchronized native boolean sslSessionReused ();
161+
136162 private synchronized native int sslSessionTicket ();
137163
138164 private synchronized native String sslCurrentCipher ();
165+
139166 private synchronized native boolean sslCanUseSendfile ();
140167
141168 static native long sslNew (int fd , long ctx , boolean serverMode ) throws IOException ;
169+
142170 static native void sslFree (long ssl );
143171}
0 commit comments