1
2
3
4
5 package oshi.util.platform.windows;
6
7 import java.time.OffsetDateTime;
8 import java.util.Locale;
9
10 import com.sun.jna.platform.win32.Variant;
11 import com.sun.jna.platform.win32.COM.Wbemcli;
12 import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
13 import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
14
15 import oshi.annotation.concurrent.ThreadSafe;
16 import oshi.util.Constants;
17 import oshi.util.ParseUtil;
18
19
20
21
22 @ThreadSafe
23 public final class WmiUtil {
24
25
26
27
28
29
30 public static final String OHM_NAMESPACE = "ROOT\\OpenHardwareMonitor";
31
32 private static final String CLASS_CAST_MSG = "%s is not a %s type. CIM Type is %d and VT type is %d";
33
34 private WmiUtil() {
35 }
36
37
38
39
40
41
42
43
44
45 public static <T extends Enum<T>> String queryToString(WmiQuery<T> query) {
46 T[] props = query.getPropertyEnum().getEnumConstants();
47 StringBuilder sb = new StringBuilder("SELECT ");
48 sb.append(props[0].name());
49 for (int i = 1; i < props.length; i++) {
50 sb.append(',').append(props[i].name());
51 }
52 sb.append(" FROM ").append(query.getWmiClassName());
53 return sb.toString();
54 }
55
56
57
58
59
60
61
62
63
64
65
66 public static <T extends Enum<T>> String getString(WmiResult<T> result, T property, int index) {
67 if (result.getCIMType(property) == Wbemcli.CIM_STRING) {
68 return getStr(result, property, index);
69 }
70 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "String",
71 result.getCIMType(property), result.getVtType(property)));
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public static <T extends Enum<T>> String getDateString(WmiResult<T> result, T property, int index) {
85 OffsetDateTime dateTime = getDateTime(result, property, index);
86
87 if (dateTime.equals(Constants.UNIX_EPOCH)) {
88 return "";
89 }
90 return dateTime.toLocalDate().toString();
91 }
92
93
94
95
96
97
98
99
100
101
102
103 public static <T extends Enum<T>> OffsetDateTime getDateTime(WmiResult<T> result, T property, int index) {
104 if (result.getCIMType(property) == Wbemcli.CIM_DATETIME) {
105 return ParseUtil.parseCimDateTimeToOffset(getStr(result, property, index));
106 }
107 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "DateTime",
108 result.getCIMType(property), result.getVtType(property)));
109 }
110
111
112
113
114
115
116
117
118
119
120
121 public static <T extends Enum<T>> String getRefString(WmiResult<T> result, T property, int index) {
122 if (result.getCIMType(property) == Wbemcli.CIM_REFERENCE) {
123 return getStr(result, property, index);
124 }
125 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "Reference",
126 result.getCIMType(property), result.getVtType(property)));
127 }
128
129 private static <T extends Enum<T>> String getStr(WmiResult<T> result, T property, int index) {
130 Object o = result.getValue(property, index);
131 if (o == null) {
132 return "";
133 } else if (result.getVtType(property) == Variant.VT_BSTR) {
134 return (String) o;
135 }
136 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "String-mapped",
137 result.getCIMType(property), result.getVtType(property)));
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151 public static <T extends Enum<T>> long getUint64(WmiResult<T> result, T property, int index) {
152 Object o = result.getValue(property, index);
153 if (o == null) {
154 return 0L;
155 } else if (result.getCIMType(property) == Wbemcli.CIM_UINT64 && result.getVtType(property) == Variant.VT_BSTR) {
156 return ParseUtil.parseLongOrDefault((String) o, 0L);
157 }
158 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "UINT64",
159 result.getCIMType(property), result.getVtType(property)));
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173 public static <T extends Enum<T>> int getUint32(WmiResult<T> result, T property, int index) {
174 if (result.getCIMType(property) == Wbemcli.CIM_UINT32) {
175 return getInt(result, property, index);
176 }
177 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "UINT32",
178 result.getCIMType(property), result.getVtType(property)));
179 }
180
181
182
183
184
185
186
187
188
189
190
191 public static <T extends Enum<T>> long getUint32asLong(WmiResult<T> result, T property, int index) {
192 if (result.getCIMType(property) == Wbemcli.CIM_UINT32) {
193 return getInt(result, property, index) & 0xFFFFFFFFL;
194 }
195 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "UINT32",
196 result.getCIMType(property), result.getVtType(property)));
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210 public static <T extends Enum<T>> int getSint32(WmiResult<T> result, T property, int index) {
211 if (result.getCIMType(property) == Wbemcli.CIM_SINT32) {
212 return getInt(result, property, index);
213 }
214 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "SINT32",
215 result.getCIMType(property), result.getVtType(property)));
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229 public static <T extends Enum<T>> int getUint16(WmiResult<T> result, T property, int index) {
230 if (result.getCIMType(property) == Wbemcli.CIM_UINT16) {
231 return getInt(result, property, index);
232 }
233 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "UINT16",
234 result.getCIMType(property), result.getVtType(property)));
235 }
236
237 private static <T extends Enum<T>> int getInt(WmiResult<T> result, T property, int index) {
238 Object o = result.getValue(property, index);
239 if (o == null) {
240 return 0;
241 } else if (result.getVtType(property) == Variant.VT_I4) {
242 return (int) o;
243 }
244 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "32-bit integer",
245 result.getCIMType(property), result.getVtType(property)));
246 }
247
248
249
250
251
252
253
254
255
256
257
258 public static <T extends Enum<T>> float getFloat(WmiResult<T> result, T property, int index) {
259 Object o = result.getValue(property, index);
260 if (o == null) {
261 return 0f;
262 } else if (result.getCIMType(property) == Wbemcli.CIM_REAL32 && result.getVtType(property) == Variant.VT_R4) {
263 return (float) o;
264 }
265 throw new ClassCastException(String.format(Locale.ROOT, CLASS_CAST_MSG, property.name(), "Float",
266 result.getCIMType(property), result.getVtType(property)));
267 }
268 }