private Artifact parseArtifact(final XMLStreamReader reader) throws XMLStreamException {
   final int count = reader.getAttributeCount();
   String groupId = null;
   String artifactId = null;
   String version = null;
   String classifier = null;
   String extension = null;
   final Set<Attribute> required =
       EnumSet.of(Attribute.GROUP_ID, Attribute.ARTIFACT_ID, Attribute.VERSION);
   for (int i = 0; i < count; i++) {
     final Attribute attribute = Attribute.of(reader.getAttributeName(i));
     required.remove(attribute);
     switch (attribute) {
       case GROUP_ID:
         groupId = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case ARTIFACT_ID:
         artifactId = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case VERSION:
         version = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case CLASSIFIER:
         classifier = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case EXTENSION:
         extension = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       default:
         throw ParsingUtils.unexpectedContent(reader);
     }
   }
   if (!required.isEmpty()) {
     throw ParsingUtils.missingAttributes(reader.getLocation(), required);
   }
   ParsingUtils.parseNoContent(reader);
   return new Artifact(groupId, artifactId, classifier, extension, version);
 }
 private String parseName(final XMLStreamReader reader) throws XMLStreamException {
   final int count = reader.getAttributeCount();
   String name = null;
   final Set<Attribute> required = EnumSet.of(Attribute.NAME);
   for (int i = 0; i < count; i++) {
     final Attribute attribute = Attribute.of(reader.getAttributeName(i));
     required.remove(attribute);
     switch (attribute) {
       case NAME:
         name = reader.getAttributeValue(i);
         break;
       default:
         throw ParsingUtils.unexpectedContent(reader);
     }
   }
   if (!required.isEmpty()) {
     throw ParsingUtils.missingAttributes(reader.getLocation(), required);
   }
   ParsingUtils.parseNoContent(reader);
   return propertyReplacer.replaceProperties(name);
 }
コード例 #3
0
  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);
    }
  }