@Override public void subscribeToTransportAdaptor() { if (this.subscriptionId == null) { int tenantId = PrivilegedCarbonContext.getCurrentContext(this.axisConfiguration).getTenantId(); String inputTransportAdaptorName = eventBuilderConfiguration.getInputStreamConfiguration().getTransportAdaptorName(); try { InputTransportAdaptorConfiguration inputTransportAdaptorConfiguration = EventBuilderServiceValueHolder.getInputTransportAdaptorManagerService() .getInputTransportAdaptorConfiguration(inputTransportAdaptorName, tenantId); this.subscriptionId = EventBuilderServiceValueHolder.getInputTransportAdaptorService() .subscribe( inputTransportAdaptorConfiguration, eventBuilderConfiguration .getInputStreamConfiguration() .getInputTransportAdaptorMessageConfiguration(), new XmlInputTransportListener(), axisConfiguration); } catch (InputTransportAdaptorManagerConfigurationException e) { log.error( "Cannot subscribe to input transport adaptor " + inputTransportAdaptorName + ":\n" + e.getMessage()); throw new EventBuilderConfigurationException(e); } } }
public boolean isStreamDefinitionValidForConfiguration( EventBuilderConfiguration eventBuilderConfiguration, StreamDefinition exportedStreamDefinition) { if (!(eventBuilderConfiguration.getToStreamName().equals(exportedStreamDefinition.getName()) && eventBuilderConfiguration .getToStreamVersion() .equals(exportedStreamDefinition.getVersion()))) { return false; } if (eventBuilderConfiguration.getInputMapping() instanceof XMLInputMapping) { XMLInputMapping xmlInputMapping = (XMLInputMapping) eventBuilderConfiguration.getInputMapping(); for (InputMappingAttribute inputMappingAttribute : xmlInputMapping.getInputMappingAttributes()) { Attribute attribute = new Attribute( inputMappingAttribute.getToElementKey(), inputMappingAttribute.getToElementType()); if (!exportedStreamDefinition.getPayloadData().contains(attribute)) { return false; } } } else { return false; } return true; }
public static void saveConfigurationToFileSystem( EventBuilderConfiguration eventBuilderConfiguration, String filePath) throws EventBuilderConfigurationException { String inputMappingType = eventBuilderConfiguration.getInputMapping().getMappingType(); OMElement ebConfigElement = EventBuilderConfigHelper.getEventBuilderConfigBuilder(inputMappingType) .eventBuilderConfigurationToOM(eventBuilderConfiguration); save(ebConfigElement.toString(), filePath); }
@Override public void loadEventBuilderConfiguration() { if (this.eventBuilderConfiguration != null) { String outputStreamName = eventBuilderConfiguration.getToStreamName(); String outputStreamVersion = eventBuilderConfiguration.getToStreamVersion(); try { this.exportedStreamDefinition = new StreamDefinition(outputStreamName, outputStreamVersion); } catch (MalformedStreamDefinitionException e) { throw new EventBuilderConfigurationException( "Could not create stream definition with " + outputStreamName + EventBuilderConstants.STREAM_NAME_VER_DELIMITER + outputStreamVersion); } subscribeToTransportAdaptor(); createMapping(eventBuilderConfiguration, exportedStreamDefinition, attributeXpathList); this.eventBuilderConfiguration.setDeploymentStatus(DeploymentStatus.DEPLOYED); } }
public void createMapping( EventBuilderConfiguration eventBuilderConfiguration, StreamDefinition exportedStreamDefinition, List<XPathData> attributeXpathList) { if (eventBuilderConfiguration != null && eventBuilderConfiguration.getInputMapping() instanceof XMLInputMapping) { XMLInputMapping xmlInputMapping = (XMLInputMapping) eventBuilderConfiguration.getInputMapping(); XPathDefinition xPathDefinition = xmlInputMapping.getXpathDefinition(); for (InputMappingAttribute inputMappingAttribute : xmlInputMapping.getInputMappingAttributes()) { String xpathExpr = inputMappingAttribute.getFromElementKey(); try { AXIOMXPath xpath = new AXIOMXPath(xpathExpr); if (xPathDefinition != null && !xPathDefinition.isEmpty()) { xpath.addNamespace(xPathDefinition.getPrefix(), xPathDefinition.getNamespaceUri()); } String type = EventBuilderConstants.ATTRIBUTE_TYPE_CLASS_TYPE_MAP.get( inputMappingAttribute.getToElementType()); attributeXpathList.add( new XPathData(xpath, type, inputMappingAttribute.getDefaultValue())); exportedStreamDefinition.addPayloadData( inputMappingAttribute.getToElementKey(), inputMappingAttribute.getToElementType()); } catch (JaxenException e) { String errMsg = "Error parsing XPath expression: " + xpathExpr; log.error(errMsg, e); throw new EventBuilderConfigurationException(errMsg, e); } } if (!isStreamDefinitionValidForConfiguration( eventBuilderConfiguration, exportedStreamDefinition)) { throw new EventBuilderValidationException( "Exported stream definition does not match the specified configuration."); } } }
@Override public void unsubscribeFromTransportAdaptor( InputTransportAdaptorConfiguration inputTransportAdaptorConfiguration) { if (this.subscriptionId != null) { EventBuilderServiceValueHolder.getInputTransportAdaptorService() .unsubscribe( eventBuilderConfiguration .getInputStreamConfiguration() .getInputTransportAdaptorMessageConfiguration(), inputTransportAdaptorConfiguration, axisConfiguration, this.subscriptionId); this.subscriptionId = null; } }
private void sendMultipleEvents(Object obj) { if (obj instanceof OMElement) { OMElement events = (OMElement) obj; if (eventBuilderConfiguration.isTraceEnabled()) { trace.info("[Event-Builder] Received batch of events as OMElement.\n" + events.toString()); } Iterator childIterator = events.getChildElements(); while (childIterator.hasNext()) { Object eventObj = childIterator.next(); sendEvent(eventObj); /** * Usually the global lookup '//' is used in the XPATH expression which works fine for * 'single event mode'. However, if global lookup is used, it will return the first element * from the whole document as specified in XPATH-2.0 Specification. Therefore the same XPATH * expression that works fine in 'single event mode' will always return the first element of * a batch in 'batch mode'. Therefore to return what the user expects, each child element is * removed after sending to simulate an iteration for the global lookup. */ childIterator.remove(); } } }
private void sendEvent(Object obj) { if (obj instanceof OMElement) { OMElement eventOMElement = (OMElement) obj; if (eventBuilderConfiguration.isTraceEnabled()) { trace.info("[Event-Builder] Received event as OMElement.\n" + eventOMElement.toString()); } OMNamespace omNamespace = null; if (this.xPathDefinition == null || this.xPathDefinition.isEmpty()) { omNamespace = eventOMElement.getNamespace(); } List<Object> objList = new ArrayList<Object>(); for (XPathData xpathData : attributeXpathList) { AXIOMXPath xpath = xpathData.getXpath(); OMElement omElementResult = null; String type = xpathData.getType(); try { if (omNamespace != null) { xpath.addNamespaces(eventOMElement); } omElementResult = (OMElement) xpath.selectSingleNode(eventOMElement); Class<?> beanClass = Class.forName(type); Object returnedObj = null; if (omElementResult != null) { returnedObj = BeanUtil.deserialize( beanClass, omElementResult, reflectionBasedObjectSupplier, null); } else if (xpathData.getDefaultValue() != null) { if (!beanClass.equals(String.class)) { Class<?> stringClass = String.class; Method valueOfMethod = beanClass.getMethod("valueOf", stringClass); returnedObj = valueOfMethod.invoke(null, xpathData.getDefaultValue()); } else { returnedObj = xpathData.getDefaultValue(); } log.warn("Unable to parse XPath to retrieve required attribute. Sending defaults."); } else { log.warn( "Unable to parse XPath to retrieve required attribute. Skipping to next attribute."); } objList.add(returnedObj); } catch (JaxenException e) { throw new EventBuilderConfigurationException("Error parsing xpath for " + xpath, e); } catch (ClassNotFoundException e) { throw new EventBuilderConfigurationException( "Cannot find specified class for type " + type); } catch (AxisFault axisFault) { throw new EventBuilderConfigurationException( "Error deserializing OMElement " + omElementResult, axisFault); } catch (NoSuchMethodException e) { throw new EventBuilderConfigurationException( "Error trying to convert default value to specified target type.", e); } catch (InvocationTargetException e) { throw new EventBuilderConfigurationException( "Error trying to convert default value to specified target type.", e); } catch (IllegalAccessException e) { throw new EventBuilderConfigurationException( "Error trying to convert default value to specified target type.", e); } } Object[] objArray = objList.toArray(new Object[objList.size()]); if (!this.basicEventListeners.isEmpty()) { if (eventBuilderConfiguration.isTraceEnabled()) { trace.info( "[Event-Builder] Sending event object array " + Arrays.toString(objArray) + " to all registered basic event listeners"); } for (BasicEventListener basicEventListener : basicEventListeners) { basicEventListener.onEvent(objArray); } } if (!this.wso2EventListeners.isEmpty()) { Event event = new Event( exportedStreamDefinition.getStreamId(), System.currentTimeMillis(), null, null, objArray); if (eventBuilderConfiguration.isTraceEnabled()) { trace.info( "[Event-Builder] Sending event " + event.toString() + " to all registered wso2 event listeners"); } for (Wso2EventListener wso2EventListener : wso2EventListeners) { wso2EventListener.onEvent(event); } } } }