Package com.espertech.esper.event

Source Code of com.espertech.esper.event.EventSenderXMLDOM

/**************************************************************************************
* Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
* http://esper.codehaus.org                                                          *
* http://www.espertech.com                                                           *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license       *
* a copy of which has been included with this distribution in the license.txt file.  *
**************************************************************************************/
package com.espertech.esper.event;

import com.espertech.esper.client.EPException;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.EventSender;
import com.espertech.esper.core.EPRuntimeEventSender;
import com.espertech.esper.core.thread.InboundUnitSendWrapped;
import com.espertech.esper.core.thread.ThreadingOption;
import com.espertech.esper.core.thread.ThreadingService;
import com.espertech.esper.event.xml.BaseXMLEventType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* Event sender for XML DOM-backed events.
* <p>
* Allows sending only event objects of type Node or Document, does check the root name of the XML document
* which must match the event type root name as configured. Any other event object generates an error.
*/
public class EventSenderXMLDOM implements EventSender
{
    private final EPRuntimeEventSender runtimeEventSender;
    private final BaseXMLEventType baseXMLEventType;
    private final boolean validateRootElement;
    private final EventAdapterService eventAdapterService;
    private final ThreadingService threadingService;

    /**
     * Ctor.
     * @param runtimeEventSender for processing events
     * @param baseXMLEventType the event type
     * @param threadingService for inbound threading
     * @param eventAdapterService for event bean creation
     */
    public EventSenderXMLDOM(EPRuntimeEventSender runtimeEventSender, BaseXMLEventType baseXMLEventType, EventAdapterService eventAdapterService, ThreadingService threadingService)
    {
        this.runtimeEventSender = runtimeEventSender;
        this.baseXMLEventType = baseXMLEventType;
        this.validateRootElement = baseXMLEventType.getConfigurationEventTypeXMLDOM().isEventSenderValidatesRoot();
        this.eventAdapterService = eventAdapterService;
        this.threadingService = threadingService;       
    }

    public void sendEvent(Object node) throws EPException
    {
        sendEvent(node, false);
    }

    public void route(Object node) throws EPException
    {
        sendEvent(node, true);
    }

    private void sendEvent(Object node, boolean isRoute) throws EPException
    {
        Node namedNode;
        if (node instanceof Document)
        {
            namedNode = ((Document) node).getDocumentElement();
        }
        else if (node instanceof Element)
        {
            namedNode = (Element) node;
        }
        else
        {
            throw new EPException("Unexpected event object type '" + node.getClass().getName() + "' encountered, please supply a org.w3c.dom.Document or Element node");
        }

        if (validateRootElement)
        {
            String getNodeName = namedNode.getLocalName();
            if (getNodeName == null)
            {
                getNodeName = namedNode.getNodeName();
            }

            if (!getNodeName.equals(baseXMLEventType.getRootElementName()))
            {
                throw new EPException("Unexpected root element name '" + getNodeName + "' encountered, expected a root element name of '" + baseXMLEventType.getRootElementName() + "'");
            }
        }

        EventBean event = eventAdapterService.adapterForTypedDOM(namedNode, baseXMLEventType);
        if (isRoute)
        {
            runtimeEventSender.routeEventBean(event);           
        }
        else
        {
            if ((ThreadingOption.isThreadingEnabled) && (threadingService.isInboundThreading()))
            {
                threadingService.submitInbound(new InboundUnitSendWrapped(event, runtimeEventSender));
            }
            else
            {
                runtimeEventSender.processWrappedEvent(event);
            }
        }
    }
}
TOP

Related Classes of com.espertech.esper.event.EventSenderXMLDOM

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.