]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/BuildInfo.java
Minor fixes for product 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.Map.Entry;
9 import java.util.Properties;
10 import java.util.TreeMap;
11 import java.util.stream.Collectors;
12
13 import org.eclipse.core.runtime.IProduct;
14 import org.eclipse.core.runtime.Platform;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * @author Jani Simomaa
20  *
21  */
22 public final class BuildInfo {
23
24     private static final Logger LOGGER = LoggerFactory.getLogger(BuildInfo.class);
25     private static final Properties PROPS;
26     private static final String ALL;
27
28     public static final String DEFAULT_BUILDINFO_PROPERTIES_FILE = "buildinfo.properties";
29     public static final String BUILDINFO_PROPERTIES_PROP = "buildinfo.properties.location";
30
31     public static final String BUILD_INFO_PROPERTIES_FILE = System.getProperty(BUILDINFO_PROPERTIES_PROP,
32             DEFAULT_BUILDINFO_PROPERTIES_FILE);
33
34     static {
35         if (LOGGER.isDebugEnabled())
36             LOGGER.debug("Trying to load build information from {}", BUILD_INFO_PROPERTIES_FILE);
37         Properties buildinfo = new Properties();
38         Path propertiesFile = Paths.get(BUILD_INFO_PROPERTIES_FILE);
39         if (Files.exists(propertiesFile)) {
40             try {
41                 buildinfo.load(Files.newInputStream(propertiesFile));
42                 if (LOGGER.isDebugEnabled())
43                     LOGGER.debug(buildinfo.toString());
44             } catch (IOException e) {
45                 LOGGER.error("Could not load build information from {}", propertiesFile.toAbsolutePath(), e);
46             }
47         } else {
48             if (LOGGER.isDebugEnabled())
49                 LOGGER.debug("Build information not loaded as {} does not exist", propertiesFile.toAbsolutePath());
50         }
51         // add information from .product if possible
52         collectInformationFromActiveProduct(buildinfo);
53         
54         PROPS = buildinfo;
55         StringBuilder sb = new StringBuilder();
56         TreeMap<String, String> orderedProps = new TreeMap<>((o1, o2) -> o1.compareTo(o2));
57         for (Entry<Object, Object> entry : PROPS.entrySet())
58             orderedProps.put(entry.getKey().toString(), entry.getValue().toString());
59         for (Entry<String, String> entry : orderedProps.entrySet()) {
60             sb.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
61         }
62         ALL = sb.toString();
63     }
64
65     private static void collectInformationFromActiveProduct(Properties buildinfo) {
66         IProduct product = Platform.getProduct();
67         if (product != null) {
68             String productName = product.getName() != null ? product.getName() : "<not-set>";
69             String productApplication = product.getApplication() != null ? product.getApplication() : "<not-set>";
70             String productDescription = product.getDescription() != null ? product.getDescription() : "<not-set>";
71             String productId = product.getId() != null ? product.getId() : "<not-set>";
72                 
73             buildinfo.setProperty("product.name", productName);
74             buildinfo.setProperty("product.application", productApplication);
75             buildinfo.setProperty("product.description", productDescription);
76             buildinfo.setProperty("product.id", productId);
77         }
78     }
79
80     /**
81      * @param key
82      * @return
83      */
84     public static String get(String key) {
85         return PROPS.getProperty(key, "<not-set>");
86     }
87
88     /**
89      * @return
90      */
91     public static String all() {
92         return ALL;
93     }
94
95     /**
96      * @return
97      */
98     public static Map<String, String> asMap() {
99         return PROPS.entrySet().stream().collect(Collectors.toMap(k -> String.valueOf(k), v -> String.valueOf(v)));
100     }
101 }