use of java.util.jar.JarFile in project reflections by ronmamo.
the class ClasspathHelper method forManifest.
/**
* Returns a distinct collection of URLs from a single URL based on the Manifest information.
* <p>
* The {@code MANIFEST.MF} file can contain a {@code Class-Path} entry that defines additional
* jar files to be included on the classpath. This method takes a single URL, tries to
* resolve it as a jar file, and if so, adds any additional manifest classpaths.
* The returned collection of URLs will always contain the input URL.
*
* @return the collection of URLs, not null
*/
public static Collection<URL> forManifest(final URL url) {
final Collection<URL> result = new ArrayList<URL>();
result.add(url);
try {
final String part = cleanPath(url);
File jarFile = new File(part);
JarFile myJar = new JarFile(part);
URL validUrl = tryToGetValidUrl(jarFile.getPath(), new File(part).getParent(), part);
if (validUrl != null) {
result.add(validUrl);
}
final Manifest manifest = myJar.getManifest();
if (manifest != null) {
final String classPath = manifest.getMainAttributes().getValue(new Attributes.Name("Class-Path"));
if (classPath != null) {
for (String jar : classPath.split(" ")) {
validUrl = tryToGetValidUrl(jarFile.getPath(), new File(part).getParent(), jar);
if (validUrl != null) {
result.add(validUrl);
}
}
}
}
} catch (IOException e) {
// don't do anything, we're going on the assumption it is a jar, which could be wrong
}
return distinctUrls(result);
}
use of java.util.jar.JarFile in project robovm by robovm.
the class URLClassLoader method createURLJarHandler.
private URLHandler createURLJarHandler(URL url) {
String prefixName;
String file = url.getFile();
if (url.getFile().endsWith("!/")) {
prefixName = "";
} else {
int sepIdx = file.lastIndexOf("!/");
if (sepIdx == -1) {
// Invalid URL, don't look here again
return null;
}
sepIdx += 2;
prefixName = file.substring(sepIdx);
}
try {
URL jarURL = ((JarURLConnection) url.openConnection()).getJarFileURL();
JarURLConnection juc = (JarURLConnection) new URL("jar", "", jarURL.toExternalForm() + "!/").openConnection();
JarFile jf = juc.getJarFile();
URLJarHandler jarH = new URLJarHandler(url, jarURL, jf, prefixName);
if (jarH.getIndex() == null) {
try {
Manifest manifest = jf.getManifest();
if (manifest != null) {
String classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
if (classpath != null) {
searchList.addAll(0, getInternalURLs(url, classpath));
}
}
} catch (IOException e) {
}
}
return jarH;
} catch (IOException e) {
}
return null;
}
use of java.util.jar.JarFile in project robovm by robovm.
the class TrustedCertificateStore method list.
private String[] list(URI file) {
if ("file".equals(file.getScheme())) {
return new File(file).list();
}
if ("jar".equals(file.getScheme())) {
try {
JarURLConnection conn = (JarURLConnection) file.toURL().openConnection();
JarFile jarFile = conn.getJarFile();
String uriStr = file.toString();
if (!uriStr.endsWith("/")) {
uriStr += "/";
}
String path = uriStr.substring(uriStr.lastIndexOf('!') + 1);
if (path.startsWith("/")) {
path = path.substring(1);
}
Enumeration<JarEntry> en = jarFile.entries();
List<String> result = new ArrayList<String>();
while (en.hasMoreElements()) {
JarEntry entry = en.nextElement();
String name = entry.getName();
if (name.startsWith(path) && !name.endsWith("/")) {
int lastSlash = name.lastIndexOf('/');
if (lastSlash == path.length() - 1) {
result.add(name.substring(lastSlash + 1));
}
}
}
return result.toArray(new String[result.size()]);
} catch (IOException e) {
}
}
return null;
}
use of java.util.jar.JarFile in project spring-boot by spring-projects.
the class ExplodedArchiveTests method createArchive.
private void createArchive(String folderName) throws Exception {
File file = this.temporaryFolder.newFile();
TestJarCreator.createTestJar(file);
this.rootFolder = StringUtils.hasText(folderName) ? this.temporaryFolder.newFolder(folderName) : this.temporaryFolder.newFolder();
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
File destination = new File(this.rootFolder.getAbsolutePath() + File.separator + entry.getName());
destination.getParentFile().mkdirs();
if (entry.isDirectory()) {
destination.mkdir();
} else {
copy(jarFile.getInputStream(entry), new FileOutputStream(destination));
}
}
this.archive = new ExplodedArchive(this.rootFolder);
jarFile.close();
}
use of java.util.jar.JarFile in project spring-boot by spring-projects.
the class AbstractExecutableArchiveLauncherTests method explode.
protected File explode(File archive) throws IOException {
File exploded = this.temp.newFolder("exploded");
JarFile jarFile = new JarFile(archive);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
File entryFile = new File(exploded, entry.getName());
if (entry.isDirectory()) {
entryFile.mkdirs();
} else {
FileCopyUtils.copy(jarFile.getInputStream(entry), new FileOutputStream(entryFile));
}
}
jarFile.close();
return exploded;
}
Aggregations