443661 Rename manifest and service constants for jetty osgi resource fragment code

This commit is contained in:
Jan Bartel 2014-09-10 16:28:29 +10:00
parent c80f556139
commit 09e22c042c
5 changed files with 86 additions and 35 deletions

View File

@ -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);

View File

@ -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<String, Resource> patchResourcesPath = new TreeMap<String, Resource>();
TreeMap<String, Resource> prependedResourcesPath = new TreeMap<String, Resource>();
TreeMap<String, Resource> appendedResourcesPath = new TreeMap<String, Resource>();
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<Resource>(appendedResourcesPath.values()));
{
LinkedHashSet<Resource> resources = new LinkedHashSet<Resource>();
//Add in any existing setting of extra resource dirs
Set<Resource> resourceDirs = (Set<Resource>)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<String, Resource> 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));
}
}

View File

@ -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

View File

@ -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);

View File

@ -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,