Skip to content

Commit 07d2e80

Browse files
committed
Modified to conform to driver style and compile at Java 6 language level JAVA-3038
1 parent 9b19e93 commit 07d2e80

21 files changed

+3985
-0
lines changed

THIRD-PARTY-NOTICES

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,47 @@ https://github.com/mongodb/mongo-java-driver.
104104
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
105105
See the License for the specific language governing permissions and
106106
limitations under the License.
107+
108+
7) The following files (originally from https://github.com/marianobarrios/tls-channel):
109+
110+
AsynchronousTlsChannel.java
111+
AsynchronousTlsChannelGroup.java
112+
ExtendedAsynchronousByteChannel.java
113+
BufferHolder.java
114+
ByteBufferSet.java
115+
ByteBufferUtil.java
116+
TlsChannelImpl.java
117+
TlsChannelCallbackException.java
118+
Util.java
119+
BufferAllocator.java
120+
ClientTlsChannel.java
121+
NeedsReadException.java
122+
NeedsTaskException.java
123+
NeedsWriteException.java
124+
TlsChannel.java
125+
TlsChannelBuilder.java
126+
TlsChannelFlowControlException.java
127+
TrackingAllocator.java
128+
WouldBlockException.java
129+
130+
Copyright (c) [2015-2018] all contributors
131+
132+
MIT License
133+
134+
Permission is hereby granted, free of charge, to any person obtaining a copy
135+
of this software and associated documentation files (the "Software"), to deal
136+
in the Software without restriction, including without limitation the rights
137+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
138+
copies of the Software, and to permit persons to whom the Software is
139+
furnished to do so, subject to the following conditions:
140+
141+
The above copyright notice and this permission notice shall be included in all
142+
copies or substantial portions of the Software.
143+
144+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
145+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
146+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
147+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
148+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
149+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
150+
SOFTWARE.

config/checkstyle-exclude.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
2222

2323
<suppressions>
24+
25+
<suppress checks="VisibilityModifier" files="com[\\/]mongodb[\\/]internal[\\/]connection[\\/]tlschannel[\\/]"/>
26+
2427
<suppress checks="MethodLength" files="QuickTour"/>
2528
<suppress checks="Regexp" files="QuickTour"/>
2629

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Original Work: MIT License, Copyright (c) [2015-2018] all contributors
17+
* https://github.com/marianobarrios/tls-channel
18+
*/
19+
20+
package com.mongodb.internal.connection.tlschannel;
21+
22+
import org.bson.ByteBuf;
23+
24+
/**
25+
* A factory for {@link ByteBuf}s. Implementations are free to return heap or
26+
* direct buffers, or to do any kind of pooling. They are also expected to be
27+
* thread-safe.
28+
*/
29+
public interface BufferAllocator {
30+
31+
/**
32+
* Allocate a {@link ByteBuf} with the given initial capacity.
33+
*/
34+
ByteBuf allocate(int size);
35+
36+
/**
37+
* Deallocate the given {@link ByteBuf}.
38+
*
39+
* @param buffer the buffer to deallocate, that should have been allocated using
40+
* the same {@link BufferAllocator} instance
41+
*/
42+
void free(ByteBuf buffer);
43+
44+
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Original Work: MIT License, Copyright (c) [2015-2018] all contributors
17+
* https://github.com/marianobarrios/tls-channel
18+
*/
19+
20+
package com.mongodb.internal.connection.tlschannel;
21+
22+
import com.mongodb.internal.connection.tlschannel.impl.BufferHolder;
23+
import com.mongodb.internal.connection.tlschannel.impl.ByteBufferSet;
24+
import com.mongodb.internal.connection.tlschannel.impl.TlsChannelImpl;
25+
26+
import javax.net.ssl.SSLContext;
27+
import javax.net.ssl.SSLEngine;
28+
import javax.net.ssl.SSLSession;
29+
import java.io.IOException;
30+
import java.nio.ByteBuffer;
31+
import java.nio.channels.ByteChannel;
32+
import java.nio.channels.Channel;
33+
import java.util.Optional;
34+
import java.util.function.Consumer;
35+
import java.util.function.Supplier;
36+
37+
/**
38+
* A client-side {@link TlsChannel}.
39+
*/
40+
public final class ClientTlsChannel implements TlsChannel {
41+
42+
/**
43+
* Builder of {@link ClientTlsChannel}
44+
*/
45+
public static final class Builder extends TlsChannelBuilder<Builder> {
46+
47+
private Supplier<SSLEngine> sslEngineFactory;
48+
49+
private Builder(final ByteChannel underlying, final SSLEngine sslEngine) {
50+
super(underlying);
51+
this.sslEngineFactory = new Supplier<SSLEngine>() {
52+
@Override
53+
public SSLEngine get() {
54+
return sslEngine;
55+
}
56+
};
57+
}
58+
59+
private Builder(final ByteChannel underlying, final SSLContext sslContext) {
60+
super(underlying);
61+
this.sslEngineFactory = new Supplier<SSLEngine>() {
62+
@Override
63+
public SSLEngine get() {
64+
return defaultSSLEngineFactory(sslContext);
65+
}
66+
};
67+
}
68+
69+
@Override
70+
Builder getThis() {
71+
return this;
72+
}
73+
74+
public ClientTlsChannel build() {
75+
return new ClientTlsChannel(underlying, sslEngineFactory.get(), sessionInitCallback, runTasks,
76+
plainBufferAllocator, encryptedBufferAllocator, releaseBuffers, waitForCloseConfirmation);
77+
}
78+
79+
}
80+
81+
private static SSLEngine defaultSSLEngineFactory(final SSLContext sslContext) {
82+
SSLEngine engine = sslContext.createSSLEngine();
83+
engine.setUseClientMode(true);
84+
return engine;
85+
}
86+
87+
/**
88+
* Create a new {@link Builder}, configured with a underlying
89+
* {@link Channel} and a fixed {@link SSLEngine}.
90+
*
91+
* @param underlying a reference to the underlying {@link ByteChannel}
92+
* @param sslEngine the engine to use with this channel
93+
*/
94+
public static Builder newBuilder(final ByteChannel underlying, final SSLEngine sslEngine) {
95+
return new Builder(underlying, sslEngine);
96+
}
97+
98+
/**
99+
* Create a new {@link Builder}, configured with a underlying
100+
* {@link Channel} and a {@link SSLContext}.
101+
*
102+
* @param underlying a reference to the underlying {@link ByteChannel}
103+
* @param sslContext a context to use with this channel, it will be used to create a client {@link SSLEngine}.
104+
*/
105+
public static Builder newBuilder(final ByteChannel underlying, final SSLContext sslContext) {
106+
return new Builder(underlying, sslContext);
107+
}
108+
109+
private final ByteChannel underlying;
110+
private final TlsChannelImpl impl;
111+
112+
private ClientTlsChannel(
113+
final ByteChannel underlying,
114+
final SSLEngine engine,
115+
final Consumer<SSLSession> sessionInitCallback,
116+
final boolean runTasks,
117+
final BufferAllocator plainBufAllocator,
118+
final BufferAllocator encryptedBufAllocator,
119+
final boolean releaseBuffers,
120+
final boolean waitForCloseNotifyOnClose) {
121+
if (!engine.getUseClientMode()) {
122+
throw new IllegalArgumentException("SSLEngine must be in client mode");
123+
}
124+
this.underlying = underlying;
125+
TrackingAllocator trackingPlainBufAllocator = new TrackingAllocator(plainBufAllocator);
126+
TrackingAllocator trackingEncryptedAllocator = new TrackingAllocator(encryptedBufAllocator);
127+
impl = new TlsChannelImpl(underlying, underlying, engine, Optional.<BufferHolder>empty(), sessionInitCallback, runTasks,
128+
trackingPlainBufAllocator, trackingEncryptedAllocator, releaseBuffers, waitForCloseNotifyOnClose);
129+
}
130+
131+
@Override
132+
public ByteChannel getUnderlying() {
133+
return underlying;
134+
}
135+
136+
@Override
137+
public SSLEngine getSslEngine() {
138+
return impl.engine();
139+
}
140+
141+
@Override
142+
public Consumer<SSLSession> getSessionInitCallback() {
143+
return impl.getSessionInitCallback();
144+
}
145+
146+
@Override
147+
public TrackingAllocator getPlainBufferAllocator() {
148+
return impl.getPlainBufferAllocator();
149+
}
150+
151+
@Override
152+
public TrackingAllocator getEncryptedBufferAllocator() {
153+
return impl.getEncryptedBufferAllocator();
154+
}
155+
156+
@Override
157+
public boolean getRunTasks() {
158+
return impl.getRunTasks();
159+
}
160+
161+
@Override
162+
public long read(final ByteBuffer[] dstBuffers, final int offset, final int length) throws IOException {
163+
ByteBufferSet dest = new ByteBufferSet(dstBuffers, offset, length);
164+
TlsChannelImpl.checkReadBuffer(dest);
165+
return impl.read(dest);
166+
}
167+
168+
@Override
169+
public long read(final ByteBuffer[] dstBuffers) throws IOException {
170+
return read(dstBuffers, 0, dstBuffers.length);
171+
}
172+
173+
@Override
174+
public int read(final ByteBuffer dstBuffer) throws IOException {
175+
return (int) read(new ByteBuffer[]{dstBuffer});
176+
}
177+
178+
@Override
179+
public long write(final ByteBuffer[] srcBuffers, final int offset, final int length) throws IOException {
180+
ByteBufferSet source = new ByteBufferSet(srcBuffers, offset, length);
181+
return impl.write(source);
182+
}
183+
184+
@Override
185+
public long write(final ByteBuffer[] outs) throws IOException {
186+
return write(outs, 0, outs.length);
187+
}
188+
189+
@Override
190+
public int write(final ByteBuffer srcBuffer) throws IOException {
191+
return (int) write(new ByteBuffer[]{srcBuffer});
192+
}
193+
194+
@Override
195+
public void renegotiate() throws IOException {
196+
impl.renegotiate();
197+
}
198+
199+
@Override
200+
public void handshake() throws IOException {
201+
impl.handshake();
202+
}
203+
204+
@Override
205+
public void close() throws IOException {
206+
impl.close();
207+
}
208+
209+
@Override
210+
public boolean isOpen() {
211+
return impl.isOpen();
212+
}
213+
214+
@Override
215+
public boolean shutdown() throws IOException {
216+
return impl.shutdown();
217+
}
218+
219+
@Override
220+
public boolean shutdownReceived() {
221+
return impl.shutdownReceived();
222+
}
223+
224+
@Override
225+
public boolean shutdownSent() {
226+
return impl.shutdownSent();
227+
}
228+
229+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Original Work: MIT License, Copyright (c) [2015-2018] all contributors
17+
* https://github.com/marianobarrios/tls-channel
18+
*/
19+
20+
package com.mongodb.internal.connection.tlschannel;
21+
22+
import java.nio.channels.ByteChannel;
23+
import java.nio.channels.Selector;
24+
import java.nio.channels.SocketChannel;
25+
26+
/**
27+
* This exception signals the caller that the operation cannot continue because
28+
* bytesProduced need to be read from the underlying {@link ByteChannel}, the channel is
29+
* non-blocking and there are no bytesProduced available. The caller should try the
30+
* operation again, either with the channel in blocking mode of after ensuring
31+
* that bytesProduced are ready.
32+
* <p>
33+
* For {@link SocketChannel}s, a {@link Selector} can be used to find out when
34+
* the method should be retried.
35+
* <p>
36+
* Caveat: Any {@link TlsChannel} I/O method can throw this exception. In
37+
* particular, <code>write</code> may want to read data. This is because TLS
38+
* handshakes may occur at any time (initiated by either the client or the
39+
* server).
40+
* <p>
41+
* This exception is akin to the SSL_ERROR_WANT_READ error code used by OpenSSL.
42+
*
43+
* @see <a href="https://www.openssl.org/docs/man1.1.0/ssl/SSL_get_error.html">
44+
* OpenSSL error documentation</a>
45+
*/
46+
public class NeedsReadException extends WouldBlockException {
47+
48+
private static final long serialVersionUID = 13846137963119227L;
49+
}

0 commit comments

Comments
 (0)