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_LOADAVERAGE = "oshi.os.windows.loadaverage";
44      public static final String OSHI_OS_WINDOWS_CPU_UTILITY = "oshi.os.windows.cpu.utility";
45  
46      public static final String OSHI_OS_WINDOWS_PERFDISK_DIABLED = "oshi.os.windows.perfdisk.disabled";
47      public static final String OSHI_OS_WINDOWS_PERFOS_DIABLED = "oshi.os.windows.perfos.disabled";
48      public static final String OSHI_OS_WINDOWS_PERFPROC_DIABLED = "oshi.os.windows.perfproc.disabled";
49  
50      public static final String OSHI_OS_UNIX_WHOCOMMAND = "oshi.os.unix.whoCommand";
51      public static final String OSHI_OS_SOLARIS_ALLOWKSTAT2 = "oshi.os.solaris.allowKstat2";
52  
53      private GlobalConfig() {
54      }
55  
56      /**
57       * Get the property associated with the given key.
58       *
59       * @param key The property key
60       * @return The property value if it exists, or null otherwise
61       */
62      public static String get(String key) {
63          return CONFIG.getProperty(key);
64      }
65  
66      /**
67       * Get the {@code String} property associated with the given key.
68       *
69       * @param key The property key
70       * @param def The default value
71       * @return The property value or the given default if not found
72       */
73      public static String get(String key, String def) {
74          return CONFIG.getProperty(key, def);
75      }
76  
77      /**
78       * Get the {@code int} property associated with the given key.
79       *
80       * @param key The property key
81       * @param def The default value
82       * @return The property value or the given default if not found
83       */
84      public static int get(String key, int def) {
85          String value = CONFIG.getProperty(key);
86          return value == null ? def : ParseUtil.parseIntOrDefault(value, def);
87      }
88  
89      /**
90       * Get the {@code double} property associated with the given key.
91       *
92       * @param key The property key
93       * @param def The default value
94       * @return The property value or the given default if not found
95       */
96      public static double get(String key, double def) {
97          String value = CONFIG.getProperty(key);
98          return value == null ? def : ParseUtil.parseDoubleOrDefault(value, def);
99      }
100 
101     /**
102      * Get the {@code boolean} property associated with the given key.
103      *
104      * @param key The property key
105      * @param def The default value
106      * @return The property value or the given default if not found
107      */
108     public static boolean get(String key, boolean def) {
109         String value = CONFIG.getProperty(key);
110         return value == null ? def : Boolean.parseBoolean(value);
111     }
112 
113     /**
114      * Set the given property, overwriting any existing value. If the given value is {@code null}, the property is
115      * removed.
116      *
117      * @param key The property key
118      * @param val The new value
119      */
120     public static void set(String key, Object val) {
121         if (val == null) {
122             CONFIG.remove(key);
123         } else {
124             CONFIG.setProperty(key, val.toString());
125         }
126     }
127 
128     /**
129      * Reset the given property to its default value.
130      *
131      * @param key The property key
132      */
133     public static void remove(String key) {
134         CONFIG.remove(key);
135     }
136 
137     /**
138      * Clear the configuration.
139      */
140     public static void clear() {
141         CONFIG.clear();
142     }
143 
144     /**
145      * Load the given {@link java.util.Properties} into the global configuration.
146      *
147      * @param properties The new properties
148      */
149     public static void load(Properties properties) {
150         CONFIG.putAll(properties);
151     }
152 
153     /**
154      * Indicates that a configuration value is invalid.
155      */
156     public static class PropertyException extends RuntimeException {
157 
158         private static final long serialVersionUID = -7482581936621748005L;
159 
160         /**
161          * @param property The property name
162          */
163         public PropertyException(String property) {
164             super("Invalid property: \"" + property + "\" = " + GlobalConfig.get(property, null));
165         }
166 
167         /**
168          * @param property The property name
169          * @param message  An exception message
170          */
171         public PropertyException(String property, String message) {
172             super("Invalid property \"" + property + "\": " + message);
173         }
174     }
175 }