下面列出了怎么用org.springframework.boot.loader.archive.Archive的API类实例代码及写法,或者点击链接到github查看源代码。
private void processIncludes(List<Archive> archives, List<Action> actions) {
if (CollectionUtils.isEmpty(properties.getIncludes())) {
return;
}
List<Dependency> includes = properties.getIncludes();
includes.forEach(include -> {
boolean replaced = replaceDependency(include, true, archives, actions);
if (!replaced) {
return;
}
// process dependencies
Archive includeArchive = findArchive(frameworkArchives, include);
List<Dependency> deps = findDependencyInfo(include, includeArchive);
deps.forEach(dep -> replaceDependency(dep, true, archives, actions));
});
}
private void processSubstitutions(List<Archive> archives, List<Action> actions) {
if (CollectionUtils.isEmpty(properties.getSubstitutions())) {
return;
}
List<Dependency> substitutions = properties.getSubstitutions();
substitutions.forEach(substitution -> {
boolean replaced = replaceDependency(substitution, false, archives, actions);
if (!replaced) {
return;
}
// process dependencies
Archive origin = findArchive(frameworkArchives, substitution);
List<Dependency> deps = findDependencyInfo(substitution, origin);
deps.forEach(dep -> replaceDependency(dep, true, archives, actions));
});
}
private List<Dependency> findDependencyInfo(Dependency dependency, Archive archive) {
Predicate<LauncherProperties.DependencyInfo> predicate = dependencyInfo ->
dependency.getArtifactId().equals(dependencyInfo.getArtifact().getArtifactId());
if (dependency.getVersion() == null) {
predicate.and(info -> {
String url = archive.toString();
return url.contains("/" + info.getArtifact().getArtifactId() + "-" + info.getArtifact().getVersion());
});
} else {
predicate.and(info -> dependency.getVersion().equals(info.getArtifact().getVersion()));
}
return properties.getDependencyInfos().stream()
.filter(predicate)
.map(LauncherProperties.DependencyInfo::getDependencies).findFirst().orElseGet(() -> {
logger.warn("dependencies info not found, return empty collection, dependency={}, archive={}",
dependency, archive);
return new ArrayList<>();
});
}
private void setSystemProperties(List<Action> actions, List<Archive> archives) {
// actions
for (int i = 0; i < actions.size(); i++) {
Action action = actions.get(i);
System.setProperty("formula.launcher.actions[" + i + "].type", action.getType().name());
System.setProperty("formula.launcher.actions[" + i + "].archive", action.getArchive());
if (action.getOldArchive() != null) {
System.setProperty("formula.launcher.actions[" + i + "].old-archive", action.getOldArchive());
}
}
// archives
for (int i = 0; i < archives.size(); i++) {
Archive archive = archives.get(i);
System.setProperty("formula.launcher.archives[" + i + "]", archive.toString());
}
}
protected final Archive createArchive() throws Exception {
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
String path = (location == null ? null : location.getSchemeSpecificPart());
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
File root = new File(path);
if (!root.exists()) {
throw new IllegalStateException(
"Unable to determine code source archive from " + root);
}
return (root.isDirectory() ? new ExplodedArchive(root)
: new JarFileArchive(root));
}
protected static Archive createArchive(Class clazz) throws Exception {
ProtectionDomain protectionDomain = clazz.getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = codeSource != null ? codeSource.getLocation().toURI() : null;
String path = location != null ? location.getSchemeSpecificPart() : null;
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
} else {
File root = new File(path);
if (!root.exists()) {
throw new IllegalStateException("Unable to determine code source archive from " + root);
} else {
return (Archive)(root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
}
}
}
/**
* Return metadata about configuration properties that are documented via <a href=
* "https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html">
* Spring Boot configuration metadata</a> and visible in an app.
*
* @param app a Spring Cloud Stream app; typically a Boot uberjar, but directories are
* supported as well
*/
@Override
public List<ConfigurationMetadataProperty> listProperties(Resource app, boolean exhaustive) {
try {
if (app != null) {
if (isDockerSchema(app.getURI())) {
return resolvePropertiesFromContainerImage(app.getURI());
}
else {
Archive archive = resolveAsArchive(app);
return listProperties(archive, exhaustive);
}
}
}
catch (Exception e) {
logger.warn("Failed to retrieve properties for resource:" + app, e);
return Collections.emptyList();
}
return Collections.emptyList();
}
protected String getClasspath(boolean includeTargetClasses) {
if (this.classpath == null) {
PathResolver resolver = new PathResolver(DependencyResolver.instance());
Archive root = ArchiveUtils.getArchive(ProcessLauncherState.class);
List<Archive> resolved = resolver.resolve(root, name, profiles);
StringBuilder builder = new StringBuilder();
if (includeTargetClasses) {
builder.append(new File("target/classes").getAbsolutePath());
}
else {
builder.append(new File("target/orm-0.0.1.BUILD-SNAPSHOT.jar")
.getAbsolutePath());
}
try {
for (Archive archive : resolved) {
if (archive.getUrl().equals(root.getUrl())) {
continue;
}
if (builder.length() > 0) {
builder.append(File.pathSeparator);
}
builder.append(file(archive.getUrl().toString()));
}
}
catch (MalformedURLException e) {
throw new IllegalStateException("Cannot find archive", e);
}
log.debug("Classpath: " + builder);
this.classpath = builder.toString();
}
return this.classpath;
}
private boolean replaceDependency(Dependency dependency, boolean forceAdd,
List<Archive> archives, List<Action> actions) {
Archive archive = findArchive(frameworkArchives, dependency);
if (archive == null) {
logger.warn("dependency not found, {}", dependency);
return false;
}
Archive currentArchive = findArchive(archives, dependency);
if (same(archive, currentArchive) && !dependency.getGroupId().contains("baidu")) {
return false;
}
if (currentArchive == null && !forceAdd) {
return false;
}
logger.info("adding launcher archive: {}", archive);
archives.add(archive);
actions.add(Action.builder().type(ActionType.ADD).archive(archive.toString()).build());
if (currentArchive != null) {
logger.info("removing launcher archive: {}", currentArchive);
archives.remove(currentArchive);
}
return true;
}
private boolean same(Archive includeArchive, Archive currentArchive) {
if (includeArchive == null || currentArchive == null) {
return includeArchive == currentArchive;
}
String left = includeArchive.toString().replaceFirst("^.*" + FormulaLauncher.BOOT_INF_LIB, "");
String right = currentArchive.toString().replaceFirst("^.*" + FormulaLauncher.BOOT_INF_LIB, "");
return left.equals(right);
}
Archive findArchive(List<Archive> archives, Dependency dependency) {
String entryName = "META-INF/maven/" + dependency.getGroupId() + "/" + dependency.getArtifactId() + "/pom.xml";
for (Archive archive : archives) {
if (StreamSupport.stream(archive.spliterator(), false).anyMatch(entry -> entry.getName().equals(entryName))) {
return archive;
}
}
return null;
}
@Override
public boolean isNestedArchive(Archive.Entry entry) {
if (entry.isDirectory()) {
return entry.getName().equals(BOOT_INF_CLASSES);
}
return entry.getName().startsWith(BOOT_INF_LIB);
}
/**
*
* @param jarFile
* @param jarEntry
* @return
* @throws IOException
*/
private static Archive getUnpackedNestedArchive(JarFile jarFile, JarEntry jarEntry) throws IOException {
String name = jarEntry.getName();
if (name.lastIndexOf("/") != -1) {
name = name.substring(name.lastIndexOf("/") + 1);
}
File file = new File(getTempUnpackFolder(), name);
if (!file.exists() || file.length() != jarEntry.getSize()) {
unpack(jarFile, jarEntry, file);
}
return new JarFileArchive(file, file.toURI().toURL());
}
protected boolean isNestedArchive(Archive.Entry entry) {
System.out.println(entry.getName());
if (entry.isDirectory()) {
return entry.getName().equals(BOOT_INF_CLASSES);
}
return entry.getName().startsWith(BOOT_INF_LIB);
}
private Archive getUnpackedNestedArchive(JarEntry jarEntry) throws IOException {
String name = jarEntry.getName();
if (name.lastIndexOf("/") != -1) {
name = name.substring(name.lastIndexOf("/") + 1);
}
File file = new File(getTempUnpackFolder(), name);
if (!file.exists() || file.length() != jarEntry.getSize()) {
unpack(jarEntry, file);
}
return new JarFileArchive(file, file.toURI().toURL());
}
/**
* Return metadata about configuration properties that are documented via
* <a href="http://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html">
* Spring Boot configuration metadata</a> and visible in an app.
* @param app a Spring Cloud Stream app; typically a Boot uberjar,
* but directories are supported as well
*/
public List<ConfigurationMetadataProperty> listProperties(Resource app, boolean exhaustive) {
try {
Archive archive = resolveAsArchive(app);
return listProperties(archive, exhaustive);
}
catch (IOException e) {
throw new RuntimeException("Failed to list properties for " + app, e);
}
}
public URLClassLoader createClassLoader() {
boolean useBoot14Layout = false;
for (Archive.Entry entry : archive) {
if (entry.getName().startsWith(BOOT_14_LIBS_LOCATION)) {
useBoot14Layout = true;
break;
}
}
ClassLoaderExposingLauncher launcher = useBoot14Layout
? new Boot14ClassLoaderExposingLauncher()
: new Boot13ClassLoaderExposingLauncher();
return launcher.createClassLoader();
}
@Override
public List<Archive> getNestedArchives(EntryFilter ignored) throws IOException {
try {
List<Archive> archives = new ArrayList<>(mavenProject.getRuntimeClasspathElements().size());
for (String dep : mavenProject.getRuntimeClasspathElements()) {
File file = new File(dep);
archives.add(file.isDirectory() ? new ExplodedArchive(file) : new JarFileArchive(file));
}
return archives;
}
catch (DependencyResolutionRequiredException e) {
throw new IOException("Could not create boot archive", e);
}
}
private List<Archive> discoverClassPathAcrhives() throws Exception {
Iterator<Archive> iter = this.getClassPathArchivesIterator();
List<Archive> classPathArchives = new ArrayList<>();
while (iter.hasNext()) {
classPathArchives.add(iter.next());
}
if (CollectionUtils.isEmpty(classPathArchives)) {
classPathArchives.add(this.getArchive());
}
return classPathArchives;
}
public URLClassLoader createClassLoader() {
boolean useBoot14Layout = false;
for (Archive.Entry entry : archive) {
if (entry.getName().startsWith(BOOT_14_LIBS_LOCATION)) {
useBoot14Layout = true;
break;
}
}
ClassLoaderExposingLauncher launcher = useBoot14Layout ? new Boot14ClassLoaderExposingLauncher()
: new Boot13ClassLoaderExposingLauncher();
return launcher.createClassLoader();
}
public List<Action> process(List<Archive> archives) {
List<Action> actions = new ArrayList<>();
boolean springBoot15 = archives.stream().anyMatch(ar -> ar.toString().contains("spring-boot-1."));
if (springBoot15) {
logger.info("spring boot 1.5 detected, skip for jar replacing, class path urls: {}", archives);
return new ArrayList<>();
}
processIncludes(archives, actions);
processExclusions(archives, actions);
processSubstitutions(archives, actions);
return actions;
}
@Override
protected void postProcessClassPathArchives(List<Archive> archives) {
List<Action> actions = dependencyProcessor.process(archives);
setSystemProperties(actions, archives);
}
protected static boolean isNestedArchive(Archive.Entry entry) {
return entry.isDirectory() ? entry.getName().equals("BOOT-INF/classes/") : entry.getName().startsWith("BOOT-INF/lib/");
}
@Override
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
URLClassLoader classLoader = (URLClassLoader) super.createClassLoader(archives);
URL[] urls = classLoader.getURLs();
return new XBootClassLoader(urls, this.getClass().getClassLoader(), xLauncher.xDecryptor, xLauncher.xEncryptor, xLauncher.xKey);
}
IntegrationTestJarLauncher(Archive archive) {
super(archive);
}
private Archive resolveAsArchive(Resource app) throws IOException {
File moduleFile = app.getFile();
return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
}
@Override
protected boolean isNestedArchive(Archive.Entry entry) {
return !entry.isDirectory() && entry.getName().startsWith(BOOT_13_LIBS_LOCATION);
}
@Override
protected void postProcessClassPathArchives(List<Archive> archives) throws Exception {
archives.add(0, getArchive());
}
@Override
protected boolean isNestedArchive(Archive.Entry entry) {
return (!entry.isDirectory() && entry.getName().startsWith(BOOT_14_LIBS_LOCATION))
|| (entry.isDirectory() && entry.getName().equals(BOOT_14_CLASSESS_LOCATION));
}
@Override
protected void postProcessClassPathArchives(List<Archive> archives) throws Exception {
archives.add(0, getArchive());
}