/**
   * Creates a CLI script for adding a Connector
   *
   * @param connAS7 object of migrated connector
   * @return string containing created CLI script
   * @throws CliScriptException if required attributes are missing
   * @deprecated Generate this from the ModelNode.
   */
  private static String createConnectorScript(ConnectorAS7Bean connAS7) throws CliScriptException {
    String errMsg = " in connector must be set.";
    Utils.throwIfBlank(connAS7.getScheme(), errMsg, "Scheme");
    Utils.throwIfBlank(connAS7.getSocketBinding(), errMsg, "Socket-binding");
    Utils.throwIfBlank(connAS7.getConnectorName(), errMsg, "Connector name");
    Utils.throwIfBlank(connAS7.getProtocol(), errMsg, "Protocol");

    CliAddScriptBuilder builder = new CliAddScriptBuilder();
    StringBuilder resultScript = new StringBuilder("/subsystem=web/connector=");

    resultScript.append(connAS7.getConnectorName()).append(":add(");

    builder.addProperty("socket-binding", connAS7.getSocketBinding());
    builder.addProperty("enable-lookups", connAS7.getEnableLookups());
    builder.addProperty("max-post-size", connAS7.getMaxPostSize());
    builder.addProperty("max-save-post-size", connAS7.getMaxSavePostSize());
    builder.addProperty("max-connections", connAS7.getMaxConnections());
    builder.addProperty("protocol", connAS7.getProtocol());
    builder.addProperty("proxy-name", connAS7.getProxyName());
    builder.addProperty("proxy-port", connAS7.getProxyPort());
    builder.addProperty("redirect-port", connAS7.getRedirectPort());
    builder.addProperty("scheme", connAS7.getScheme());
    builder.addProperty("secure", connAS7.getSecure());
    builder.addProperty("enabled", connAS7.getEnabled());

    resultScript.append(builder.asString()).append(")");

    return resultScript.toString();
  }
  /**
   * Creates CopyFileAction for keystores files from Connectors
   *
   * @param resource helping class containing all resources for ServerMigrator
   * @param fName name of the keystore file to be copied into AS7
   * @return null if the file is already set for copying or the file cannot be found in the AS5
   *     structure else created CopyFileAction
   */
  private CopyFileAction createCopyActionForKeyFile(ServerMigratorResource resource, String fName) {
    // TODO:
    final String property = "${jboss.server.home.dir}";

    // TODO: MIGR-54 The paths in AS 5 config relate to some base dir. Find out which and use that,
    // instead of searching.
    //       Then, create the actions directly in the code creating this "files to copy" collection.
    File as5profileDir = getGlobalConfig().getAS5Config().getProfileDir();
    File src;
    try {
      src = Utils.searchForFile(fName, as5profileDir).iterator().next();
    } catch (CopyException ex) {
      // throw new ActionException("Failed copying a security file: " + ex.getMessage(), ex);
      // Some files referenced in security may not exist. (?)
      log.warn("Couldn't find file referenced in AS 5 server config: " + fName);
      return null;
    }

    if (!resource.getKeystores().add(src)) return null;

    File target =
        Utils.createPath(getGlobalConfig().getAS7Config().getConfigDir(), "keys", src.getName());
    CopyFileAction action =
        new CopyFileAction(this.getClass(), src, target, CopyFileAction.IfExists.SKIP);
    return action;
  }
  /**
   * Creates a CLI script for adding virtual-server to AS7
   *
   * @param virtualServer object representing migrated virtual-server
   * @return string containing created CLI script
   * @deprecated Generate this from the ModelNode.
   */
  private static String createVirtualServerScript(VirtualServerBean virtualServer)
      throws CliScriptException {
    String errMsg = "in virtual-server (engine in AS5) must be set";
    Utils.throwIfBlank(virtualServer.getVirtualServerName(), errMsg, "Server name");

    CliAddScriptBuilder builder = new CliAddScriptBuilder();
    StringBuilder resultScript = new StringBuilder("/subsystem=web/virtual-server=");
    resultScript.append(virtualServer.getVirtualServerName()).append(":add(");

    builder.addProperty("enable-welcome-root", virtualServer.getEnableWelcomeRoot());
    builder.addProperty("default-web-module", virtualServer.getDefaultWebModule());

    String aliases = "";
    if (virtualServer.getAliasName() != null) {
      StringBuilder aliasBuilder = new StringBuilder();
      for (String alias : virtualServer.getAliasName()) {
        aliasBuilder.append(", \"").append(alias).append("\"");
      }

      aliases = aliasBuilder.toString();
      aliases = aliases.replaceFirst(", ", "");

      if (!aliases.isEmpty()) {
        aliases = ", alias=[" + aliases + "]";
      }
    }

    resultScript.append(builder.asString()).append(aliases).append(")");

    return resultScript.toString();
  }
  @Override
  public void loadAS5Data(MigrationContext ctx) throws LoadMigrationException {

    // TBC: Maybe use FileUtils and list all files with that name?
    File file =
        Utils.createPath(
            super.getGlobalConfig().getAS5Config().getDir(),
            "server",
            super.getGlobalConfig().getAS5Config().getProfileName(),
            "deploy",
            "jbossweb.sar",
            "server.xml");

    if (!file.canRead())
      throw new LoadMigrationException(
          "Cannot find/open file: " + file.getAbsolutePath(), new FileNotFoundException());

    try {
      Unmarshaller unmarshaller = JAXBContext.newInstance(ServerAS5Bean.class).createUnmarshaller();

      ServerAS5Bean serverAS5 = (ServerAS5Bean) unmarshaller.unmarshal(file);

      MigrationData mData = new MigrationData();
      for (ServiceBean s : serverAS5.getServices()) {
        mData.getConfigFragments().add(s.getEngine());
        mData.getConfigFragments().addAll(s.getConnectorAS5s());
      }

      ctx.getMigrationData().put(ServerMigrator.class, mData);
    } catch (JAXBException e) {
      throw new LoadMigrationException("Failed parsing logging config file: " + file.getPath(), e);
    }
  }
  /**
   * Creates CliCommandAction for adding a Virtual-Server
   *
   * @param server Virtual-Server
   * @return created CliCommandAction for adding the Virtual-Server
   * @throws CliScriptException if required attributes for a creation of the CLI command of the
   *     Virtual-Server are missing or are empty (server-name)
   */
  public static CliCommandAction createVirtualServerCliAction(VirtualServerBean server)
      throws CliScriptException {
    String errMsg = "in virtual-server (engine in AS5) must be set";
    Utils.throwIfBlank(server.getVirtualServerName(), errMsg, "Server name");

    ModelNode serverCmd = new ModelNode();
    serverCmd.get(ClientConstants.OP).set(ClientConstants.ADD);
    serverCmd.get(ClientConstants.OP_ADDR).add("subsystem", "web");
    serverCmd.get(ClientConstants.OP_ADDR).add("virtual-server", server.getVirtualServerName());

    if (server.getAliasName() != null) {
      ModelNode aliasesNode = new ModelNode();
      for (String alias : server.getAliasName()) {
        ModelNode aliasNode = new ModelNode();

        aliasNode.set(alias);
        aliasesNode.add(aliasNode);
      }
      serverCmd.get("alias").set(aliasesNode);
    }

    CliApiCommandBuilder builder = new CliApiCommandBuilder(serverCmd);

    builder.addProperty("enable-welcome-root", server.getEnableWelcomeRoot());
    builder.addProperty("default-web-module", server.getDefaultWebModule());

    return new CliCommandAction(
        ServerMigrator.class, createVirtualServerScript(server), builder.getCommand());
  }
  /**
   * Creates a CLI script for adding socket-binding to AS7
   *
   * @param socketBinding object representing socket-binding
   * @return string containing created CLI script
   * @throws CliScriptException if required attributes are missing
   * @deprecated Generate this from the ModelNode.
   */
  private static String createSocketBindingScript(SocketBindingBean socketBinding)
      throws CliScriptException {
    String errMsg = " in socket-binding must be set.";
    Utils.throwIfBlank(socketBinding.getSocketPort(), errMsg, "Port");
    Utils.throwIfBlank(socketBinding.getSocketName(), errMsg, "Name");

    CliAddScriptBuilder builder = new CliAddScriptBuilder();
    StringBuilder resultScript =
        new StringBuilder("/socket-binding-group=standard-sockets/socket-binding=");

    resultScript.append(socketBinding.getSocketName()).append(":add(");
    resultScript.append("port=").append(socketBinding.getSocketPort());

    builder.addProperty("interface", socketBinding.getSocketInterface());

    resultScript.append(builder.asString()).append(")");

    return resultScript.toString();
  }
  /**
   * Creates CliCommandAction for adding a Socket-Binding
   *
   * @param socket Socket-Binding
   * @return created CliCommandAction for adding the Socket-Binding
   * @throws CliScriptException if required attributes for a creation of the CLI command of the
   *     Security-Domain are missing or are empty (security-domain-name)
   */
  public static CliCommandAction createSocketBindingCliAction(SocketBindingBean socket)
      throws CliScriptException {
    String errMsg = " in socket-binding must be set.";
    Utils.throwIfBlank(socket.getSocketPort(), errMsg, "Port");
    Utils.throwIfBlank(socket.getSocketName(), errMsg, "Name");

    ModelNode serverCmd = new ModelNode();
    serverCmd.get(ClientConstants.OP).set(ClientConstants.ADD);
    serverCmd.get(ClientConstants.OP_ADDR).add("socket-binding-group", "standard-sockets");
    serverCmd.get(ClientConstants.OP_ADDR).add("socket-binding", socket.getSocketName());

    serverCmd.get("port").set(socket.getSocketPort());

    CliApiCommandBuilder builder = new CliApiCommandBuilder(serverCmd);
    builder.addProperty("interface", socket.getSocketInterface());

    return new CliCommandAction(
        ServerMigrator.class, createSocketBindingScript(socket), builder.getCommand());
  }
  /**
   * Creates a list of CliCommandActions for adding a Connector
   *
   * @param connAS7 Connector
   * @return created list containing CliCommandActions for adding the Connector
   * @throws CliScriptException if required attributes for a creation of the CLI command of the
   *     Connector are missing or are empty (socket-binding, connector-name, protocol)
   */
  private static List<CliCommandAction> createConnectorCliAction(ConnectorAS7Bean connAS7)
      throws CliScriptException {
    String errMsg = " in connector must be set.";
    Utils.throwIfBlank(connAS7.getScheme(), errMsg, "Scheme");
    Utils.throwIfBlank(connAS7.getSocketBinding(), errMsg, "Socket-binding");
    Utils.throwIfBlank(connAS7.getConnectorName(), errMsg, "Connector name");
    Utils.throwIfBlank(connAS7.getProtocol(), errMsg, "Protocol");

    List<CliCommandAction> actions = new LinkedList();

    actions.add(
        new CliCommandAction(
            ServerMigrator.class,
            createConnectorScript(connAS7),
            createConnectorModelNode(connAS7)));

    if (connAS7.getScheme().equals("https")) {
      actions.add(
          new CliCommandAction(
              ServerMigrator.class, createSSLConfScript(connAS7), createSSLConfModelNode(connAS7)));
    }

    return actions;
  }