Safer simpler version parsing #2284

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-03-15 22:49:20 +11:00
parent 28489abc3a
commit 4ce7e9591b
4 changed files with 34 additions and 25 deletions

View File

@ -371,7 +371,7 @@ public class Server extends HandlerWrapper implements Attributes
String gitHash = Jetty.GIT_HASH;
String timestamp = Jetty.BUILD_TIMESTAMP;
LOG.info("jetty-{}; built: {}; git: {}; jvm {}", getVersion(), timestamp, gitHash, JavaVersion.VERSION);
LOG.info("jetty-{}; built: {}; git: {}; jvm {}", getVersion(), timestamp, gitHash, System.getProperty("java.runtime.version",System.getProperty("java.version")));
if (!Jetty.STABLE)
{
LOG.warn("THIS IS NOT A STABLE RELEASE! DO NOT USE IN PRODUCTION!");

View File

@ -1265,12 +1265,11 @@ public class StartArgs
try
{
JavaVersion ver = JavaVersion.parse(value);
properties.setProperty("java.version",ver.getVersion(),source);
properties.setProperty("java.version",System.getProperty("java.version"),source);
properties.setProperty("java.version.platform",Integer.toString(ver.getPlatform()),source);
properties.setProperty("java.version.major",Integer.toString(ver.getMajor()),source);
properties.setProperty("java.version.minor",Integer.toString(ver.getMinor()),source);
properties.setProperty("java.version.micro",Integer.toString(ver.getMicro()),source);
properties.setProperty("java.version.update",Integer.toString(ver.getUpdate()),source);
}
catch (Throwable x)
{

View File

@ -33,34 +33,33 @@ public class JavaVersion
*/
public static final String JAVA_TARGET_PLATFORM = "org.eclipse.jetty.javaTargetPlatform";
/** Regex for Java version numbers */
private static final String VSTR_FORMAT = "(?<VNUM>[1-9][0-9]*(?:(?:\\.0)*\\.[0-9]+)*).*";
static final Pattern VSTR_PATTERN = Pattern.compile(VSTR_FORMAT);
public static final JavaVersion VERSION = parse(System.getProperty("java.runtime.version",System.getProperty("java.version","1.8")));
public static final JavaVersion VERSION = parse(System.getProperty("java.version"));
public static JavaVersion parse(String v)
{
Matcher m = VSTR_PATTERN.matcher(v);
if (!m.matches() || m.group("VNUM")==null)
{
System.err.println("ERROR: Invalid version string: '" + v + "'");
return new JavaVersion(v+"-UNKNOWN",8,1,8,0);
}
// $VNUM is a dot-separated list of integers of arbitrary length
String[] split = m.group("VNUM").split("\\.");
int[] version = new int[split.length];
for (int i = 0; i < split.length; i++)
version[i] = Integer.parseInt(split[i]);
String[] split = v.split("[^0-9]");
int len = Math.min(split.length,3);
int[] version = new int[len];
for (int i = 0; i < len; i++)
{
try
{
version[i] = Integer.parseInt(split[i]);
}
catch(Throwable e)
{
len = i-1;
break;
}
}
return new JavaVersion(
v,
(version[0]>=9 || version.length==1)?version[0]:version[1],
(version[0]>=9 || len==1)?version[0]:version[1],
version[0],
version.length>1?version[1]:0,
version.length>2?version[2]:0);
len>1?version[1]:0,
len>2?version[2]:0);
}
private final String version;

View File

@ -28,6 +28,17 @@ import org.junit.Test;
*/
public class JavaVersionTest
{
@Test
public void testAndroid()
{
JavaVersion version = JavaVersion.parse("0.9");
assertThat(version.toString(),is("0.9"));
assertThat(version.getPlatform(),is(9));
assertThat(version.getMajor(),is(0));
assertThat(version.getMinor(),is(9));
assertThat(version.getMicro(),is(0));
}
@Test
public void test9()
{