<commit_before>/*
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terracotta.management.integration.tests;

import org.terracotta.ipceventbus.proc.AnyProcess;
import org.terracotta.ipceventbus.proc.AnyProcessBuilder;
import org.terracotta.testing.config.DefaultStartupCommandBuilder;
import org.terracotta.testing.config.StartupCommandBuilder;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static org.terracotta.testing.config.ConfigConstants.DEFAULT_CLUSTER_NAME;
import static org.terracotta.testing.demos.TestHelpers.isWindows;

public class DynamicConfigStartupBuilder extends DefaultStartupCommandBuilder {
  private int stripeId;
  private String[] builtCommand;
  private int debugPort = Integer.getInteger("serverDebugPortStart", 0);

  @Override
  public StartupCommandBuilder stripeName(String stripeName) {
    this.stripeId = Integer.parseInt(stripeName.substring(6));
    return this;
  }

  @Override
  public String[] build() {
    if (builtCommand == null) {
      try {
        installServer();
        Path basePath = getKitDir().resolve("server").resolve("bin").resolve("start-tc-server");
        String startScript = isWindows() ? basePath + ".bat" : basePath + ".sh";
        if (isConsistentStartup()) {
          throw new UnsupportedOperationException("Consistent startup is not supported with start-node script");
        } else {
          Path generatedRepositories = convertConfigFiles();
          // moves the generated files onto the server folder, but only for this server we are building
          Files.move(generatedRepositories.resolve("stripe-" + stripeId).resolve(getServerName()), getServerWorkingDir().resolve("repository"));
          builtCommand = new String[]{startScript, "-r", "repository"};
        }
      } catch (IOException e) {
        throw new UncheckedIOException(e);
      }
    }

    return builtCommand;
  }

  private Path convertConfigFiles() {
    Path generatedRepositories = getServerWorkingDir().getParent().getParent().resolve("generated-repositories");

    if (Files.exists(generatedRepositories)) {
      // this builder is called fro each server, but the CLI will generate the repositories for all.
      return generatedRepositories;
    }
<commit_msg>Add support for consistent startup in DynamicConfigStartupBuilder<commit_after>/*
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terracotta.management.integration.tests;

import org.terracotta.ipceventbus.proc.AnyProcess;
import org.terracotta.ipceventbus.proc.AnyProcessBuilder;
import org.terracotta.testing.config.DefaultStartupCommandBuilder;
import org.terracotta.testing.config.StartupCommandBuilder;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static org.terracotta.testing.config.ConfigConstants.DEFAULT_CLUSTER_NAME;
import static org.terracotta.testing.demos.TestHelpers.isWindows;

public class DynamicConfigStartupBuilder extends DefaultStartupCommandBuilder {
  private int stripeId;
  private String[] builtCommand;
  private int debugPort = Integer.getInteger("serverDebugPortStart", 0);

  @Override
  public StartupCommandBuilder stripeName(String stripeName) {
    this.stripeId = Integer.parseInt(stripeName.substring(6));
    return this;
  }

  @Override
  public String[] build() {
    if (builtCommand == null) {
      try {
        installServer();
        Path generatedRepositories = convertConfigFiles();
        // moves the generated files onto the server folder, but only for this server we are building
        Files.move(generatedRepositories.resolve("stripe-" + stripeId).resolve(getServerName()), getServerWorkingDir().resolve("repository"));
        buildStartupCommand();
      } catch (IOException e) {
        throw new UncheckedIOException(e);
      }
    }
    return builtCommand;
  }

  private void buildStartupCommand() {
    List<String> command = new ArrayList<>();
    Path basePath = getKitDir().resolve("server").resolve("bin").resolve("start-tc-server");
    String startScript = isWindows() ? basePath + ".bat" : basePath + ".sh";
    command.add(startScript);
    if (isConsistentStartup()) {
      command.add("-c");
    }
    command.add("-r");
    command.add("repository");
    builtCommand = command.toArray(new String[0]);
  }

  private Path convertConfigFiles() {
    Path generatedRepositories = getServerWorkingDir().getParent().getParent().resolve("generated-repositories");

    if (Files.exists(generatedRepositories)) {
      // this builder is called fro each server, but the CLI will generate the repositories for all.
      return generatedRepositories;
    }

{'fraction_non_alphanumeric': 0.09888814016172506, 'fraction_numerical': 0.0021900269541778976, 'mean_word_length': 4.1447140381282495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6895423', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1562, 'n_words': 461}