Skip to content

AlertDefinitionCommand sync fails with class cast error #22

Description

@rmc3

Attempting to sync the following XML via hqapi.sh alertdefinition sync --file produces the error immediately after it.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AlertDefinitionResponse>
    <Status>Success</Status>
    <AlertDefinition mtime="1354626457188" ctime="1353803425597" id="298258" name="Linux Platform Availability - PROBLEM" description="https://extranet.contegix.com/display/CNT/Platform+Availability" parent="98431" priority="2" enabled="false" active="false" frequency="0" count="0" range="0" willRecover="true" notifyFiltered="false" controlFiltered="false">
        <Resource id="178032" name="rmc-testing.contegix.com"/>
        <Escalation id="11009" name="RMC Escalation" description="Test escalation for Rich" pauseAllowed="false" maxPauseTime="9223372036854775807" notifyAll="false" repeat="true"/>
        <AlertCondition required="true" type="1" thresholdValue="1.0" thresholdComparator="!=" thresholdMetric="Availability"/>
        <AlertAction id="11744316" className="org.hyperic.hq.measurement.action.MetricAlertAction"/>
    </AlertDefinition>
</AlertDefinitionResponse>

Error running command: null
java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at org.hyperic.hq.hqapi1.XmlUtil.deserialize(XmlUtil.java:68)
at org.hyperic.hq.hqapi1.tools.AlertDefinitionCommand.sync(AlertDefinitionCommand.java:439)
at org.hyperic.hq.hqapi1.tools.AlertDefinitionCommand.handleCommand(AlertDefinitionCommand.java:152)
at org.hyperic.hq.hqapi1.tools.Shell.dispatchCommand(Shell.java:244)
at org.hyperic.hq.hqapi1.tools.Shell.main(Shell.java:259)

Digging into the source, the issue seems to be that the sync method in AlertDefinitionCommand.java passes an object of type AlertDefinitionsResponse to XmlUtil.deserialize, which then attempts to cast it to an object of type AlertDefinitionResponse. I confirmed this by modifying the deserialize method in XmlUtil.java to have some debugging text:

    public static <T> T deserialize(Class<T> res, InputStream is)
        throws JAXBException
    {
        String pkg = res.getPackage().getName();
        JAXBContext jc = getCachedContext(pkg);
        Unmarshaller u = jc.createUnmarshaller();
        u.setEventHandler(new DefaultValidationEventHandler());
        Object unmarshalOut = u.unmarshal(is);
        System.out.println(res.getName());
        System.out.println(unmarshalOut.getClass().getName());
        return res.cast(unmarshalOut);
    }

When built and run with the same command, it produces the following output:

org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse
org.hyperic.hq.hqapi1.types.AlertDefinitionResponse
Error running command: null
java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at org.hyperic.hq.hqapi1.XmlUtil.deserialize(XmlUtil.java:68)
at org.hyperic.hq.hqapi1.tools.AlertDefinitionCommand.sync(AlertDefinitionCommand.java:439)
at org.hyperic.hq.hqapi1.tools.AlertDefinitionCommand.handleCommand(AlertDefinitionCommand.java:152)
at org.hyperic.hq.hqapi1.tools.Shell.dispatchCommand(Shell.java:244)
at org.hyperic.hq.hqapi1.tools.Shell.main(Shell.java:259)

This demonstrates that the res object is of type org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse while the object returned by the JAXB unmarshaller is of type org.hyperic.hq.hqapi1.types.AlertDefinitionResponse.

The only workaround I have found so far is to manually change the root node of the XML to be synced to be AlertDefinitionsResponse instead of AlertDefinitionResponse, such as the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AlertDefinitionsResponse>
    <Status>Success</Status>
    <AlertDefinition mtime="1354626457188" ctime="1353803425597" id="298258" name="Linux Platform Availability - PROBLEM" description="https://extranet.contegix.com/display/CNT/Platform+Availability" parent="98431" priority="2" enabled="false" active="false" frequency="0" count="0" range="0" willRecover="true" notifyFiltered="false" controlFiltered="false">
        <Resource id="178032" name="rmc-testing.contegix.com"/>
        <Escalation id="11009" name="RMC Escalation" description="Test escalation for Rich" pauseAllowed="false" maxPauseTime="9223372036854775807" notifyAll="false" repeat="true"/>
        <AlertCondition required="true" type="1" thresholdValue="1.0" thresholdComparator="!=" thresholdMetric="Availability"/>
        <AlertAction id="11744316" className="org.hyperic.hq.measurement.action.MetricAlertAction"/>
    </AlertDefinition>
</AlertDefinitionsResponse>

This issue has also been reported here: http://communities.vmware.com/message/2164480

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions