View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.windows.wmi;
6   
7   import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
8   import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
9   
10  import oshi.annotation.concurrent.ThreadSafe;
11  import oshi.util.platform.windows.WmiQueryHandler;
12  
13  /**
14   * Utility to query WMI classes in Storage namespace assocaited with Storage Pools
15   */
16  @ThreadSafe
17  public final class MSFTStorage {
18  
19      private static final String STORAGE_NAMESPACE = "ROOT\\Microsoft\\Windows\\Storage";
20      private static final String MSFT_STORAGE_POOL_WHERE_IS_PRIMORDIAL_FALSE = "MSFT_StoragePool WHERE IsPrimordial=FALSE";
21      private static final String MSFT_STORAGE_POOL_TO_PHYSICAL_DISK = "MSFT_StoragePoolToPhysicalDisk";
22      private static final String MSFT_PHYSICAL_DISK = "MSFT_PhysicalDisk";
23      private static final String MSFT_VIRTUAL_DISK = "MSFT_VirtualDisk";
24  
25      /**
26       * Properties to identify the storage pool. The Object ID uniquely defines the pool.
27       */
28      public enum StoragePoolProperty {
29          FRIENDLYNAME, OBJECTID;
30      }
31  
32      /**
33       * Properties to link a storage pool with a physical disk. OSHI parses these references to strings that can match
34       * the object IDs.
35       */
36      public enum StoragePoolToPhysicalDiskProperty {
37          STORAGEPOOL, PHYSICALDISK;
38      }
39  
40      /**
41       * Properties for a physical disk. The Object ID uniquely defines the disk.
42       */
43      public enum PhysicalDiskProperty {
44          FRIENDLYNAME, PHYSICALLOCATION, OBJECTID;
45      }
46  
47      /**
48       * Properties for a virtual disk. The Object ID uniquely defines the disk.
49       */
50      public enum VirtualDiskProperty {
51          FRIENDLYNAME, OBJECTID;
52      }
53  
54      private MSFTStorage() {
55      }
56  
57      /**
58       * Query the storage pools.
59       *
60       * @param h An instantiated {@link WmiQueryHandler}. User should have already initialized COM.
61       * @return Storage pools that are not primordial (raw disks not added to a storage space).
62       */
63      public static WmiResult<StoragePoolProperty> queryStoragePools(WmiQueryHandler h) {
64          WmiQuery<StoragePoolProperty> storagePoolQuery = new WmiQuery<>(STORAGE_NAMESPACE,
65                  MSFT_STORAGE_POOL_WHERE_IS_PRIMORDIAL_FALSE, StoragePoolProperty.class);
66          return h.queryWMI(storagePoolQuery, false);
67      }
68  
69      /**
70       * Query the storage pool to physical disk connection.
71       *
72       * @param h An instantiated {@link WmiQueryHandler}. User should have already initialized COM.
73       * @return Links between physical disks and storage pools. All raw disks will be part of the primordial pool in
74       *         addition to the storage space they are a member of.
75       */
76      public static WmiResult<StoragePoolToPhysicalDiskProperty> queryStoragePoolPhysicalDisks(WmiQueryHandler h) {
77          WmiQuery<StoragePoolToPhysicalDiskProperty> storagePoolToPhysicalDiskQuery = new WmiQuery<>(STORAGE_NAMESPACE,
78                  MSFT_STORAGE_POOL_TO_PHYSICAL_DISK, StoragePoolToPhysicalDiskProperty.class);
79          return h.queryWMI(storagePoolToPhysicalDiskQuery, false);
80      }
81  
82      /**
83       * Query the physical disks.
84       *
85       * @param h An instantiated {@link WmiQueryHandler}. User should have already initialized COM.
86       * @return The physical disks.
87       */
88      public static WmiResult<PhysicalDiskProperty> queryPhysicalDisks(WmiQueryHandler h) {
89          WmiQuery<PhysicalDiskProperty> physicalDiskQuery = new WmiQuery<>(STORAGE_NAMESPACE, MSFT_PHYSICAL_DISK,
90                  PhysicalDiskProperty.class);
91          return h.queryWMI(physicalDiskQuery, false);
92      }
93  
94      /**
95       * Query the virtual disks.
96       *
97       * @param h An instantiated {@link WmiQueryHandler}. User should have already initialized COM.
98       * @return The virtual disks.
99       */
100     public static WmiResult<VirtualDiskProperty> queryVirtualDisks(WmiQueryHandler h) {
101         WmiQuery<VirtualDiskProperty> virtualDiskQuery = new WmiQuery<>(STORAGE_NAMESPACE, MSFT_VIRTUAL_DISK,
102                 VirtualDiskProperty.class);
103         return h.queryWMI(virtualDiskQuery, false);
104     }
105 
106 }