
org.komamitsu.fluency.sender.SSLSender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluency Show documentation
Show all versions of fluency Show documentation
Yet another fluent logger
The newest version!
/*
* Copyright 2018 Mitsunori Komatsu (komamitsu)
*
* Licensed 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.komamitsu.fluency.sender;
import org.komamitsu.fluency.sender.failuredetect.FailureDetectStrategy;
import org.komamitsu.fluency.sender.failuredetect.FailureDetector;
import org.komamitsu.fluency.sender.heartbeat.Heartbeater;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
public class SSLSender
extends NetworkSender
{
private final AtomicReference socket = new AtomicReference();
private final SSLSocketBuilder socketBuilder;
private final Config config;
public SSLSender(Config config)
{
super(config.baseConfig);
socketBuilder = new SSLSocketBuilder(
config.getHost(),
config.getPort(),
config.getConnectionTimeoutMilli(),
config.getReadTimeoutMilli());
this.config = config;
}
@Override
protected SSLSocket getOrCreateSocketInternal()
throws IOException
{
if (socket.get() == null) {
socket.set(socketBuilder.build());
}
return socket.get();
}
@Override
protected void sendBuffers(SSLSocket sslSocket, List buffers)
throws IOException
{
for (ByteBuffer buffer : buffers) {
OutputStream outputStream = sslSocket.getOutputStream();
if (buffer.isDirect()) {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
outputStream.write(bytes);
}
else {
outputStream.write(buffer.array());
}
}
}
@Override
protected void recvResponse(SSLSocket sslSocket, ByteBuffer buffer)
throws IOException
{
InputStream inputStream = sslSocket.getInputStream();
byte[] tempBuf = new byte[buffer.remaining()];
int read = inputStream.read(tempBuf);
buffer.put(tempBuf, 0, read);
}
@Override
protected void closeSocket()
throws IOException
{
SSLSocket existingSocket;
if ((existingSocket = socket.getAndSet(null)) != null) {
existingSocket.close();
}
}
@Override
public String toString()
{
return "SSLSender{" +
"config=" + config +
"} " + super.toString();
}
public static class Config
implements Instantiator
{
private final NetworkSender.Config baseConfig = new NetworkSender.Config();
public NetworkSender.Config getBaseConfig()
{
return baseConfig;
}
public SenderErrorHandler getSenderErrorHandler()
{
return baseConfig.getSenderErrorHandler();
}
public Config setSenderErrorHandler(SenderErrorHandler senderErrorHandler)
{
baseConfig.setSenderErrorHandler(senderErrorHandler);
return this;
}
public String getHost()
{
return baseConfig.getHost();
}
public Config setHost(String host)
{
baseConfig.setHost(host);
return this;
}
public int getPort()
{
return baseConfig.getPort();
}
public Config setPort(int port)
{
baseConfig.setPort(port);
return this;
}
public int getConnectionTimeoutMilli()
{
return baseConfig.getConnectionTimeoutMilli();
}
public Config setConnectionTimeoutMilli(int connectionTimeoutMilli)
{
baseConfig.setConnectionTimeoutMilli(connectionTimeoutMilli);
return this;
}
public int getReadTimeoutMilli()
{
return baseConfig.getReadTimeoutMilli();
}
public Config setReadTimeoutMilli(int readTimeoutMilli)
{
baseConfig.setReadTimeoutMilli(readTimeoutMilli);
return this;
}
public Heartbeater.Instantiator getHeartbeaterConfig()
{
return baseConfig.getHeartbeaterConfig();
}
public Config setHeartbeaterConfig(Heartbeater.Instantiator heartbeaterConfig)
{
baseConfig.setHeartbeaterConfig(heartbeaterConfig);
return this;
}
public FailureDetector.Config getFailureDetectorConfig()
{
return baseConfig.getFailureDetectorConfig();
}
public Config setFailureDetectorConfig(FailureDetector.Config failureDetectorConfig)
{
baseConfig.setFailureDetectorConfig(failureDetectorConfig);
return this;
}
public FailureDetectStrategy.Instantiator getFailureDetectorStrategyConfig()
{
return baseConfig.getFailureDetectorStrategyConfig();
}
public Config setFailureDetectorStrategyConfig(FailureDetectStrategy.Instantiator failureDetectorStrategyConfig)
{
baseConfig.setFailureDetectorStrategyConfig(failureDetectorStrategyConfig);
return this;
}
public int getWaitBeforeCloseMilli()
{
return baseConfig.getWaitBeforeCloseMilli();
}
public Config setWaitBeforeCloseMilli(int waitBeforeCloseMilli)
{
baseConfig.setWaitBeforeCloseMilli(waitBeforeCloseMilli);
return this;
}
@Override
public SSLSender createInstance()
{
return new SSLSender(this);
}
@Override
public String toString()
{
return "Config{" +
"baseConfig=" + baseConfig +
'}';
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy