2828import java .util .*;
2929
3030import nsk .share .*;
31- import nsk .share .jpda .*;
3231
3332/**
3433 * <code>BindServer</code> is an utility to perform JPDA tests
5857 * @see DebugeeBinder
5958 * @see DebugeeArgumentHandler
6059 */
61- public class BindServer implements Finalizable {
60+ public final class BindServer {
6261
6362 /** Version of <code>BindServer</code> implementation. */
6463 public static final long VERSION = 2 ;
@@ -88,7 +87,6 @@ public class BindServer implements Finalizable {
8887
8988 private static String pathConvertions [][] = null ;
9089
91- private ListeningThread listeningThread = null ;
9290
9391 private int totalRequests = 0 ;
9492 private int acceptedRequests = 0 ;
@@ -149,8 +147,6 @@ private int runIt(String argv[], PrintStream out) {
149147 log = new Log (out , argHandler );
150148 logger = new Log .Logger (log , "" );
151149
152- registerCleanup ();
153-
154150 logger .trace (TRACE_LEVEL_THREADS , "BindServer: starting main thread" );
155151
156152 logger .display ("Listening to port: " + argHandler .getBindPortNumber ());
@@ -178,45 +174,39 @@ private int runIt(String argv[], PrintStream out) {
178174
179175 BufferedReader stdIn = new BufferedReader (
180176 new InputStreamReader (System .in ));
177+ try (ListeningThread listeningThread = new ListeningThread (this )) {
178+ listeningThread .bind ();
179+ listeningThread .start ();
181180
182- listeningThread = new ListeningThread (this );
183- listeningThread .bind ();
184- listeningThread .start ();
185-
186- System .out .println ("\n "
187- + "BindServer started" + "\n "
188- + "Type \" exit\" to shut down BindServer"
189- + "\n " );
181+ System .out .println ("\n "
182+ + "BindServer started" + "\n "
183+ + "Type \" exit\" to shut down BindServer"
184+ + "\n " );
190185
191- for (;;) {
192- try {
193- String userInput = stdIn .readLine ();
194- if (userInput == null || userInput .equals ("exit" )
195- || userInput .equals ("quit" )) {
196- logger .display ("Shutting down BindServer" );
197- stdIn .close ();
198- stdIn = null ;
199- break ;
200- } else if (userInput .trim ().equals ("" )) {
201- continue ;
202- } else {
203- System .out .println ("ERROR: Unknown command: " + userInput );
186+ for (; ; ) {
187+ try {
188+ String userInput = stdIn .readLine ();
189+ if (userInput == null || userInput .equals ("exit" )
190+ || userInput .equals ("quit" )) {
191+ logger .display ("Shutting down BindServer" );
192+ stdIn .close ();
193+ stdIn = null ;
194+ break ;
195+ } else if (userInput .trim ().equals ("" )) {
196+ continue ;
197+ } else {
198+ System .out .println ("ERROR: Unknown command: " + userInput );
199+ }
200+ } catch (IOException e ) {
201+ e .printStackTrace (log .getOutStream ());
202+ throw new Failure ("Caught exception while reading console command:\n \t "
203+ + e );
204204 }
205- } catch (IOException e ) {
206- e .printStackTrace (log .getOutStream ());
207- throw new Failure ("Caught exception while reading console command:\n \t "
208- + e );
209205 }
210- }
211206
212- printSummary (System .out );
207+ printSummary (System .out );
213208
214- logger .trace (TRACE_LEVEL_THREADS , "BindServer: exiting main thread" );
215- try {
216- cleanup ();
217- } catch (Throwable e ) {
218- e .printStackTrace (log .getOutStream ());
219- logger .complain ("Caught exception while finalization of BindServer:\n \t " + e );
209+ logger .trace (TRACE_LEVEL_THREADS , "BindServer: exiting main thread" );
220210 }
221211
222212 return PASSED ;
@@ -387,38 +377,6 @@ private static void waitInterruptThread(Thread thr) {
387377 waitInterruptThread (thr , THREAD_TIMEOUT );
388378 }
389379
390- /**
391- * Close <code>BindServer</code> by finishing all threads and closing
392- * all conections.
393- */
394- public synchronized void close () {
395- if (listeningThread != null ) {
396- listeningThread .close ();
397- listeningThread = null ;
398- }
399- }
400-
401- /**
402- * Make finalization of <code>BindServer</code> object by invoking
403- * method <code>close()</code>.
404- *
405- * @see #close()
406- */
407- @ Override
408- public void cleanup () {
409- close ();
410- }
411-
412- /**
413- * Make finalization of <code>BindServer</code> object at program exit
414- * by invoking method <code>cleanup()</code>.
415- *
416- */
417- public void finalizeAtExit () throws Throwable {
418- cleanup ();
419- logger .trace (TRACE_LEVEL_THREADS , "BindServer: finalization at exit completed" );
420- }
421-
422380///////// Thread listening a TCP/IP socket //////////
423381
424382 /**
@@ -427,7 +385,7 @@ public void finalizeAtExit() throws Throwable {
427385 *
428386 * @see ServingThread
429387 */
430- private static class ListeningThread extends Thread {
388+ private static class ListeningThread extends Thread implements AutoCloseable {
431389 private volatile boolean shouldStop = false ;
432390 private volatile boolean closed = false ;
433391
@@ -673,23 +631,29 @@ private void closeConnection() {
673631
674632 /**
675633 * Close thread by closing all connections and waiting
676- * foor thread finished .
634+ * for thread to finish .
677635 *
678636 * @see #closeConnection()
679637 */
638+ @ Override
680639 public synchronized void close () {
681640 if (closed ) {
682641 return ;
683642 }
684- closeHostConnection ();
685- if (servingThread != null ) {
686- servingThread .close ();
687- servingThread = null ;
643+ try {
644+ closeHostConnection ();
645+ if (servingThread != null ) {
646+ servingThread .close ();
647+ servingThread = null ;
648+ }
649+ waitForThread (THREAD_TIMEOUT );
650+ closeConnection ();
651+ closed = true ;
652+ logger .trace (TRACE_LEVEL_THREADS , "ListeningThread closed" );
653+ } catch (Throwable e ) {
654+ e .printStackTrace (log .getOutStream ());
655+ logger .complain ("Caught exception while closing ListeningThread:\n \t " + e );
688656 }
689- waitForThread (THREAD_TIMEOUT );
690- closeConnection ();
691- closed = true ;
692- logger .trace (TRACE_LEVEL_THREADS , "ListeningThread closed" );
693657 }
694658
695659 } // ListeningThread
0 commit comments