private void increaseSearchIndexStartTimeDelay() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {
      AXIOMXPath xpathExpression =
          new AXIOMXPath("/wso2registry/indexingConfiguration/startingDelayInSeconds");
      OMElement indexConfigNode = (OMElement) xpathExpression.selectSingleNode(documentElement);
      indexConfigNode.setText("2");

      AXIOMXPath xpathExpression1 =
          new AXIOMXPath("/wso2registry/indexingConfiguration/indexingFrequencyInSeconds");
      OMElement indexConfigNode1 = (OMElement) xpathExpression1.selectSingleNode(documentElement);
      indexConfigNode1.setText("1");

      fileOutputStream = new FileOutputStream(getRegistryXMLPath());
      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
      documentElement.serialize(writer);
      documentElement.build();
      Thread.sleep(2000);

    } catch (Exception e) {
      log.error("registry.xml edit fails" + e.getMessage());
      throw new Exception("registry.xml edit fails" + e.getMessage());
    } finally {
      assert fileOutputStream != null;
      fileOutputStream.close();
      assert writer != null;
      writer.flush();
    }
  }
示例#2
0
  /**
   * @param xpath XPATH expression to be read.
   * @param elt OMElement to be used for the search.
   * @return List matching OMNode list
   */
  @SuppressWarnings("unchecked")
  public List<OMNode> getMatchingNodes(final String xpath, final OMElement elt) {

    AXIOMXPath axiomXpath;
    List<OMNode> nodeList = null;
    try {
      axiomXpath = new AXIOMXPath(xpath);
      nodeList = axiomXpath.selectNodes(elt);
    } catch (JaxenException e) {
      String msg = "Error occurred while reading the Xpath (" + xpath + ")";
      log.error(msg, e);
      throw new CloudControllerException(msg, e);
    }

    return nodeList;
  }
  /**
   * Set values in OmElement
   *
   * @param template Jenkins job configuration template
   * @param selector Selector of the template
   * @param value related value from the project
   * @throws AppFactoryException
   */
  protected void setValueUsingXpath(OMElement template, String selector, String value)
      throws AppFactoryException {

    try {
      AXIOMXPath axiomxPath = new AXIOMXPath(selector);
      Object selectedObject = axiomxPath.selectSingleNode(template);

      if (selectedObject != null && selectedObject instanceof OMElement) {
        OMElement svnRepoPathElement = (OMElement) selectedObject;
        svnRepoPathElement.setText(value);
      } else {
        log.warn("Unable to find xml element matching selector : " + selector);
      }

    } catch (Exception e) {
      String msg = "Error while setting values using Xpath selector:" + selector;
      log.error(msg, e);
      throw new AppFactoryException(msg, e);
    }
  }
示例#4
0
 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.");
     }
   }
 }
示例#5
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);
        }
      }
    }
  }
  /*
  * This method is used to capture the lifecycle attribute from the configuration.
  *
  * expected configuration elements are
  *
  * <field type="options">
          <name label="Lifecycle Name" >Lifecycle Name</name>
          <values class="org.wso2.carbon.governance.generic.ui.utils.LifecycleListPopulator"/>
      </field>
  *
  * or
  *
  *  <field type="options">
          <name label="Lifecycle Name" >Lifecycle Name</name>
          <values class="com.foo.bar.LifecycleListPopulator" isLifecycle="true"/>
      </field>
  *  */
  private static String BuilLifecycleAttribute(
      GovernanceArtifactConfiguration configuration,
      String defaultLifecycleGeneratorClass,
      String lifecycleAttribute) {
    try {
      //            This part checks whether the user has given a lifecycle populates.
      //            If not, then we check whether there is an attribute called, "isLifecycle"
      //            This attribute will identify the lifecycle attribute from the configuration.
      OMElement configurationElement = configuration.getContentDefinition();
      String xpathExpression = "//@class";

      AXIOMXPath xpath = new AXIOMXPath(xpathExpression);
      List resultNodes = xpath.selectNodes(configurationElement);

      if (resultNodes != null && resultNodes.size() > 0) {
        String lifecycleParentName = null;
        String lifecycleName = null;

        for (Object resultNode : resultNodes) {
          OMElement parentElement = ((OMAttribute) resultNode).getOwner();
          if (parentElement
              .getAttributeValue(new QName("class"))
              .equals(defaultLifecycleGeneratorClass)) {
            Iterator childrenIterator = parentElement.getParent().getChildrenWithLocalName("name");
            while (childrenIterator.hasNext()) {
              OMElement next = (OMElement) childrenIterator.next();
              lifecycleName = next.getAttributeValue(new QName("label"));
            }
            OMElement rootElement = (OMElement) ((OMElement) parentElement.getParent()).getParent();
            lifecycleParentName = rootElement.getAttributeValue(new QName("name"));
            break;
          } else if (parentElement.getAttributeValue(new QName("isLifecycle")) != null
              && parentElement.getAttributeValue(new QName("isLifecycle")).equals("true")) {
            Iterator childrenIterator = parentElement.getParent().getChildrenWithLocalName("name");
            while (childrenIterator.hasNext()) {
              OMElement next = (OMElement) childrenIterator.next();
              lifecycleName = next.getAttributeValue(new QName("label"));
            }
            OMElement rootElement = (OMElement) ((OMElement) parentElement.getParent()).getParent();
            lifecycleParentName = rootElement.getAttributeValue(new QName("name"));
            break;
          }
        }
        if (lifecycleParentName != null && lifecycleName != null) {
          return convertName(lifecycleParentName.split(" "))
              + "_"
              + convertName(lifecycleName.split(" "));
        }
      }

    } catch (OMException e) {
      log.error(
          "Governance artifact configuration of configuration key:"
              + configuration.getKey()
              + " is invalid",
          e);
    } catch (JaxenException e) {
      log.error("Error in getting the lifecycle attribute", e);
    }
    return null;
  }