/** * Hedwig Console * * @param args arguments * @throws IOException * @throws InterruptedException */ public HedwigConsole(String[] args) throws IOException, InterruptedException { // Setup Terminal terminal = Terminal.setupTerminal(); HedwigCommands.init(); cl.parseOptions(args); if (cl.getCommand() == null) { inConsole = true; } else { inConsole = false; } org.apache.bookkeeper.conf.ClientConfiguration bkClientConf = new org.apache.bookkeeper.conf.ClientConfiguration(); ServerConfiguration hubServerConf = new ServerConfiguration(); String serverCfgFile = cl.getOption("server-cfg"); if (serverCfgFile != null) { try { hubServerConf.loadConf(new File(serverCfgFile).toURI().toURL()); } catch (ConfigurationException e) { throw new IOException(e); } try { bkClientConf.loadConf(new File(serverCfgFile).toURI().toURL()); } catch (ConfigurationException e) { throw new IOException(e); } } ClientConfiguration hubClientCfg = new ClientConfiguration(); String clientCfgFile = cl.getOption("client-cfg"); if (clientCfgFile != null) { try { hubClientCfg.loadConf(new File(clientCfgFile).toURI().toURL()); } catch (ConfigurationException e) { throw new IOException(e); } } printMessage("Connecting to zookeeper/bookkeeper using HedwigAdmin"); try { admin = new HedwigAdmin(bkClientConf, hubServerConf); admin.getZkHandle().register(new MyWatcher()); } catch (Exception e) { throw new IOException(e); } printMessage("Connecting to default hub server " + hubClientCfg.getDefaultServerHost()); hubClient = new HedwigClient(hubClientCfg); publisher = hubClient.getPublisher(); subscriber = hubClient.getSubscriber(); subscriber.addSubscriptionListener(new ConsoleSubscriptionListener()); // other parameters myRegion = hubServerConf.getMyRegion(); }
public void executeLine(String line) { if (!line.equals("")) { cl.parseCommand(line); addToHistory(commandCount, line); processCmd(cl); commandCount++; } }
protected boolean processCmd(MyCommandOptions co) { String[] args = co.getArgArray(); String cmd = co.getCommand(); if (args.length < 1) { usage(); return false; } if (!getHedwigCommands().containsKey(cmd)) { usage(); return false; } LOG.debug("Processing {}", cmd); MyCommand myCommand = myCommands.get(cmd); if (myCommand == null) { System.err.println("No Command Processor found for command " + cmd); usage(); return false; } long startTime = MathUtils.now(); boolean success = false; try { success = myCommand.runCmd(args); } catch (Exception e) { e.printStackTrace(); success = false; } long elapsedTime = MathUtils.now() - startTime; if (inConsole) { if (success) { System.out.println("Finished " + ((double) elapsedTime / 1000) + " s."); } else { COMMAND c = getHedwigCommands().get(cmd); if (c != null) { c.printUsage(); } } } return success; }
@SuppressWarnings("unchecked") void run() throws IOException { inConsole = true; myCommands = buildMyCommands(); if (cl.getCommand() == null) { System.out.println("Welcome to Hedwig!"); System.out.println("JLine support is enabled"); console = new ConsoleReader(); JLineHedwigCompletor completor = new JLineHedwigCompletor(admin); console.addCompletor(completor); // load history file History history = new History(); File file = new File( System.getProperty( "hw.history", new File(System.getProperty("user.home"), HW_HISTORY_FILE).toString())); if (LOG.isDebugEnabled()) { LOG.debug("History file is " + file.toString()); } history.setHistoryFile(file); // set history to console reader console.setHistory(history); // load history from history file history.moveToFirstEntry(); while (history.next()) { String entry = history.current(); if (!entry.equals("")) { addToHistory(commandCount, entry); } commandCount++; } System.out.println("JLine history support is enabled"); String line; while ((line = console.readLine(getPrompt())) != null) { executeLine(line); history.addToHistory(line); } } inConsole = false; processCmd(cl); try { myCommands.get(EXIT).runCmd(new String[0]); } catch (Exception e) { } }