private InterpreterContext getInterpreterContext() {
    AngularObjectRegistry registry = null;

    if (!getNoteReplLoader().getInterpreterSettings().isEmpty()) {
      InterpreterSetting intpGroup = getNoteReplLoader().getInterpreterSettings().get(0);
      registry = intpGroup.getInterpreterGroup().getAngularObjectRegistry();
    }

    List<InterpreterContextRunner> runners = new LinkedList<InterpreterContextRunner>();
    for (Paragraph p : note.getParagraphs()) {
      runners.add(new ParagraphRunner(note, note.id(), p.getId()));
    }

    InterpreterContext interpreterContext =
        new InterpreterContext(
            note.id(),
            getId(),
            this.getTitle(),
            this.getText(),
            this.getConfig(),
            this.settings,
            registry,
            runners);
    return interpreterContext;
  }
  private InterpreterContext getInterpreterContext() {
    AngularObjectRegistry registry = null;
    ResourcePool resourcePool = null;

    if (!getNoteReplLoader().getInterpreterSettings().isEmpty()) {
      InterpreterSetting intpGroup = getNoteReplLoader().getInterpreterSettings().get(0);
      registry = intpGroup.getInterpreterGroup().getAngularObjectRegistry();
      resourcePool = intpGroup.getInterpreterGroup().getResourcePool();
    }

    List<InterpreterContextRunner> runners = new LinkedList<InterpreterContextRunner>();
    for (Paragraph p : note.getParagraphs()) {
      runners.add(new ParagraphRunner(note, note.id(), p.getId()));
    }

    final Paragraph self = this;
    InterpreterContext interpreterContext =
        new InterpreterContext(
            note.id(),
            getId(),
            this.getTitle(),
            this.getText(),
            this.getConfig(),
            this.settings,
            registry,
            resourcePool,
            runners,
            new InterpreterOutput(
                new InterpreterOutputListener() {
                  @Override
                  public void onAppend(InterpreterOutput out, byte[] line) {
                    updateParagraphResult(out);
                    ((ParagraphJobListener) getListener())
                        .onOutputAppend(self, out, new String(line));
                  }

                  @Override
                  public void onUpdate(InterpreterOutput out, byte[] output) {
                    updateParagraphResult(out);
                    ((ParagraphJobListener) getListener())
                        .onOutputUpdate(self, out, new String(output));
                  }

                  private void updateParagraphResult(InterpreterOutput out) {
                    // update paragraph result
                    Throwable t = null;
                    String message = null;
                    try {
                      message = new String(out.toByteArray());
                    } catch (IOException e) {
                      logger().error(e.getMessage(), e);
                      t = e;
                    }
                    setReturn(new InterpreterResult(Code.SUCCESS, out.getType(), message), t);
                  }
                }));
    return interpreterContext;
  }
 public List<InterpreterCompletion> getInterpreterCompletion() {
   List<InterpreterCompletion> completion = new LinkedList();
   for (InterpreterSetting intp : factory.getInterpreterSettings(note.getId())) {
     List<InterpreterInfo> intInfo = intp.getInterpreterInfos();
     if (intInfo.size() > 1) {
       for (InterpreterInfo info : intInfo) {
         String name = intp.getName() + "." + info.getName();
         completion.add(new InterpreterCompletion(name, name));
       }
     } else {
       completion.add(new InterpreterCompletion(intp.getName(), intp.getName()));
     }
   }
   return completion;
 }
  private boolean hasPermission(String user, List<String> intpUsers) {
    if (1 > intpUsers.size()) {
      return true;
    }

    for (String u : intpUsers) {
      if (user.trim().equals(u.trim())) {
        return true;
      }
    }
    return false;
  }
  private InterpreterContext getInterpreterContext(InterpreterOutput output) {
    AngularObjectRegistry registry = null;
    ResourcePool resourcePool = null;

    if (!factory.getInterpreterSettings(note.getId()).isEmpty()) {
      InterpreterSetting intpGroup = factory.getInterpreterSettings(note.getId()).get(0);
      registry = intpGroup.getInterpreterGroup(getUser(), note.getId()).getAngularObjectRegistry();
      resourcePool = intpGroup.getInterpreterGroup(getUser(), note.getId()).getResourcePool();
    }

    List<InterpreterContextRunner> runners = new LinkedList<>();
    for (Paragraph p : note.getParagraphs()) {
      runners.add(new ParagraphRunner(note, note.getId(), p.getId()));
    }

    final Paragraph self = this;

    Credentials credentials = note.getCredentials();
    if (authenticationInfo != null) {
      UserCredentials userCredentials =
          credentials.getUserCredentials(authenticationInfo.getUser());
      authenticationInfo.setUserCredentials(userCredentials);
    }

    InterpreterContext interpreterContext =
        new InterpreterContext(
            note.getId(),
            getId(),
            getRequiredReplName(),
            this.getTitle(),
            this.getText(),
            this.getAuthenticationInfo(),
            this.getConfig(),
            this.settings,
            registry,
            resourcePool,
            runners,
            output);
    return interpreterContext;
  }
  public ApplicationState createOrGetApplicationState(HeliumPackage pkg) {
    synchronized (apps) {
      for (ApplicationState as : apps) {
        if (as.equals(pkg)) {
          return as;
        }
      }

      String appId = getApplicationId(pkg);
      ApplicationState appState = new ApplicationState(appId, pkg);
      apps.add(appState);
      return appState;
    }
  }
  @Override
  protected Object jobRun() throws Throwable {
    String replName = getRequiredReplName();
    Interpreter repl = getRepl(replName);
    logger.info("run paragraph {} using {} " + repl, getId(), replName);
    if (repl == null) {
      logger.error("Can not find interpreter name " + repl);
      throw new RuntimeException("Can not find interpreter for " + getRequiredReplName());
    }
    InterpreterSetting intp = getInterpreterSettingById(repl.getInterpreterGroup().getId());
    while (intp.getStatus()
        .equals(
            org.apache.zeppelin.interpreter.InterpreterSetting.Status.DOWNLOADING_DEPENDENCIES)) {
      Thread.sleep(200);
    }
    if (this.noteHasUser() && this.noteHasInterpreters()) {
      if (intp != null
          && interpreterHasUser(intp)
          && isUserAuthorizedToAccessInterpreter(intp.getOption()) == false) {
        logger.error("{} has no permission for {} ", authenticationInfo.getUser(), repl);
        return new InterpreterResult(
            Code.ERROR,
            authenticationInfo.getUser() + " has no permission for " + getRequiredReplName());
      }
    }

    String script = getScriptBody();
    // inject form
    if (repl.getFormType() == FormType.NATIVE) {
      settings.clear();
    } else if (repl.getFormType() == FormType.SIMPLE) {
      String scriptBody = getScriptBody();
      Map<String, Input> inputs = Input.extractSimpleQueryParam(scriptBody); // inputs will be built
      // from script body

      final AngularObjectRegistry angularRegistry =
          repl.getInterpreterGroup().getAngularObjectRegistry();

      scriptBody = extractVariablesFromAngularRegistry(scriptBody, inputs, angularRegistry);

      settings.setForms(inputs);
      script = Input.getSimpleQuery(settings.getParams(), scriptBody);
    }
    logger.debug("RUN : " + script);
    try {
      InterpreterContext context = getInterpreterContext();
      InterpreterContext.set(context);
      InterpreterResult ret = repl.interpret(script, context);

      if (Code.KEEP_PREVIOUS_RESULT == ret.code()) {
        return getReturn();
      }

      context.out.flush();
      List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
      resultMessages.addAll(ret.message());
      return new InterpreterResult(ret.code(), resultMessages);
    } finally {
      InterpreterContext.remove();
    }
  }