|
| 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 | +} |
0 commit comments