And also #1117
This commit is contained in:
Jan Bartel 2017-03-23 11:24:22 +11:00
parent ee59b42321
commit 976ffb7ecd
6 changed files with 159 additions and 10 deletions

View File

@ -49,13 +49,14 @@ public class JettyEffectiveWebXml extends JettyRunMojo
protected File target;
/**
* The target directory
* The name of the file to generate into
*
* @parameter
*/
protected File effectiveWebXml;
protected boolean deleteOnExit = true;
@ -91,12 +92,12 @@ public class JettyEffectiveWebXml extends JettyRunMojo
//ensure config of the webapp based on settings in plugin
configureWebApplication();
//set the webapp up to do very little other than generate the quickstart-web.xml
webApp.setCopyWebDir(false);
webApp.setCopyWebInf(false);
webApp.setGenerateQuickStart(true);
//if the user didn't nominate a file to generate into, pick the name and
//make sure that it is deleted on exit
if (webApp.getQuickStartWebDescriptor() == null)

View File

@ -102,6 +102,8 @@ public class JettyWebAppContext extends WebAppContext
private String _jettyEnvXml;
private List<Overlay> _overlays;
private Resource _quickStartWebXml;
private String _originAttribute;
private boolean _generateOrigin;
@ -227,6 +229,38 @@ public class JettyWebAppContext extends WebAppContext
_overlays = overlays;
}
/**
* @return the originAttribute
*/
public String getOriginAttribute()
{
return _originAttribute;
}
/**
* @param originAttribute the originAttribute to set
*/
public void setOriginAttribute(String originAttribute)
{
_originAttribute = originAttribute;
}
/**
* @return the generateOrigin
*/
public boolean isGenerateOrigin()
{
return _generateOrigin;
}
/**
* @param generateOrigin the generateOrigin to set
*/
public void setGenerateOrigin(boolean generateOrigin)
{
_generateOrigin = generateOrigin;
}
/* ------------------------------------------------------------ */
public List<Overlay> getOverlays()
{
@ -311,7 +345,7 @@ public class JettyWebAppContext extends WebAppContext
if (getQuickStartWebDescriptor() == null)
throw new IllegalStateException ("No location to generate quickstart descriptor");
QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, _preconfigProcessor.getXML());
QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, _preconfigProcessor.getXML(), _originAttribute, _generateOrigin);
try (FileOutputStream fos = new FileOutputStream(getQuickStartWebDescriptor().getFile()))
{
generator.generateQuickStartWebXml(fos);

View File

@ -56,6 +56,7 @@ import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
@ -76,10 +77,15 @@ public class QuickStartDescriptorGenerator
{
private static final Logger LOG = Log.getLogger(QuickStartDescriptorGenerator.class);
public static final String ORIGIN = "org.eclipse.jetty.originAttribute";
public static final String DEFAULT_QUICKSTART_DESCRIPTOR_NAME = "quickstart-web.xml";
public static final String DEFAULT_ORIGIN_ATTRIBUTE_NAME = "origin";
protected WebAppContext _webApp;
protected String _extraXML;
protected String _originAttribute;
protected boolean _generateOrigin;
protected int _count;
@ -87,10 +93,13 @@ public class QuickStartDescriptorGenerator
* @param w the source WebAppContext
* @param extraXML any extra xml snippet to append
*/
public QuickStartDescriptorGenerator (WebAppContext w, String extraXML)
public QuickStartDescriptorGenerator (WebAppContext w, String extraXML, String originAttribute, boolean generateOrigin)
{
_webApp = w;
_extraXML = extraXML;
_originAttribute = (StringUtil.isBlank(originAttribute)?DEFAULT_ORIGIN_ATTRIBUTE_NAME:originAttribute);
_generateOrigin = generateOrigin || LOG.isDebugEnabled();
_count = 0;
}
@ -144,6 +153,16 @@ public class QuickStartDescriptorGenerator
addContextParamFromAttribute(out,MetaInfConfiguration.METAINF_RESOURCES,normalizer);
//add the name of the origin attribute, if it is being used
if (_generateOrigin)
{
out.openTag("context-param")
.tag("param-name", ORIGIN)
.tag("param-value", _originAttribute)
.closeTag();
}
// init params
for (String p : _webApp.getInitParams().keySet())
out.openTag("context-param",origin(md,"context-param." + p))
@ -694,7 +713,7 @@ public class QuickStartDescriptorGenerator
*/
public Map<String, String> origin(MetaData md, String name)
{
if (!LOG.isDebugEnabled())
if (!_generateOrigin)
return Collections.emptyMap();
if (name == null)
return Collections.emptyMap();
@ -702,7 +721,7 @@ public class QuickStartDescriptorGenerator
if (LOG.isDebugEnabled()) LOG.debug("origin of "+name+" is "+origin);
if (origin == null)
return Collections.emptyMap();
return Collections.singletonMap("origin",origin.toString());
return Collections.singletonMap(_originAttribute,origin.toString()+":"+(_count++));
}
}

View File

@ -28,9 +28,12 @@ import javax.servlet.ServletContext;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.webapp.DefaultsDescriptor;
import org.eclipse.jetty.webapp.Descriptor;
import org.eclipse.jetty.webapp.IterativeDescriptorProcessor;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
@ -44,11 +47,18 @@ import org.eclipse.jetty.xml.XmlParser;
*/
public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
{
private String _originAttributeName = null;
/**
*
*/
public QuickStartDescriptorProcessor()
{
try
{
registerVisitor("context-param", this.getClass().getMethod("visitContextParam", __signature));
registerVisitor("servlet-mapping", this.getClass().getMethod("visitServletMapping", __signature));
}
catch (Exception e)
{
@ -62,6 +72,7 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
@Override
public void start(WebAppContext context, Descriptor descriptor)
{
_originAttributeName = context.getInitParameter(QuickStartDescriptorGenerator.ORIGIN);
}
/**
@ -70,8 +81,50 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
@Override
public void end(WebAppContext context, Descriptor descriptor)
{
_originAttributeName = null;
}
/**
* Process a servlet-mapping element
*
* @param context the webapp
* @param descriptor the xml file to process
* @param node the servlet-mapping element in the xml file to process
*/
public void visitServletMapping(WebAppContext context, Descriptor descriptor, XmlParser.Node node)
{
String servletName = node.getString("servlet-name", false, true);
ServletMapping mapping = null;
ServletMapping[] mappings = context.getServletHandler().getServletMappings();
if (mappings != null)
{
for (ServletMapping m:mappings)
{
if (servletName.equals(m.getServletName()))
{
mapping = m;
break;
}
}
}
if (mapping != null && _originAttributeName != null)
{
String origin = node.getAttribute(_originAttributeName);
if (!StringUtil.isBlank(origin) && origin.startsWith(DefaultsDescriptor.class.getSimpleName()))
mapping.setDefault(true);
}
}
/**
* Process a context-param element
* @param context the webapp
* @param descriptor the xml file to process
* @param node the context-param node in the xml file
* @throws Exception
*/
public void visitContextParam (WebAppContext context, Descriptor descriptor, XmlParser.Node node)
throws Exception
{
@ -82,11 +135,16 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
// extract values
switch(name)
{
case QuickStartDescriptorGenerator.ORIGIN:
{
//value already contains what we need
break;
}
case ServletContext.ORDERED_LIBS:
case AnnotationConfiguration.CONTAINER_INITIALIZERS:
case MetaInfConfiguration.METAINF_TLDS:
case MetaInfConfiguration.METAINF_RESOURCES:
{
context.removeAttribute(name);
QuotedStringTokenizer tok = new QuotedStringTokenizer(value,",");
@ -94,7 +152,7 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
values.add(tok.nextToken().trim());
break;
}
default:
values.add(value);
}
@ -103,6 +161,11 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
// handle values
switch(name)
{
case QuickStartDescriptorGenerator.ORIGIN:
{
context.setAttribute(QuickStartDescriptorGenerator.ORIGIN, value);
break;
}
case ServletContext.ORDERED_LIBS:
{
List<Object> libs = new ArrayList<>();

View File

@ -46,6 +46,9 @@ public class QuickStartWebApp extends WebAppContext
private boolean _autoPreconfigure=false;
private boolean _startWebapp=false;
private PreconfigureDescriptorProcessor _preconfigProcessor;
private String _originAttribute;
private boolean _generateOrigin;
public static final String[] __preconfigurationClasses = new String[]
{
@ -89,6 +92,35 @@ public class QuickStartWebApp extends WebAppContext
_autoPreconfigure = autoPrecompile;
}
public void setOriginAttribute (String name)
{
_originAttribute = name;
}
/**
* @return the originAttribute
*/
public String getOriginAttribute()
{
return _originAttribute;
}
/**
* @return the generateOrigin
*/
public boolean getGenerateOrigin()
{
return _generateOrigin;
}
/**
* @param generateOrigin the generateOrigin to set
*/
public void setGenerateOrigin(boolean generateOrigin)
{
_generateOrigin = generateOrigin;
}
@Override
protected void startWebapp() throws Exception
{
@ -174,7 +206,7 @@ public class QuickStartWebApp extends WebAppContext
Resource descriptor = getWebInf().addPath(QuickStartDescriptorGenerator.DEFAULT_QUICKSTART_DESCRIPTOR_NAME);
if (!descriptor.exists())
descriptor.getFile().createNewFile();
QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, extraXML);
QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, extraXML, _originAttribute, _generateOrigin);
try (FileOutputStream fos = new FileOutputStream(descriptor.getFile()))
{
generator.generateQuickStartWebXml(fos);

View File

@ -125,7 +125,7 @@ public class MetaData
if (descriptor!=null)
return descriptor.toString();
if (annotation!=null)
return "@"+annotation.annotationType().getSimpleName()+" on "+annotated.getName();
return "@"+annotation.annotationType().getSimpleName()+"("+annotated.getName()+")";
return origin.toString();
}
}