예제 #1
0
 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();
     }
   }
 }
예제 #2
0
  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);
        }
      }
    }
  }