001package org.hl7.fhir.utilities;
002
003/*
004 * #%L
005 * org.hl7.fhir.utilities
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
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 java.io.InputStream;
024import java.util.Properties;
025
026import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
027import static org.apache.commons.lang3.StringUtils.left;
028
029/**
030 * Used internally by HAPI to log the version of the HAPI FHIR framework
031 * once, when the framework is first loaded by the classloader.
032 */
033public class VersionUtil {
034
035  private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ca.uhn.fhir.util.VersionUtil.class);
036  private static String ourVersion;
037  private static String ourBuildNumber;
038  private static String ourBuildTime;
039
040  static {
041    initialize();
042  }
043
044  public static String getBuildNumber() {
045    return ourBuildNumber;
046  }
047
048  public static String getBuildTime() {
049    return ourBuildTime;
050  }
051
052  public static String getVersion() {
053    return ourVersion;
054  }
055
056  private static void initialize() {
057    try (InputStream is = ca.uhn.fhir.util.VersionUtil.class.getResourceAsStream("/fhir-build.properties")) {
058
059      Properties p = new Properties();
060      if (is != null) {
061        p.load(is);
062      }
063
064      ourVersion = p.getProperty("orgfhir.version");
065      ourVersion = defaultIfBlank(ourVersion, "(unknown)");
066
067      ourBuildNumber = p.getProperty("orgfhir.buildnumber");
068      ourBuildTime = p.getProperty("orgfhir.timestamp");
069
070    } catch (Exception e) {
071      ourLog.warn("Unable to determine version information", e);
072    }
073  }
074
075  public static String getVersionString() {
076    return "Version " + getVersion() + " - Built " + getBuildTime() + " - Git " + left(getBuildNumber(), 12);
077  }
078
079}