Move JavaVersion to a reusable location.
This commit is contained in:
parent
2c2b9be4be
commit
0ccd57285f
|
|
@ -22,9 +22,8 @@ import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.springframework.boot.system.JavaVersion;
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Conditional} that matches based on the JVM version the application is running
|
* {@link Conditional} that matches based on the JVM version the application is running
|
||||||
|
|
@ -73,69 +72,4 @@ public @interface ConditionalOnJava {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Java versions.
|
|
||||||
*/
|
|
||||||
enum JavaVersion {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Java 1.9.
|
|
||||||
*/
|
|
||||||
NINE(9, "1.9", "java.security.cert.URICertStoreParameters"),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Java 1.8.
|
|
||||||
*/
|
|
||||||
EIGHT(8, "1.8", "java.util.function.Function");
|
|
||||||
|
|
||||||
private final int value;
|
|
||||||
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
private final boolean available;
|
|
||||||
|
|
||||||
JavaVersion(int value, String name, String className) {
|
|
||||||
this.value = value;
|
|
||||||
this.name = name;
|
|
||||||
this.available = ClassUtils.isPresent(className, getClass().getClassLoader());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines if this version is within the specified range of versions.
|
|
||||||
* @param range the range
|
|
||||||
* @param version the bounds of the range
|
|
||||||
* @return if this version is within the specified range
|
|
||||||
*/
|
|
||||||
public boolean isWithin(Range range, JavaVersion version) {
|
|
||||||
Assert.notNull(range, "Range must not be null");
|
|
||||||
Assert.notNull(version, "Version must not be null");
|
|
||||||
switch (range) {
|
|
||||||
case EQUAL_OR_NEWER:
|
|
||||||
return this.value >= version.value;
|
|
||||||
case OLDER_THAN:
|
|
||||||
return this.value < version.value;
|
|
||||||
}
|
|
||||||
throw new IllegalStateException("Unknown range " + range);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the {@link JavaVersion} of the current runtime.
|
|
||||||
* @return the {@link JavaVersion}
|
|
||||||
*/
|
|
||||||
public static JavaVersion getJavaVersion() {
|
|
||||||
for (JavaVersion candidate : JavaVersion.values()) {
|
|
||||||
if (candidate.available) {
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return EIGHT;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ package org.springframework.boot.autoconfigure.condition;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
|
||||||
|
import org.springframework.boot.system.JavaVersion;
|
||||||
import org.springframework.context.annotation.Condition;
|
import org.springframework.context.annotation.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
|
@ -51,7 +51,7 @@ class OnJavaCondition extends SpringBootCondition {
|
||||||
|
|
||||||
protected ConditionOutcome getMatchOutcome(Range range, JavaVersion runningVersion,
|
protected ConditionOutcome getMatchOutcome(Range range, JavaVersion runningVersion,
|
||||||
JavaVersion version) {
|
JavaVersion version) {
|
||||||
boolean match = runningVersion.isWithin(range, version);
|
boolean match = isWithin(runningVersion, range, version);
|
||||||
String expected = String.format(
|
String expected = String.format(
|
||||||
range == Range.EQUAL_OR_NEWER ? "(%s or newer)" : "(older than %s)",
|
range == Range.EQUAL_OR_NEWER ? "(%s or newer)" : "(older than %s)",
|
||||||
version);
|
version);
|
||||||
|
|
@ -61,4 +61,22 @@ class OnJavaCondition extends SpringBootCondition {
|
||||||
return new ConditionOutcome(match, message);
|
return new ConditionOutcome(match, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if the {@code runningVersion} is within the specified range of versions.
|
||||||
|
* @param runningVersion the current version.
|
||||||
|
* @param range the range
|
||||||
|
* @param version the bounds of the range
|
||||||
|
* @return if this version is within the specified range
|
||||||
|
*/
|
||||||
|
private boolean isWithin(JavaVersion runningVersion, Range range, JavaVersion version) {
|
||||||
|
int i = runningVersion.compareTo(version);
|
||||||
|
if (range == Range.EQUAL_OR_NEWER) {
|
||||||
|
return i >= 0;
|
||||||
|
}
|
||||||
|
else if (range == Range.OLDER_THAN) {
|
||||||
|
return i < 0;
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("Unknown range " + range);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ import java.util.function.Function;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
|
||||||
|
import org.springframework.boot.system.JavaVersion;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -107,7 +107,7 @@ public class ConditionalOnJavaTests {
|
||||||
URL[] urls = ((URLClassLoader) getClass().getClassLoader()).getURLs();
|
URL[] urls = ((URLClassLoader) getClass().getClassLoader()).getURLs();
|
||||||
URLClassLoader classLoader = new ClassHidingClassLoader(urls, hiddenClasses);
|
URLClassLoader classLoader = new ClassHidingClassLoader(urls, hiddenClasses);
|
||||||
Class<?> javaVersionClass = classLoader
|
Class<?> javaVersionClass = classLoader
|
||||||
.loadClass(ConditionalOnJava.JavaVersion.class.getName());
|
.loadClass(JavaVersion.class.getName());
|
||||||
Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass,
|
Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass,
|
||||||
"getJavaVersion");
|
"getJavaVersion");
|
||||||
Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
|
Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.system;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supported Java versions.
|
||||||
|
*
|
||||||
|
* @author Oliver Gierke
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public enum JavaVersion {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java 1.8.
|
||||||
|
*/
|
||||||
|
EIGHT("1.8", "java.util.function.Function"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java 1.9.
|
||||||
|
*/
|
||||||
|
NINE("1.9", "java.security.cert.URICertStoreParameters");
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
private final boolean available;
|
||||||
|
|
||||||
|
JavaVersion(String name, String className) {
|
||||||
|
this.name = name;
|
||||||
|
this.available = ClassUtils.isPresent(className, getClass().getClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the {@link JavaVersion} of the current runtime.
|
||||||
|
* @return the {@link JavaVersion}
|
||||||
|
*/
|
||||||
|
public static JavaVersion getJavaVersion() {
|
||||||
|
List<JavaVersion> candidates = Arrays.asList(JavaVersion.values());
|
||||||
|
Collections.reverse(candidates);
|
||||||
|
for (JavaVersion candidate : candidates) {
|
||||||
|
if (candidate.available) {
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return EIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.system;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link JavaVersion}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class JavaVersionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void currentVersionIsAvailable() {
|
||||||
|
assertThat(JavaVersion.getJavaVersion()).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void java8IsOlderThanJava9() {
|
||||||
|
assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.NINE)).isLessThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void java9IsNewerThanJava8() {
|
||||||
|
assertThat(JavaVersion.NINE.compareTo(JavaVersion.EIGHT)).isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void comparisonOfSameVersion() {
|
||||||
|
assertThat(JavaVersion.EIGHT.compareTo(JavaVersion.EIGHT)).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue