private void processSubsystemConfigInFeaturePack(
      ServerProvisioningFeaturePack provisioningFeaturePack,
      ServerProvisioning serverProvisioning,
      ArtifactFileResolver artifactFileResolver)
      throws IOException {

    File artifactFile =
        artifactFileResolver.getArtifactFile(
            provisioningFeaturePack.getFeaturePack().getArtifact());
    // features packs themselves can contain a 'subsystem-templates' directory. Templates in the
    // feature pack override ones from modules
    serverProvisioning
        .getConfig()
        .getInputStreamSources()
        .addAllSubsystemFileSourcesFromZipFile(artifactFile);
  }
  private void processCopyArtifacts(
      List<CopyArtifact> copyArtifacts,
      ArtifactResolver artifactResolver,
      File outputDirectory,
      Set<String> filesProcessed,
      ArtifactFileResolver artifactFileResolver,
      File schemaOutputDirectory)
      throws IOException {
    for (CopyArtifact copyArtifact : copyArtifacts) {

      // first resolve the artifact
      Artifact artifact = artifactResolver.getArtifact(copyArtifact.getArtifact());
      if (artifact == null) {
        throw new RuntimeException(
            "Could not resolve artifact " + copyArtifact.getArtifact() + " to copy");
      }
      File artifactFile = artifactFileResolver.getArtifactFile(artifact);
      if (artifactFile == null) {
        throw new RuntimeException(
            "Could not resolve file for artifact " + copyArtifact.getArtifact() + " to copy");
      }

      String location = copyArtifact.getToLocation();
      if (location.endsWith("/")) {
        // if the to location ends with a / then it is a directory
        // so we need to append the artifact name
        location += artifactFile.getName();
      }

      if (!filesProcessed.add(location)) {
        continue;
      }
      File target = new File(outputDirectory, location);
      if (!target.getParentFile().isDirectory()) {
        if (!target.getParentFile().mkdirs()) {
          throw new IOException("Could not create directory " + target.getParentFile());
        }
      }
      if (copyArtifact.isExtract()) {
        extractArtifact(artifactFile, target, copyArtifact);
      } else {
        FileUtils.copyFile(artifactFile, target);
      }

      extractSchema(schemaOutputDirectory, artifact, artifactFile);
    }
  }
  private void processFeaturePackModules(
      FeaturePack featurePack,
      List<FeaturePack.Module> includedModules,
      ServerProvisioning serverProvisioning,
      File outputDirectory,
      Set<String> filesProcessed,
      ArtifactFileResolver artifactFileResolver,
      File schemaOutputDirectory)
      throws IOException {
    final boolean thinServer = !serverProvisioning.getDescription().isCopyModuleArtifacts();
    // create the module's artifact property replacer
    final BuildPropertyReplacer buildPropertyReplacer =
        thinServer
            ? new BuildPropertyReplacer(
                new ModuleArtifactPropertyResolver(featurePack.getArtifactResolver()))
            : null;
    // process each module file
    try (JarFile jar = new JarFile(featurePack.getFeaturePackFile())) {
      for (FeaturePack.Module module : includedModules) {
        // process the module file
        final String jarEntryName = module.getModuleFile();
        filesProcessed.add(jarEntryName);
        File targetFile = new File(outputDirectory, jarEntryName);
        // ensure parent dirs exist
        targetFile.getParentFile().mkdirs();
        // extract the module file
        FileUtils.extractFile(jar, jarEntryName, targetFile);
        // read module xml to string for content update
        String moduleXmlContents = FileUtils.readFile(targetFile);
        // parse the module xml
        ModuleParseResult result = module.getModuleParseResult();
        // process module artifacts
        for (ModuleParseResult.ArtifactName artifactName : result.getArtifacts()) {
          String artifactCoords = artifactName.getArtifactCoords();
          String options = artifactName.getOptions();
          boolean jandex = false;
          if (options != null) {
            jandex =
                options.contains(
                    "jandex"); // todo: eventually we may need options to have a proper query string
                               // type syntax
            moduleXmlContents =
                moduleXmlContents.replace(
                    artifactName.toString(),
                    artifactCoords); // todo: all these replace calls are a bit yuck, we may need
                                     // proper solution if this gets more complex
          }
          Artifact artifact = featurePack.getArtifactResolver().getArtifact(artifactCoords);
          if (artifact == null) {
            throw new RuntimeException(
                "Could not resolve module resource artifact "
                    + artifactName
                    + " for feature pack "
                    + featurePack.getFeaturePackFile());
          }
          try {
            // process the module artifact
            File artifactFile = artifactFileResolver.getArtifactFile(artifact);
            // extract schemas if needed
            extractSchema(schemaOutputDirectory, artifact, artifactFile);
            if (jandex) {
              String baseName =
                  artifactFile.getName().substring(0, artifactFile.getName().lastIndexOf("."));
              String extension =
                  artifactFile.getName().substring(artifactFile.getName().lastIndexOf("."));
              File target = new File(targetFile.getParent(), baseName + "-jandex" + extension);
              JandexIndexer.createIndex(artifactFile, new FileOutputStream(target));
              moduleXmlContents =
                  moduleXmlContents.replaceAll(
                      "(\\s*)<artifact\\s+name=\"\\$\\{" + artifactCoords + "\\}\"\\s*/>",
                      "$1<artifact name=\"\\${"
                          + artifactCoords
                          + "}\" />$1<resource-root path=\""
                          + target.getName()
                          + "\"/>");
              // it's also possible that this is an element with nested content
              // this regex involves a good deal of backtracking but it seems to work
              moduleXmlContents =
                  Pattern.compile(
                          "(\\s*)<artifact\\s+name=\"\\$\\{"
                              + artifactCoords
                              + "\\}\"\\s*>(.*)</artifact>",
                          Pattern.DOTALL)
                      .matcher(moduleXmlContents)
                      .replaceAll(
                          "$1<artifact name=\"\\${"
                              + artifactCoords
                              + "}\">$2</artifact>$1<resource-root path=\""
                              + target.getName()
                              + "\">$2</resource-root>");
            }
            if (!thinServer) {
              // copy the artifact
              String artifactFileName = artifactFile.getName();
              FileUtils.copyFile(artifactFile, new File(targetFile.getParent(), artifactFileName));
              // update module xml content
              moduleXmlContents =
                  moduleXmlContents.replaceAll(
                      "<artifact\\s+name=\"\\$\\{" + artifactCoords + "\\}\"\\s*/>",
                      "<resource-root path=\"" + artifactFileName + "\"/>");
              // it's also possible that this is an element with nested content
              // this regex involves a good deal of backtracking but it seems to work
              moduleXmlContents =
                  Pattern.compile(
                          "<artifact\\s+name=\"\\$\\{"
                              + artifactCoords
                              + "\\}\"\\s*>(.*)</artifact>",
                          Pattern.DOTALL)
                      .matcher(moduleXmlContents)
                      .replaceAll(
                          "<resource-root path=\"" + artifactFileName + "\">$1</resource-root>");
            }
          } catch (Throwable t) {
            throw new RuntimeException("Could not extract resources from " + artifactName, t);
          }
        }
        if (thinServer) {
          // replace artifact coords properties with the ones expected by jboss-modules
          moduleXmlContents = buildPropertyReplacer.replaceProperties(moduleXmlContents);
        }
        // write updated module xml content
        FileUtils.copyFile(
            new ByteArrayInputStream(moduleXmlContents.getBytes("UTF-8")), targetFile);

        // extract all other files in the module dir
        for (String moduleDirFile : module.getModuleDirFiles()) {
          filesProcessed.add(moduleDirFile);
          FileUtils.extractFile(jar, moduleDirFile, new File(outputDirectory, moduleDirFile));
        }
      }
    } catch (Throwable e) {
      throw new RuntimeException(
          "Failed to process feature pack " + featurePack.getFeaturePackFile() + " modules", e);
    }
  }
 private void processModules(
     ServerProvisioning serverProvisioning,
     File outputDirectory,
     Set<String> filesProcessed,
     ArtifactFileResolver artifactFileResolver,
     File schemaOutputDirectory)
     throws IOException, XMLStreamException {
   // 1. gather the modules for each feature pack
   final Map<FeaturePack, List<FeaturePack.Module>> featurePackModulesMap = new HashMap<>();
   Set<ModuleIdentifier> moduleIdentifiers = new HashSet<>();
   for (ServerProvisioningFeaturePack provisioningFeaturePack :
       serverProvisioning.getFeaturePacks()) {
     getLog()
         .debugf(
             "Gathering modules for provisioning feature pack %s",
             provisioningFeaturePack.getFeaturePack().getFeaturePackFile());
     for (FeaturePack.Module module :
         provisioningFeaturePack
             .getModules(
                 artifactFileResolver, serverProvisioning.getDescription().isExcludeDependencies())
             .values()) {
       final ModuleIdentifier moduleIdentifier = module.getIdentifier();
       if (moduleIdentifiers.add(moduleIdentifier)) {
         getLog()
             .debugf(
                 "Adding module %s from feature pack %s",
                 moduleIdentifier, module.getFeaturePack().getFeaturePackFile());
         List<FeaturePack.Module> featurePackModules =
             featurePackModulesMap.get(module.getFeaturePack());
         if (featurePackModules == null) {
           featurePackModules = new ArrayList<>();
           featurePackModulesMap.put(module.getFeaturePack(), featurePackModules);
         }
         featurePackModules.add(module);
       } else {
         getLog()
             .debugf(
                 "Skipping %s from feature pack %s. A module with such identifier is already in the provisioning module set.",
                 moduleIdentifier, module.getFeaturePack().getFeaturePackFile());
       }
     }
     // we always need to resolve all subsystem templates, regardless of the value of
     // exclude-dependencies
     for (FeaturePack.Module module :
         provisioningFeaturePack.getModules(artifactFileResolver, false).values()) {
       for (ModuleParseResult.ArtifactName artifactName :
           module.getModuleParseResult().getArtifacts()) {
         String artifactCoords = artifactName.getArtifactCoords();
         String options = artifactName.getOptions();
         Artifact artifact =
             module.getFeaturePack().getArtifactResolver().getArtifact(artifactCoords);
         if (artifact == null) {
           throw new RuntimeException(
               "Could not resolve module resource artifact "
                   + artifactName
                   + " for feature pack "
                   + module.getFeaturePack().getFeaturePackFile());
         }
         File artifactFile = artifactFileResolver.getArtifactFile(artifact);
         // add all subsystem templates
         serverProvisioning
             .getConfig()
             .getInputStreamSources()
             .addAllSubsystemFileSourcesFromZipFile(artifactFile);
       }
     }
   }
   // 2. provision each feature pack modules
   for (Map.Entry<FeaturePack, List<FeaturePack.Module>> mapEntry :
       featurePackModulesMap.entrySet()) {
     FeaturePack featurePack = mapEntry.getKey();
     List<FeaturePack.Module> includedModules = mapEntry.getValue();
     processFeaturePackModules(
         featurePack,
         includedModules,
         serverProvisioning,
         outputDirectory,
         filesProcessed,
         artifactFileResolver,
         schemaOutputDirectory);
   }
 }