Search in sources :

Example 1 with JIComServer

use of org.jinterop.dcom.core.JIComServer in project opennms by OpenNMS.

the class WmiClient method connect.

/**
 * {@inheritDoc}
 */
@Override
public void connect(final String domain, final String username, final String password, final String namespace) throws WmiException {
    try {
        m_Session = JISession.createSession(domain, username, password);
        m_Session.useSessionSecurity(true);
        m_Session.setGlobalSocketTimeout(5000);
        JIComServer m_ComStub = new JIComServer(JIProgId.valueOf(WMI_PROGID), m_Address, m_Session);
        final IJIComObject unknown = m_ComStub.createInstance();
        IJIComObject m_ComObject = unknown.queryInterface(WMI_CLSID);
        // This will obtain the dispatch interface
        IJIDispatch m_Dispatch = (IJIDispatch) JIObjectFactory.narrowObject(m_ComObject.queryInterface(IJIDispatch.IID));
        final JIVariant[] results = m_Dispatch.callMethodA("ConnectServer", new Object[] { new JIString(m_Address), new JIString(namespace), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), 0, JIVariant.OPTIONAL_PARAM() });
        m_WbemServices = (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    } catch (final JIException e) {
        if (m_Session != null) {
            try {
                JISession.destroySession(m_Session);
            } catch (JIException e1) {
                LOG.error("Failed to destroy session after incomplete connect with host '{}'.", m_Address, e1);
            }
        }
        throw new WmiException("Failed to establish COM session with host '" + m_Address + "': " + e.getMessage(), e);
    } catch (final UnknownHostException e) {
        if (m_Session != null) {
            try {
                JISession.destroySession(m_Session);
            } catch (JIException e1) {
                LOG.error("Failed to destroy session after unknown host '{}'.", m_Address, e1);
            }
        }
        throw new WmiException("Unknown host '" + m_Address + "'. Failed to connect to WMI agent.", e);
    }
}
Also used : JIComServer(org.jinterop.dcom.core.JIComServer) UnknownHostException(java.net.UnknownHostException) IJIComObject(org.jinterop.dcom.core.IJIComObject) JIVariant(org.jinterop.dcom.core.JIVariant) JIString(org.jinterop.dcom.core.JIString) IJIDispatch(org.jinterop.dcom.impls.automation.IJIDispatch) JIException(org.jinterop.dcom.common.JIException)

Aggregations

UnknownHostException (java.net.UnknownHostException)1 JIException (org.jinterop.dcom.common.JIException)1 IJIComObject (org.jinterop.dcom.core.IJIComObject)1 JIComServer (org.jinterop.dcom.core.JIComServer)1 JIString (org.jinterop.dcom.core.JIString)1 JIVariant (org.jinterop.dcom.core.JIVariant)1 IJIDispatch (org.jinterop.dcom.impls.automation.IJIDispatch)1