com.jn.langx.classpath.cp.JarFileClasspath Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of langx-java Show documentation
Show all versions of langx-java Show documentation
Java lang extensions for java6+, a supplement to , replacement of a Guava, commons-lang.
Core utilities, Collection utilities, IO utilities, Cache, Configuration library ...
package com.jn.langx.classpath.cp;
import com.jn.langx.classpath.Classpaths;
import com.jn.langx.io.resource.*;
import com.jn.langx.util.Emptys;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.collection.Pipeline;
import com.jn.langx.util.function.Function;
import com.jn.langx.util.function.Supplier;
import com.jn.langx.util.io.IOs;
import com.jn.langx.util.io.file.Filenames;
import com.jn.langx.util.net.URLs;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarFileClasspath extends AbstractClasspath {
/**
* key: file suffix
* values: file path relative the jar
*/
private Map> fileEntries = Collects.emptyNonAbsentHashMap(new Supplier>() {
@Override
public Set get(String key) {
return Collects.emptyHashSet();
}
});
private String jarfileURL;
private Location root;
private String getSuffix(String path) {
String suffix = Filenames.getSuffix(path);
return Emptys.isNotEmpty(suffix) ? suffix : "__langx_other__";
}
public JarFileClasspath(String jarPath) {
this(new File(jarPath));
}
public JarFileClasspath(File file) {
if (file.exists() && file.isFile() && file.canRead()) {
JarFile jarfile = null;
try {
jarfile = new JarFile(file);
for (JarEntry entry : Collections.list(jarfile.entries())) {
if (!entry.isDirectory()) {
String suffix = getSuffix(entry.getName());
fileEntries.get(suffix).add(entry.getName());
}
}
this.jarfileURL = file.getCanonicalFile().toURI().toURL().toString();
} catch (IOException e) {
throw new ResourceNotFoundException(file.getName());
} finally {
IOs.close(jarfile);
}
}
root = new Location(URLs.URL_PREFIX_JAR, jarfileURL + URLs.JAR_URL_SEPARATOR);
}
@Override
public Resource findResource(String relativePath) {
relativePath = Classpaths.getCanonicalFilePath(relativePath);
String suffix = getSuffix(relativePath);
if (relativePath.startsWith("/")) {
relativePath = relativePath.substring(1);
}
if (this.fileEntries.get(suffix).contains(relativePath)) {
String url = getUrl(relativePath);
return Resources.loadUrlResource(url);
}
return null;
}
@Override
public Location getRoot() {
return root;
}
@Override
public Set allResources() {
return Pipeline.of(fileEntries).flatMap(new Function() {
@Override
public Location apply(String relativePath) {
relativePath = Classpaths.getCanonicalFilePath(relativePath);
return Locations.newLocation(root, relativePath);
}
}).asSet(false);
}
private String getUrl(String relativePath) {
relativePath = Classpaths.getCanonicalFilePath(relativePath);
return this.root.getLocation() + relativePath;
}
}