Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for Java Web Start (WELD-1040) #203

Closed
wants to merge 3 commits into from

3 participants

@alesj

The bottom exceptions could use better msg, or just catch plain Exception.

@agattiker

It's true it's a case of exceptionitis but it's always good to keep a close watch on reflection calls in case of any unexpected runtime environments...

@jharting jharting closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
28 environments/se/core/src/main/java/org/jboss/weld/environment/se/discovery/url/FileSystemURLHandler.java
@@ -21,6 +21,8 @@
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
@@ -50,6 +52,16 @@ public void handle(Collection<String> paths, List<String> discoveredClasses, Lis
if (urlPath.indexOf('!') > 0) {
urlPath = urlPath.substring(0, urlPath.indexOf('!'));
}
+ // WebStart support: get path to local cached copy of remote JAR file
+ if (urlPath.startsWith("http:") || urlPath.startsWith("https:")) {
+ // Class loader should be an instance of com.sun.jnlp.JNLPClassLoader
+ ClassLoader jnlpClassLoader = WeldSEResourceLoader.getClassLoader();
+ // Try to call com.sun.jnlp.JNLPClassLoader#getJarFile(URL) from JDK 6
+ Method m = jnlpClassLoader.getClass().getMethod("getJarFile", URL.class);
+ // returns a reference to the local cached copy of the JAR
+ ZipFile jarFile = (ZipFile) m.invoke(jnlpClassLoader, new URL(urlPath));
+ urlPath = jarFile.getName();
+ }
File file = new File(urlPath);
if (file.isDirectory()) {
@@ -57,8 +69,20 @@ public void handle(Collection<String> paths, List<String> discoveredClasses, Lis
} else {
handleArchiveByFile(file, discoveredClasses, discoveredBeansXmlUrls);
}
- } catch (IOException ioe) {
- FileSystemURLHandler.log.warn("could not read entries", ioe);
+ } catch (MalformedURLException mue) {
+ log.warn("could not read entries, method JNLPClassLoader#getJarFile(URL) did not return a valid URL", mue);
+ } catch (SecurityException se) {
+ log.warn("could not invoke JNLPClassLoader#getJarFile(URL) on context class loader", se);
+ } catch (NoSuchMethodException nsme) {
+ log.warn("could not invoke JNLPClassLoader#getJarFile(URL) on context class loader, expecting Web Start class loader", nsme);
+ } catch (IllegalArgumentException iarge) {
+ log.warn("could not invoke JNLPClassLoader#getJarFile(URL) on context class loader, expecting Web Start class loader", iarge);
+ } catch (IllegalAccessException iacce) {
+ log.warn("could not invoke JNLPClassLoader#getJarFile(URL) on context class loader", iacce);
+ } catch (InvocationTargetException ite) {
+ log.warn("JNLPClassLoader#getJarFile(URL) threw exception", ite);
+ } catch (IOException ioe) { // from handleArchiveByFile
+ log.warn("could not read entries", ioe);
}
}
}
View
74 environments/se/core/src/test/java/org/jboss/weld/environment/se/test/WebStartTest.java
@@ -0,0 +1,74 @@
+package org.jboss.weld.environment.se.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import org.jboss.weld.bootstrap.WeldBootstrap;
+import org.jboss.weld.bootstrap.api.Bootstrap;
+import org.jboss.weld.environment.se.discovery.url.WeldSEResourceLoader;
+import org.jboss.weld.environment.se.discovery.url.WeldSEUrlDeployment;
+import org.jboss.weld.resources.spi.ResourceLoader;
+import org.junit.Test;
+
+/**
+ * Tests that Java Web Start to remote JARs are resolved to their local cached
+ * copy (WELD-1040).
+ *
+ * @author Alexandre Gattiker
+ */
+public class WebStartTest {
+
+ /**
+ * Mock implementation of com.sun.jnlp.JNLPClassLoader#getJarFile(URL)
+ */
+ public static final class MockJNLPClassLoader extends ClassLoader {
+ public JarFile getJarFile(URL url) {
+ try {
+ File jarFile = File.createTempFile("jarfile", ".jar");
+ ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
+ jarFile));
+ out.putNextEntry(new ZipEntry("MockClass.class"));
+ out.close();
+ return new JarFile(jarFile);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+ }
+
+ @Test
+ public void testJavaWebStartClassLoaderURL() {
+ ResourceLoader resourceLoader = new WeldSEResourceLoader() {
+ @Override
+ public Collection<URL> getResources(String name) {
+ try {
+ return Collections.singleton(new URL(
+ "http://example.com/dummy"));
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ };
+
+ Thread.currentThread().setContextClassLoader(new MockJNLPClassLoader());
+ Bootstrap bootstrap = new WeldBootstrap();
+ WeldSEUrlDeployment depl = new WeldSEUrlDeployment(resourceLoader,
+ bootstrap);
+
+ assertEquals(Collections.singleton("MockClass"), new HashSet<String>(
+ depl.getBeanDeploymentArchives().get(0).getBeanClasses()));
+ }
+}
Something went wrong with that request. Please try again.