Remove unused field _preconfigProcessor; reformat src for

o.e.j.maven.plugin.JettyWebAppContext; fix comment header in
o.e.j.quickstart.ExtraXmlDescriptorProcessor.
This commit is contained in:
Jan Bartel 2018-10-17 15:52:58 +11:00
parent 9787dbd594
commit fefc3db0ba
2 changed files with 178 additions and 199 deletions

View File

@ -33,7 +33,6 @@ import java.util.TreeSet;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.quickstart.ExtraXmlDescriptorProcessor;
import org.eclipse.jetty.quickstart.QuickStartConfiguration;
import org.eclipse.jetty.quickstart.QuickStartConfiguration.Mode;
import org.eclipse.jetty.servlet.FilterHolder;
@ -53,10 +52,10 @@ import org.eclipse.jetty.webapp.WebInfConfiguration;
/**
* JettyWebAppContext
*
* Extends the WebAppContext to specialize for the maven environment.
* We pass in the list of files that should form the classpath for
* the webapp when executing in the plugin, and any jetty-env.xml file
* that may have been configured.
* Extends the WebAppContext to specialize for the maven environment. We pass in
* the list of files that should form the classpath for the webapp when
* executing in the plugin, and any jetty-env.xml file that may have been
* configured.
*
*/
public class JettyWebAppContext extends WebAppContext
@ -64,55 +63,53 @@ public class JettyWebAppContext extends WebAppContext
private static final Logger LOG = Log.getLogger(JettyWebAppContext.class);
private static final String DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN = ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$|.*javax.servlet.jsp.jstl-[^/]*\\.jar|.*taglibs-standard-impl-.*\\.jar";
private static final String WEB_INF_CLASSES_PREFIX = "/WEB-INF/classes";
private static final String WEB_INF_LIB_PREFIX = "/WEB-INF/lib";
private File _classes = null;
private File _testClasses = null;
private final List<File> _webInfClasses = new ArrayList<>();
private final List<File> _webInfJars = new ArrayList<>();
private final Map<String, File> _webInfJarMap = new HashMap<String, File>();
private List<File> _classpathFiles; //webInfClasses+testClasses+webInfJars
private List<File> _classpathFiles; // webInfClasses+testClasses+webInfJars
private String _jettyEnvXml;
private List<Overlay> _overlays;
/**
* Set the "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern" with a pattern for matching jars on
* container classpath to scan. This is analogous to the WebAppContext.setAttribute() call.
* Set the "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern" with
* a pattern for matching jars on container classpath to scan. This is
* analogous to the WebAppContext.setAttribute() call.
*/
private String _containerIncludeJarPattern = null;
/**
* Set the "org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern" with a pattern for matching jars on
* webapp's classpath to scan. This is analogous to the WebAppContext.setAttribute() call.
* Set the "org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern" with a
* pattern for matching jars on webapp's classpath to scan. This is
* analogous to the WebAppContext.setAttribute() call.
*/
private String _webInfIncludeJarPattern = null;
/**
* If there is no maven-war-plugin config for ordering of the current project in the
* sequence of overlays, use this to control whether the current project is added
* first or last in list of overlaid resources
* If there is no maven-war-plugin config for ordering of the current
* project in the sequence of overlays, use this to control whether the
* current project is added first or last in list of overlaid resources
*/
private boolean _baseAppFirst = true;
private boolean _isGenerateQuickStart;
private ExtraXmlDescriptorProcessor _preconfigProcessor;
/* ------------------------------------------------------------ */
public JettyWebAppContext ()
throws Exception
public JettyWebAppContext() throws Exception
{
super();
super();
// Turn off copyWebInf option as it is not applicable for plugin.
super.setCopyWebInf(false);
addConfiguration(new MavenWebInfConfiguration());
@ -121,95 +118,87 @@ public class JettyWebAppContext extends WebAppContext
addConfiguration(new PlusConfiguration());
addConfiguration(new AnnotationConfiguration());
}
/* ------------------------------------------------------------ */
public void setContainerIncludeJarPattern(String pattern)
{
_containerIncludeJarPattern = pattern;
}
/* ------------------------------------------------------------ */
public String getContainerIncludeJarPattern()
{
return _containerIncludeJarPattern;
}
/* ------------------------------------------------------------ */
public String getWebInfIncludeJarPattern()
{
return _webInfIncludeJarPattern;
}
/* ------------------------------------------------------------ */
public void setWebInfIncludeJarPattern(String pattern)
{
_webInfIncludeJarPattern = pattern;
}
/* ------------------------------------------------------------ */
public List<File> getClassPathFiles()
{
return this._classpathFiles;
}
/* ------------------------------------------------------------ */
public void setJettyEnvXml (String jettyEnvXml)
public void setJettyEnvXml(String jettyEnvXml)
{
this._jettyEnvXml = jettyEnvXml;
}
/* ------------------------------------------------------------ */
public String getJettyEnvXml()
{
return this._jettyEnvXml;
}
/* ------------------------------------------------------------ */
public void setClasses(File dir)
{
_classes = dir;
}
/* ------------------------------------------------------------ */
public File getClasses()
{
return _classes;
}
/* ------------------------------------------------------------ */
public void setWebInfLib (List<File> jars)
public void setWebInfLib(List<File> jars)
{
_webInfJars.addAll(jars);
}
/* ------------------------------------------------------------ */
public void setTestClasses (File dir)
public void setTestClasses(File dir)
{
_testClasses = dir;
}
/* ------------------------------------------------------------ */
public File getTestClasses ()
public File getTestClasses()
{
return _testClasses;
}
/* ------------------------------------------------------------ */
/**
* Ordered list of wars to overlay on top of the current project. The list
* may contain an overlay that represents the current project.
*
* @param overlays the list of overlays
*/
public void setOverlays (List<Overlay> overlays)
public void setOverlays(List<Overlay> overlays)
{
_overlays = overlays;
}
public void setOriginAttribute (String name)
/**
* Set the name of the attribute that is used in each generated xml element
* to indicate the source of the xml element (eg annotation, web.xml etc).
*
* @param name the name of the attribute to use.
*/
public void setOriginAttribute(String name)
{
setAttribute(QuickStartConfiguration.ORIGIN_ATTRIBUTE,name);
setAttribute(QuickStartConfiguration.ORIGIN_ATTRIBUTE, name);
}
/**
@ -218,122 +207,127 @@ public class JettyWebAppContext extends WebAppContext
public String getOriginAttribute()
{
Object attr = getAttribute(QuickStartConfiguration.ORIGIN_ATTRIBUTE);
return attr==null?null:attr.toString();
return attr == null ? null : attr.toString();
}
/**
* @param generateOrigin the generateOrigin to set
* Toggle whether or not the origin attribute will be generated into the
* xml.
*
* @param generateOrigin if true then the origin of each xml element is
* added, otherwise it is omitted.
*/
public void setGenerateOrigin(boolean generateOrigin)
{
setAttribute(QuickStartConfiguration.GENERATE_ORIGIN,generateOrigin);
setAttribute(QuickStartConfiguration.GENERATE_ORIGIN, generateOrigin);
}
/**
* @return the generateOrigin
* @return true if the origin attribute will be generated, false otherwise
*/
public boolean isGenerateOrigin()
{
Object attr = getAttribute(QuickStartConfiguration.GENERATE_ORIGIN);
return attr==null?false:Boolean.valueOf(attr.toString());
return attr == null ? false : Boolean.valueOf(attr.toString());
}
/* ------------------------------------------------------------ */
public List<Overlay> getOverlays()
{
return _overlays;
}
/* ------------------------------------------------------------ */
public void setBaseAppFirst(boolean value)
{
_baseAppFirst = value;
}
/* ------------------------------------------------------------ */
public boolean getBaseAppFirst()
{
return _baseAppFirst;
}
/* ------------------------------------------------------------ */
public void setQuickStartWebDescriptor (String quickStartWebXml) throws Exception
/**
* Set the file to use into which to generate the quickstart output.
*
* @param quickStartWebXml the full path to the file to use
* @throws Exception
*/
public void setQuickStartWebDescriptor(String quickStartWebXml) throws Exception
{
setQuickStartWebDescriptor(Resource.newResource(quickStartWebXml));
}
/* ------------------------------------------------------------ */
protected void setQuickStartWebDescriptor (Resource quickStartWebXml)
/**
* Set the Resource to use into which to generate the quickstart output.
*
* @param quickStartWebXml
*/
protected void setQuickStartWebDescriptor(Resource quickStartWebXml)
{
setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML,quickStartWebXml.toString());
setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, quickStartWebXml.toString());
}
/* ------------------------------------------------------------ */
public Resource getQuickStartWebDescriptor ()
throws Exception
public Resource getQuickStartWebDescriptor() throws Exception
{
Object o = getAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML);
if (o == null)
return null;
else if (o instanceof Resource)
return (Resource)o;
return (Resource) o;
else
return Resource.newResource((String)o);
return Resource.newResource((String) o);
}
/* ------------------------------------------------------------ */
/**
* This method is provided as a convenience for jetty maven plugin configuration
* @param resourceBases Array of resources strings to set as a {@link ResourceCollection}. Each resource string may be a comma separated list of resources
* @see Resource
* This method is provided as a convenience for jetty maven plugin
* configuration
*
* @param resourceBases Array of resources strings to set as a
* {@link ResourceCollection}. Each resource string may be a
* comma separated list of resources
*/
public void setResourceBases(String[] resourceBases)
{
List<String> resources = new ArrayList<String>();
for (String rl:resourceBases)
for (String rl : resourceBases)
{
String[] rs = StringUtil.csvSplit(rl);
for (String r:rs)
for (String r : rs)
resources.add(r);
}
setBaseResource(new ResourceCollection(resources.toArray(new String[resources.size()])));
}
/* ------------------------------------------------------------ */
public List<File> getWebInfLib()
{
return _webInfJars;
}
/* ------------------------------------------------------------ */
public List<File> getWebInfClasses()
{
return _webInfClasses;
}
/* ------------------------------------------------------------ */
public void setGenerateQuickStart (boolean quickStart)
/**
* If true, a quickstart for the webapp is generated.
*
* @param quickStart if true the quickstart is generated, false otherwise
*/
public void setGenerateQuickStart(boolean quickStart)
{
_isGenerateQuickStart = quickStart;
}
/* ------------------------------------------------------------ */
public boolean isGenerateQuickStart()
{
return _isGenerateQuickStart;
}
/* ------------------------------------------------------------ */
@Override
public void doStart () throws Exception
public void doStart() throws Exception
{
//choose if this will be a quickstart or normal start
// choose if this will be a quickstart or normal start
if (!isGenerateQuickStart() && getQuickStartWebDescriptor() != null)
{
MavenQuickStartConfiguration quickStart = new MavenQuickStartConfiguration();
@ -349,30 +343,30 @@ public class JettyWebAppContext extends WebAppContext
addConfiguration(quickStart);
}
// Set up the pattern that tells us where the jars are that need
// scanning
//Set up the pattern that tells us where the jars are that need scanning
//Allow user to set up pattern for names of jars from the container classpath
//that will be scanned - note that by default NO jars are scanned
// Allow user to set up pattern for names of jars from the container
// classpath
// that will be scanned - note that by default NO jars are scanned
String tmp = _containerIncludeJarPattern;
if (tmp==null || "".equals(tmp))
tmp = (String)getAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN);
if (tmp == null || "".equals(tmp)) tmp = (String) getAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN);
tmp = addPattern(tmp, DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN);
setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, tmp);
//Allow user to set up pattern of jar names from WEB-INF that will be scanned.
//Note that by default ALL jars considered to be in WEB-INF will be scanned - setting
//a pattern restricts scanning
if (_webInfIncludeJarPattern != null)
setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, _webInfIncludeJarPattern);
//Set up the classes dirs that comprises the equivalent of WEB-INF/classes
if (_testClasses != null)
_webInfClasses.add(_testClasses);
if (_classes != null)
_webInfClasses.add(_classes);
// Allow user to set up pattern of jar names from WEB-INF that will be
// scanned.
// Note that by default ALL jars considered to be in WEB-INF will be
// scanned - setting
// a pattern restricts scanning
if (_webInfIncludeJarPattern != null) setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, _webInfIncludeJarPattern);
// Set up the classes dirs that comprises the equivalent of
// WEB-INF/classes
if (_testClasses != null) _webInfClasses.add(_testClasses);
if (_classes != null) _webInfClasses.add(_classes);
// Set up the classpath
_classpathFiles = new ArrayList<>();
_classpathFiles.addAll(_webInfClasses);
@ -384,74 +378,67 @@ public class JettyWebAppContext extends WebAppContext
{
// Return all jar files from class path
String fileName = file.getName();
if (fileName.endsWith(".jar"))
_webInfJarMap.put(fileName, file);
if (fileName.endsWith(".jar")) _webInfJarMap.put(fileName, file);
}
//check for CDI
// check for CDI
initCDI();
// CHECK setShutdown(false);
super.doStart();
}
@Override
protected void loadConfigurations()
{
super.loadConfigurations();
try
{
//inject configurations with config from maven plugin
for (Configuration c:getWebAppConfigurations())
// inject configurations with config from maven plugin
for (Configuration c : getWebAppConfigurations())
{
if (c instanceof EnvConfiguration && getJettyEnvXml() != null)
((EnvConfiguration)c).setJettyEnvXml(Resource.toURL(new File(getJettyEnvXml())));
((EnvConfiguration) c).setJettyEnvXml(Resource.toURL(new File(getJettyEnvXml())));
}
}
catch(Exception e)
catch (Exception e)
{
throw new RuntimeException(e);
}
}
/* ------------------------------------------------------------ */
@Override
public void doStop () throws Exception
{
if (_classpathFiles != null)
_classpathFiles.clear();
public void doStop() throws Exception
{
if (_classpathFiles != null) _classpathFiles.clear();
_classpathFiles = null;
_classes = null;
_testClasses = null;
if (_webInfJarMap != null)
_webInfJarMap.clear();
if (_webInfJarMap != null) _webInfJarMap.clear();
_webInfClasses.clear();
_webInfJars.clear();
// CHECK setShutdown(true);
//just wait a little while to ensure no requests are still being processed
// just wait a little while to ensure no requests are still being
// processed
Thread.currentThread().sleep(500L);
super.doStop();
//remove all listeners, servlets and filters. This is because we will re-apply
//any context xml file, which means they would potentially be added multiple times.
// remove all listeners, servlets and filters. This is because we will
// re-apply
// any context xml file, which means they would potentially be added
// multiple times.
setEventListeners(new EventListener[0]);
getServletHandler().setFilters(new FilterHolder[0]);
getServletHandler().setFilterMappings(new FilterMapping[0]);
getServletHandler().setServlets(new ServletHolder[0]);
getServletHandler().setServletMappings(new ServletMapping[0]);
}
/* ------------------------------------------------------------ */
@Override
public Resource getResource(String uriInContext) throws MalformedURLException
{
@ -459,57 +446,54 @@ public class JettyWebAppContext extends WebAppContext
// Try to get regular resource
resource = super.getResource(uriInContext);
// If no regular resource exists check for access to /WEB-INF/lib or /WEB-INF/classes
// If no regular resource exists check for access to /WEB-INF/lib or
// /WEB-INF/classes
if ((resource == null || !resource.exists()) && uriInContext != null && _classes != null)
{
String uri = URIUtil.canonicalPath(uriInContext);
if (uri == null)
return null;
if (uri == null) return null;
try
{
// Replace /WEB-INF/classes with candidates for the classpath
if (uri.startsWith(WEB_INF_CLASSES_PREFIX))
{
if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX+"/"))
if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX + "/"))
{
//exact match for a WEB-INF/classes, so preferentially return the resource matching the web-inf classes
//rather than the test classes
// exact match for a WEB-INF/classes, so preferentially
// return the resource matching the web-inf classes
// rather than the test classes
if (_classes != null)
return Resource.newResource(_classes);
else if (_testClasses != null)
return Resource.newResource(_testClasses);
else if (_testClasses != null) return Resource.newResource(_testClasses);
}
else
{
//try matching
// try matching
Resource res = null;
int i=0;
int i = 0;
while (res == null && (i < _webInfClasses.size()))
{
String newPath = uri.replace(WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath());
res = Resource.newResource(newPath);
if (!res.exists())
{
res = null;
res = null;
i++;
}
}
return res;
}
}
}
else if (uri.startsWith(WEB_INF_LIB_PREFIX))
{
// Return the real jar file for all accesses to
// /WEB-INF/lib/*.jar
String jarName = uri.replace(WEB_INF_LIB_PREFIX, "");
if (jarName.startsWith("/") || jarName.startsWith("\\"))
jarName = jarName.substring(1);
if (jarName.length()==0)
return null;
if (jarName.startsWith("/") || jarName.startsWith("\\")) jarName = jarName.substring(1);
if (jarName.length() == 0) return null;
File jarFile = _webInfJarMap.get(jarName);
if (jarFile != null)
return Resource.newResource(jarFile.getPath());
if (jarFile != null) return Resource.newResource(jarFile.getPath());
return null;
}
@ -525,21 +509,20 @@ public class JettyWebAppContext extends WebAppContext
}
return resource;
}
/* ------------------------------------------------------------ */
@Override
public Set<String> getResourcePaths(String path)
{
// Try to get regular resource paths - this will get appropriate paths from any overlaid wars etc
// Try to get regular resource paths - this will get appropriate paths
// from any overlaid wars etc
Set<String> paths = super.getResourcePaths(path);
if (path != null)
{
TreeSet<String> allPaths = new TreeSet<>();
allPaths.addAll(paths);
//add in the dependency jars as a virtual WEB-INF/lib entry
// add in the dependency jars as a virtual WEB-INF/lib entry
if (path.startsWith(WEB_INF_LIB_PREFIX))
{
for (String fileName : _webInfJarMap.keySet())
@ -550,8 +533,8 @@ public class JettyWebAppContext extends WebAppContext
}
else if (path.startsWith(WEB_INF_CLASSES_PREFIX))
{
int i=0;
int i = 0;
while (i < _webInfClasses.size())
{
String newPath = path.replace(WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath());
@ -563,35 +546,31 @@ public class JettyWebAppContext extends WebAppContext
}
return paths;
}
/* ------------------------------------------------------------ */
public String addPattern (String s, String pattern)
public String addPattern(String s, String pattern)
{
if (s == null)
s = "";
else
s = s.trim();
if (!s.contains(pattern))
{
if (s.length() != 0)
s = s + "|";
if (s.length() != 0) s = s + "|";
s = s + pattern;
}
return s;
}
/* ------------------------------------------------------------ */
public void initCDI()
{
Class cdiInitializer = null;
try
{
cdiInitializer = Thread.currentThread().getContextClassLoader().loadClass("org.eclipse.jetty.cdi.servlet.JettyWeldInitializer");
Method initWebAppMethod = cdiInitializer.getMethod("initWebApp", new Class[]{WebAppContext.class});
initWebAppMethod.invoke(null, new Object[]{this});
Method initWebAppMethod = cdiInitializer.getMethod("initWebApp", new Class[] { WebAppContext.class });
initWebAppMethod.invoke(null, new Object[] { this });
}
catch (ClassNotFoundException e)
{
@ -603,7 +582,7 @@ public class JettyWebAppContext extends WebAppContext
}
catch (Exception e)
{
LOG.warn("Problem initializing cdi", e);
LOG.warn("Problem initializing cdi", e);
}
}
}

View File

@ -26,9 +26,9 @@ import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlParser;
/**
* Preconfigure DescriptorProcessor
* ExtraXmlDescriptorProcessor
*
* Saves literal XML snippets
* Saves literal XML snippets from web.xml.
*
*/