All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.contrib.staticinstrumenter.plugin.maven.PackagingSupport Maven / Gradle / Ivy

There is a newer version: 1.26.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.staticinstrumenter.plugin.maven;

import static io.opentelemetry.contrib.staticinstrumenter.plugin.maven.JarSupport.consumeEntries;
import static io.opentelemetry.contrib.staticinstrumenter.plugin.maven.ZipEntryCreator.moveEntry;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipOutputStream;

class PackagingSupport {

  static final PackagingSupport EMPTY = new PackagingSupport("");

  private final String classesPrefix;
  private final Set filesToRepackage = new HashSet<>();

  PackagingSupport(String classesPrefix) {
    this.classesPrefix = classesPrefix;
  }

  String getClassesPrefix() {
    return classesPrefix;
  }

  void copyRemovingPrefix(JarFile inputJar, ZipOutputStream targetOut) throws IOException {

    consumeEntries(
        inputJar,
        (entry) -> {
          if (!entry.isDirectory() && entry.getName().startsWith(classesPrefix)) {
            String newEntryPath = entry.getName().replace(getClassesPrefix(), "");
            moveEntry(targetOut, newEntryPath, entry, inputJar);
            filesToRepackage.add(newEntryPath);
          } else {
            moveEntry(targetOut, entry.getName(), entry, inputJar);
          }
        });
  }

  void copyAddingPrefix(JarEntry entry, JarFile inputJar, ZipOutputStream targetOut)
      throws IOException {

    if (!entry.isDirectory() && filesToRepackage.contains(entry.getName())) {
      String newEntryPath = classesPrefix + entry.getName();
      moveEntry(targetOut, newEntryPath, entry, inputJar);
    } else {
      moveEntry(targetOut, entry.getName(), entry, inputJar);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy