Search in sources :

Example 1 with BConnector

use of org.ballerinalang.model.values.BConnector in project carbon-apimgt by wso2.

the class Publish method execute.

@Override
public BValue execute(Context context) {
    log.info("publishing event to DAS");
    BConnector bConnector = (BConnector) getRefArgument(context, 0);
    BJSON json = (BJSON) getRefArgument(context, 1);
    String streamName = json.value().get(Constants.STREAM_NAME).asText();
    String streamVersion = json.value().get(Constants.STREAM_VERSION).asText();
    ArrayNode metaData = (ArrayNode) json.value().get(Constants.META_DATA);
    ArrayNode correlationData = (ArrayNode) json.value().get(Constants.CORRELATION_DATA);
    ArrayNode payloadData = (ArrayNode) json.value().get(Constants.PAYLOAD_DATA);
    Object[] metaDataArr = new Object[metaData.size()];
    JSONArray jsonMetaData = new JSONArray(metaData.toString());
    for (int i = 0; i < jsonMetaData.length(); i++) {
        metaDataArr[i] = jsonMetaData.get(i);
    }
    Object[] correlationDataArr = new Object[correlationData.size()];
    JSONArray jsonCorrelationData = new JSONArray(correlationData.toString());
    for (int i = 0; i < jsonCorrelationData.length(); i++) {
        correlationDataArr[i] = jsonCorrelationData.get(i);
    }
    Object[] payloadDataArr = new Object[payloadData.size()];
    JSONArray jsonPayloadData = new JSONArray(payloadData.toString());
    for (int i = 0; i < jsonPayloadData.length(); i++) {
        payloadDataArr[i] = jsonPayloadData.get(i);
    }
    BMap sharedMap = (BMap) bConnector.getRefField(1);
    EventPublisher pub = (EventPublisher) sharedMap.get(Constants.PUBLISHER_INSTANCE);
    Event event = new Event();
    event.setStreamId(DataBridgeCommonsUtils.generateStreamId(streamName, streamVersion));
    event.setMetaData(metaDataArr);
    event.setCorrelationData(correlationDataArr);
    event.setPayloadData(payloadDataArr);
    pub.publish(event);
    return null;
}
Also used : BConnector(org.ballerinalang.model.values.BConnector) BMap(org.ballerinalang.model.values.BMap) JSONArray(org.json.JSONArray) Event(org.wso2.carbon.databridge.commons.Event) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with BConnector

use of org.ballerinalang.model.values.BConnector in project carbon-apimgt by wso2.

the class Init method execute.

@Override
public BValue execute(Context context) {
    BConnector bConnector = (BConnector) getRefArgument(context, 0);
    BMap optionMap = (BMap) bConnector.getRefField(0);
    BMap sharedMap = (BMap) bConnector.getRefField(1);
    System.setProperty("javax.net.ssl.trustStore", System.getProperty("ballerina.home") + File.separator + "bre" + File.separator + "security" + File.separator + "wso2carbon.jks");
    System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
    String type = optionMap.get(Constants.TYPE).stringValue();
    String receiverURLSet = optionMap.get(Constants.RECEIVER_URL_SET).stringValue();
    String authURLSet = optionMap.get(Constants.AUTH_URL_SET).stringValue();
    String username = optionMap.get(Constants.USERNAME).stringValue();
    String password = optionMap.get(Constants.PASSWORD).stringValue();
    String configPath = optionMap.get(Constants.CONFIG_PATH).stringValue();
    configPath = System.getProperty("ballerina.home") + File.separator + configPath;
    try {
        initDataPublisher(type, receiverURLSet, authURLSet, username, password, configPath);
        sharedMap.put(Constants.PUBLISHER_INSTANCE, this);
    } catch (DataEndpointAuthenticationException e) {
        log.error("Error occurred while authenticating.", e);
    } catch (DataEndpointAgentConfigurationException e) {
        log.error("Error occurred while configuring the publisher.", e);
    } catch (TransportException e) {
        log.error("Transport level exception occurred.", e);
    } catch (DataEndpointException e) {
        log.error("Data endpoint exception occurred.", e);
    } catch (DataEndpointConfigurationException e) {
        log.error("Data endpoint configuration exception occurred.", e);
    }
    return null;
}
Also used : BConnector(org.ballerinalang.model.values.BConnector) BMap(org.ballerinalang.model.values.BMap) DataEndpointAuthenticationException(org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException) DataEndpointAgentConfigurationException(org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException) DataEndpointException(org.wso2.carbon.databridge.agent.exception.DataEndpointException) TransportException(org.wso2.carbon.databridge.commons.exception.TransportException) DataEndpointConfigurationException(org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException)

Aggregations

BConnector (org.ballerinalang.model.values.BConnector)2 BMap (org.ballerinalang.model.values.BMap)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 BJSON (org.ballerinalang.model.values.BJSON)1 JSONArray (org.json.JSONArray)1 DataEndpointAgentConfigurationException (org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException)1 DataEndpointAuthenticationException (org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException)1 DataEndpointConfigurationException (org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException)1 DataEndpointException (org.wso2.carbon.databridge.agent.exception.DataEndpointException)1 Event (org.wso2.carbon.databridge.commons.Event)1 TransportException (org.wso2.carbon.databridge.commons.exception.TransportException)1