Example usage for javax.xml.bind Unmarshaller getEventHandler

List of usage examples for javax.xml.bind Unmarshaller getEventHandler

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller getEventHandler.

Prototype

public ValidationEventHandler getEventHandler() throws JAXBException;

Source Link

Document

Return the current event handler or the default event handler if one hasn't been set.

Usage

From source file:cz.cas.lib.proarc.common.workflow.profile.WorkflowProfiles.java

private void read() throws JAXBException {
    long currentTime = file.lastModified();
    if (currentTime == lastModified) {
        return;/*  w  w w. j  a  va2s . c om*/
    }
    Unmarshaller unmarshaller = getUnmarshaller();
    ValidationEventCollector errors = (ValidationEventCollector) unmarshaller.getEventHandler();
    WorkflowDefinition fetchedWf = null;
    try {
        WorkflowDefinition wf = (WorkflowDefinition) unmarshaller.unmarshal(file);
        if (!errors.hasEvents()) {
            readCaches(wf);
            fetchedWf = wf;
        }
    } catch (UnmarshalException ex) {
        if (!errors.hasEvents()) {
            throw ex;
        }
    } finally {
        setProfiles(fetchedWf, currentTime);
    }
    if (errors.hasEvents()) {
        StringBuilder err = new StringBuilder();
        for (ValidationEvent event : errors.getEvents()) {
            err.append(event).append('\n');
        }
        throw new JAXBException(err.toString());
    }
}

From source file:org.rhq.enterprise.server.xmlschema.ServerPluginDescriptorUtil.java

/**
 * Loads a plugin descriptor from the given plugin jar and returns it. If the given jar does not
 * have a server plugin descriptor, <code>null</code> will be returned, meaning this is not
 * a server plugin jar.//ww w  .j  a  va  2s. c  o m
 *  
 * @param pluginJarFileUrl URL to a plugin jar file
 * @return the plugin descriptor found in the given plugin jar file, or <code>null</code> if there
 *         is no plugin descriptor in the jar file
 * @throws Exception if failed to parse the descriptor file found in the plugin jar
 */
public static ServerPluginDescriptorType loadPluginDescriptorFromUrl(URL pluginJarFileUrl) throws Exception {

    final Log logger = LogFactory.getLog(ServerPluginDescriptorUtil.class);

    if (pluginJarFileUrl == null) {
        throw new Exception("A valid plugin JAR URL must be supplied.");
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Loading plugin descriptor from plugin jar at [" + pluginJarFileUrl + "]...");
    }

    testPluginJarIsReadable(pluginJarFileUrl);

    JarInputStream jis = null;
    JarEntry descriptorEntry = null;

    try {
        jis = new JarInputStream(pluginJarFileUrl.openStream());
        JarEntry nextEntry = jis.getNextJarEntry();
        while (nextEntry != null && descriptorEntry == null) {
            if (PLUGIN_DESCRIPTOR_PATH.equals(nextEntry.getName())) {
                descriptorEntry = nextEntry;
            } else {
                jis.closeEntry();
                nextEntry = jis.getNextJarEntry();
            }
        }

        ServerPluginDescriptorType pluginDescriptor = null;

        if (descriptorEntry != null) {
            Unmarshaller unmarshaller = null;
            try {
                unmarshaller = getServerPluginDescriptorUnmarshaller();
                Object jaxbElement = unmarshaller.unmarshal(jis);
                pluginDescriptor = ((JAXBElement<? extends ServerPluginDescriptorType>) jaxbElement).getValue();
            } finally {
                if (unmarshaller != null) {
                    ValidationEventCollector validationEventCollector = (ValidationEventCollector) unmarshaller
                            .getEventHandler();
                    logValidationEvents(pluginJarFileUrl, validationEventCollector);
                }
            }
        }

        return pluginDescriptor;

    } catch (Exception e) {
        throw new Exception("Could not successfully parse the plugin descriptor [" + PLUGIN_DESCRIPTOR_PATH
                + "] found in plugin jar at [" + pluginJarFileUrl + "]", e);
    } finally {
        if (jis != null) {
            try {
                jis.close();
            } catch (Exception e) {
                logger.warn("Cannot close jar stream [" + pluginJarFileUrl + "]. Cause: " + e);
            }
        }
    }
}