/**
  * Checks the existence of a resource. If the resource exists, {@code true} is returned, otherwise
  * {@code false}.
  *
  * @param address the address of the resource to check.
  * @param client the client used to execute the operation.
  * @return {@code true} if the resources exists, otherwise {@code false}.
  * @throws IOException if an error occurs executing the operation.
  * @throws RuntimeException if the operation fails.
  */
 private boolean resourceExists(final ModelNode address, final ModelControllerClient client)
     throws IOException {
   final Property childAddress = ServerOperations.getChildAddress(address);
   final ModelNode parentAddress = ServerOperations.getParentAddress(address);
   final ModelNode r =
       client.execute(
           ServerOperations.createOperation(ServerOperations.READ_RESOURCE, parentAddress, false));
   reportFailure(r);
   boolean found = false;
   final String name = childAddress.getName();
   if (ServerOperations.isSuccessfulOutcome(r)) {
     for (ModelNode dataSource : ServerOperations.readResult(r).get(name).asList()) {
       if (dataSource.asProperty().getName().equals(childAddress.getValue().asString())) {
         found = true;
       }
     }
   }
   return found;
 }
  @Override
  protected void doExecute() throws MojoExecutionException, MojoFailureException {
    final Log log = getLog();
    final File deploymentFile = file();
    // The deployment must exist before we do anything
    if (!deploymentFile.exists()) {
      throw new MojoExecutionException(
          String.format(
              "The deployment '%s' could not be found.", deploymentFile.getAbsolutePath()));
    }
    // Validate the environment
    final Path jbossHome = extractIfRequired(deploymentFile.getParentFile().toPath());
    if (!Files.isDirectory(jbossHome)) {
      throw new MojoExecutionException(
          String.format("JBOSS_HOME '%s' is not a valid directory.", jbossHome));
    }
    final StandaloneCommandBuilder commandBuilder =
        StandaloneCommandBuilder.of(jbossHome)
            .setJavaHome(javaHome)
            .addModuleDirs(modulesPath.getModulePaths());

    // Set the JVM options
    if (javaOpts != null) {
      commandBuilder.setJavaOptions(javaOpts);
    } else if (jvmArgs != null) {
      commandBuilder.setJavaOptions(jvmArgs.split("\\s+"));
    }

    if (serverConfig != null) {
      commandBuilder.setServerConfiguration(serverConfig);
    }

    if (propertiesFile != null) {
      commandBuilder.setPropertiesFile(propertiesFile);
    }

    if (serverArgs != null) {
      commandBuilder.addServerArguments(serverArgs);
    }

    // Check for management overrides
    final ModelControllerClientConfiguration clientConfiguration = getClientConfiguration();
    final String host = clientConfiguration.getHost();
    final int port = clientConfiguration.getPort();
    if (host != null) {
      commandBuilder.setBindAddressHint("management", host);
    }
    if (port > 0) {
      commandBuilder.addServerArguments("-Djboss.management.http.port=" + port);
    }

    // Print some server information
    log.info(String.format("JAVA_HOME=%s", commandBuilder.getJavaHome()));
    log.info(String.format("JBOSS_HOME=%s%n", commandBuilder.getWildFlyHome()));
    Server server = null;
    try (final ManagementClient client = createClient()) {
      // Create the server
      server = Server.create(commandBuilder, client);
      // Start the server
      log.info("Server is starting up. Press CTRL + C to stop the server.");
      server.start(startupTimeout);
      // Deploy the application
      server.checkServerState();
      if (server.isRunning()) {
        log.info(String.format("Deploying application '%s'%n", deploymentFile.getName()));
        final Deployment deployment =
            StandaloneDeployment.create(client, deploymentFile, name, getType(), null, null);
        switch (executeDeployment(client, deployment, jbossHome)) {
          case REQUIRES_RESTART:
            {
              client.execute(ServerOperations.createOperation(ServerOperations.RELOAD));
              break;
            }
          case SUCCESS:
            break;
        }
      } else {
        throw new DeploymentFailureException("Cannot deploy to a server that is not running.");
      }
      while (server.isRunning()) {
        TimeUnit.SECONDS.sleep(1L);
      }
    } catch (Exception e) {
      throw new MojoExecutionException("The server failed to start", e);
    } finally {
      if (server != null) server.stop();
    }
  }