View Javadoc
1   /*
2    * Copyright 2019-2024 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util;
6   
7   import java.util.Properties;
8   
9   import oshi.annotation.concurrent.NotThreadSafe;
10  
11  /**
12   * The global configuration utility. See {@code src/main/resources/oshi.properties} for default values.
13   * <p>
14   * This class is not thread safe if methods manipulating the configuration are used. These methods are intended for use
15   * by a single thread at startup, before instantiation of any other OSHI classes. OSHI does not guarantee re- reading of
16   * any configuration changes.
17   */
18  @NotThreadSafe
19  public final class GlobalConfig {
20  
21      private static final String OSHI_PROPERTIES = "oshi.properties";
22  
23      private static final Properties CONFIG = FileUtil.readPropertiesFromFilename(OSHI_PROPERTIES);
24  
25      public static final String OSHI_UTIL_MEMOIZER_EXPIRATION = "oshi.util.memoizer.expiration";
26      public static final String OSHI_UTIL_WMI_TIMEOUT = "oshi.util.wmi.timeout";
27      public static final String OSHI_UTIL_PROC_PATH = "oshi.util.proc.path";
28      public static final String OSHI_UTIL_SYS_PATH = "oshi.util.sys.path";
29      public static final String OSHI_UTIL_DEV_PATH = "oshi.util.dev.path";
30  
31      public static final String OSHI_PSEUDO_FILESYSTEM_TYPES = "oshi.pseudo.filesystem.types";
32      public static final String OSHI_NETWORK_FILESYSTEM_TYPES = "oshi.network.filesystem.types";
33  
34      public static final String OSHI_OS_LINUX_ALLOWUDEV = "oshi.os.linux.allowudev";
35      public static final String OSHI_OS_LINUX_PROCFS_LOGWARNING = "oshi.os.linux.procfs.logwarning";
36  
37      public static final String OSHI_OS_MAC_SYSCTL_LOGWARNING = "oshi.os.mac.sysctl.logwarning";
38  
39      public static final String OSHI_OS_WINDOWS_EVENTLOG = "oshi.os.windows.eventlog";
40      public static final String OSHI_OS_WINDOWS_PROCSTATE_SUSPENDED = "oshi.os.windows.procstate.suspended";
41      public static final String OSHI_OS_WINDOWS_COMMANDLINE_BATCH = "oshi.os.windows.commandline.batch";
42      public static final String OSHI_OS_WINDOWS_HKEYPERFDATA = "oshi.os.windows.hkeyperfdata";
43      public static final String OSHI_OS_WINDOWS_LEGACY_SYSTEM_COUNTERS = "oshi.os.windows.legacy.system.counters";
44      public static final String OSHI_OS_WINDOWS_LOADAVERAGE = "oshi.os.windows.loadaverage";
45      public static final String OSHI_OS_WINDOWS_CPU_UTILITY = "oshi.os.windows.cpu.utility";
46  
47      public static final String OSHI_OS_WINDOWS_PERFDISK_DIABLED = "oshi.os.windows.perfdisk.disabled";
48      public static final String OSHI_OS_WINDOWS_PERFOS_DIABLED = "oshi.os.windows.perfos.disabled";
49      public static final String OSHI_OS_WINDOWS_PERFPROC_DIABLED = "oshi.os.windows.perfproc.disabled";
50      public static final String OSHI_OS_WINDOWS_PERF_DISABLE_ALL_ON_FAILURE = "oshi.os.windows.perf.disable.all.on.failure";
51  
52      public static final String OSHI_OS_UNIX_WHOCOMMAND = "oshi.os.unix.whoCommand";
53      public static final String OSHI_OS_SOLARIS_ALLOWKSTAT2 = "oshi.os.solaris.allowKstat2";
54  
55      private GlobalConfig() {
56      }
57  
58      /**
59       * Get the property associated with the given key.
60       *
61       * @param key The property key
62       * @return The property value if it exists, or null otherwise
63       */
64      public static String get(String key) {
65          return CONFIG.getProperty(key);
66      }
67  
68      /**
69       * Get the {@code String} property associated with the given key.
70       *
71       * @param key The property key
72       * @param def The default value
73       * @return The property value or the given default if not found
74       */
75      public static String get(String key, String def) {
76          return CONFIG.getProperty(key, def);
77      }
78  
79      /**
80       * Get the {@code int} property associated with the given key.
81       *
82       * @param key The property key
83       * @param def The default value
84       * @return The property value or the given default if not found
85       */
86      public static int get(String key, int def) {
87          String value = CONFIG.getProperty(key);
88          return value == null ? def : ParseUtil.parseIntOrDefault(value, def);
89      }
90  
91      /**
92       * Get the {@code double} property associated with the given key.
93       *
94       * @param key The property key
95       * @param def The default value
96       * @return The property value or the given default if not found
97       */
98      public static double get(String key, double def) {
99          String value = CONFIG.getProperty(key);
100         return value == null ? def : ParseUtil.parseDoubleOrDefault(value, def);
101     }
102 
103     /**
104      * Get the {@code boolean} property associated with the given key.
105      *
106      * @param key The property key
107      * @param def The default value
108      * @return The property value or the given default if not found
109      */
110     public static boolean get(String key, boolean def) {
111         String value = CONFIG.getProperty(key);
112         return value == null ? def : Boolean.parseBoolean(value);
113     }
114 
115     /**
116      * Set the given property, overwriting any existing value. If the given value is {@code null}, the property is
117      * removed.
118      *
119      * @param key The property key
120      * @param val The new value
121      */
122     public static void set(String key, Object val) {
123         if (val == null) {
124             CONFIG.remove(key);
125         } else {
126             CONFIG.setProperty(key, val.toString());
127         }
128     }
129 
130     /**
131      * Reset the given property to its default value.
132      *
133      * @param key The property key
134      */
135     public static void remove(String key) {
136         CONFIG.remove(key);
137     }
138 
139     /**
140      * Clear the configuration.
141      */
142     public static void clear() {
143         CONFIG.clear();
144     }
145 
146     /**
147      * Load the given {@link java.util.Properties} into the global configuration.
148      *
149      * @param properties The new properties
150      */
151     public static void load(Properties properties) {
152         CONFIG.putAll(properties);
153     }
154 
155     /**
156      * Indicates that a configuration value is invalid.
157      */
158     public static class PropertyException extends RuntimeException {
159 
160         private static final long serialVersionUID = -7482581936621748005L;
161 
162         /**
163          * @param property The property name
164          */
165         public PropertyException(String property) {
166             super("Invalid property: \"" + property + "\" = " + GlobalConfig.get(property, null));
167         }
168 
169         /**
170          * @param property The property name
171          * @param message  An exception message
172          */
173         public PropertyException(String property, String message) {
174             super("Invalid property \"" + property + "\": " + message);
175         }
176     }
177 }