|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.engine.common.env; |
| 19 | + |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.time.ZoneId; |
| 25 | +import java.time.format.DateTimeFormatter; |
| 26 | +import java.util.Properties; |
| 27 | + |
| 28 | +import static org.apache.seatunnel.engine.common.Constant.PROP_FILE; |
| 29 | + |
| 30 | +@Slf4j |
| 31 | +public class EnvironmentUtil { |
| 32 | + |
| 33 | + private static String getProperty(Properties properties, String key, String defaultValue) { |
| 34 | + String value = properties.getProperty(key); |
| 35 | + if (value == null || value.charAt(0) == '$') { |
| 36 | + return defaultValue; |
| 37 | + } |
| 38 | + return value; |
| 39 | + } |
| 40 | + |
| 41 | + public static Version getVersion() { |
| 42 | + |
| 43 | + Version version = new Version(); |
| 44 | + ClassLoader classLoader = EnvironmentUtil.class.getClassLoader(); |
| 45 | + |
| 46 | + try (InputStream propFile = classLoader.getResourceAsStream(PROP_FILE)) { |
| 47 | + |
| 48 | + if (propFile != null) { |
| 49 | + Properties properties = new Properties(); |
| 50 | + |
| 51 | + properties.load(propFile); |
| 52 | + |
| 53 | + version.setProjectVersion( |
| 54 | + getProperty(properties, "project.version", version.getProjectVersion())); |
| 55 | + version.setGitCommitId( |
| 56 | + getProperty(properties, "git.commit.id", version.getGitCommitId())); |
| 57 | + version.setGitCommitAbbrev( |
| 58 | + getProperty( |
| 59 | + properties, "git.commit.id.abbrev", version.getGitCommitAbbrev())); |
| 60 | + |
| 61 | + DateTimeFormatter gitDateTimeFormatter = |
| 62 | + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"); |
| 63 | + |
| 64 | + DateTimeFormatter systemDefault = |
| 65 | + DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()); |
| 66 | + |
| 67 | + version.setBuildTime( |
| 68 | + systemDefault.format( |
| 69 | + gitDateTimeFormatter.parse( |
| 70 | + getProperty( |
| 71 | + properties, |
| 72 | + "git.build.time", |
| 73 | + version.getBuildTime())))); |
| 74 | + version.setCommitTime( |
| 75 | + systemDefault.format( |
| 76 | + gitDateTimeFormatter.parse( |
| 77 | + getProperty( |
| 78 | + properties, |
| 79 | + "git.commit.time", |
| 80 | + version.getCommitTime())))); |
| 81 | + } |
| 82 | + |
| 83 | + } catch (IOException ioException) { |
| 84 | + log.info("Unable to read version property file: {}", ioException.getMessage()); |
| 85 | + } |
| 86 | + |
| 87 | + return version; |
| 88 | + } |
| 89 | +} |
0 commit comments