Class WmiQueryHandler
java.lang.Object
oshi.util.platform.windows.WmiQueryHandler
- All Implemented Interfaces:
WmiQueryExecutor
Utility to handle WMI Queries. Designed to be extended with user-customized behavior.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic WmiQueryHandlerFactory method to create an instance of this class.intReturns the current threading model for COM initialization, as OSHI is required to match if an external program has COM initialized already.intGets the current WMI timeout.protected voidhandleComException(WbemcliUtil.WmiQuery<?> query, COMException ex) COM Exception handler.booleaninitCOM()Initializes COM library and sets security to impersonate the local userprotected booleaninitCOM(int coInitThreading) Initializes COM with a specific threading modelbooleanSecurity only needs to be initialized once.<T extends Enum<T>>
WbemcliUtil.WmiResult<T> queryWMI(WbemcliUtil.WmiQuery<T> query) Query WMI for values.<T extends Enum<T>>
WbemcliUtil.WmiResult<T> queryWMI(WbemcliUtil.WmiQuery<T> query, boolean initCom) Query WMI for values.Query WMI for values.Query WMI for values.static voidsetInstanceClass(Class<? extends WmiQueryHandler> instanceClass) Define a subclass to be instantiated bycreateInstance().voidsetWmiTimeout(int wmiTimeout) Sets the WMI timeout.intSwitches the current threading model for COM initialization, as OSHI is required to match if an external program has COM initialized already.intswitchComThreadingFrom(int expected) Switches the current threading model only if it still matches the expected value, avoiding a toggle race when multiple threads callinitCOM()concurrently.voidUnInitializes COM library.
-
Constructor Details
-
WmiQueryHandler
protected WmiQueryHandler()Creates the WMI query handler. Subclasses may override viasetInstanceClass(Class).
-
-
Method Details
-
createInstance
Factory method to create an instance of this class. To override this class, usesetInstanceClass(Class)to define a subclass which extendsWmiQueryHandler.- Returns:
- An instance of this class or a class defined by
setInstanceClass(Class)
-
setInstanceClass
Define a subclass to be instantiated bycreateInstance(). The class must extendWmiQueryHandler.- Parameters:
instanceClass- The class to instantiate withcreateInstance().
-
queryWMI
Query WMI for values. Makes no assumptions on whether the user has previously initialized COM.- Type Parameters:
T- WMI queries use an Enum to identify the fields to query, and use the enum values as keys to retrieve the results.- Parameters:
query- A WmiQuery object encapsulating the namespace, class, and properties- Returns:
- a WmiResult object containing the query results, wrapping an EnumMap
-
queryWMI
public <T extends Enum<T>> WbemcliUtil.WmiResult<T> queryWMI(WbemcliUtil.WmiQuery<T> query, boolean initCom) Query WMI for values.- Type Parameters:
T- WMI queries use an Enum to identify the fields to query, and use the enum values as keys to retrieve the results.- Parameters:
query- A WmiQuery object encapsulating the namespace, class, and propertiesinitCom- Whether to initialize COM. Iftrue, initializes COM before the query and uninitializes after. Iffalse, assumes the user has initialized COM separately. This can improve WMI query performance.- Returns:
- a WmiResult object containing the query results, wrapping an EnumMap
-
queryWMI
Description copied from interface:WmiQueryExecutorQuery WMI for values. Initializes and uninitializes COM for each query.- Specified by:
queryWMIin interfaceWmiQueryExecutor- Type Parameters:
T- the enum type for the query properties- Parameters:
query- the WMI query- Returns:
- the query results
-
queryWMI
Description copied from interface:WmiQueryExecutorQuery WMI for values.- Specified by:
queryWMIin interfaceWmiQueryExecutor- Type Parameters:
T- the enum type for the query properties- Parameters:
query- the WMI queryinitCom- whether to initialize COM for this query. Iffalse, assumes COM is already initialized.- Returns:
- the query results
-
handleComException
COM Exception handler. Logs at debug level for known-optional classes, otherwise warns.- Parameters:
query- aWbemcliUtil.WmiQueryobject.ex- aCOMExceptionobject.
-
initCOM
public boolean initCOM()Initializes COM library and sets security to impersonate the local user- Returns:
- True if COM was initialized and needs to be uninitialized, false otherwise
- Throws:
COMException- if COM initialization fails with an unexpected error
-
initCOM
protected boolean initCOM(int coInitThreading) Initializes COM with a specific threading model- Parameters:
coInitThreading- The threading model- Returns:
- True if COM was initialized and needs to be uninitialized, false otherwise
- Throws:
COMException- if COM initialization fails with an unexpected error
-
unInitCOM
public void unInitCOM()UnInitializes COM library. This should be called once for every successful call to initCOM. -
getComThreading
public int getComThreading()Returns the current threading model for COM initialization, as OSHI is required to match if an external program has COM initialized already.- Returns:
- The current threading model
-
switchComThreading
public int switchComThreading()Switches the current threading model for COM initialization, as OSHI is required to match if an external program has COM initialized already.- Returns:
- The new threading model after switching
-
switchComThreadingFrom
public int switchComThreadingFrom(int expected) Switches the current threading model only if it still matches the expected value, avoiding a toggle race when multiple threads callinitCOM()concurrently.- Parameters:
expected- the threading model observed before the failed initCOM attempt- Returns:
- the new threading model if switched, or the current value if already switched by another thread
-
isSecurityInitialized
public boolean isSecurityInitialized()Security only needs to be initialized once. This boolean identifies whether that has happened.- Returns:
- Returns the securityInitialized.
-
getWmiTimeout
public int getWmiTimeout()Gets the current WMI timeout. WMI queries will fail if they take longer than this number of milliseconds. A value of -1 is infinite (no timeout).- Returns:
- Returns the current value of wmiTimeout.
-
setWmiTimeout
public void setWmiTimeout(int wmiTimeout) Sets the WMI timeout. WMI queries will fail if they take longer than this number of milliseconds.- Parameters:
wmiTimeout- The wmiTimeout to set, in milliseconds. To disable timeouts, set timeout as -1 (infinite).
-