]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/BuildInfo.java
SCL-functionality for build information querying
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / BuildInfo.java
1 package org.simantics;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.Map;
8 import java.util.Properties;
9 import java.util.stream.Collectors;
10
11 import org.eclipse.core.runtime.IProduct;
12 import org.eclipse.core.runtime.Platform;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
17  * @author Jani Simomaa
18  *
19  */
20 public final class BuildInfo {
21
22     private static final Logger LOGGER = LoggerFactory.getLogger(BuildInfo.class);
23     private static final Properties PROPS;
24
25     public static final String DEFAULT_BUILDINFO_PROPERTIES_FILE = "buildinfo.properties";
26     public static final String BUILDINFO_PROPERTIES_PROP = "buildinfo.properties.location";
27
28     public static final String BUILD_INFO_PROPERTIES_FILE = System.getProperty(BUILDINFO_PROPERTIES_PROP,
29             DEFAULT_BUILDINFO_PROPERTIES_FILE);
30
31     static {
32         if (LOGGER.isDebugEnabled())
33             LOGGER.debug("Trying to load build information from {}", BUILD_INFO_PROPERTIES_FILE);
34         Properties buildinfo = new Properties();
35         Path propertiesFile = Paths.get(BUILD_INFO_PROPERTIES_FILE);
36         if (Files.exists(propertiesFile)) {
37             try {
38                 buildinfo.load(Files.newInputStream(propertiesFile));
39                 if (LOGGER.isDebugEnabled())
40                     LOGGER.debug(buildinfo.toString());
41             } catch (IOException e) {
42                 LOGGER.error("Could not load build information from {}", propertiesFile.toAbsolutePath(), e);
43             }
44         } else {
45             if (LOGGER.isDebugEnabled())
46                 LOGGER.debug("Build information not loaded as {} does not exist", propertiesFile.toAbsolutePath());
47         }
48         // add information from .product if possible
49         collectInformationFromActiveProduct(buildinfo);
50         
51         PROPS = buildinfo;
52     }
53
54     private static void collectInformationFromActiveProduct(Properties buildinfo) {
55         IProduct product = Platform.getProduct();
56         if (product != null) {
57             buildinfo.setProperty("product.name", product.getName());
58             buildinfo.setProperty("product.application", product.getApplication());
59             buildinfo.setProperty("product.description", product.getDescription());
60             buildinfo.setProperty("product.id", product.getId());
61         }
62     }
63
64     /**
65      * @param key
66      * @return
67      */
68     public static String get(String key) {
69         return PROPS.getProperty(key, "<not-set>");
70     }
71
72     /**
73      * @return
74      */
75     public static String all() {
76         return PROPS.toString();
77     }
78
79     /**
80      * @return
81      */
82     public static Map<String, String> asMap() {
83         return PROPS.entrySet().stream().collect(Collectors.toMap(k -> String.valueOf(k), v -> String.valueOf(v)));
84     }
85 }