org.infinispan.commons.util.uberjar.ManifestUberJarDuplicatedJarsWarner Maven / Gradle / Ivy
package org.infinispan.commons.util.uberjar;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.jar.Manifest;
import org.infinispan.commons.logging.Log;
import org.infinispan.commons.logging.LogFactory;
/**
* Manifest based implementation of a {@link UberJarDuplicatedJarsWarner}.
*
* Incorrect combinations:
*
* - Commons + any of the Uber Jars
* - Embedded + Remote Uber Jar
* - Commons + Embedded + Remote Uber Jar
*
*
*
* @author slaskawi
*/
public class ManifestUberJarDuplicatedJarsWarner implements UberJarDuplicatedJarsWarner {
private static final Log logger = LogFactory.getLog(MethodHandles.lookup().lookupClass());
private static final String MANIFEST_LOCATION = "META-INF/MANIFEST.MF";
private final String SYMBOLIC_NAME_MANIFEST_ENTRY = "Bundle-SymbolicName";
@Override
public boolean isClasspathCorrect() {
List bundleNames = getBundleSymbolicNames();
long numberOfMatches = bundleNames.stream()
.filter(hasRemoteUberJar()
.or(hasEmbeddedUberJar())
.or(hasCommons()))
.count();
return numberOfMatches < 2;
}
List getBundleSymbolicNames() {
List symbolicNames = new ArrayList<>();
try {
Enumeration resources = getClass().getClassLoader().getResources(MANIFEST_LOCATION);
while (resources.hasMoreElements()) {
URL manifestUrl = resources.nextElement();
try (InputStream is = manifestUrl.openStream()) {
Manifest manifest = new Manifest(is);
symbolicNames.add(manifest.getMainAttributes().getValue(SYMBOLIC_NAME_MANIFEST_ENTRY));
}
}
} catch (IOException e) {
logger.warn("Can not extract jar manifest from the classpath. Uber Jar classpath check is skipped.");
}
return symbolicNames;
}
Predicate hasCommons() {
return jarSymbolicName -> "org.infinispan.commons".equals(jarSymbolicName);
}
Predicate hasEmbeddedUberJar() {
return jarSymbolicName -> "org.infinispan.embedded".equals(jarSymbolicName);
}
Predicate hasRemoteUberJar() {
return jarSymbolicName -> "org.infinispan.remote".equals(jarSymbolicName);
}
@Override
public CompletableFuture isClasspathCorrectAsync() {
return CompletableFuture.supplyAsync(() -> isClasspathCorrect());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy