Skip to content

Commit e6dc0bc

Browse files
committed
GH-2814 Add version numbers of currently ruunning stream/function
Version information is addedc to the standard Spring Boot banner Resolves #2814
1 parent f919d26 commit e6dc0bc

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2019-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package org.springframework.cloud.stream.config;
19+
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
25+
26+
import org.springframework.boot.SpringApplication;
27+
import org.springframework.boot.env.EnvironmentPostProcessor;
28+
import org.springframework.cloud.function.context.FunctionCatalog;
29+
import org.springframework.cloud.stream.function.FunctionConfiguration;
30+
import org.springframework.core.env.ConfigurableEnvironment;
31+
import org.springframework.util.StringUtils;
32+
/**
33+
*
34+
* @author Oleg Zhurakousky
35+
* @since 4.2.x
36+
*/
37+
class VersionExtractor implements EnvironmentPostProcessor {
38+
39+
protected final Log logger = LogFactory.getLog(getClass());
40+
41+
private final Pattern versionPattern = Pattern.compile("(\\d+)\\.{1}(\\d+)\\.{1}(\\d+)(?:\\-{1}(\\w+))?");
42+
43+
@Override
44+
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
45+
String streamJarPath = FunctionConfiguration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
46+
if (logger.isDebugEnabled()) {
47+
logger.debug("Spring Cloud Stream is loaded from: " + streamJarPath);
48+
}
49+
System.setProperty("spring-cloud-stream.version", this.extractVersion(streamJarPath));
50+
String functionJarPath = FunctionCatalog.class.getProtectionDomain().getCodeSource().getLocation().getPath();
51+
if (logger.isDebugEnabled()) {
52+
logger.debug("Spring Cloud Function is loaded from: " + streamJarPath);
53+
}
54+
System.setProperty("spring-cloud-function.version", this.extractVersion(functionJarPath));
55+
}
56+
57+
private String extractVersion(String jarFilePath) {
58+
try {
59+
if (jarFilePath.endsWith(".jar")) {
60+
Matcher m = versionPattern.matcher(jarFilePath);
61+
if (!m.find()) {
62+
return "";
63+
}
64+
StringBuilder versionBuilder = new StringBuilder();
65+
String delimiter = "";
66+
for (int i = 0; i < m.groupCount();) {
67+
String value = m.group(++i);
68+
if (StringUtils.hasText(value)) {
69+
versionBuilder.append(delimiter);
70+
versionBuilder.append(value);
71+
delimiter = (i + 1 < m.groupCount()) ? "." : "-";
72+
}
73+
}
74+
return versionBuilder.toString();
75+
}
76+
}
77+
catch (Throwable e) {
78+
logger.warn("Failed to determine version of: " + jarFilePath, e);
79+
}
80+
return "";
81+
}
82+
}

core/spring-cloud-stream/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.boot.env.EnvironmentPostProcessor:\
22
org.springframework.cloud.stream.function.RoutingFunctionEnvironmentPostProcessor,\
3+
org.springframework.cloud.stream.config.VersionExtractor,\
34
org.springframework.cloud.stream.config.PollerConfigEnvironmentPostProcessor
45
org.springframework.context.ApplicationContextInitializer:\
56
org.springframework.cloud.stream.function.PollableSourceInitializer
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
. ____ _ __ _ _
2+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
3+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
4+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
5+
' |____| .__|_| |_|_| |_\__, | / / / /
6+
=========|_|==============|___/=/_/_/_/
7+
8+
:: Spring Boot :: v${spring-boot.version}
9+
:: Spring Cloud Stream :: v${spring-cloud-stream.version}
10+
:: Spring Cloud Function :: v${spring-cloud-function.version}

0 commit comments

Comments
 (0)