Package org.wso2.carbon.cep.core.listener

Source Code of org.wso2.carbon.cep.core.listener.CEPEventListener

/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wso2.carbon.cep.core.listener;

import org.apache.axiom.om.OMElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.broker.core.BrokerConfiguration;
import org.wso2.carbon.cep.core.Output;
import org.wso2.carbon.cep.core.internal.config.BrokerConfigurationHelper;
import org.wso2.carbon.cep.core.internal.process.CEPEventProcessor;
import org.wso2.carbon.cep.core.internal.ds.CEPServiceValueHolder;
import org.wso2.carbon.broker.core.exception.BrokerEventProcessingException;
import org.wso2.carbon.broker.core.BrokerService;
import org.wso2.carbon.core.multitenancy.SuperTenantCarbonContext;

import java.util.List;

/**
* this class used to receive the events from the cep engine. And then it passes those events
* through the event sender.
*/

public class CEPEventListener {

    private static final Log log = LogFactory.getLog(CEPEventListener.class);

    private Output output;

    private int tenantId;

    private String userName;

    public CEPEventListener(Output output, int tenantId, String userName) {
        this.output = output;
        this.tenantId = tenantId;
        this.userName = userName;
    }

    /**
     * this method is called by the back end runtime engine. then it process the events and
     * convert them back to omElements as specified in the event description elements.
     *
     * @param events - row events generated by the cep engine
     */
    public void onComplexEvent(List events) {

        try {
            SuperTenantCarbonContext.startTenantFlow();
            SuperTenantCarbonContext.getCurrentContext().setTenantId(tenantId);
            SuperTenantCarbonContext.getCurrentContext().getTenantDomain(true);
            SuperTenantCarbonContext.getCurrentContext().setUsername(userName);

            OMElement omElementToSend = null;
            for (Object event : events) {
                if (output.getXmlMapping() != null) {
                    omElementToSend =
                            CEPEventProcessor.getXMLElement(event, output.getXmlMapping());
                } else if (output.getElementMapping() != null) {
                    omElementToSend =
                            CEPEventProcessor.getXMLElement(event, output.getElementMapping());
                }

                if (output.getBrokerName() != null) {
                    BrokerService brokerService = CEPServiceValueHolder.getInstance().getBrokerService();
                    BrokerConfigurationHelper brokerConfigurationHelper = new BrokerConfigurationHelper();
                    BrokerConfiguration brokerConfiguration =
                            brokerConfigurationHelper.getBrokerConfiguration(output.getBrokerName(), tenantId);
                    brokerService.publish(brokerConfiguration,
                            output.getTopic(),
                            omElementToSend);
                }
            }
        } catch (BrokerEventProcessingException e) {
            log.error("Can not send the message using broker ", e);
        } finally {
            SuperTenantCarbonContext.endTenantFlow();
        }
    }
}
TOP

Related Classes of org.wso2.carbon.cep.core.listener.CEPEventListener

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.