org.springframework.shell.core.JLineShell Java Examples

The following examples show how to use org.springframework.shell.core.JLineShell. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LensInterpreter.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private InterpreterResult handleHelp(JLineShell shell, String st) {
  java.util.logging.StreamHandler sh = null;
  java.util.logging.Logger springLogger = null;
  java.util.logging.Formatter formatter = new java.util.logging.Formatter() {
    public String format(java.util.logging.LogRecord record) {
      return record.getMessage();
    }
  };
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try {
    sh = new java.util.logging.StreamHandler(baos, formatter);
    springLogger = HandlerUtils.getLogger(org.springframework.shell.core.SimpleParser.class);
    springLogger.addHandler(sh);
    shell.executeCommand(st);
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
    return new InterpreterResult(Code.ERROR, e.getMessage());
  } finally {
    sh.flush();
    springLogger.removeHandler(sh);
    sh.close();
  }
  return new InterpreterResult(Code.SUCCESS, baos.toString());
}
 
Example #2
Source File: LensInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private JLineShell getJLineShell(Bootstrap bs) {
  if (bs instanceof LensBootstrap) {
    return ((LensBootstrap) bs).getLensJLineShellComponent();
  } else {
    return bs.getJLineShellComponent();
  }
}
 
Example #3
Source File: LensInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void closeShell(JLineShell shell) {
  if (shell instanceof LensJLineShellComponent) {
    ((LensJLineShellComponent) shell).stop();
  } else {
    ((JLineShellComponent) shell).stop();
  }
}
 
Example #4
Source File: BaseShellAutoConfiguration.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(JLineShell.class)
public JLineShellComponent shell() {
	return new JLineShellComponent();
}
 
Example #5
Source File: BaseShellAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(JLineShell.class)
public JLineShellComponent shell(ConfigCommands configCommands, TargetHolder targetHolder) {
	return new DataflowJLineShellComponent(configCommands, targetHolder);
}
 
Example #6
Source File: LensInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public InterpreterResult interpret(String input, InterpreterContext context) {
  if (input == null || input.length() == 0) {
    return new InterpreterResult(Code.ERROR, "no command submitted");
  }
  String st = input.replaceAll("\\n", " ");
  LOGGER.info("LensInterpreter command: " + st);
  
  Bootstrap bs = createBootstrap();
  JLineShell  shell = getJLineShell(bs);
  CommandResult res;
  LensClient lensClient = null;
  String qh = null;
  
  if (st.trim().startsWith("help")) {
    return handleHelp(shell, st);
  }
  
  try {
    lensClient = createAndSetLensClient(bs);
    clientMap.put(lensClient, true);
    
    String lensCommand = modifyQueryStatement(st);
    
    LOGGER.info("executing command : " + lensCommand);
    res = shell.executeCommand(lensCommand);
    
    if (!lensCommand.equals(st) && res != null 
        && res.getResult() != null 
        && res.getResult().toString().trim().matches("[a-z0-9-]+")) {
      // setup query progress tracking
      qh = res.getResult().toString();
      paraToQH.put(context.getParagraphId(), new ExecutionDetail(qh, lensClient, shell));
      String getResultsCmd = "query results --async false " + qh;
      LOGGER.info("executing query results command : " + context.getParagraphId() + " : "
              + getResultsCmd);
      res = shell.executeCommand(getResultsCmd); 
      paraToQH.remove(context.getParagraphId());
    }
  } catch (Exception ex) {
    LOGGER.error("error in interpret", ex);
    return new InterpreterResult(Code.ERROR, ex.getMessage());
  } finally {
    if (shell != null) {
      closeShell(shell);
    }
    if (lensClient != null) {
      closeLensClient(lensClient);
      clientMap.remove(lensClient);
    }
    if (qh != null) {
      paraToQH.remove(context.getParagraphId());
    }
  }
  return new InterpreterResult(Code.SUCCESS, formatResult(st, res));
}
 
Example #7
Source File: LensInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public int getProgress(InterpreterContext context) {
  if (paraToQH.containsKey(context.getParagraphId())) {
    LOGGER.info("number of items for which progress can be reported :" + paraToQH.size());
    LOGGER.info("number of open lensclient :" + clientMap.size());
    Bootstrap bs = createBootstrap();
    JLineShell shell = getJLineShell(bs);
    LensClient lensClient = null;
    String qh = paraToQH.get(context.getParagraphId()).getQueryHandle();
    try {
      LOGGER.info("fetch query status for : (" + context.getParagraphId() + ") :" + qh);
      lensClient = createAndSetLensClient(bs);
      clientMap.put(lensClient, true);
      CommandResult res = shell.executeCommand("query status " + qh);
      LOGGER.info(context.getParagraphId() + " --> " + res.getResult().toString());
      //change to debug
      Pattern pattern = Pattern.compile(".*(Progress : (\\d\\.\\d)).*");
      Matcher matcher = pattern.matcher(res.getResult().toString().replaceAll("\\n", " "));
      if (matcher.find(2)) {
        Double d = Double.parseDouble(matcher.group(2)) * 100;
        if (d.intValue() == 100) {
          paraToQH.remove(context.getParagraphId());
        }
        return d.intValue();
      } else {
        return 1;
      }
    } catch (Exception e) {
      LOGGER.error("unable to get progress for (" + context.getParagraphId() + ") :" + qh, e);
      paraToQH.remove(context.getParagraphId());
      return 0;
    } finally {
      if (lensClient != null) {
        closeLensClient(lensClient);
        clientMap.remove(lensClient);
      }
      if (shell != null) {
        closeShell(shell);
      }
    }
  }
  return 0;
}
 
Example #8
Source File: ExecutionDetail.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
ExecutionDetail(String qh, LensClient lensClient, JLineShell shell) {
  this.queryHandle = qh;
  this.lensClient = lensClient;
  this.shell = shell;
}
 
Example #9
Source File: ExecutionDetail.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public JLineShell getShell() {
  return shell;
}