From 09e22c042ce1ce72cc75a61a1f983f49d77a585d Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Wed, 10 Sep 2014 16:28:29 +1000 Subject: [PATCH] 443661 Rename manifest and service constants for jetty osgi resource fragment code --- .../jetty/osgi/boot/BundleWebAppProvider.java | 6 +- .../osgi/boot/OSGiWebInfConfiguration.java | 84 +++++++++++-------- .../jetty/osgi/boot/OSGiWebappConstants.java | 7 ++ .../osgi/boot/ServiceWebAppProvider.java | 2 + .../eclipse/jetty/osgi/boot/utils/Util.java | 22 +++++ 5 files changed, 86 insertions(+), 35 deletions(-) diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java index e1251df52eb..9fbade35d8b 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/BundleWebAppProvider.java @@ -25,6 +25,7 @@ import java.util.Map; import org.eclipse.jetty.deploy.App; import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper; +import org.eclipse.jetty.osgi.boot.utils.Util; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.osgi.framework.Bundle; @@ -122,9 +123,10 @@ public class BundleWebAppProvider extends AbstractWebAppProvider implements Bund Dictionary headers = bundle.getHeaders(); //does the bundle have a OSGiWebappConstants.JETTY_WAR_FOLDER_PATH - if (headers.get(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH) != null) + String resourcePath = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_RESOURCE_PATH, headers); + if (resourcePath != null) { - String base = (String)headers.get(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH); + String base = resourcePath; contextPath = getContextPath(bundle); String originId = getOriginId(bundle, base); diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebInfConfiguration.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebInfConfiguration.java index 4482640bcfd..8f878c70645 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebInfConfiguration.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebInfConfiguration.java @@ -22,13 +22,16 @@ import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.regex.Pattern; import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelperFactory; +import org.eclipse.jetty.osgi.boot.utils.Util; import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -197,7 +200,7 @@ public class OSGiWebInfConfiguration extends WebInfConfiguration @Override public void configure(WebAppContext context) throws Exception { - TreeMap patchResourcesPath = new TreeMap(); + TreeMap prependedResourcesPath = new TreeMap(); TreeMap appendedResourcesPath = new TreeMap(); Bundle bundle = (Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE); @@ -221,46 +224,33 @@ public class OSGiWebInfConfiguration extends WebInfConfiguration // looked up. for (Bundle frag : fragments) { - String fragFolder = (String) frag.getHeaders().get(OSGiWebappConstants.JETTY_WAR_FRAGMENT_FOLDER_PATH); - String patchFragFolder = (String) frag.getHeaders().get(OSGiWebappConstants.JETTY_WAR_PATCH_FRAGMENT_FOLDER_PATH); - if (fragFolder != null) - { - URL fragUrl = frag.getEntry(fragFolder); - if (fragUrl == null) { throw new IllegalArgumentException("Unable to locate " + fragFolder - + " inside " - + " the fragment '" - + frag.getSymbolicName() - + "'"); } - fragUrl = BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(fragUrl); - String key = fragFolder.startsWith("/") ? fragFolder.substring(1) : fragFolder; - appendedResourcesPath.put(key + ";" + frag.getSymbolicName(), Resource.newResource(fragUrl)); - } - if (patchFragFolder != null) - { - URL patchFragUrl = frag.getEntry(patchFragFolder); - if (patchFragUrl == null) - { - throw new IllegalArgumentException("Unable to locate " + patchFragUrl - + " inside fragment '"+frag.getSymbolicName()+ "'"); - } - patchFragUrl = BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(patchFragUrl); - String key = patchFragFolder.startsWith("/") ? patchFragFolder.substring(1) : patchFragFolder; - patchResourcesPath.put(key + ";" + frag.getSymbolicName(), Resource.newResource(patchFragUrl)); - } + String path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_FRAGMENT_FOLDER_PATH,OSGiWebappConstants.JETTY_WAR_FRAGMENT_RESOURCE_PATH,frag.getHeaders()); + convertFragmentPathToResource(path, frag, appendedResourcesPath); + path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_PATCH_FRAGMENT_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_PREPEND_FRAGMENT_RESOURCE_PATH, frag.getHeaders()); + convertFragmentPathToResource(path, frag, prependedResourcesPath); } if (!appendedResourcesPath.isEmpty()) - context.setAttribute(WebInfConfiguration.RESOURCE_DIRS, new HashSet(appendedResourcesPath.values())); + { + LinkedHashSet resources = new LinkedHashSet(); + //Add in any existing setting of extra resource dirs + Set resourceDirs = (Set)context.getAttribute(WebInfConfiguration.RESOURCE_DIRS); + if (resourceDirs != null && !resourceDirs.isEmpty()) + resources.addAll(resourceDirs); + //Then append the values from JETTY_WAR_FRAGMENT_FOLDER_PATH + resources.addAll(appendedResourcesPath.values()); + + context.setAttribute(WebInfConfiguration.RESOURCE_DIRS, resources); + } } } super.configure(context); - // place the patch resources at the beginning of the contexts's resource base - if (!patchResourcesPath.isEmpty()) + // place the prepended resources at the beginning of the contexts's resource base + if (!prependedResourcesPath.isEmpty()) { - Resource[] resources = new Resource[1+patchResourcesPath.size()]; - ResourceCollection mergedResources = new ResourceCollection (patchResourcesPath.values().toArray(new Resource[patchResourcesPath.size()])); - System.arraycopy(patchResourcesPath.values().toArray(new Resource[patchResourcesPath.size()]), 0, resources, 0, patchResourcesPath.size()); + Resource[] resources = new Resource[1+prependedResourcesPath.size()]; + System.arraycopy(prependedResourcesPath.values().toArray(new Resource[prependedResourcesPath.size()]), 0, resources, 0, prependedResourcesPath.size()); resources[resources.length-1] = context.getBaseResource(); context.setBaseResource(new ResourceCollection(resources)); } @@ -306,4 +296,32 @@ public class OSGiWebInfConfiguration extends WebInfConfiguration return resources; } + + + /** + * Convert a path inside a fragment into a Resource + * @param resourcePath + * @param fragment + * @param resourceMap + * @throws Exception + */ + private void convertFragmentPathToResource (String resourcePath, Bundle fragment, Map resourceMap ) + throws Exception + { + if (resourcePath == null) + return; + + URL url = fragment.getEntry(resourcePath); + if (url == null) + { + throw new IllegalArgumentException("Unable to locate " + resourcePath + + " inside " + + " the fragment '" + + fragment.getSymbolicName() + + "'"); + } + url = BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(url); + String key = resourcePath.startsWith("/") ? resourcePath.substring(1) : resourcePath; + resourceMap.put(key + ";" + fragment.getSymbolicName(), Resource.newResource(url)); + } } diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebappConstants.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebappConstants.java index e82f2e813c6..0080d0fa274 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebappConstants.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiWebappConstants.java @@ -67,16 +67,23 @@ public class OSGiWebappConstants public static final String JETTY_CONTEXT_FILE_PATH = "Jetty-ContextFilePath"; /** path within the bundle to the folder that contains the basic resources. */ + @Deprecated public static final String JETTY_WAR_FOLDER_PATH = "Jetty-WarFolderPath"; + public static final String JETTY_WAR_RESOURCE_PATH = "Jetty-WarResourcePath"; /** path within a fragment hosted by a web-bundle to a folder that contains basic resources. * the path is appended to the lookup path where jetty locates static resources */ + @Deprecated public static final String JETTY_WAR_FRAGMENT_FOLDER_PATH = "Jetty-WarFragmentFolderPath"; + public static final String JETTY_WAR_FRAGMENT_RESOURCE_PATH = "Jetty-WarFragmentResourcePath"; + /** path within a fragment hosted by a web-bundle to a folder that contains basic resources. * The path is prefixed to the lookup path where jetty locates static resources: * this will override static resources with the same name in the web-bundle. */ + @Deprecated public static final String JETTY_WAR_PATCH_FRAGMENT_FOLDER_PATH = "Jetty-WarPatchFragmentFolderPath"; + public static final String JETTY_WAR_PREPEND_FRAGMENT_RESOURCE_PATH = "Jetty-WarPrependFragmentResourcePath"; /** installation path of webapp bundle diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java index e3fbf3b2562..81b62806938 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/ServiceWebAppProvider.java @@ -127,6 +127,8 @@ public class ServiceWebAppProvider extends AbstractWebAppProvider implements Ser return false; //No context path String base = (String)serviceRef.getProperty(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH); + if (base == null) + base = (String)serviceRef.getProperty(OSGiWebappConstants.JETTY_WAR_RESOURCE_PATH); if (base == null) base = (String)serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_WAR); diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java index d688f68d5a7..6eaa8012304 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/Util.java @@ -35,6 +35,28 @@ public class Util { public static final String DEFAULT_DELIMS = ",;"; + + /** + * Get the value of a manifest header. + * + * @param name the name of the header + * @param altName an alternative name for the header (useful for deprecated names) + * @param manifest + * @return + */ + public static String getManifestHeaderValue (String name, String altName, Dictionary manifest) + { + if (manifest == null) + return null; + if (name == null && altName == null) + return null; + if (name != null) + return (String)manifest.get(name); + return (String)manifest.get(altName); + } + + + /* ------------------------------------------------------------ */ /** * Treating the string as a separated list of filenames,