use of java.net.JarURLConnection in project openj9 by eclipse.
the class RecreateClassUtils method createClassFile.
/* Reads a class file from the jar file from which the class was loaded
* and creates a local copy in current working directory.
*/
public static void createClassFile(Class<?> testClass, String classFileName) throws Exception {
URL url = testClass.getResource(classFileName + ".class");
if (url != null) {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
JarURLConnection urlConnection = (JarURLConnection) connection;
urlConnection.connect();
try (InputStream inStream = urlConnection.getInputStream()) {
File outFile = new File(classFileName + ".class");
try (FileOutputStream outStream = new FileOutputStream(outFile)) {
byte[] data = new byte[1024];
int size;
while ((size = inStream.read(data)) > 0) {
outStream.write(data, 0, size);
}
}
}
}
}
}
use of java.net.JarURLConnection in project openj9 by eclipse.
the class CustomPartitioningURLLoader method locateClass.
private int locateClass(String className) {
int classAtEntry = -1;
String name = className.replace('.', '/').concat(".class");
for (int index = 0; index < urls.length; index++) {
URL currentUrl = urls[index];
if (currentUrl.getProtocol().equals("jar")) {
JarEntry entry = null;
JarFile jf = (JarFile) jarCache.get(currentUrl);
if (jf == null) {
/* First time we have encountered this jar.
* Lets cache its metaData.
*/
try {
URL jarFileUrl = ((JarURLConnection) currentUrl.openConnection()).getJarFileURL();
JarURLConnection jarUrlConnection = (JarURLConnection) new URL("jar", "", jarFileUrl.toExternalForm() + "!/").openConnection();
try {
jf = jarUrlConnection.getJarFile();
} catch (Exception e) {
}
if (jf != null) {
jarCache.put(currentUrl, jf);
}
} catch (Exception e) {
e.printStackTrace();
}
if (jf != null) {
Manifest manifest = null;
java.security.cert.Certificate[] certs = null;
URL csUrl = currentUrl;
CodeSource codeSource;
try {
manifest = jf.getManifest();
} catch (Exception e) {
e.printStackTrace();
}
entry = jf.getJarEntry(name);
if (entry != null) {
certs = entry.getCertificates();
}
codeSource = new CodeSource(csUrl, certs);
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = manifest;
metaData.codeSource = codeSource;
metaDataArray[index] = metaData;
}
}
if (entry == null && jf != null) {
entry = jf.getJarEntry(name);
}
if (entry != null) {
/* We have the first match on the class path, return the current url index */
return index;
}
} else {
String filename = currentUrl.getFile();
String host = currentUrl.getHost();
if (host != null && host.length() > 0) {
filename = new StringBuffer(host.length() + filename.length() + name.length() + 2).append("//").append(host).append(filename).append(name).toString();
} else {
filename = new StringBuffer(filename.length() + name.length()).append(filename).append(name).toString();
}
File file = new File(filename);
// Don't throw exceptions for speed
if (file.exists()) {
if (metaDataArray[index] == null) {
java.security.cert.Certificate[] certs = null;
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = null;
metaData.codeSource = new CodeSource(currentUrl, certs);
metaDataArray[index] = metaData;
}
return index;
}
}
}
return classAtEntry;
}
use of java.net.JarURLConnection in project embulk by embulk.
the class PluginClassLoader method findResourceFromEmbeddedJars.
private URL findResourceFromEmbeddedJars(final String resourceName) {
for (final String embeddedJarPath : this.embeddedJarPathsInNestedJar) {
final URL embeddedJarUrl;
final JarURLConnection embeddedJarUrlConnection;
final JarInputStream embeddedJarInputStream;
try {
embeddedJarUrl = getEmbeddedJarUrl(embeddedJarPath);
embeddedJarUrlConnection = getEmbeddedJarUrlConnection(embeddedJarUrl);
embeddedJarInputStream = getEmbeddedJarInputStream(embeddedJarUrlConnection);
} catch (IOException ex) {
// TODO: Notify this to reporters as far as possible.
System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
ex.printStackTrace();
continue;
}
JarEntry jarEntry;
try {
jarEntry = embeddedJarInputStream.getNextJarEntry();
} catch (IOException ex) {
// TODO: Notify this to reporters as far as possible.
System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
ex.printStackTrace();
continue;
}
jarEntries: while (jarEntry != null) {
if (jarEntry.getName().equals(resourceName)) {
// The URL lives only in the JVM execution.
try {
// The URL lives only in the JVM execution.
return new URL("embulk-plugin-jar", "", -1, embeddedJarUrl + "!!/" + resourceName, new PluginClassUrlStreamHandler("embulk-plugin-jar"));
} catch (MalformedURLException ex) {
// TODO: Notify this to reporters as far as possible.
System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
ex.printStackTrace();
break jarEntries;
}
}
try {
jarEntry = embeddedJarInputStream.getNextJarEntry();
} catch (IOException ex) {
// TODO: Notify this to reporters as far as possible.
System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
ex.printStackTrace();
break jarEntries;
}
}
}
return null;
}
use of java.net.JarURLConnection in project embulk by embulk.
the class PluginClassLoader method getEmbeddedJarUrlConnection.
private JarURLConnection getEmbeddedJarUrlConnection(final URL embeddedJarUrl) throws IOException {
final JarURLConnection embeddedJarUrlConnection;
try {
final URLConnection urlConnection = embeddedJarUrl.openConnection();
embeddedJarUrlConnection = (JarURLConnection) urlConnection;
} catch (IOException ex) {
// TODO: Notify this to reporters as far as possible.
System.err.println("Failed to load entry in embedded JAR: " + embeddedJarUrl.toString());
ex.printStackTrace();
throw ex;
}
return embeddedJarUrlConnection;
}
use of java.net.JarURLConnection in project embulk by embulk.
the class JarPluginLoader method load.
public static JarPluginLoader load(final Path jarPath, final PluginClassLoaderFactory classLoaderFactory) throws InvalidJarPluginException {
final JarURLConnection connection = openJarUrlConnection(jarPath);
final Manifest manifest = loadJarPluginManifest(connection, jarPath);
final Attributes manifestAttributes = manifest.getMainAttributes();
final int spiVersion = getPluginSpiVersionFromManifest(manifestAttributes);
if (spiVersion == 0) {
final String mainClassName = getPluginMainClassNameFromManifest(manifestAttributes);
final List<String> pluginClassPath = getPluginClassPathFromManifest(manifestAttributes);
final Class mainClass = loadJarPluginMainClass(jarPath, mainClassName, pluginClassPath, classLoaderFactory);
return new JarPluginLoader(manifest, manifestAttributes, mainClass);
}
throw new InvalidJarPluginException("Unknown SPI version of JAR plugin: " + spiVersion);
}
Aggregations