Java Code Examples for com.hubspot.jinjava.Jinjava#render()

The following examples show how to use com.hubspot.jinjava.Jinjava#render() . 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: UpdateIntelliJSdksTaskTest.java    From curiostack with MIT License 6 votes vote down vote up
private String testTemplate(String path) {
  String template = resource(path);

  Jinjava jinjava = new Jinjava();
  return jinjava.render(
      template,
      ImmutableMap.<String, Object>builder()
          .put("gradleHome", testGradleHome.toAbsolutePath().toString().replace('\\', '/'))
          .put("jdkFolder", "jdk-zulu13.28.11-ca-jdk13.0.1")
          .put("javaVersion", "zulu13.28.11-ca-jdk13.0.1")
          .put(
              "jdk8Folder", "zulu8.42.0.21-ca-jdk8.0.232/zulu8.42.0.21-ca-jdk8.0.232-" + suffix())
          .put("java8Version", "zulu8.42.0.21-ca-jdk8.0.232")
          .put("goVersion", ToolDependencies.getDefaultVersion("golang"))
          .build());
}
 
Example 2
Source File: SubmarineUI.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void createUsageUI() {
  try {
    List<CommandlineOption> commandlineOptions = getCommandlineOptions();
    HashMap<String, Object> mapParams = new HashMap();
    mapParams.put(unifyKey(SubmarineConstants.PARAGRAPH_ID), intpContext.getParagraphId());
    mapParams.put(SubmarineConstants.COMMANDLINE_OPTIONS, commandlineOptions);

    URL urlTemplate = Resources.getResource(SUBMARINE_USAGE_JINJA);
    String template = Resources.toString(urlTemplate, Charsets.UTF_8);
    Jinjava jinjava = new Jinjava();
    String submarineUsage = jinjava.render(template, mapParams);

    InterpreterResultMessageOutput outputUI = intpContext.out.getOutputAt(0);
    outputUI.clear();
    outputUI.write(submarineUsage);
    outputUI.flush();

    // UI update, log needs to be cleaned at the same time
    InterpreterResultMessageOutput outputLOG = intpContext.out.getOutputAt(1);
    outputLOG.clear();
    outputLOG.flush();
  } catch (IOException e) {
    LOGGER.error("Can't print usage", e);
  }
}
 
Example 3
Source File: SubmarineUI.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void createLogHeadUI() {
  try {
    HashMap<String, Object> mapParams = new HashMap();
    URL urlTemplate = Resources.getResource(SUBMARINE_LOG_HEAD_JINJA);
    String template = Resources.toString(urlTemplate, Charsets.UTF_8);
    Jinjava jinjava = new Jinjava();
    String submarineUsage = jinjava.render(template, mapParams);

    InterpreterResultMessageOutput outputUI = intpContext.out.getOutputAt(1);
    outputUI.clear();
    outputUI.write(submarineUsage);
    outputUI.flush();
  } catch (IOException e) {
    LOGGER.error("Can't print usage", e);
  }
}
 
Example 4
Source File: SubmarineUI.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void printCommnadUI(SubmarineCommand submarineCmd) {
  try {
    HashMap<String, Object> mapParams = new HashMap();
    mapParams.put(unifyKey(SubmarineConstants.PARAGRAPH_ID), intpContext.getParagraphId());

    URL urlTemplate = Resources.getResource(SUBMARINE_DASHBOARD_JINJA);
    String template = Resources.toString(urlTemplate, Charsets.UTF_8);
    Jinjava jinjava = new Jinjava();
    String submarineUsage = jinjava.render(template, mapParams);

    InterpreterResultMessageOutput outputUI = intpContext.out.getOutputAt(0);
    outputUI.clear();
    outputUI.write(submarineUsage);
    outputUI.flush();

    // UI update, log needs to be cleaned at the same time
    InterpreterResultMessageOutput outputLOG = intpContext.out.getOutputAt(1);
    outputLOG.clear();
    outputLOG.flush();
  } catch (IOException e) {
    LOGGER.error("Can't print usage", e);
  }
}
 
Example 5
Source File: JinjaTemplatesTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public String jobRunJinjaTemplateTest(Boolean dist, Boolean launchMode) throws IOException {
  URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_JOBRUN_TF_JINJA);
  String template = Resources.toString(urlTemplate, Charsets.UTF_8);
  Jinjava jinjava = new Jinjava();
  HashMap<String, Object> jinjaParams = initJinjaParams(dist, launchMode);

  String submarineCmd = jinjava.render(template, jinjaParams);
  int pos = submarineCmd.indexOf("\n");
  if (pos == 0) {
    submarineCmd = submarineCmd.replaceFirst("\n", "");
  }

  LOGGER.info("------------------------");
  LOGGER.info(submarineCmd);
  LOGGER.info("------------------------");

  return submarineCmd;
}
 
Example 6
Source File: JinjaTemplatesTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public String tensorboardJinjaTemplateTest(Boolean dist, Boolean launchMode) throws IOException {
  URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_TENSORBOARD_JINJA);
  String template = Resources.toString(urlTemplate, Charsets.UTF_8);
  Jinjava jinjava = new Jinjava();
  HashMap<String, Object> jinjaParams = initJinjaParams(dist, launchMode);

  String submarineCmd = jinjava.render(template, jinjaParams);
  int pos = submarineCmd.indexOf("\n");
  if (pos == 0) {
    submarineCmd = submarineCmd.replaceFirst("\n", "");
  }
  LOGGER.info("------------------------");
  LOGGER.info(submarineCmd);
  LOGGER.info("------------------------");

  return submarineCmd;
}
 
Example 7
Source File: K8sRemoteInterpreterProcess.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
String sparkUiWebUrlFromTemplate(String templateString, int port, String serviceName, String serviceDomain) {
  ImmutableMap<String, Object> binding = ImmutableMap.of(
      "PORT", port,
      "SERVICE_NAME", serviceName,
      ENV_SERVICE_DOMAIN, serviceDomain
  );

  ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    Jinjava jinja = new Jinjava();
    return jinja.render(templateString, binding);
  } finally {
    Thread.currentThread().setContextClassLoader(oldCl);
  }
}
 
Example 8
Source File: UpdateIntelliJSdksTask.java    From curiostack with MIT License 5 votes vote down vote up
private static void addJdkSnippet(
    String templatePath,
    Map<String, Object> templateVars,
    ImmutableList.Builder<String> lines,
    boolean skipStart)
    throws IOException {
  Jinjava jinjava = new Jinjava();

  String template =
      Resources.toString(Resources.getResource(templatePath), StandardCharsets.UTF_8);

  String rendered = jinjava.render(template, templateVars);

  rendered.lines().skip(skipStart ? 2 : 0).forEach(lines::add);
}
 
Example 9
Source File: SubmarineUI.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public void createSubmarineUI(SubmarineCommand submarineCmd) {
  try {
    HashMap<String, Object> mapParams = new HashMap();
    mapParams.put(unifyKey(SubmarineConstants.PARAGRAPH_ID), intpContext.getParagraphId());
    mapParams.put(unifyKey(SubmarineConstants.COMMAND_TYPE), submarineCmd.getCommand());

    String templateName = "";
    switch (submarineCmd) {
      case USAGE:
        templateName = SUBMARINE_USAGE_JINJA;
        List<CommandlineOption> commandlineOptions = getCommandlineOptions();
        mapParams.put(SubmarineConstants.COMMANDLINE_OPTIONS, commandlineOptions);
        break;
      default:
        templateName = SUBMARINE_DASHBOARD_JINJA;
        break;
    }

    URL urlTemplate = Resources.getResource(templateName);
    String template = Resources.toString(urlTemplate, Charsets.UTF_8);
    Jinjava jinjava = new Jinjava();
    String submarineUsage = jinjava.render(template, mapParams);

    // UI
    InterpreterResultMessageOutput outputUI = intpContext.out.getOutputAt(0);
    outputUI.clear();
    outputUI.write(submarineUsage);
    outputUI.flush();

    // UI update, log needs to be cleaned at the same time
    InterpreterResultMessageOutput outputLOG = intpContext.out.getOutputAt(1);
    outputLOG.clear();
    outputLOG.flush();
  } catch (IOException e) {
    LOGGER.error("Can't print usage", e);
  }
}
 
Example 10
Source File: DockerSpecTemplate.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public String render(String template) {
  ClassLoader oldClazzLoader = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    Jinjava jinja = new Jinjava();
    return jinja.render(template, this);
  } finally {
    Thread.currentThread().setContextClassLoader(oldClazzLoader);
  }
}
 
Example 11
Source File: K8sSpecTemplate.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public String render(String template) {
  ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    Jinjava jinja = new Jinjava();
    return jinja.render(template, this);
  } finally {
    Thread.currentThread().setContextClassLoader(oldCl);
  }
}
 
Example 12
Source File: JinjaArtifactExtractor.java    From kork with Apache License 2.0 5 votes vote down vote up
private String jinjaTransform(String messagePayload) {
  if (StringUtils.isEmpty(jinjaTemplate)) {
    return messagePayload;
  }
  Jinjava jinja = jinjavaFactory.create();
  Map<String, ?> context = readMapValue(messagePayload);
  return jinja.render(jinjaTemplate, context);
}
 
Example 13
Source File: UpdateGradleWrapperTask.java    From curiostack with MIT License 4 votes vote down vote up
@TaskAction
public void exec() throws IOException {
  var rootProject = getProject().getRootProject();

  String version = ToolDependencies.getOpenJdkVersion(rootProject);

  String urlBase = "https://cdn.azul.com/zulu/bin/" + version + '-';
  Map<String, String> templateVars =
      ImmutableMap.<String, String>builder()
          .put("dest_folder", "jdk-" + version)
          .put("url_linux", urlBase + "linux_x64.tar.gz")
          .put("url_mac", urlBase + "macosx_x64.tar.gz")
          .put("url_windows", urlBase + "win_x64.zip")
          .put("dest_archive_name", "jdk-" + version + ".tar.gz.or.zip")
          .put("version", version)
          .build();

  Jinjava jinjava = new Jinjava(JinjavaConfig.newBuilder().withFailOnUnknownTokens(true).build());
  String rendered =
      jinjava.render(
          Resources.toString(
              Resources.getResource("curiostack/get-jdk.sh.tmpl"), StandardCharsets.UTF_8),
          templateVars);

  Files.writeString(
      rootProject.file("gradle/get-jdk.sh").toPath(), rendered, StandardCharsets.UTF_8);

  var gradlew = rootProject.file("gradlew").toPath();
  var gradleWrapperLines = Files.readAllLines(gradlew);
  if (gradleWrapperLines.stream().anyMatch(line -> line.contains(". ./gradle/get-jdk.sh"))) {
    return;
  }

  // First line is always shebang, skip it.
  int lineIndexAfterCopyright = 1;
  // Skip empty lines
  for (; lineIndexAfterCopyright < gradleWrapperLines.size(); lineIndexAfterCopyright++) {
    if (!gradleWrapperLines.get(lineIndexAfterCopyright).isEmpty()) {
      break;
    }
  }
  // Skip comment lines, they are all the license
  for (; lineIndexAfterCopyright < gradleWrapperLines.size(); lineIndexAfterCopyright++) {
    if (!gradleWrapperLines.get(lineIndexAfterCopyright).startsWith("#")) {
      break;
    }
  }
  // Found first empty line after license, insert our JDK download script and write it out.
  var linesWithGetJdk =
      ImmutableList.<String>builderWithExpectedSize(gradleWrapperLines.size() + 2);
  linesWithGetJdk.addAll(gradleWrapperLines.subList(0, lineIndexAfterCopyright));
  linesWithGetJdk.add("").add(". ./gradle/get-jdk.sh");
  linesWithGetJdk.addAll(
      gradleWrapperLines.subList(lineIndexAfterCopyright, gradleWrapperLines.size()));

  Files.writeString(gradlew, String.join("\n", linesWithGetJdk.build()));
}