private void processProvisioningFeaturePackContents(
     ServerProvisioningFeaturePack provisioningFeaturePack,
     File outputDirectory,
     Set<String> filesProcessed,
     boolean excludeDependencies)
     throws IOException {
   if (provisioningFeaturePack.getDescription().includesContentFiles()) {
     processFeaturePackContents(
         provisioningFeaturePack.getFeaturePack(),
         provisioningFeaturePack.getDescription().getContentFilters(),
         outputDirectory,
         filesProcessed,
         excludeDependencies);
   }
 }
 private void processFeaturePackConfig(
     ServerProvisioningFeaturePack provisioningFeaturePack,
     ServerProvisioning.Config provisioningConfig)
     throws IOException, XMLStreamException {
   FeaturePack featurePack = provisioningFeaturePack.getFeaturePack();
   getLog()
       .debug(
           "Processing provisioning feature pack "
               + featurePack.getFeaturePackFile()
               + " configs");
   try (ZipFile zipFile = new ZipFile(featurePack.getFeaturePackFile())) {
     for (ServerProvisioningFeaturePack.ConfigFile serverProvisioningFeaturePackConfigFile :
         provisioningFeaturePack.getDomainConfigFiles()) {
       processFeaturePackConfigFile(
           serverProvisioningFeaturePackConfigFile,
           zipFile,
           provisioningFeaturePack,
           provisioningConfig.getDomainConfigFiles());
     }
     for (ServerProvisioningFeaturePack.ConfigFile serverProvisioningFeaturePackConfigFile :
         provisioningFeaturePack.getStandaloneConfigFiles()) {
       processFeaturePackConfigFile(
           serverProvisioningFeaturePackConfigFile,
           zipFile,
           provisioningFeaturePack,
           provisioningConfig.getStandaloneConfigFiles());
     }
     for (ServerProvisioningFeaturePack.ConfigFile serverProvisioningFeaturePackConfigFile :
         provisioningFeaturePack.getHostConfigFiles()) {
       processFeaturePackConfigFile(
           serverProvisioningFeaturePackConfigFile,
           zipFile,
           provisioningFeaturePack,
           provisioningConfig.getHostConfigFiles());
     }
   }
 }
  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);
  }
  public void build() {
    final ServerProvisioning serverProvisioning = new ServerProvisioning(description);
    final List<String> errors = new ArrayList<>();
    try {
      // create the feature packs
      for (ServerProvisioningDescription.FeaturePack serverProvisioningFeaturePackDescription :
          description.getFeaturePacks()) {
        final FeaturePack featurePack =
            FeaturePackFactory.createPack(
                serverProvisioningFeaturePackDescription.getArtifact(),
                artifactFileResolver,
                versionOverrideArtifactResolver);
        serverProvisioning
            .getFeaturePacks()
            .add(
                new ServerProvisioningFeaturePack(
                    serverProvisioningFeaturePackDescription, featurePack, artifactFileResolver));
      }
      // create output dir
      FileUtils.deleteRecursive(outputDirectory);
      outputDirectory.mkdirs();
      // create schema output dir if needed
      final File schemaOutputDirectory;
      if (description.isExtractSchemas()) {
        schemaOutputDirectory = new File(outputDirectory, SUBSYSTEM_SCHEMA_TARGET_DIRECTORY);
        if (!schemaOutputDirectory.exists()) {
          schemaOutputDirectory.mkdirs();
        }
      } else {
        schemaOutputDirectory = null;
      }
      final Set<String> filesProcessed = new HashSet<>();
      // process server provisioning copy-artifacts
      processCopyArtifacts(
          serverProvisioning.getDescription().getCopyArtifacts(),
          versionOverrideArtifactResolver,
          outputDirectory,
          filesProcessed,
          artifactFileResolver,
          schemaOutputDirectory);
      // process modules (needs to be done for all feature packs before any config is processed, due
      // to subsystem template gathering)
      processModules(
          serverProvisioning,
          outputDirectory,
          filesProcessed,
          artifactFileResolver,
          schemaOutputDirectory);

      // process everything else for each feature pack
      for (ServerProvisioningFeaturePack provisioningFeaturePack :
          serverProvisioning.getFeaturePacks()) {
        processSubsystemConfigInFeaturePack(
            provisioningFeaturePack, serverProvisioning, artifactFileResolver);
        processFeaturePackCopyArtifacts(
            provisioningFeaturePack.getFeaturePack(),
            outputDirectory,
            filesProcessed,
            artifactFileResolver,
            schemaOutputDirectory,
            description.isExcludeDependencies());
        processProvisioningFeaturePackContents(
            provisioningFeaturePack,
            outputDirectory,
            filesProcessed,
            description.isExcludeDependencies());
        processFeaturePackFilePermissions(
            provisioningFeaturePack.getFeaturePack(),
            outputDirectory,
            description.isExcludeDependencies());
      }
      // process the server config
      processConfig(serverProvisioning, outputDirectory, filesProcessed);
    } catch (Throwable e) {
      throw new RuntimeException(e);
    } finally {
      if (!errors.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("Some errors were encountered creating the feature pack\n");
        for (String error : errors) {
          sb.append(error);
          sb.append("\n");
        }
        throw new RuntimeException(sb.toString());
      }
    }
  }
 private void processFeaturePackConfigFile(
     ServerProvisioningFeaturePack.ConfigFile serverProvisioningFeaturePackConfigFile,
     ZipFile zipFile,
     ServerProvisioningFeaturePack provisioningFeaturePack,
     Map<String, ServerProvisioning.ConfigFile> provisioningConfigFiles)
     throws IOException, XMLStreamException {
   ConfigFile configFile = serverProvisioningFeaturePackConfigFile.getFeaturePackConfigFile();
   // get provisioning config file for the output file being processed
   ServerProvisioning.ConfigFile provisioningConfigFile =
       provisioningConfigFiles.get(configFile.getOutputFile());
   if (provisioningConfigFile == null) {
     // the provisioning config file does not exists yet, create one
     provisioningConfigFile = new ServerProvisioning.ConfigFile(configFile.getOutputFile());
     provisioningConfigFiles.put(configFile.getOutputFile(), provisioningConfigFile);
   }
   ConfigFileOverride configFileOverride =
       serverProvisioningFeaturePackConfigFile.getConfigFileOverride();
   // process template
   if (configFileOverride == null || configFileOverride.isUseTemplate()) {
     // template file from this config file to be used
     // get the template's file zip entry
     ZipEntry templateFileZipEntry = zipFile.getEntry(configFile.getTemplate());
     if (templateFileZipEntry == null) {
       throw new RuntimeException(
           "Feature pack "
               + provisioningFeaturePack.getFeaturePack().getFeaturePackFile()
               + " template file "
               + configFile.getTemplate()
               + " not found");
     }
     // set the input stream source
     provisioningConfigFile.setTemplateInputStreamSource(
         new ZipEntryInputStreamSource(
             provisioningFeaturePack.getFeaturePack().getFeaturePackFile(), templateFileZipEntry));
   }
   // get this config file subsystems
   Map<String, Map<String, SubsystemConfig>> subsystems =
       serverProvisioningFeaturePackConfigFile.getSubsystems();
   // merge the subsystems in the provisioning config file
   for (Map.Entry<String, Map<String, SubsystemConfig>> subsystemsEntry : subsystems.entrySet()) {
     // get the subsystems in the provisioning config file
     String profileName = subsystemsEntry.getKey();
     Map<String, SubsystemConfig> subsystemConfigMap = subsystemsEntry.getValue();
     Map<String, SubsystemConfig> provisioningSubsystems =
         provisioningConfigFile.getSubsystems().get(profileName);
     if (provisioningSubsystems == null) {
       // do not exist yet, create it
       provisioningSubsystems = new LinkedHashMap<>();
       provisioningConfigFile.getSubsystems().put(profileName, provisioningSubsystems);
     }
     // add the 'new' subsystem configs and related input stream sources
     for (Map.Entry<String, SubsystemConfig> subsystemConfigMapEntry :
         subsystemConfigMap.entrySet()) {
       String subsystemFile = subsystemConfigMapEntry.getKey();
       SubsystemConfig subsystemConfig = subsystemConfigMapEntry.getValue();
       getLog()
           .debugf(
               "Adding subsystem config %s to provisioning config file %s",
               subsystemFile, provisioningConfigFile.getOutputFile());
       // put subsystem config
       provisioningSubsystems.put(subsystemFile, subsystemConfig);
     }
   }
 }
 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);
   }
 }