2929import java .util .logging .Level ;
3030import java .util .logging .Logger ;
3131
32+ /** Interactive command-line interface for a running Q2 instance. */
3233public class CLI implements Runnable {
3334 final private static String DEFAULT_PROMPT = "q2> " ;
3435 final private static String ESCAPED_SEMICOLON = "__semicolon__" ;
3536 private Thread t ;
3637 private String line = null ;
3738 private boolean keepRunning = false ;
3839 private boolean interactive = false ;
40+ /** The context for this CLI session. */
3941 protected CLIContext ctx ;
4042 private CLICommandInterface cmdInterface ;
4143 private Terminal terminal ;
@@ -44,10 +46,23 @@ public class CLI implements Runnable {
4446 private String prompt = DEFAULT_PROMPT ;
4547 private History mainHistory ;
4648
49+ /** @param q2 the Q2 instance
50+ * @param line the initial command line
51+ * @param keepRunning true to keep running after the first command
52+ * @throws IOException on I/O failure
53+ */
4754 public CLI (Q2 q2 , String line , boolean keepRunning ) throws IOException {
4855 this (q2 , System .in , System .out , line , keepRunning , true );
4956 }
5057
58+ /** @param q2 the Q2 instance
59+ * @param in input stream
60+ * @param rawout output stream
61+ * @param line initial command
62+ * @param keepRunning true to keep running
63+ * @param interactive true for interactive mode
64+ * @throws IOException on I/O failure
65+ */
5166 public CLI (Q2 q2 , InputStream in , OutputStream rawout , String line , boolean keepRunning , boolean interactive ) throws IOException {
5267 Logger .getLogger ("org.jline" ).setLevel (Level .SEVERE );
5368 this .q2 = q2 ;
@@ -63,18 +78,23 @@ public CLI(Q2 q2, InputStream in, OutputStream rawout, String line, boolean keep
6378 initCmdInterface (getCompletionPrefixes (), mainHistory );
6479 }
6580
81+ /** @return true if this CLI is still running */
6682 protected boolean running () {
6783 return getQ2 () == null || getQ2 ().running ();
6884 }
6985
86+ /** Called when the CLI is stopping; subclasses may override. */
7087 protected void markStopped () { }
7188
89+ /** Called when the CLI is starting; subclasses may override. */
7290 protected void markStarted () { }
7391
92+ /** @return array of command prefixes for tab-completion */
7493 protected String [] getCompletionPrefixes () {
7594 return new String [] {"org.jpos.q2.cli." };
7695 }
7796
97+ /** Called on normal exit; subclasses may override. */
7898 protected void handleExit () { }
7999
80100 void setPrompt (String prompt , String [] completionPrefixes ) throws IOException {
@@ -95,13 +115,17 @@ private void initCmdInterface(String[] completionPrefixes, History history) thro
95115 }
96116 }
97117
118+ /** Starts the CLI session.
119+ * @throws Exception on startup failure
120+ */
98121 public void start () throws Exception {
99122 markStarted ();
100123 t = new Thread (this );
101124 t .setName ("Q2-CLI" );
102125 t .start ();
103126 }
104127
128+ /** Stops the CLI session. */
105129 public void stop () {
106130 markStopped ();
107131 try {
@@ -173,30 +197,47 @@ public void run() {
173197 handleExit ();
174198 }
175199
200+ /** @return the Q2 instance this CLI is attached to */
176201 public Q2 getQ2 () {
177202 return q2 ;
178203 }
179204
205+ /** @return true if this is an interactive session */
180206 public boolean isInteractive () {
181207 return interactive ;
182208 }
183209
210+ /** @return the JLine3 LineReader for this session */
184211 public LineReader getReader () {
185212 return reader ;
186213 }
187214
215+ /** @param in input stream
216+ * @param out output stream
217+ * @param command command to execute
218+ * @throws Exception on execution failure
219+ */
188220 public static void exec (InputStream in , OutputStream out , String command ) throws Exception {
189221 CLI cli = new CLI (Q2 .getQ2 (), in , out , command , false , false );
190222 cli .start ();
191223 cli .stop ();
192224 }
193225
226+ /** @param command command string to execute
227+ * @return captured output
228+ * @throws Exception on execution failure
229+ */
194230 public static String exec (String command ) throws Exception {
195231 ByteArrayOutputStream out = new ByteArrayOutputStream ();
196232 exec (null , out , command );
197233 return out .toString ();
198234 }
199235
236+ /** @param in input stream
237+ * @param out output stream
238+ * @return a JLine3 Terminal for this session
239+ * @throws IOException on I/O failure
240+ */
200241 protected Terminal buildTerminal (InputStream in , OutputStream out ) throws IOException {
201242 TerminalBuilder builder = TerminalBuilder .builder ()
202243 .streams (in ,out )
0 commit comments