Class WmiQueryHandler


  • @ThreadSafe
    public class WmiQueryHandler
    extends java.lang.Object
    Utility to handle WMI Queries. Designed to be extended with user-customized behavior.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected WmiQueryHandler()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static WmiQueryHandler createInstance()
      Factory method to create an instance of this class.
      int getComThreading()
      Returns the current threading model for COM initialization, as OSHI is required to match if an external program has COM initialized already.
      int getWmiTimeout()
      Gets the current WMI timeout.
      protected void handleComException​(com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery<?> query, com.sun.jna.platform.win32.COM.COMException ex)
      COM Exception handler.
      boolean initCOM()
      Initializes COM library and sets security to impersonate the local user
      protected boolean initCOM​(int coInitThreading)
      Initializes COM with a specific threading model
      boolean isSecurityInitialized()
      Security only needs to be initialized once.
      <T extends java.lang.Enum<T>>
      com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult<T>
      queryWMI​(com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery<T> query)
      Query WMI for values.
      <T extends java.lang.Enum<T>>
      com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult<T>
      queryWMI​(com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery<T> query, boolean initCom)
      Query WMI for values.
      static void setInstanceClass​(java.lang.Class<? extends WmiQueryHandler> instanceClass)
      Define a subclass to be instantiated by createInstance().
      void setWmiTimeout​(int wmiTimeout)
      Sets the WMI timeout.
      int switchComThreading()
      Switches the current threading model for COM initialization, as OSHI is required to match if an external program has COM initialized already.
      void unInitCOM()
      UnInitializes COM library.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • globalTimeout

        private static int globalTimeout
      • wmiTimeout

        private int wmiTimeout
      • failedWmiClassNames

        private final java.util.Set<java.lang.String> failedWmiClassNames
      • comThreading

        private int comThreading
      • securityInitialized

        private boolean securityInitialized
      • EMPTY_CLASS_ARRAY

        private static final java.lang.Class<?>[] EMPTY_CLASS_ARRAY
      • EMPTY_OBJECT_ARRAY

        private static final java.lang.Object[] EMPTY_OBJECT_ARRAY
      • customClass

        private static java.lang.Class<? extends WmiQueryHandler> customClass
    • Constructor Detail

      • WmiQueryHandler

        protected WmiQueryHandler()
    • Method Detail

      • queryWMI

        public <T extends java.lang.Enum<T>> com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult<T> queryWMI​(com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery<T> query)
        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 java.lang.Enum<T>> com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult<T> queryWMI​(com.sun.jna.platform.win32.COM.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 properties
        initCom - Whether to initialize COM. If true, initializes COM before the query and uninitializes after. If false, assumes the user has initialized COM separately. This can improve WMI query performance.
        Returns:
        a WmiResult object containing the query results, wrapping an EnumMap
      • handleComException

        protected void handleComException​(com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery<?> query,
                                          com.sun.jna.platform.win32.COM.COMException ex)
        COM Exception handler. Logs a warning message.
        Parameters:
        query - a WbemcliUtil.WmiQuery object.
        ex - a COMException object.
      • 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
      • 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
      • 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
      • 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).