001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 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 org.apache.commons.lang3.StringUtils;
024
025import java.io.InputStream;
026import java.util.Properties;
027
028import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
029
030/**
031 * Used internally by HAPI to log the version of the HAPI FHIR framework
032 * once, when the framework is first loaded by the classloader.
033 */
034public class VersionUtil {
035
036        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(VersionUtil.class);
037        private static String ourVersion;
038        private static String ourBuildNumber;
039        private static String ourBuildTime;
040        private static boolean ourSnapshot;
041
042        static {
043                initialize();
044        }
045
046        public static boolean isSnapshot() {
047                return ourSnapshot;
048        }
049
050        public static String getBuildNumber() {
051                return ourBuildNumber;
052        }
053
054        public static String getBuildTime() {
055                return ourBuildTime;
056        }
057
058        public static String getVersion() {
059                return ourVersion;
060        }
061
062        private static void initialize() {
063                try (InputStream is = VersionUtil.class.getResourceAsStream("/ca/uhn/fhir/hapi-fhir-base-build.properties")) {
064
065                        Properties p = new Properties();
066                        if (is != null) {
067                                p.load(is);
068                        }
069
070                        ourVersion = p.getProperty("hapifhir.version");
071                        ourVersion = defaultIfBlank(ourVersion, "(unknown)");
072
073                        ourSnapshot = ourVersion.contains("SNAPSHOT");
074
075                        ourBuildNumber = StringUtils.left(p.getProperty("hapifhir.buildnumber"), 10);
076                        ourBuildTime = p.getProperty("hapifhir.timestamp");
077
078                        if (System.getProperty("suppress_hapi_fhir_version_log") == null) {
079                                String buildNumber = ourBuildNumber;
080                                if (isSnapshot()) {
081                                        buildNumber = buildNumber + "/" + getBuildDate();
082                                }
083
084                                ourLog.info("HAPI FHIR version {} - Rev {}", ourVersion, buildNumber);
085                        }
086
087                } catch (Exception e) {
088                        ourLog.warn("Unable to determine HAPI version information", e);
089                }
090        }
091
092        public static String getBuildDate() {
093                return ourBuildTime.substring(0, 10);
094        }
095
096}