Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.common; 16 : 17 : import static java.nio.charset.StandardCharsets.UTF_8; 18 : 19 : import com.google.common.annotations.VisibleForTesting; 20 : import com.google.common.flogger.FluentLogger; 21 : import java.io.BufferedReader; 22 : import java.io.IOException; 23 : import java.io.InputStream; 24 : import java.io.InputStreamReader; 25 : 26 : public class Version { 27 153 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 28 : 29 : @VisibleForTesting static final String DEV = "(dev)"; 30 : 31 : private static final String VERSION; 32 : 33 : public static String getVersion() { 34 153 : return VERSION; 35 : } 36 : 37 : static { 38 153 : VERSION = loadVersion(); 39 153 : } 40 : 41 : private static String loadVersion() { 42 153 : try (InputStream in = Version.class.getResourceAsStream("Version")) { 43 153 : if (in == null) { 44 152 : return DEV; 45 : } 46 1 : try (BufferedReader r = new BufferedReader(new InputStreamReader(in, UTF_8))) { 47 1 : String vs = r.readLine(); 48 1 : if (vs != null && vs.startsWith("v")) { 49 1 : vs = vs.substring(1); 50 : } 51 1 : if (vs != null && vs.isEmpty()) { 52 0 : vs = null; 53 : } 54 1 : return vs; 55 : } 56 152 : } catch (IOException e) { 57 0 : logger.atSevere().withCause(e).log("%s", e.getMessage()); 58 0 : return "(unknown version)"; 59 : } 60 : } 61 : 62 : private Version() {} 63 : }