@@ -28,7 +28,9 @@ public class CmdLineUI {
2828 public static final String SLEEP = "sleep" ;
2929 public static final String SHOW_LOG = "showlog" ;
3030
31- private PrintStream consoleOutput ;
31+ private PrintStream standardOut = System .out ;
32+ private PrintStream standardError = System .err ;
33+
3234 private BufferedReader userInput ;
3335
3436 public static final String PEERS_ROOT_FOLDER = "asapPeers" ;
@@ -48,6 +50,11 @@ public CmdLineUI(PrintStream out) throws IOException, ASAPException {
4850 this (out , null );
4951 }
5052
53+ public void setOutStreams (PrintStream ps ) {
54+ this .standardOut = ps ;
55+ this .standardError = ps ;
56+ }
57+
5158 /**
5259 * only for batch processing - removes anything from the past
5360 * @throws IOException
@@ -58,7 +65,7 @@ public CmdLineUI() {
5865 }
5966
6067 public CmdLineUI (PrintStream os , InputStream is ) throws IOException , ASAPException {
61- this .consoleOutput = os ;
68+ this .standardOut = os ;
6269 this .userInput = is != null ? new BufferedReader (new InputStreamReader (is )) : null ;
6370
6471 // set up peers
@@ -132,11 +139,11 @@ public void printUsage() {
132139 b .append (EXIT );
133140 b .append (".. exit" );
134141
135- this .consoleOutput .println (b .toString ());
142+ this .standardOut .println (b .toString ());
136143 }
137144
138145 public void printUsage (String cmdString , String comment ) throws ASAPException {
139- PrintStream out = this .consoleOutput ;
146+ PrintStream out = this .standardOut ;
140147
141148 if (comment == null ) comment = " " ;
142149 out .println ("malformed command: " + comment );
@@ -228,7 +235,7 @@ public void printUsage(String cmdString, String comment) throws ASAPException {
228235 private List <String > cmds = new ArrayList <>();
229236
230237 public void runCommandLoop (PrintStream os , InputStream is ) {
231- this .consoleOutput = os ;
238+ this .standardOut = os ;
232239 this .userInput = is != null ? new BufferedReader (new InputStreamReader (is )) : null ;
233240
234241 this .runCommandLoop ();
@@ -301,15 +308,15 @@ public void runCommandLoop() {
301308 this .doKill ("all" );
302309 again = false ; break ; // end loop
303310
304- default : System . err .println ("unknown command:" + cmdLineString );
311+ default : this . standardError .println ("unknown command:" + cmdLineString );
305312 this .printUsage ();
306313 rememberCommand = false ;
307314 break ;
308315 }
309316 } catch (ASAPException ex ) {
310317 rememberCommand = false ;
311318 } catch (IOException ex ) {
312- this .consoleOutput .println ("cannot read from input stream" );
319+ this .standardOut .println ("cannot read from input stream" );
313320 System .exit (0 );
314321 }
315322
@@ -350,7 +357,7 @@ private ASAPStorage getStorage(String peername, String appName) throws ASAPExcep
350357 return asapPeer .getEngineByFormat (appName );
351358 }
352359
353- private ASAPPeer getASAPPeer (String peerName ) throws ASAPException {
360+ public ASAPPeer getASAPPeer (String peerName ) throws ASAPException {
354361 ASAPPeer asapPeer = this .peers .get (peerName );
355362 if (asapPeer == null ) {
356363 throw new ASAPException ("engine does not exist: " + peerName );
@@ -381,14 +388,14 @@ public TCPStreamCreatedHandler(ASAPPeer asapPeer) {
381388
382389 @ Override
383390 public void streamCreated (TCPStream channel ) {
384- CmdLineUI .this .consoleOutput .println ("Channel created" );
391+ CmdLineUI .this .standardOut .println ("Channel created" );
385392
386393 try {
387394 this .asapPeer .handleConnection (
388395 channel .getInputStream (),
389396 channel .getOutputStream ());
390397 } catch (IOException | ASAPException e ) {
391- CmdLineUI .this .consoleOutput .println ("call of engine.handleConnection failed: "
398+ CmdLineUI .this .standardOut .println ("call of engine.handleConnection failed: "
392399 + e .getLocalizedMessage ());
393400 }
394401 }
@@ -446,9 +453,9 @@ public void doOpen(String parameterString) throws ASAPException {
446453 }
447454
448455 public void doList () throws ASAPException {
449- this .consoleOutput .println ("connections:" );
456+ this .standardOut .println ("connections:" );
450457 for (String connectionName : this .streams .keySet ()) {
451- this .consoleOutput .println (connectionName );
458+ this .standardOut .println (connectionName );
452459 }
453460
454461 this .doPrintAllInformation ();
@@ -460,23 +467,23 @@ public void doKill(String parameterString) throws ASAPException {
460467 try {
461468 String channelName = st .nextToken ();
462469 if (channelName .equalsIgnoreCase ("all" )) {
463- System . out .println ("kill all open channels.." );
470+ this . standardOut .println ("kill all open channels.." );
464471 for (TCPStream channel : this .streams .values ()) {
465472 channel .kill ();
466473 }
467474 this .streams = new HashMap <>();
468- System . out .println (".. done" );
475+ this . standardOut .println (".. done" );
469476 } else {
470477
471478 TCPStream channel = this .streams .remove (channelName );
472479 if (channel == null ) {
473- System . err .println ("channel does not exist: " + channelName );
480+ this . standardError .println ("channel does not exist: " + channelName );
474481 return ;
475482 }
476- System . out .println ("kill channel" );
483+ this . standardOut .println ("kill channel" );
477484 channel .kill ();
478485
479- System . out .println (".. done" );
486+ this . standardOut .println (".. done" );
480487 }
481488 }
482489 catch (RuntimeException e ) {
@@ -580,7 +587,7 @@ public void doCreateASAPMessage(String parameterString) throws ASAPException {
580587 // first - get storage
581588 ASAPStorage asapStorage = this .getStorage (peername , appName );
582589 if (asapStorage == null ) {
583- System . err .println ("storage does not exist: " + peername + ":" + appName );
590+ this . standardError .println ("storage does not exist: " + peername + ":" + appName );
584591 return ;
585592 }
586593 asapStorage .add (uri , message );
@@ -618,10 +625,10 @@ public void doSetSendReceivedMessage(String parameterString) throws ASAPExceptio
618625
619626 public void doPrintAllInformation () throws ASAPException {
620627 try {
621- this .consoleOutput .println (this .peers .keySet ().size () + " peers in folder: " + PEERS_ROOT_FOLDER );
628+ this .standardOut .println (this .peers .keySet ().size () + " peers in folder: " + PEERS_ROOT_FOLDER );
622629 for (String peername : this .peers .keySet ()) {
623- this .consoleOutput .println ("+++++++++++++++++++" );
624- this .consoleOutput .println ("Peer: " + peername );
630+ this .standardOut .println ("+++++++++++++++++++" );
631+ this .standardOut .println ("Peer: " + peername );
625632 ASAPPeer asapPeer = this .peers .get (peername );
626633
627634 for (CharSequence format : asapPeer .getFormats ()) {
@@ -631,7 +638,7 @@ public void doPrintAllInformation() throws ASAPException {
631638 this .printChannelInfo (asapStorage , uri , format );
632639 }
633640 }
634- this .consoleOutput .println ("+++++++++++++++++++\n " );
641+ this .standardOut .println ("+++++++++++++++++++\n " );
635642 }
636643 }
637644 catch (RuntimeException | IOException | ASAPException e ) {
@@ -654,7 +661,7 @@ public void doPrintStorageInformation(String parameterString) throws ASAPExcepti
654661 }
655662
656663 // iterate URI
657- this .consoleOutput .println (asapStorage .getChannelURIs ().size () +
664+ this .standardOut .println (asapStorage .getChannelURIs ().size () +
658665 " channels in storage " + appName +
659666 " (note: channels without messages are considered non-existent)" );
660667 for (CharSequence uri : asapStorage .getChannelURIs ()) {
@@ -673,7 +680,7 @@ public void doSleep(String parameterString) throws ASAPException {
673680 Thread .sleep (Long .parseLong (parameterString ));
674681 }
675682 catch (InterruptedException e ) {
676- this .consoleOutput .println ("sleep interrupted" );
683+ this .standardOut .println ("sleep interrupted" );
677684 }
678685 catch (RuntimeException e ) {
679686 this .printUsage (PRINT_STORAGE_INFORMATION , e .getLocalizedMessage ());
@@ -686,14 +693,14 @@ private void doShowLog() {
686693 boolean first = true ;
687694 for (String c : this .cmds ) {
688695 if (!first ) {
689- this .consoleOutput .println ("\\ n\" + " );
696+ this .standardOut .println ("\\ n\" + " );
690697 } else {
691698 first = false ;
692699 }
693- this .consoleOutput .print ("\" " );
694- this .consoleOutput .print (c );
700+ this .standardOut .print ("\" " );
701+ this .standardOut .print (c );
695702 }
696- this .consoleOutput .println ("\" " );
703+ this .standardOut .println ("\" " );
697704 }
698705
699706 public void doPrintChannelInformation (String parameterString ) throws ASAPException {
@@ -708,7 +715,7 @@ public void doPrintChannelInformation(String parameterString) throws ASAPExcepti
708715 // first - get storage
709716 ASAPStorage asapStorage = this .getStorage (peername , appName );
710717 if (asapStorage == null ) {
711- System . err .println ("storage does not exist: " + peername + ":" + appName );
718+ this . standardError .println ("storage does not exist: " + peername + ":" + appName );
712719 return ;
713720 }
714721
@@ -726,12 +733,12 @@ private void printChannelInfo(ASAPStorage asapStorage, CharSequence uri, CharSeq
726733 ASAPChannel channel = asapStorage .getChannel (uri );
727734 Set <CharSequence > recipients = channel .getRecipients ();
728735
729- this .consoleOutput .println ("Peer:App:Channel == " + channel .getOwner () + ":" + appName + ":" + channel .getUri ());
730- this .consoleOutput .println ("#Messages == " + channel .getMessages ().size ());
731- this .consoleOutput .println ("#Recipients == " + recipients .size () +
736+ this .standardOut .println ("Peer:App:Channel == " + channel .getOwner () + ":" + appName + ":" + channel .getUri ());
737+ this .standardOut .println ("#Messages == " + channel .getMessages ().size ());
738+ this .standardOut .println ("#Recipients == " + recipients .size () +
732739 " (0 means: open channel - no restrictions - anybody receives from this channel)" );
733740 for (CharSequence recipient : recipients ) {
734- this .consoleOutput .println (recipient );
741+ this .standardOut .println (recipient );
735742 }
736743 }
737744
0 commit comments