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