From 5948ea79eb1526856f79bde0c90d2efff5365cff Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sat, 24 Apr 2010 01:13:50 +0000 Subject: [PATCH] helper method to resolve equinox 'resourcebundle' urls. git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1559 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../DefaultBundleClassLoaderHelper.java | 3 + .../internal/DefaultFileLocatorHelper.java | 70 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultBundleClassLoaderHelper.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultBundleClassLoaderHelper.java index a3541b60ac3..10d1698ae34 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultBundleClassLoaderHelper.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultBundleClassLoaderHelper.java @@ -14,6 +14,8 @@ package org.eclipse.jetty.osgi.boot.utils.internal; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLConnection; import org.eclipse.jetty.osgi.boot.utils.BundleClassLoaderHelper; import org.osgi.framework.Bundle; @@ -180,4 +182,5 @@ public class DefaultBundleClassLoaderHelper implements BundleClassLoaderHelper } return null; } + } diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java index 5dfc3676df7..cd2732825af 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java @@ -14,6 +14,7 @@ package org.eclipse.jetty.osgi.boot.utils.internal; import java.io.File; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.net.URI; import java.net.URL; import java.net.URLConnection; @@ -209,5 +210,74 @@ public class DefaultFileLocatorHelper implements BundleFileLocatorHelper { jasperLocation }; } } + + + //introspection on equinox to invoke the getLocalURL method on BundleURLConnection + private static Method BUNDLE_URL_CONNECTION_getLocalURL = null; + private static Method BUNDLE_URL_CONNECTION_getFileURL = null; + /** + * Only useful for equinox: on felix we get the file:// or jar:// url already. + * Other OSGi implementations have not been tested + *

+ * Get a URL to the bundle entry that uses a common protocol (i.e. file: + * jar: or http: etc.). + *

+ * @return a URL to the bundle entry that uses a common protocol + */ + public static URL getLocalURL(URL url) { + if ("bundleresource".equals(url.getProtocol())) { + try { + URLConnection conn = url.openConnection(); + if (BUNDLE_URL_CONNECTION_getLocalURL == null && + conn.getClass().getName().equals( + "org.eclipse.osgi.framework.internal.core.BundleURLConnection")) { + BUNDLE_URL_CONNECTION_getLocalURL = conn.getClass().getMethod("getLocalURL", null); + BUNDLE_URL_CONNECTION_getLocalURL.setAccessible(true); + } + if (BUNDLE_URL_CONNECTION_getLocalURL != null) { + return (URL)BUNDLE_URL_CONNECTION_getLocalURL.invoke(conn, null); + } + } catch (Throwable t) { + t.printStackTrace(); + } + } + return url; + } + /** + * Only useful for equinox: on felix we get the file:// url already. + * Other OSGi implementations have not been tested + *

+ * Get a URL to the content of the bundle entry that uses the file: protocol. + * The content of the bundle entry may be downloaded or extracted to the local + * file system in order to create a file: URL. + * @return a URL to the content of the bundle entry that uses the file: protocol + *

+ */ + public static URL getFileURL(URL url) + { + if ("bundleresource".equals(url.getProtocol())) + { + try + { + URLConnection conn = url.openConnection(); + if (BUNDLE_URL_CONNECTION_getFileURL == null && + conn.getClass().getName().equals( + "org.eclipse.osgi.framework.internal.core.BundleURLConnection")) + { + BUNDLE_URL_CONNECTION_getFileURL = conn.getClass().getMethod("getFileURL", null); + BUNDLE_URL_CONNECTION_getFileURL.setAccessible(true); + } + if (BUNDLE_URL_CONNECTION_getFileURL != null) + { + return (URL)BUNDLE_URL_CONNECTION_getFileURL.invoke(conn, null); + } + } + catch (Throwable t) + { + t.printStackTrace(); + } + } + return url; + } }