Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Wilkins2016-01-13 19:00:06 -0500
committerGreg Wilkins2016-01-13 19:00:06 -0500
commitc5cbc64e7799409101e6dd28c143dbea397724cf (patch)
tree6505450207f77caf51950534b5c5cfa22e7fae82 /jetty-quickstart
parent122ac9c4459818313bed8bf1663680b6cc93f29d (diff)
parent6e85554aadf6bb0e4904a93f0841c9d23c346efc (diff)
downloadorg.eclipse.jetty.project-c5cbc64e7799409101e6dd28c143dbea397724cf.tar.gz
org.eclipse.jetty.project-c5cbc64e7799409101e6dd28c143dbea397724cf.tar.xz
org.eclipse.jetty.project-c5cbc64e7799409101e6dd28c143dbea397724cf.zip
Merge remote-tracking branch 'origin/jetty-9.3.x'
Diffstat (limited to 'jetty-quickstart')
-rw-r--r--jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java157
-rw-r--r--jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorGenerator.java42
-rw-r--r--jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/QuickStartDescriptorProcessor.java9
3 files changed, 193 insertions, 15 deletions
diff --git a/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java
new file mode 100644
index 0000000000..8494386324
--- /dev/null
+++ b/jetty-quickstart/src/main/java/org/eclipse/jetty/quickstart/AttributeNormalizer.java
@@ -0,0 +1,157 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.quickstart;
+
+import java.io.File;
+import java.net.URI;
+import java.net.URL;
+import java.nio.file.Path;
+
+import org.eclipse.jetty.util.URIUtil;
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
+import org.eclipse.jetty.util.resource.Resource;
+
+/**
+ * Normalize Attribute to String.
+ * <p>Replaces and expands:
+ * <ul>
+ * <li>${WAR}</li>
+ * <li>${jetty.base}</li>
+ * <li>${jetty.home}</li>
+ * <li>${user.home}</li>
+ * <li>${user.dir}</li>
+ * </ul>
+ */
+public class AttributeNormalizer
+{
+ private static final Logger LOG = Log.getLogger(AttributeNormalizer.class);
+ private final Path _warPath;
+ private final Path _jettyBasePath;
+ private final Path _jettyHomePath;
+ private final Path _userHomePath;
+ private final Path _userDirPath;
+
+
+ public AttributeNormalizer(Resource baseResource)
+ {
+ try
+ {
+ _warPath=baseResource==null?null:baseResource.getFile().toPath();
+ _jettyBasePath=systemPath("jetty.base");
+ _jettyHomePath=systemPath("jetty.home");
+ _userHomePath=systemPath("user.home");
+ _userDirPath=systemPath("user.dir");
+ }
+ catch(Exception e)
+ {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ private static Path systemPath(String property) throws Exception
+ {
+ String p=System.getProperty(property);
+ if (p!=null)
+ return new File(p).getAbsoluteFile().getCanonicalFile().toPath();
+ return null;
+ }
+
+ public String normalize(Object o)
+ {
+ try
+ {
+ // Find a URI
+ URI uri=null;
+ if (o instanceof URI)
+ uri=(URI)o;
+ else if (o instanceof URL)
+ uri = ((URL)o).toURI();
+ else if (o instanceof File)
+ uri = ((File)o).toURI();
+ else
+ {
+ String s=o.toString();
+ uri=new URI(s);
+ if (uri.getScheme()==null)
+ return s;
+ }
+
+ if ("jar".equalsIgnoreCase(uri.getScheme()))
+ {
+ String raw = uri.getRawSchemeSpecificPart();
+ int bang=raw.indexOf("!/");
+ String normal=normalize(raw.substring(0,bang));
+ String suffix=raw.substring(bang);
+ return "jar:"+normal+suffix;
+ }
+ else if ("file".equalsIgnoreCase(uri.getScheme()))
+ {
+ return "file:"+normalizePath(new File(uri).toPath());
+ }
+
+ }
+ catch(Exception e)
+ {
+ LOG.warn(e);
+ }
+ return String.valueOf(o);
+ }
+
+ public String normalizePath(Path path)
+ {
+ if (_warPath!=null && path.startsWith(_warPath))
+ return URIUtil.addPaths("${WAR}",_warPath.relativize(path).toString());
+ if (_jettyBasePath!=null && path.startsWith(_jettyBasePath))
+ return URIUtil.addPaths("${jetty.base}",_jettyBasePath.relativize(path).toString());
+ if (_jettyHomePath!=null && path.startsWith(_jettyHomePath))
+ return URIUtil.addPaths("${jetty.home}",_jettyHomePath.relativize(path).toString());
+ if (_userHomePath!=null && path.startsWith(_userHomePath))
+ return URIUtil.addPaths("${user.home}",_userHomePath.relativize(path).toString());
+ if (_userDirPath!=null && path.startsWith(_userDirPath))
+ return URIUtil.addPaths("${user.dir}",_userDirPath.relativize(path).toString());
+
+ return path.toString();
+ }
+
+
+ public String expand(String s)
+ {
+ int i=s.indexOf("${");
+ if (i<0)
+ return s;
+ int e=s.indexOf('}',i+3);
+ String prop=s.substring(i+2,e);
+ switch(prop)
+ {
+ case "WAR":
+ return s.substring(0,i)+_warPath+expand(s.substring(e+1));
+ case "jetty.base":
+ return s.substring(0,i)+_jettyBasePath+expand(s.substring(e+1));
+ case "jetty.home":
+ return s.substring(0,i)+_jettyHomePath+expand(s.substring(e+1));
+ case "user.home":
+ return s.substring(0,i)+_userHomePath+expand(s.substring(e+1));
+ case "user.dir":
+ return s.substring(0,i)+_userDirPath+expand(s.substring(e+1));
+ default:
+ return s;
+ }
+ }
+}
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 5db988d893..34c401222a 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
@@ -22,6 +22,9 @@ package org.eclipse.jetty.quickstart;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.EventListener;
@@ -55,6 +58,7 @@ import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
+import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.webapp.MetaData;
import org.eclipse.jetty.webapp.MetaData.OriginInfo;
@@ -128,16 +132,16 @@ public class QuickStartDescriptorGenerator
// Set some special context parameters
// The location of the war file on disk
- String resourceBase = _webApp.getBaseResource().getFile().getCanonicalFile().getAbsoluteFile().toURI().toString();
+ AttributeNormalizer normalizer = new AttributeNormalizer(_webApp.getBaseResource());
// The library order
addContextParamFromAttribute(out,ServletContext.ORDERED_LIBS);
//the servlet container initializers
addContextParamFromAttribute(out,AnnotationConfiguration.CONTAINER_INITIALIZERS);
//the tlds discovered
- addContextParamFromAttribute(out,MetaInfConfiguration.METAINF_TLDS,resourceBase);
+ addContextParamFromAttribute(out,MetaInfConfiguration.METAINF_TLDS,normalizer);
//the META-INF/resources discovered
- addContextParamFromAttribute(out,MetaInfConfiguration.METAINF_RESOURCES,resourceBase);
+ addContextParamFromAttribute(out,MetaInfConfiguration.METAINF_RESOURCES,normalizer);
// init params
@@ -515,7 +519,27 @@ public class QuickStartDescriptorGenerator
*/
private void addContextParamFromAttribute(XmlAppendable out, String attribute) throws IOException
{
- addContextParamFromAttribute(out,attribute,null);
+ Object o = _webApp.getAttribute(attribute);
+ if (o == null)
+ return;
+
+ Collection<?> c = (o instanceof Collection)? (Collection<?>)o:Collections.singletonList(o);
+ StringBuilder v=new StringBuilder();
+ for (Object i:c)
+ {
+ if (i!=null)
+ {
+ if (v.length()>0)
+ v.append(",\n ");
+ else
+ v.append("\n ");
+ QuotedStringTokenizer.quote(v,i.toString());
+ }
+ }
+ out.openTag("context-param")
+ .tag("param-name",attribute)
+ .tagCDATA("param-value",v.toString())
+ .closeTag();
}
/**
@@ -526,7 +550,7 @@ public class QuickStartDescriptorGenerator
* @param resourceBase
* @throws IOException
*/
- private void addContextParamFromAttribute(XmlAppendable out, String attribute, String resourceBase) throws IOException
+ private void addContextParamFromAttribute(XmlAppendable out, String attribute, AttributeNormalizer normalizer) throws IOException
{
Object o = _webApp.getAttribute(attribute);
if (o == null)
@@ -542,16 +566,14 @@ public class QuickStartDescriptorGenerator
v.append(",\n ");
else
v.append("\n ");
- if (resourceBase==null)
- QuotedStringTokenizer.quote(v,i.toString());
- else
- QuotedStringTokenizer.quote(v,i.toString().replace(resourceBase,"${WAR}/"));
+ QuotedStringTokenizer.quote(v,normalizer.normalize(i));
}
}
out.openTag("context-param")
.tag("param-name",attribute)
.tagCDATA("param-value",v.toString())
- .closeTag();
+ .closeTag();
+
}
/**
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 924b0dd5f9..35d41ea17a 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
@@ -99,6 +99,7 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
values.add(value);
}
+ AttributeNormalizer normalizer = new AttributeNormalizer(context.getBaseResource());
// handle values
switch(name)
{
@@ -125,15 +126,14 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
case MetaInfConfiguration.METAINF_TLDS:
{
List<Object> tlds = new ArrayList<>();
- String war=context.getBaseResource().getURI().toString();
Object o=context.getAttribute(MetaInfConfiguration.METAINF_TLDS);
if (o instanceof Collection<?>)
tlds.addAll((Collection<?>)o);
for (String i : values)
{
- Resource r = Resource.newResource(i.replace("${WAR}/",war));
+ Resource r = Resource.newResource(normalizer.expand(i));
if (r.exists())
- tlds.add(r.getURL());
+ tlds.add(r.getURI().toURL());
else
throw new IllegalArgumentException("TLD not found: "+r);
}
@@ -145,10 +145,9 @@ public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
case MetaInfConfiguration.METAINF_RESOURCES:
{
- String war=context.getBaseResource().getURI().toString();
for (String i : values)
{
- Resource r = Resource.newResource(i.replace("${WAR}/",war));
+ Resource r = Resource.newResource(normalizer.expand(i));
if (r.exists())
visitMetaInfResource(context,r);
else

Back to the top