Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
0ccd5728
Commit
0ccd5728
authored
Sep 28, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move JavaVersion to a reusable location.
parent
2c2b9be4
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
146 additions
and
71 deletions
+146
-71
ConditionalOnJava.java
...ework/boot/autoconfigure/condition/ConditionalOnJava.java
+1
-67
OnJavaCondition.java
...amework/boot/autoconfigure/condition/OnJavaCondition.java
+20
-2
ConditionalOnJavaTests.java
.../boot/autoconfigure/condition/ConditionalOnJavaTests.java
+2
-2
JavaVersion.java
...ain/java/org/springframework/boot/system/JavaVersion.java
+73
-0
JavaVersionTests.java
...ava/org/springframework/boot/system/JavaVersionTests.java
+50
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJava.java
View file @
0ccd5728
...
@@ -22,9 +22,8 @@ import java.lang.annotation.Retention;
...
@@ -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 {
...
@@ -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
;
}
}
}
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnJavaCondition.java
View file @
0ccd5728
...
@@ -18,8 +18,8 @@ package org.springframework.boot.autoconfigure.condition;
...
@@ -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 {
...
@@ -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 {
...
@@ -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
);
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJavaTests.java
View file @
0ccd5728
...
@@ -27,8 +27,8 @@ import java.util.function.Function;
...
@@ -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 {
...
@@ -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
);
...
...
spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java
0 → 100644
View file @
0ccd5728
/*
* 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
;
}
}
spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java
0 → 100644
View file @
0ccd5728
/*
* 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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment