Search in sources :

Example 6 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class OnmsWbemObjectImpl method getWmiMethods.

/**
 * <p>getWmiMethods</p>
 *
 * @return a {@link org.opennms.protocols.wmi.wbem.OnmsWbemMethodSet} object.
 * @throws org.opennms.protocols.wmi.WmiException if any.
 */
@Override
public OnmsWbemMethodSet getWmiMethods() throws WmiException {
    try {
        // Get the WbemMethodSet dispatcher.
        final IJIComObject methodComObject = wbemObjectDispatch.get("Methods_").getObjectAsComObject();
        final IJIDispatch methodsSet_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(methodComObject);
        return new OnmsWbemMethodSetImpl(methodsSet_dispatch);
    } catch (final JIException e) {
        throw new WmiException("Failed to retrieve list of methods: " + e.getMessage(), e);
    }
}
Also used : IJIComObject(org.jinterop.dcom.core.IJIComObject) IJIDispatch(org.jinterop.dcom.impls.automation.IJIDispatch) WmiException(org.opennms.protocols.wmi.WmiException) JIException(org.jinterop.dcom.common.JIException)

Example 7 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class OnmsWbemPropertySetImpl method get.

/**
 * {@inheritDoc}
 */
@Override
public OnmsWbemProperty get(final Integer idx) throws WmiException {
    try {
        final IJIComObject enumComObject = wbemPropertySetDispatch.get("_NewEnum").getObjectAsComObject();
        final IJIEnumVariant enumVariant = (IJIEnumVariant) JIObjectFactory.narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
        OnmsWbemProperty wbemObj;
        IJIDispatch wbemProperty_dispatch = null;
        for (int i = 0; i < (idx + 1); i++) {
            final Object[] values = enumVariant.next(1);
            final JIArray array = (JIArray) values[0];
            final Object[] arrayObj = (Object[]) array.getArrayInstance();
            for (int j = 0; j < arrayObj.length; j++) {
                wbemProperty_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(((JIVariant) arrayObj[j]).getObjectAsComObject());
            }
        }
        wbemObj = new OnmsWbemPropertyImpl(wbemProperty_dispatch);
        return wbemObj;
    } catch (final JIException e) {
        throw new WmiException("Failed to enumerate WbemProperty variant: " + e.getMessage(), e);
    }
}
Also used : JIArray(org.jinterop.dcom.core.JIArray) IJIComObject(org.jinterop.dcom.core.IJIComObject) IJIEnumVariant(org.jinterop.dcom.impls.automation.IJIEnumVariant) IJIDispatch(org.jinterop.dcom.impls.automation.IJIDispatch) IJIComObject(org.jinterop.dcom.core.IJIComObject) OnmsWbemProperty(org.opennms.protocols.wmi.wbem.OnmsWbemProperty) WmiException(org.opennms.protocols.wmi.WmiException) JIException(org.jinterop.dcom.common.JIException)

Example 8 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class WmiManagerTest method testPerformOpValidObject.

/**
 * Test the performOp method with a valid WMI class and valid WMI object.
 *
 * Test method for
 * {@link org.opennms.protocols.wmi.WmiManager#performOp(org.opennms.protocols.wmi.WmiParams)}.
 *
 * @throws WmiException if there is a problem with the mock object.
 */
public final void testPerformOpValidObject() throws WmiException {
    // 
    // Create parameter holder.
    WmiParams params = new WmiParams(WmiParams.WMI_OPERATION_INSTANCEOF, "2/12/2004 00:00:00", "EQ", "Win32_BIOS", "ReleaseDate");
    // Set up WMI mock client.
    // 1) Expect a call to connect() with a bad hostname.
    // 2) Throw a new WmiException indictating a bad hostname.
    OnmsWbemObjectSet wos = new OnmsWbemObjectSetBiosStub(new OnmsWbemObjectBiosStub(new OnmsWbemPropSetBiosStub(new OnmsWbemPropBiosStub())));
    m_WmiMock.connect("127.0.0.1", "Administrator", "password", WmiParams.WMI_DEFAULT_NAMESPACE);
    expect(m_WmiMock.performInstanceOf("Win32_BIOS")).andReturn(wos);
    replay(m_WmiMock);
    try {
        // Create a manager.
        WmiManager wmiManager = new WmiManager("127.0.0.1", "Administrator", "password");
        // Initialize
        wmiManager.init(m_WmiMock);
        // Perform an operation.
        // WmiResult res =
        wmiManager.performOp(params);
    // assertTrue(res)
    } catch (WmiException e) {
    // assertTrue("Exception missing message: Unknown name: " + e, e
    // .getMessage().contains("Unknown name"));
    }
    verify(m_WmiMock);
    reset(m_WmiMock);
}
Also used : OnmsWbemObjectSetBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectSetBiosStub) OnmsWbemPropBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemPropBiosStub) OnmsWbemObjectBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectBiosStub) WmiParams(org.opennms.protocols.wmi.WmiParams) OnmsWbemPropSetBiosStub(org.opennms.protocols.wmi.test.stubs.OnmsWbemPropSetBiosStub) OnmsWbemObjectSet(org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet) WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Example 9 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class WmiManagerTest method testInitBadUserPass.

/**
 * Test that the WMI manager handles invalid username or password exceptions
 * properly from the WMI client.
 *
 * Test method for {@link org.opennms.protocols.wmi.WmiManager#init()}.
 *
 * @throws WmiException if there is a problem with the mock object.
 */
public final void testInitBadUserPass() throws WmiException {
    // Set up WMI mock client.
    // 1) Expect a call to connect() with a bad hostname.
    // 2) Throw a new WmiException indictating a user or password.
    m_WmiMock.connect("127.0.0.1", "Administrator", "wrongpassword", WmiParams.WMI_DEFAULT_NAMESPACE);
    expectLastCall().andThrow(new WmiException("Failed to connect to host '127.0.0.1': The attempted logon is invalid. This is either due to a bad username or authentication information. [0xC000006D]"));
    replay(m_WmiMock);
    try {
        // Create a manager.
        WmiManager wmiManager = new WmiManager("127.0.0.1", "Administrator", "wrongpassword");
        // Initialize
        wmiManager.init(m_WmiMock);
    } catch (WmiException e) {
        assertTrue("Exception missing message: The attempted logon is invalid: " + e, e.getMessage().contains("The attempted logon is invalid"));
    }
    verify(m_WmiMock);
    reset(m_WmiMock);
}
Also used : WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Example 10 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class WmiManagerTest method testInitBadHostname.

/**
 * Test that the WMI manager properly handles invalid host exceptions
 * from the WMI client.
 *
 * Test method for {@link org.opennms.protocols.wmi.WmiManager#init()}.
 *
 * @throws WmiException if there is a problem with the mock object.
 */
public final void testInitBadHostname() throws WmiException {
    // Set up WMI mock client.
    // 1) Expect a call to connect() with a bad hostname.
    // 2) Throw a new WmiException indictating a bad hostname.
    m_WmiMock.connect("bad-hostname", "Administrator", "password", WmiParams.WMI_DEFAULT_NAMESPACE);
    expectLastCall().andThrow(new WmiException("Unknown host 'bad-hostname'. Failed to connect to WMI agent."));
    replay(m_WmiMock);
    try {
        // Create a manager.
        WmiManager wmiManager = new WmiManager("bad-hostname", "Administrator", "password");
        // Initialize
        wmiManager.init(m_WmiMock);
    } catch (WmiException e) {
        assertTrue("Exception missing message: Unknown host: " + e, e.getMessage().contains("Unknown host"));
    }
    verify(m_WmiMock);
    reset(m_WmiMock);
}
Also used : WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Aggregations

WmiException (org.opennms.protocols.wmi.WmiException)17 WmiManager (org.opennms.protocols.wmi.WmiManager)9 JIException (org.jinterop.dcom.common.JIException)7 IJIComObject (org.jinterop.dcom.core.IJIComObject)7 IJIDispatch (org.jinterop.dcom.impls.automation.IJIDispatch)7 WmiParams (org.opennms.protocols.wmi.WmiParams)5 JIArray (org.jinterop.dcom.core.JIArray)4 IJIEnumVariant (org.jinterop.dcom.impls.automation.IJIEnumVariant)4 WmiResult (org.opennms.protocols.wmi.WmiResult)3 OnmsWbemObjectSet (org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet)3 WmiAgentConfig (org.opennms.netmgt.config.wmi.WmiAgentConfig)2 OnmsWbemObjectBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectBiosStub)2 OnmsWbemObjectSetBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectSetBiosStub)2 OnmsWbemPropBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemPropBiosStub)2 OnmsWbemPropSetBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemPropSetBiosStub)2 OnmsWbemObject (org.opennms.protocols.wmi.wbem.OnmsWbemObject)2 OnmsWbemProperty (org.opennms.protocols.wmi.wbem.OnmsWbemProperty)2 InetAddress (java.net.InetAddress)1 JIVariant (org.jinterop.dcom.core.JIVariant)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1