use of java.net.JarURLConnection in project Bytecoder by mirkosertic.
the class URLClassPath method check.
/*
* Check whether the resource URL should be returned.
* Throw exception on failure.
* Called internally within this file.
*/
public static void check(URL url) throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) {
URLConnection urlConnection = url.openConnection();
Permission perm = urlConnection.getPermission();
if (perm != null) {
try {
security.checkPermission(perm);
} catch (SecurityException se) {
// security managers
if ((perm instanceof java.io.FilePermission) && perm.getActions().indexOf("read") != -1) {
security.checkRead(perm.getName());
} else if ((perm instanceof java.net.SocketPermission) && perm.getActions().indexOf("connect") != -1) {
URL locUrl = url;
if (urlConnection instanceof JarURLConnection) {
locUrl = ((JarURLConnection) urlConnection).getJarFileURL();
}
security.checkConnect(locUrl.getHost(), locUrl.getPort());
} else {
throw se;
}
}
}
}
}
use of java.net.JarURLConnection in project n4js by eclipse.
the class ShippedCodeAccess method getShippedRuntimeCodePath.
/**
* Returns path for the shipped code from the provided location. If location is plain file returns its absolute path
* as string. If location is inside jar file, unpacks desired resource to the temporal location and returns path to
* that location.
*
* @param rootName
* name of shipped root to be located
* @return the path pointing to the shipped code
*/
protected static String getShippedRuntimeCodePath(String rootName) {
try {
URL resourceUrl = getResource(rootName);
final URLConnection connection = resourceUrl.openConnection();
if (connection instanceof JarURLConnection) {
return recursivelyCopyContent((JarURLConnection) connection, rootName);
}
return new File(resourceUrl.toURI()).getCanonicalFile().getAbsolutePath().replace("\\", "\\\\");
} catch (final Exception e) {
throw new RuntimeException("Error while getting shipped code path.", e);
}
}
use of java.net.JarURLConnection in project mlib by myshzzx.
the class JarTest1 method t1.
@Test
@Ignore
public void t1() throws IOException {
final URL url = new URL("jar:file:///L:/javafx-mx.jar!/");
final JarURLConnection jar = (JarURLConnection) url.openConnection();
final JarFile jarFile = jar.getJarFile();
jarFile.stream().forEach(e -> {
System.out.println(e.getName());
System.out.println(e.getTime());
});
}
use of java.net.JarURLConnection in project gate-core by GateNLP.
the class Tools method findSubclasses.
/**
* Finds all subclasses of a given class or interface. It will only search
* within the loaded packages and not the entire classpath.
* @param parentClass the class for which subclasses are sought
* @return a list of {@link Class} objects.
*/
public static List<Class<?>> findSubclasses(Class<?> parentClass) {
Package[] packages = Package.getPackages();
List<Class<?>> result = new ArrayList<Class<?>>();
for (int i = 0; i < packages.length; i++) {
String packageDir = packages[i].getName();
// look in the file system
if (!packageDir.startsWith("/"))
packageDir = "/" + packageDir;
packageDir = packageDir.replace('.', Strings.getPathSep().charAt(0));
URL packageURL = Gate.getClassLoader().getResource(packageDir);
if (packageURL != null) {
File directory = Files.fileFromURL(packageURL);
if (directory.exists()) {
String[] files = directory.list();
for (int j = 0; j < files.length; j++) {
// we are only interested in .class files
if (files[j].endsWith(".class")) {
// removes the .class extension
String classname = files[j].substring(0, files[j].length() - 6);
try {
// Try to create an instance of the object
Class<?> aClass = Class.forName(packages[i] + "." + classname, true, Gate.getClassLoader());
if (parentClass.isAssignableFrom(aClass))
result.add(aClass);
} catch (ClassNotFoundException cnfex) {
}
}
}
} else {
// look in jar files
try {
JarURLConnection conn = (JarURLConnection) packageURL.openConnection();
String starts = conn.getEntryName();
JarFile jFile = conn.getJarFile();
Enumeration<JarEntry> e = jFile.entries();
while (e.hasMoreElements()) {
String entryname = e.nextElement().getName();
if (entryname.startsWith(starts) && // not sub dir
(entryname.lastIndexOf('/') <= starts.length()) && entryname.endsWith(".class")) {
String classname = entryname.substring(0, entryname.length() - 6);
if (classname.startsWith("/"))
classname = classname.substring(1);
classname = classname.replace('/', '.');
try {
// Try to create an instance of the object
Class<?> aClass = Class.forName(packages[i] + "." + classname, true, Gate.getClassLoader());
if (parentClass.isAssignableFrom(aClass))
result.add(aClass);
} catch (ClassNotFoundException cnfex) {
}
}
}
} catch (java.io.IOException ioe) {
}
}
}
}
return result;
}
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;
}
Aggregations