001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.system.HapiSystemProperties;
024import org.apache.commons.lang3.StringUtils;
025
026import java.io.InputStream;
027import java.util.Properties;
028
029import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
030
031/**
032 * Used internally by HAPI to log the version of the HAPI FHIR framework
033 * once, when the framework is first loaded by the classloader.
034 */
035public class VersionUtil {
036
037        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(VersionUtil.class);
038        private static String ourVersion;
039        private static String ourBuildNumber;
040        private static String ourBuildTime;
041        private static boolean ourSnapshot;
042
043        static {
044                initialize();
045        }
046
047        public static boolean isSnapshot() {
048                return ourSnapshot;
049        }
050
051        public static String getBuildNumber() {
052                return ourBuildNumber;
053        }
054
055        public static String getBuildTime() {
056                return ourBuildTime;
057        }
058
059        public static String getVersion() {
060                return ourVersion;
061        }
062
063        private static void initialize() {
064                try (InputStream is = VersionUtil.class.getResourceAsStream("/ca/uhn/fhir/hapi-fhir-base-build.properties")) {
065
066                        Properties p = new Properties();
067                        if (is != null) {
068                                p.load(is);
069                        }
070
071                        ourVersion = p.getProperty("hapifhir.version");
072                        ourVersion = defaultIfBlank(ourVersion, "(unknown)");
073
074                        ourSnapshot = ourVersion.contains("SNAPSHOT");
075
076                        ourBuildNumber = StringUtils.left(p.getProperty("hapifhir.buildnumber"), 10);
077                        ourBuildTime = p.getProperty("hapifhir.timestamp");
078
079                        if (!HapiSystemProperties.isSuppressHapiFhirVersionLogEnabled()) {
080                                String buildNumber = ourBuildNumber;
081                                if (isSnapshot()) {
082                                        buildNumber = buildNumber + "/" + getBuildDate();
083                                }
084
085                                ourLog.info("HAPI FHIR version {} - Rev {}", ourVersion, buildNumber);
086                        }
087
088                } catch (Exception e) {
089                        ourLog.warn("Unable to determine HAPI version information", e);
090                }
091        }
092
093        public static String getBuildDate() {
094                return ourBuildTime.substring(0, 10);
095        }
096
097}