|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.ratis.util; |
| 19 | + |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.EnumMap; |
| 27 | +import java.util.LinkedHashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Objects; |
| 30 | +import java.util.Properties; |
| 31 | +import java.util.function.Consumer; |
| 32 | + |
| 33 | +/** |
| 34 | + * This class is for the resource generated by hadoop-maven-plugins:version-info. |
| 35 | + * <p> |
| 36 | + * This class is immutable. |
| 37 | + */ |
| 38 | +public final class VersionInfo { |
| 39 | + static final Logger LOG = LoggerFactory.getLogger(VersionInfo.class); |
| 40 | + |
| 41 | + private static final String RATIS_VERSION_PROPERTIES = "ratis-version.properties"; |
| 42 | + private static final String UNKNOWN = "<unknown>"; |
| 43 | + private static final String FORMAT = " %20s: %s"; |
| 44 | + |
| 45 | + private enum SoftwareInfo { |
| 46 | + // the ordering is the output ordering |
| 47 | + NAME, VERSION, URL, REVISION; |
| 48 | + |
| 49 | + static SoftwareInfo parse(String key) { |
| 50 | + for (SoftwareInfo info : SoftwareInfo.values()) { |
| 51 | + if (info.name().toLowerCase().equals(key)) { |
| 52 | + return info; |
| 53 | + } |
| 54 | + } |
| 55 | + return null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private enum RuntimeInfo { |
| 60 | + // the ordering is the output ordering |
| 61 | + JAVA, USER; |
| 62 | + |
| 63 | + static final InfoMap<RuntimeInfo> MAP; |
| 64 | + |
| 65 | + static { |
| 66 | + final EnumMap<RuntimeInfo, String> map = new EnumMap<>(RuntimeInfo.class); |
| 67 | + final Properties properties = System.getProperties(); |
| 68 | + map.put(JAVA, properties.getProperty("java.vm.name") + " " + properties.getProperty("java.runtime.version")); |
| 69 | + map.put(USER, properties.getProperty("user.name")); |
| 70 | + MAP = new InfoMap<>(map); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static class InfoMap<INFO extends Enum<INFO>> { |
| 75 | + private final Map<INFO, String> map; |
| 76 | + |
| 77 | + InfoMap(EnumMap<INFO, String> map) { |
| 78 | + this.map = Collections.unmodifiableMap(map); |
| 79 | + } |
| 80 | + |
| 81 | + String getOrDefault(INFO info) { |
| 82 | + return map.getOrDefault(info, UNKNOWN); |
| 83 | + } |
| 84 | + |
| 85 | + String format(INFO info) { |
| 86 | + return String.format(FORMAT, info.name().toLowerCase(), getOrDefault(info)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static VersionInfo load(Class<?> clazz) { |
| 91 | + final Properties properties = new Properties(); |
| 92 | + |
| 93 | + try (InputStream in = clazz.getClassLoader().getResourceAsStream(RATIS_VERSION_PROPERTIES)) { |
| 94 | + if (in != null) { |
| 95 | + properties.load(in); |
| 96 | + } else { |
| 97 | + LOG.warn("Resource '{}' not found for {}", RATIS_VERSION_PROPERTIES, clazz); |
| 98 | + } |
| 99 | + } catch (IOException e) { |
| 100 | + LOG.warn("Failed to load resource '{}' for {}", RATIS_VERSION_PROPERTIES, clazz, e); |
| 101 | + } |
| 102 | + return new VersionInfo(clazz, properties); |
| 103 | + } |
| 104 | + |
| 105 | + private final Class<?> clazz; |
| 106 | + private final InfoMap<RuntimeInfo> runtimeInfos = RuntimeInfo.MAP; |
| 107 | + private final InfoMap<SoftwareInfo> softwareInfos; |
| 108 | + private final Map<String, String> otherInfos; |
| 109 | + |
| 110 | + private VersionInfo(Class<?> clazz, Properties properties) { |
| 111 | + this.clazz = Objects.requireNonNull(clazz, "clazz == null"); |
| 112 | + |
| 113 | + final EnumMap<SoftwareInfo, String> softwareInfoMap = new EnumMap<>(SoftwareInfo.class); |
| 114 | + final Map<String, String> others = new LinkedHashMap<>(); // preserve insertion order |
| 115 | + for (Map.Entry<Object, Object> e : properties.entrySet()) { |
| 116 | + final String key = e.getKey().toString(); |
| 117 | + final String value = e.getValue().toString(); |
| 118 | + final SoftwareInfo k = SoftwareInfo.parse(key); |
| 119 | + if (k != null) { |
| 120 | + softwareInfoMap.put(k, value); |
| 121 | + } else { |
| 122 | + others.put(key, value); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + this.softwareInfos = new InfoMap<>(softwareInfoMap); |
| 127 | + this.otherInfos = Collections.unmodifiableMap(others); |
| 128 | + } |
| 129 | + |
| 130 | + public void printStartupMessages(Object name, Consumer<String> log) { |
| 131 | + Objects.requireNonNull(name, "name == null"); |
| 132 | + log.accept(String.format("Starting %s -- %s %s", |
| 133 | + softwareInfos.getOrDefault(SoftwareInfo.NAME), clazz.getSimpleName(), name)); |
| 134 | + final SoftwareInfo[] softwareInfoValues = SoftwareInfo.values(); |
| 135 | + for(int i = 1; i < softwareInfoValues.length; i++) { |
| 136 | + log.accept(softwareInfos.format(softwareInfoValues[i])); |
| 137 | + } |
| 138 | + for(RuntimeInfo runtimeInfo : RuntimeInfo.values()) { |
| 139 | + log.accept(runtimeInfos.format(runtimeInfo)); |
| 140 | + } |
| 141 | + for (Map.Entry<String, String> e : otherInfos.entrySet()) { |
| 142 | + log.accept(String.format(FORMAT, e.getKey(), e.getValue())); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public static void main(String[] args) { |
| 147 | + VersionInfo.load(VersionInfo.class).printStartupMessages(":", System.out::println); |
| 148 | + } |
| 149 | +} |
0 commit comments