public CLI(Q2 q2, InputStream in, OutputStream rawout, String line, boolean keepRunning, boolean interactive) throws IOException { this.q2 = q2; PrintStream out = rawout instanceof PrintStream ? (PrintStream) rawout : new PrintStream(rawout); ctx = buildCLIContext(in, out); this.line = line; this.keepRunning = keepRunning; this.interactive = interactive; this.mainHistory = new DefaultHistory(); if (interactive) { terminal = buildTerminal(in, out); } initCmdInterface(getCompletionPrefixes(), mainHistory); }
public void run() { while (running()) { try { LineReader reader = getReader(); String p = prompt; if (line == null) { getReader().getTerminal().close(); } catch (IOException e) { ctx.printThrowable(e); handleExit();
private void initCmdInterface(String[] completionPrefixes, History history) throws IOException { completionPrefixes = completionPrefixes == null ? getCompletionPrefixes() : completionPrefixes; cmdInterface = new CLICommandInterface(ctx); for (String s : completionPrefixes) { cmdInterface.addPrefix(s); } cmdInterface.addPrefix("org.jpos.q2.cli.builtin."); if (terminal != null) { reader = buildReader(terminal, completionPrefixes, history); ctx.setReader(reader); } }
public static String exec (String command) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); exec (null, out, command); return out.toString(); }
void setPrompt(String prompt, String[] completionPrefixes) throws IOException { this.prompt = prompt != null ? prompt : DEFAULT_PROMPT; initCmdInterface(completionPrefixes, completionPrefixes == null ? mainHistory : new DefaultHistory()); }
@Override public void exec(CLIContext ctx, String[] args) throws Exception { final ObjectName on = new ObjectName("Q2:type=qbean,service=*"); MBeanServer server = ctx.getCLI().getQ2().getMBeanServer(); Set<ObjectInstance> b = server.queryMBeans(on, null); Iterator<ObjectInstance> it = b.iterator(); while (it.hasNext()) { ObjectInstance instance = it.next(); int status = (Integer) server.getAttribute(instance.getObjectName(), "State"); if (status == QBean.STARTED) { ctx.println(instance.getObjectName().getKeyProperty("service") + "\t\t" + instance.getClassName()); } } }
@Override public void exec(CLIContext ctx, String[] args) throws Exception { if (args.length < 2) { ctx.println("Usage: enable <xml_file(no extenssion)>"); return; } File deployDir = ctx.getCLI().getQ2().getDeployDir(); File deploy = new File(deployDir.getAbsolutePath()+"/"+args[1]+".xml"); if(deploy.exists() && deploy.isFile()){ File dest = new File(deployDir.getAbsolutePath()+"/"+args[1]+".xml.off"); deploy.renameTo(dest); ctx.println("DISABLED: " + args[1]); return; } File dest = new File(deployDir.getAbsolutePath()+"/"+args[1]+".xml.off"); if (dest.exists()) { ctx.println("Already disabled: " + args[1]); return; } ctx.println("Can't find the bean descriptor: "+args[1]); return; } }
@Override public void exec(CLIContext ctx, String[] args) throws Exception { if (args.length < 2) { ctx.println("Usage: enable <xml_file(no extenssion)>"); return; } File deployDir = ctx.getCLI().getQ2().getDeployDir(); File deploy = new File(deployDir.getAbsolutePath() + "/" + args[1] + ".xml.off"); if (deploy.exists() && deploy.isFile()) { File dest = new File(deployDir.getAbsolutePath() + "/" + args[1] + ".xml"); deploy.renameTo(dest); ctx.println("ENABLED: " + args[1]); return; } File dest = new File(deployDir.getAbsolutePath() + "/" + args[1] + ".xml"); if (dest.exists()) { ctx.println("Already enabled: " + args[1]); return; } ctx.println("Can't find the bean descriptor: " + args[1]); return; } }
@Override public void exec(CLIContext ctx, String[] args) throws Exception { Q2 q2 = ctx.getCLI().getQ2(); File deployDir = q2.getDeployDir(); ctx.println(printDirectoryTree(deployDir)); return; }
public void exec(CLIContext ctx, String[] args) throws Exception { ctx.println(ISOUtil.millisToString(ctx.getCLI().getQ2().getUptime())); } }
MBeanServer server = ctx.getCLI().getQ2().getMBeanServer();
binding.setVariable("q2", cli.getCLI().getQ2()); binding.setVariable("cli", cli.getCLI()); binding.setVariable("out", new PrintStream(out)); // PrintStream "out" in the Binding will redirect println()
public void exec(CLIContext cli, String[] args) throws IOException { boolean shutdown; if (cli.isInteractive() && cli.getOutputStream() != System.out) { cli.println ("Can't shutdown remotely"); return; } if (hasOption(args, "-f", "--force", "-fq")) { shutdown = true; } else { shutdown = cli.confirm("Confirm shutdown (Yes/No) ? "); } if (shutdown) { if (!hasOption (args, "-q", "--quiet", "-fq")) cli.println("Shutting down."); cli.getCLI().getQ2().shutdown(); } else { cli.println("Q2 will continue running."); } }