001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
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
041        static {
042                initialize();
043        }
044
045        public static String getBuildNumber() {
046                return ourBuildNumber;
047        }
048
049        public static String getBuildTime() {
050                return ourBuildTime;
051        }
052
053        public static String getVersion() {
054                return ourVersion;
055        }
056
057        private static void initialize() {
058                try (InputStream is = VersionUtil.class.getResourceAsStream("/ca/uhn/fhir/hapi-fhir-base-build.properties")) {
059
060                        Properties p = new Properties();
061                        if (is != null) {
062                                p.load(is);
063                        }
064
065                        ourVersion = p.getProperty("hapifhir.version");
066                        ourVersion = defaultIfBlank(ourVersion, "(unknown)");
067
068                        ourBuildNumber = p.getProperty("hapifhir.buildnumber");
069                        ourBuildTime = p.getProperty("hapifhir.timestamp");
070
071                        if (System.getProperty("suppress_hapi_fhir_version_log") == null) {
072                                ourLog.info("HAPI FHIR version {} - Rev {}", ourVersion, StringUtils.right(ourBuildNumber, 10));
073                        }
074
075                } catch (Exception e) {
076                        ourLog.warn("Unable to determine HAPI version information", e);
077                }
078        }
079
080}