Skip to content

Commit

Permalink
refactored SocketRemote to extend ReceiverBase
Browse files Browse the repository at this point in the history
Also fixed up the taxonomy -- what was a SocketRemote is now a receiver
component that connects to a remote appender peer.
  • Loading branch information
ceharris committed Apr 11, 2013
1 parent fab370a commit 66993de
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
import javax.net.ssl.SSLContext;

import ch.qos.logback.core.net.ssl.ConfigurableSSLSocketFactory;
import ch.qos.logback.core.net.ssl.SSLComponent;
import ch.qos.logback.core.net.ssl.SSLConfiguration;
import ch.qos.logback.core.net.ssl.SSLParametersConfiguration;

/**
* A {@link SocketRemote} that supports SSL.
* A {@link SocketReceiver} that supports SSL.
*
* @author Carl Harris
*/
public class SSLSocketRemote extends SocketRemote {
public class SSLSocketReceiver extends SocketReceiver
implements SSLComponent {

private SSLConfiguration ssl;
private SocketFactory socketFactory;
Expand All @@ -45,17 +47,18 @@ protected SocketFactory getSocketFactory() {
* {@inheritDoc}
*/
@Override
public void start() {
protected boolean shouldStart() {
try {
SSLContext sslContext = getSsl().createContext(this);
SSLParametersConfiguration parameters = getSsl().getParameters();
parameters.setContext(getContext());
socketFactory = new ConfigurableSSLSocketFactory(parameters,
sslContext.getSocketFactory());
super.start();
return super.shouldStart();
}
catch (Exception ex) {
addError(ex.getMessage(), ex);
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@

import javax.net.SocketFactory;

import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.net.SocketAppenderBase;
import ch.qos.logback.core.net.SocketConnector;
import ch.qos.logback.core.net.SocketConnectorBase;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.LifeCycle;
import ch.qos.logback.core.util.CloseUtil;

/**
Expand All @@ -46,8 +41,8 @@
*
* @author Carl Harris
*/
public class SocketRemote extends ContextAwareBase
implements LifeCycle, SocketConnector.ExceptionHandler, Runnable {
public class SocketReceiver extends ReceiverBase
implements SocketConnector.ExceptionHandler {

private static final int DEFAULT_ACCEPT_CONNECTION_DELAY = 5000;

Expand All @@ -57,21 +52,13 @@ public class SocketRemote extends ContextAwareBase
private int reconnectionDelay;
private int acceptConnectionTimeout = DEFAULT_ACCEPT_CONNECTION_DELAY;

private ExecutorService executor;
private boolean started;
private String remoteId;
private volatile Socket socket;

/**
* {@inheritDoc}
*/
public void start() {
if (isStarted()) return;

if (getContext() == null) {
throw new IllegalStateException("context not set");
}

protected boolean shouldStart() {
int errorCount = 0;
if (port == 0) {
errorCount++;
Expand Down Expand Up @@ -101,43 +88,31 @@ public void start() {

if (errorCount == 0) {
remoteId = "remote " + host + ":" + port + ": ";
executor = createExecutorService();
executor.execute(this);
started = true;
}

return errorCount == 0;
}

/**
* {@inheritDoc}
*/
public void stop() {
if (!isStarted()) return;
protected void onStop() {
if (socket != null) {
CloseUtil.closeQuietly(socket);
}
executor.shutdownNow();
started = false;
}

/**
* {@inheritDoc}
*/
public boolean isStarted() {
return started;
}

/**
* {@inheritDoc}
*/
public void run() {
try {
LoggerContext lc = awaitConfiguration();
LoggerContext lc = (LoggerContext) getContext();
SocketConnector connector = createConnector(address, port, 0,
reconnectionDelay);
while (!executor.isShutdown() &&
!Thread.currentThread().isInterrupted()) {
while (!Thread.currentThread().isInterrupted()) {
try {
executor.execute(connector);
getExecutor().execute(connector);
}
catch (RejectedExecutionException ex) {
// executor is shutting down...
Expand All @@ -154,16 +129,6 @@ public void run() {
addInfo("shutting down");
}

private LoggerContext awaitConfiguration() throws InterruptedException {
ILoggerFactory factory = LoggerFactory.getILoggerFactory();
while (!(factory instanceof LoggerContext)
&& !Thread.currentThread().isInterrupted()) {
Thread.sleep(500);
factory = LoggerFactory.getILoggerFactory();
}
return (LoggerContext) factory;
}

private void dispatchEvents(LoggerContext lc) {
try {
socket.setSoTimeout(acceptConnectionTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
@SuiteClasses( { SyslogAppenderTest.class, DilutedSMTPAppenderTest.class,
SocketAppenderTest.class, JMSQueueAppenderTest.class, JMSTopicAppenderTest.class,
SMTPAppender_GreenTest.class, SMTPAppender_SubethaSMTPTest.class,
SocketRemoteTest.class })
SocketReceiverTest.class, SSLSocketReceiverTest.class })
public class PackageTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@

import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import ch.qos.logback.core.net.mock.MockContext;
import ch.qos.logback.classic.LoggerContext;

/**
* Unit tests for {@link SSLSocketRemote}.
* Unit tests for {@link SSLSocketReceiver}.
*
* @author Carl Harris
*/
public class SSLSocketRemoteTest {
public class SSLSocketReceiverTest {

private MockContext context = new MockContext();

private SSLSocketRemote remote =
new SSLSocketRemote();
private SSLSocketReceiver remote =
new SSLSocketReceiver();

@Before
public void setUp() throws Exception {
remote.setContext(context);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
remote.setContext(lc);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.spi.LoggingEventVO;
import ch.qos.logback.core.net.SocketConnector;
import ch.qos.logback.core.net.mock.MockContext;
import ch.qos.logback.core.net.server.ServerSocketUtil;
import ch.qos.logback.core.status.Status;

/**
* Unit tests for {@link SocketRemote}.
* Unit tests for {@link SocketReceiver}.
*
* @author Carl Harris
*/
public class SocketRemoteTest {
public class SocketReceiverTest {

private static final int DELAY = 200;
private static final String TEST_HOST_NAME = "NOT.A.VALID.HOST.NAME";
Expand All @@ -62,25 +62,24 @@ public class SocketRemoteTest {
private ServerSocket serverSocket;
private Socket socket;
private ExecutorService executor = Executors.newCachedThreadPool();
private MockContext context = new MockContext();
private MockSocketFactory socketFactory = new MockSocketFactory();
private MockSocketConnector connector;
private MockAppender appender;
private LoggerContext lc;
private Logger logger;

private InstrumentedSocketRemote remote =
new InstrumentedSocketRemote();
private InstrumentedSocketReceiver remote =
new InstrumentedSocketReceiver();

@Before
public void setUp() throws Exception {
serverSocket = ServerSocketUtil.createServerSocket();
socket = new Socket(serverSocket.getInetAddress(),
serverSocket.getLocalPort());
connector = new MockSocketConnector(socket);
remote.setContext(context);

lc = (LoggerContext) LoggerFactory.getILoggerFactory();
remote.setContext(lc);
appender = new MockAppender();
appender.start();
logger = lc.getLogger(getClass());
Expand All @@ -102,22 +101,31 @@ public void tearDown() throws Exception {
@Test
public void testStartNoRemoteAddress() throws Exception {
remote.start();
assertTrue(context.getLastStatus().getMessage().contains("host"));
assertFalse(remote.isStarted());
int count = lc.getStatusManager().getCount();
Status status = lc.getStatusManager().getCopyOfStatusList().get(count - 1);
assertTrue(status.getMessage().contains("host"));
}

@Test
public void testStartNoPort() throws Exception {
remote.setHost(TEST_HOST_NAME);
remote.start();
assertTrue(context.getLastStatus().getMessage().contains("port"));
assertFalse(remote.isStarted());
int count = lc.getStatusManager().getCount();
Status status = lc.getStatusManager().getCopyOfStatusList().get(count - 1);
assertTrue(status.getMessage().contains("port"));
}

@Test
public void testStartUnknownHost() throws Exception {
remote.setPort(6000);
remote.setHost(TEST_HOST_NAME);
remote.start();
assertTrue(context.getLastStatus().getMessage().contains("unknown host"));
assertFalse(remote.isStarted());
int count = lc.getStatusManager().getCount();
Status status = lc.getStatusManager().getCopyOfStatusList().get(count - 1);
assertTrue(status.getMessage().contains("unknown host"));
}

@Test
Expand Down Expand Up @@ -200,9 +208,9 @@ public void testNoDispatchEventForDisabledLevel() throws Exception {
}

/**
* A {@link SocketRemote} with instrumentation for unit testing.
* A {@link SocketReceiver} with instrumentation for unit testing.
*/
private class InstrumentedSocketRemote extends SocketRemote {
private class InstrumentedSocketReceiver extends SocketReceiver {

private boolean connectorCreated;
private boolean executorCreated;
Expand Down

0 comments on commit 66993de

Please sign in to comment.