下面列出了怎么用org.springframework.boot.loader.archive.JarFileArchive的API类实例代码及写法,或者点击链接到github查看源代码。
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));
}
}
}
public DependencyProcessor(LauncherProperties properties, DependencyResolver dependencyResolver) throws Exception {
this.properties = properties;
this.dependencyResolver = dependencyResolver;
JarFileArchive frameworkArchive = new JarFileArchive(new File(properties.getFrameworkPath()));
frameworkArchives = frameworkArchive
.getNestedArchives(entry -> entry.getName().startsWith(FormulaLauncher.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());
}
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());
}
@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);
}
}
public FormulaLauncher(LauncherProperties properties, DependencyProcessor dependencyProcessor) throws Exception {
super(new JarFileArchive(new File(properties.getApplicationPath())));
this.dependencyProcessor = dependencyProcessor;
}
@SneakyThrows
public ConfigurableApplicationContext run() {
System.setProperty("plugins.dir", findPluginsDir().getAbsolutePath());
System.setProperty("plugins.pathMatcher", "*/build/libs/*.jar");
var tempFile = Files.createTempFile("app", ".jar");
tempFile.toFile().deleteOnExit();
try (var appJarStream = getClass().getClassLoader().getResourceAsStream("app-boot.jar")) {
Files.copy(appJarStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
}
var launcher = new JarLauncher(new JarFileArchive(tempFile.toFile(), tempFile.toUri().toURL())) {
ClassLoader createClassLoader() throws Exception {
return super.createClassLoader(getClassPathArchives());
}
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
var systemClassLoader = ClassLoader.getSystemClassLoader();
return new LaunchedURLClassLoader(urls, systemClassLoader.getParent()) {
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
var classFile = findResource(name.replace(".", "/") + ".class");
if (classFile != null) {
// If exists in the app.jar, load it from the system classloader instead
log.debug("Loading class '{}' from the system ClassLoader instead", name);
return systemClassLoader.loadClass(name);
}
return super.loadClass(name, resolve);
}
};
}
};
var currentClassLoader = Thread.currentThread().getContextClassLoader();
try {
var appClassLoader = launcher.createClassLoader();
Thread.currentThread().setContextClassLoader(appClassLoader);
var applicationClass = appClassLoader.loadClass("com.github.bsideup.liiklus.Application");
var createSpringApplicationMethod = applicationClass.getDeclaredMethod("createSpringApplication", String[].class);
var application = (SpringApplication) createSpringApplicationMethod.invoke(null, (Object) new String[0]);
application.setDefaultProperties(properties);
return application.run();
} finally {
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
}
private static ClassLoader getJarClassLoader() throws Exception {
File jar = new File("../karate-core/src/test/resources/karate-bootjar-test.jar");
assertTrue(jar.exists());
return new IntegrationTestJarLauncher(new JarFileArchive(jar)).createClassLoader();
}
private Archive resolveAsArchive(Resource app) throws IOException {
File moduleFile = app.getFile();
return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
}
private Archive resolveAsArchive(Resource app) throws IOException {
File moduleFile = app.getFile();
return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
}
private static Archive resolveAsArchive(Resource app) throws IOException {
Assert.notNull(app, "The resource specified for the app must not be null");
File moduleFile = app.getFile();
return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
}