From 976ffb7ecd3c91286c9941f7a03eaacb196f994e Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 23 Mar 2017 11:24:22 +1100 Subject: [PATCH] Issue #877 And also #1117 --- .../maven/plugin/JettyEffectiveWebXml.java | 5 +- .../maven/plugin/JettyWebAppContext.java | 36 +++++++++- .../QuickStartDescriptorGenerator.java | 25 ++++++- .../QuickStartDescriptorProcessor.java | 67 ++++++++++++++++++- .../jetty/quickstart/QuickStartWebApp.java | 34 +++++++++- .../org/eclipse/jetty/webapp/MetaData.java | 2 +- 6 files changed, 159 insertions(+), 10 deletions(-) diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java index 49577dbde47..30785b43419 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyEffectiveWebXml.java @@ -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) diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java index 4cbc442abc2..638f8bf9996 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyWebAppContext.java @@ -102,6 +102,8 @@ public class JettyWebAppContext extends WebAppContext private String _jettyEnvXml; private List _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 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); diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java index 1f4dac3363f..b9380e2eb94 100644 --- a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java +++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java @@ -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 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++)); } } diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java index 58713a7e190..6b6a6bd3fe9 100644 --- a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java +++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java @@ -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 libs = new ArrayList<>(); diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartWebApp.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartWebApp.java index c3719c134c5..89af1b996db 100644 --- a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartWebApp.java +++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartWebApp.java @@ -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); diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java index 5cb75e83d61..93efb31bf84 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java @@ -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(); } }