From bf8d76750f1062c9499fb26025d1130c588c3b89 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Tue, 6 Dec 2016 11:35:38 -0500 Subject: [PATCH] Cleanup for release stage Signed-off-by: Emmanuel Bigeon --- .../socket/SocketConsoleApplicationShell.java | 18 +++- .../gclc/command/ParametrizedCommand.java | 3 +- .../bigeon/gclc/manager/ReadingRunnable.java | 96 +++++++++++++------ .../gclc/manager/ReadingRunnableTest.java | 73 ++++++++++++++ 4 files changed, 155 insertions(+), 35 deletions(-) create mode 100644 gclc/src/test/java/fr/bigeon/gclc/manager/ReadingRunnableTest.java diff --git a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java index 4a47e8e..4ebdff3 100644 --- a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java +++ b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java @@ -96,8 +96,7 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { public void run() { try { while (!socket.isClosed()) { - while (!socket.isClosed() && - !consoleManager.available()) { + while (!socket.isClosed() && !consoleManager.available()) { waitASec(); } if (socket.isClosed()) { @@ -192,7 +191,8 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { // Create the streams runSokectServer(); } catch (final IOException e) { - LOGGER.log(Level.SEVERE, + LOGGER.severe("Communication error between client and server"); //$NON-NLS-1$ + LOGGER.log(Level.FINE, "Communication error between client and server", e); //$NON-NLS-1$ } } @@ -229,12 +229,20 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { LOGGER.log(Level.FINE, "Socket closed with exception (probably due to server interruption)", //$NON-NLS-1$ e); + } catch (IOException e) { + throw e; } LOGGER.info("Closing client"); //$NON-NLS-1$ } runnable.setRunning(false); - consoleManager.type(applicationShutdown); - consoleManager.close(); + try { + consoleManager.type(applicationShutdown); + consoleManager.close(); + } catch (IOException e) { + LOGGER.warning("Unable to close application correctly"); //$NON-NLS-1$ + LOGGER.log(Level.FINE, "Application closing caused an exception", + e); + } LOGGER.info("Closing Server"); //$NON-NLS-1$ } diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java b/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java index 50d4bbd..787c507 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java +++ b/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java @@ -167,7 +167,8 @@ public abstract class ParametrizedCommand extends Command { /** @param param the parameter * @param stringParameter the string parameter type * @param needed if the parameter is needed - * @throws InvalidParameterException if the new definition is invalid */ + * @throws InvalidParameterException if the new definition is invalid + * @deprecated since 1.3.3 */ @Deprecated private void checkParam(String param, boolean stringParameter, boolean needed) throws InvalidParameterException { diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java b/gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java index acd08c4..dfd8a7d 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java +++ b/gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java @@ -53,6 +53,61 @@ import java.util.logging.Logger; * @author Emmanuel Bigeon */ public class ReadingRunnable implements Runnable { + /** The runnable to wait for notification on an object + * + * @author Emmanuel Bigeon */ + private final class ToWaitRunnable implements Runnable { + /** The Object */ + private final Object obj; + /** The locking object */ + private final Object start; + /** The message */ + private final String message; + /** The started status */ + private boolean started = false; + + /** @param obj the object to lock on + * @param start the object to notify when ready to wait + * @param message the message to wait for */ + public ToWaitRunnable(Object obj, Object start, + String message) { + this.obj = obj; + this.start = start; + this.message = message; + } + + @SuppressWarnings("synthetic-access") + @Override + public void run() { + synchronized (obj) { + synchronized (start) { + started = true; + start.notify(); + } + while (isRunning()) { + try { + obj.wait(); + if (delivering.equals(message)) { + return; + } + } catch (InterruptedException e) { + LOGGER.log(Level.SEVERE, + THREAD_INTERRUPTION_EXCEPTION, e); + } + } + } + } + + /** @return the started */ + public boolean isStarted() { + synchronized (start) { + return started; + } + } + } + + /** The thread intteruption logging message */ + private static final String THREAD_INTERRUPTION_EXCEPTION = "Thread interruption exception."; //$NON-NLS-1$ /** The closed pipe message */ private static final String CLOSED_PIPE = "Closed pipe"; //$NON-NLS-1$ /** Wait timeout */ @@ -79,6 +134,7 @@ public class ReadingRunnable implements Runnable { * The lock */ private final Object messageBlockerLock = new Object(); + /** The message being delivered */ private String delivering; /** @param reader the input to read from */ @@ -146,7 +202,7 @@ public class ReadingRunnable implements Runnable { try { lock.wait(TIMEOUT); } catch (InterruptedException e) { - LOGGER.log(Level.SEVERE, "Thread interruption exception.", //$NON-NLS-1$ + LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, e); } if (messages.isEmpty() && !running) { @@ -218,36 +274,18 @@ public class ReadingRunnable implements Runnable { } final Object obj = messageBlocker.get(message); final Object start = new Object(); - Thread th = new Thread(new Runnable() { - - @SuppressWarnings("synthetic-access") - @Override - public void run() { - synchronized (obj) { - synchronized (start) { - start.notify(); - } - while (isRunning()) { - try { - obj.wait(); - if (delivering.equals(message)) { - return; - } - } catch (InterruptedException e) { - LOGGER.log(Level.SEVERE, - "Thread interruption exception.", e); //$NON-NLS-1$ - } - } - } - } - }); + ToWaitRunnable waitRunn = new ToWaitRunnable(obj, start, message); + Thread th = new Thread(waitRunn); + synchronized (start) { th.start(); - try { - start.wait(); - } catch (InterruptedException e) { - LOGGER.log(Level.SEVERE, "Thread interruption exception.", //$NON-NLS-1$ - e); + while (!waitRunn.isStarted()) { + try { + start.wait(TIMEOUT); + } catch (InterruptedException e) { + LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, + e); + } } } return th; diff --git a/gclc/src/test/java/fr/bigeon/gclc/manager/ReadingRunnableTest.java b/gclc/src/test/java/fr/bigeon/gclc/manager/ReadingRunnableTest.java new file mode 100644 index 0000000..96083a4 --- /dev/null +++ b/gclc/src/test/java/fr/bigeon/gclc/manager/ReadingRunnableTest.java @@ -0,0 +1,73 @@ +/** + * gclc:fr.bigeon.gclc.manager.ReadingRunnableTest.java + * Created on: Dec 6, 2016 + */ +package fr.bigeon.gclc.manager; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.BufferedReader; +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +/** + *

+ * TODO + * + * @author Emmanuel Bigeon + * + */ +public class ReadingRunnableTest { + + /** + */ + @Before + public void setUp() {} + + /** + * Test method for {@link fr.bigeon.gclc.manager.ReadingRunnable#getMessage()}. + */ + @Test + public final void testGetMessage(){ + BufferedReader reader = null; + ReadingRunnable runnable = new ReadingRunnable(reader); + runnable.setRunning(false); + + try { + runnable.getMessage(); + fail("reading from closed runnable"); + } catch (IOException e) { + assertNotNull(e); + } + + } + + /** + * Test method for {@link fr.bigeon.gclc.manager.ReadingRunnable#hasMessage()}. + */ + @Test + public final void testHasMessage(){ + + BufferedReader reader = null; + ReadingRunnable runnable = new ReadingRunnable(reader); + runnable.setRunning(false); + + try { + runnable.getMessage(); + fail("reading from closed runnable"); + } catch (IOException e) { + assertNotNull(e); + } + } + + /** + * Test method for {@link fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String)}. + */ + @Test + public final void testGetWaitForDelivery(){ + } + +}