use of java.net.JarURLConnection in project earth-snow by justlive1.
the class AbstractResourceLoader method findJarMatchPath.
/**
* 获取资源下匹配的jar中的文件
*
* @param resource
* @param rootUrl
* @param subPattern
* @return
* @throws IOException
*/
protected List<SourceResource> findJarMatchPath(SourceResource resource, URL rootUrl, String subPattern) throws IOException {
List<SourceResource> all = new LinkedList<>();
URLConnection con = rootUrl.openConnection();
JarFile jarFile;
String jarFileUrl;
String rootEntryPath;
if (con instanceof JarURLConnection) {
JarURLConnection jarCon = (JarURLConnection) con;
jarFile = jarCon.getJarFile();
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
JarEntry jarEntry = jarCon.getJarEntry();
rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
} else {
String urlFile = rootUrl.getFile();
int separatorLength = BaseConstants.WAR_URL_SEPARATOR.length();
int separatorIndex = urlFile.indexOf(BaseConstants.WAR_URL_SEPARATOR);
if (separatorIndex == -1) {
separatorIndex = urlFile.indexOf(BaseConstants.JAR_URL_SEPARATOR);
separatorLength = BaseConstants.JAR_URL_SEPARATOR.length();
}
if (separatorIndex != -1) {
jarFileUrl = urlFile.substring(0, separatorIndex);
rootEntryPath = urlFile.substring(separatorIndex + separatorLength);
jarFile = this.getJarFile(jarFileUrl);
} else {
jarFile = new JarFile(urlFile);
jarFileUrl = urlFile;
rootEntryPath = "";
}
}
try {
if (log.isDebugEnabled()) {
log.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
}
if (rootEntryPath.length() > 0 && !rootEntryPath.endsWith(BaseConstants.PATH_SEPARATOR)) {
rootEntryPath += BaseConstants.PATH_SEPARATOR;
}
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (entryPath.startsWith(rootEntryPath)) {
String relativePath = entryPath.substring(rootEntryPath.length());
if (matcher.match(subPattern, relativePath)) {
all.add(resource.createRelative(relativePath));
}
}
}
} finally {
jarFile.close();
}
return all;
}
use of java.net.JarURLConnection in project spring-cloud-framework by zhuwj921.
the class ClassUtil method getClasses.
/**
* 获取某个包下所有的class信息
*
* @param packageName
* 包名
* @return
*/
public static List<Class<?>> getClasses(String packageName) throws Exception {
String packageFileName = packageName.replace(POINT, File.separator);
URL fullPath = getDefaultClassLoader().getResource(packageFileName);
String protocol = fullPath.getProtocol();
if (protocol.equals(GlobalContanst.PROTOCOL_FILE)) {
try {
File dir = new File(fullPath.toURI());
return findClassesByFile(dir, packageName);
} catch (URISyntaxException e) {
throw new Exception(e);
}
} else if (protocol.equals(GlobalContanst.PROTOCOL_JAR)) {
try {
return findClassesByJar(((JarURLConnection) fullPath.openConnection()).getJarFile(), packageName);
} catch (IOException e) {
throw new Exception(e);
}
}
return null;
}
use of java.net.JarURLConnection in project vertx-zero by silentbalanceyh.
the class PackScanner method getClasses.
private static Set<Class<?>> getClasses(final String packageDir, final String packName, final URL url, final boolean recursive) {
final Set<Class<?>> classes = new LinkedHashSet<>();
Fn.getJvm(() -> {
String packageName = (packName.startsWith(Strings.DOT)) ? packName.substring(1, packName.length()) : packName;
// Get jar file
final JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
// List all entries of this jar
final Enumeration<JarEntry> entries = jar.entries();
// Loop for jar entry.
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
String name = entry.getName();
// Start with /
if (name.charAt(0) == '/') {
name = name.substring(1);
}
// The same with package dir
if (name.startsWith(packageDir)) {
final int idx = name.lastIndexOf('/');
// end with /, it's package.
if (idx != -1) {
packageName = name.substring(0, idx).replace('/', '.');
}
if ((idx != -1) || recursive) {
// .class and not directory
if (name.endsWith(Strings.DOT + FileSuffix.CLASS) && !entry.isDirectory()) {
// Extract class Name
final String className = name.substring(packageName.length() + 1, name.length() - 6);
try {
classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + Strings.DOT + className));
} catch (final Throwable ex) {
// LOGGER.info(ex.getMessage());
}
}
}
}
}
return null;
}, packageDir, packName, url);
return classes;
}
use of java.net.JarURLConnection in project HotswapAgent by HotswapProjects.
the class PathMatchingResourcePatternResolver method doFindPathMatchingJarResources.
/**
* Find all resources in jar files that match the given location pattern via
* the Ant-style PathMatcher.
*
* @param rootDirResource
* the root directory as Resource
* @param subPattern
* the sub pattern to match (below the root directory)
* @return a mutable Set of matching Resource instances
* @throws IOException
* in case of I/O errors
* @see java.net.JarURLConnection
* @see org.hotswap.agent.util.spring.path.springframework.util.PathMatcher
*/
protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern) throws IOException {
URLConnection con = rootDirResource.getURL().openConnection();
JarFile jarFile;
String jarFileUrl;
String rootEntryPath;
boolean newJarFile = false;
if (con instanceof JarURLConnection) {
// Should usually be the case for traditional JAR files.
JarURLConnection jarCon = (JarURLConnection) con;
ResourceUtils.useCachesIfNecessary(jarCon);
jarFile = jarCon.getJarFile();
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
JarEntry jarEntry = jarCon.getJarEntry();
rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
} else {
// No JarURLConnection -> need to resort to URL file parsing.
// We'll assume URLs of the format "jar:path!/entry", with the
// protocol
// being arbitrary as long as following the entry format.
// We'll also handle paths with and without leading "file:" prefix.
String urlFile = rootDirResource.getURL().getFile();
try {
int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
if (separatorIndex != -1) {
jarFileUrl = urlFile.substring(0, separatorIndex);
rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
jarFile = getJarFile(jarFileUrl);
} else {
jarFile = new JarFile(urlFile);
jarFileUrl = urlFile;
rootEntryPath = "";
}
newJarFile = true;
} catch (ZipException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Skipping invalid jar classpath entry [" + urlFile + "]");
}
return Collections.emptySet();
}
}
try {
if (logger.isDebugEnabled()) {
logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
}
if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
// Root entry path must end with slash to allow for proper
// matching.
// The Sun JRE does not return a slash here, but BEA JRockit
// does.
rootEntryPath = rootEntryPath + "/";
}
Set<Resource> result = new LinkedHashSet<Resource>(8);
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (entryPath.startsWith(rootEntryPath)) {
String relativePath = entryPath.substring(rootEntryPath.length());
if (getPathMatcher().match(subPattern, relativePath)) {
result.add(rootDirResource.createRelative(relativePath));
}
}
}
return result;
} finally {
// not from JarURLConnection, which might cache the file reference.
if (newJarFile) {
jarFile.close();
}
}
}
use of java.net.JarURLConnection in project jboss-modules by jboss-modules.
the class ResourceRootPathsTest method getFileFromJarUri.
private File getFileFromJarUri(URI uri) throws Exception {
JarURLConnection connection = (JarURLConnection) uri.toURL().openConnection();
File file = new File(connection.getJarFileURL().toURI());
return file;
}
Aggregations