1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.utils;
1920import org.apache.log4j.Logger;
2122import com.google.common.collect.Maps;
23import com.google.common.io.Resources;
2425import java.io.IOException;
26import java.net.URL;
27import java.util.Map;
28import java.util.Properties;
2930/**31 * Versions of Giraph dependencies. This pulls version information from a known32 * properties file. That file is created by maven when building the jar and33 * populated with versions from the pom. We put the properties file in a known34 * resources folder so that it is easy to pick up.35 *36 * See http://bit.ly/19LQyrK for more information.37 */38publicclassGiraphDepVersions {
39/** Logger */40privatestaticfinal Logger LOG = Logger.getLogger(GiraphDepVersions.class);
4142/** Path to resource */43privatestaticfinal String RESOURCE_NAME =
44"org/apache/giraph/versions.properties";
4546/** Singleton */47privatestaticfinalGiraphDepVersions INSTANCE = newGiraphDepVersions();
4849/** The properties read */50privatefinal Properties properties;
5152/** Constructor */53privateGiraphDepVersions() {
54 URL url = Resources.getResource(RESOURCE_NAME);
55 properties = new Properties();
56try {
57 properties.load(url.openStream());
58 } catch (IOException e) {
59 LOG.error("Could not read giraph versions from file " + RESOURCE_NAME);
60 }
61 }
6263/**64 * Get singleton instance65 *66 * @return singleton67 */68publicstaticGiraphDepVersions get() {
69return INSTANCE;
70 }
7172public Properties getProperties() {
73return properties;
74 }
7576/**77 * Get version of the named dependency, or null if not found78 *79 * @param name dependency name80 * @return version, or null81 */82public String versionOf(String name) {
83return properties.getProperty(name);
84 }
8586/** Log the dependency versions we're using */87publicvoid logVersionsUsed() {
88 Map<String, String> sortedVersions = Maps.newTreeMap();
89for (Map.Entry<Object, Object> entry : properties.entrySet()) {
90 sortedVersions.put(entry.getKey().toString(),
91 entry.getValue().toString());
92 }
93 StringBuilder sb = new StringBuilder(sortedVersions.size() * 20);
94for (Map.Entry<String, String> entry : sortedVersions.entrySet()) {
95 sb.append(" ").append(entry.getKey()).append(": ").
96 append(entry.getValue()).append("\n");
97 }
98 LOG.info("Versions of Giraph dependencies =>\n" + sb);
99 }
100 }