Esempio n. 1
0
 /**
  * load.
  *
  * @param resource a {@link org.springframework.core.io.Resource} object.
  * @return a {@link java.util.List} object.
  */
 public List<ReconfigBeanDefinitionHolder> load(Resource resource) {
   List<ReconfigBeanDefinitionHolder> holders = new ArrayList<ReconfigBeanDefinitionHolder>();
   try {
     InputStream inputStream = resource.getInputStream();
     try {
       InputSource inputSource = new InputSource(inputStream);
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder docBuilder = factory.newDocumentBuilder();
       Document doc = docBuilder.parse(inputSource);
       Element root = doc.getDocumentElement();
       NodeList nl = root.getChildNodes();
       BeanDefinitionParser parser = new BeanDefinitionParser();
       for (int i = 0; i < nl.getLength(); i++) {
         Node node = nl.item(i);
         if (node instanceof Element) {
           Element ele = (Element) node;
           ReconfigBeanDefinitionHolder holder = parser.parseBeanDefinitionElement(ele);
           holders.add(holder);
         }
       }
     } finally {
       if (null != inputStream) {
         inputStream.close();
       }
     }
   } catch (Exception ex) {
     throw new RuntimeException(
         "IOException parsing XML document from " + resource.getDescription(), ex);
   }
   return holders;
 }
Esempio n. 2
0
  protected void injectBeanPostProcessor(
      Element element,
      ParserContext parserContext,
      String contextId,
      BeanDefinitionBuilder builder) {
    Element childElement = element.getOwnerDocument().createElement("beanPostProcessor");
    element.appendChild(childElement);

    String beanPostProcessorId = contextId + ":beanPostProcessor";
    childElement.setAttribute("id", beanPostProcessorId);
    BeanDefinition definition = beanPostProcessorParser.parse(childElement, parserContext);
    // only register to camel context id as a String. Then we can look it up later
    // otherwise we get a circular reference in spring and it will not allow custom bean post
    // processing
    // see more at CAMEL-1663
    definition.getPropertyValues().addPropertyValue("camelId", contextId);
    builder.addPropertyReference("beanPostProcessor", beanPostProcessorId);
  }
Esempio n. 3
0
 private void registerEndpoint(
     Element childElement, ParserContext parserContext, String contextId) {
   String id = childElement.getAttribute("id");
   // must have an id to be registered
   if (ObjectHelper.isNotEmpty(id)) {
     BeanDefinition definition = endpointParser.parse(childElement, parserContext);
     definition
         .getPropertyValues()
         .addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
     // Need to add this dependency of CamelContext for Spring 3.0
     try {
       Method method = definition.getClass().getMethod("setDependsOn", String[].class);
       method.invoke(definition, (Object) new String[] {contextId});
     } catch (Exception e) {
       // Do nothing here
     }
     parserContext.registerBeanComponent(new BeanComponentDefinition(definition, id));
   }
 }
Esempio n. 4
0
    @Override
    protected void doParse(
        Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
      super.doParse(element, builder);

      // Note: prefer to use doParse from parent and postProcess; however, parseUsingJaxb requires
      // parserContext for no apparent reason.
      Binder<Node> binder;
      try {
        binder = getJaxbContext().createBinder();
      } catch (JAXBException e) {
        throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e);
      }

      Object value = parseUsingJaxb(element, parserContext, binder);

      if (value instanceof SSLContextParametersFactoryBean) {
        SSLContextParametersFactoryBean bean = (SSLContextParametersFactoryBean) value;

        builder.addPropertyValue("cipherSuites", bean.getCipherSuites());
        builder.addPropertyValue("cipherSuitesFilter", bean.getCipherSuitesFilter());
        builder.addPropertyValue("secureSocketProtocols", bean.getSecureSocketProtocols());
        builder.addPropertyValue(
            "secureSocketProtocolsFilter", bean.getSecureSocketProtocolsFilter());
        builder.addPropertyValue("keyManagers", bean.getKeyManagers());
        builder.addPropertyValue("trustManagers", bean.getTrustManagers());
        builder.addPropertyValue("secureRandom", bean.getSecureRandom());

        builder.addPropertyValue("clientParameters", bean.getClientParameters());
        builder.addPropertyValue("serverParameters", bean.getServerParameters());
      } else {
        throw new BeanDefinitionStoreException(
            "Parsed type is not of the expected type. Expected "
                + SSLContextParametersFactoryBean.class.getName()
                + " but found "
                + value.getClass().getName());
      }
    }
Esempio n. 5
0
    @Override
    protected void doParse(
        Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
      renameNamespaceRecursive(element);
      super.doParse(element, parserContext, builder);

      // now lets parse the routes with JAXB
      Binder<Node> binder;
      try {
        binder = getJaxbContext().createBinder();
      } catch (JAXBException e) {
        throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e);
      }
      Object value = parseUsingJaxb(element, parserContext, binder);

      if (value instanceof CamelRouteContextFactoryBean) {
        CamelRouteContextFactoryBean factoryBean = (CamelRouteContextFactoryBean) value;
        builder.addPropertyValue("routes", factoryBean.getRoutes());
      }

      // lets inject the namespaces into any namespace aware POJOs
      injectNamespaces(element, binder);
    }
Esempio n. 6
0
  /** Used for auto registering producer and consumer templates if not already defined in XML. */
  protected void registerTemplates(Element element, ParserContext parserContext, String contextId) {
    boolean template = false;
    boolean consumerTemplate = false;

    NodeList list = element.getChildNodes();
    int size = list.getLength();
    for (int i = 0; i < size; i++) {
      Node child = list.item(i);
      if (child instanceof Element) {
        Element childElement = (Element) child;
        String localName = childElement.getLocalName();
        if ("template".equals(localName)) {
          template = true;
        } else if ("consumerTemplate".equals(localName)) {
          consumerTemplate = true;
        }
      }
    }

    if (!template) {
      // either we have not used template before or we have auto registered it already and therefore
      // we
      // need it to allow to do it so it can remove the existing auto registered as there is now a
      // clash id
      // since we have multiple camel contexts
      boolean existing = autoRegisterMap.get("template") != null;
      boolean inUse = false;
      try {
        inUse = parserContext.getRegistry().isBeanNameInUse("template");
      } catch (BeanCreationException e) {
        // Spring Eclipse Tooling may throw an exception when you edit the Spring XML online in
        // Eclipse
        // when the isBeanNameInUse method is invoked, so ignore this and continue (CAMEL-2739)
        LOG.debug("Error checking isBeanNameInUse(template). This exception will be ignored", e);
      }
      if (!inUse || existing) {
        String id = "template";
        // auto create a template
        Element templateElement = element.getOwnerDocument().createElement("template");
        templateElement.setAttribute("id", id);
        BeanDefinitionParser parser = parserMap.get("template");
        BeanDefinition definition = parser.parse(templateElement, parserContext);

        // auto register it
        autoRegisterBeanDefinition(id, definition, parserContext, contextId);
      }
    }

    if (!consumerTemplate) {
      // either we have not used template before or we have auto registered it already and therefore
      // we
      // need it to allow to do it so it can remove the existing auto registered as there is now a
      // clash id
      // since we have multiple camel contexts
      boolean existing = autoRegisterMap.get("consumerTemplate") != null;
      boolean inUse = false;
      try {
        inUse = parserContext.getRegistry().isBeanNameInUse("consumerTemplate");
      } catch (BeanCreationException e) {
        // Spring Eclipse Tooling may throw an exception when you edit the Spring XML online in
        // Eclipse
        // when the isBeanNameInUse method is invoked, so ignore this and continue (CAMEL-2739)
        LOG.debug(
            "Error checking isBeanNameInUse(consumerTemplate). This exception will be ignored", e);
      }
      if (!inUse || existing) {
        String id = "consumerTemplate";
        // auto create a template
        Element templateElement = element.getOwnerDocument().createElement("consumerTemplate");
        templateElement.setAttribute("id", id);
        BeanDefinitionParser parser = parserMap.get("consumerTemplate");
        BeanDefinition definition = parser.parse(templateElement, parserContext);

        // auto register it
        autoRegisterBeanDefinition(id, definition, parserContext, contextId);
      }
    }
  }
Esempio n. 7
0
    @Override
    protected void doParse(
        Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
      renameNamespaceRecursive(element);
      super.doParse(element, parserContext, builder);

      String contextId = element.getAttribute("id");
      boolean implicitId = false;

      // lets avoid folks having to explicitly give an ID to a camel context
      if (ObjectHelper.isEmpty(contextId)) {
        // if no explicit id was set then use a default auto generated name
        CamelContextNameStrategy strategy = new DefaultCamelContextNameStrategy();
        contextId = strategy.getName();
        element.setAttributeNS(null, "id", contextId);
        implicitId = true;
      }

      // now lets parse the routes with JAXB
      Binder<Node> binder;
      try {
        binder = getJaxbContext().createBinder();
      } catch (JAXBException e) {
        throw new BeanDefinitionStoreException("Failed to create the JAXB binder", e);
      }
      Object value = parseUsingJaxb(element, parserContext, binder);

      if (value instanceof CamelContextFactoryBean) {
        // set the property value with the JAXB parsed value
        CamelContextFactoryBean factoryBean = (CamelContextFactoryBean) value;
        builder.addPropertyValue("id", contextId);
        builder.addPropertyValue("implicitId", implicitId);
        builder.addPropertyValue("routes", factoryBean.getRoutes());
        builder.addPropertyValue("intercepts", factoryBean.getIntercepts());
        builder.addPropertyValue("interceptFroms", factoryBean.getInterceptFroms());
        builder.addPropertyValue(
            "interceptSendToEndpoints", factoryBean.getInterceptSendToEndpoints());
        builder.addPropertyValue("dataFormats", factoryBean.getDataFormats());
        builder.addPropertyValue("onCompletions", factoryBean.getOnCompletions());
        builder.addPropertyValue("onExceptions", factoryBean.getOnExceptions());
        builder.addPropertyValue("builderRefs", factoryBean.getBuilderRefs());
        builder.addPropertyValue("routeRefs", factoryBean.getRouteRefs());
        builder.addPropertyValue("properties", factoryBean.getProperties());
        builder.addPropertyValue("packageScan", factoryBean.getPackageScan());
        builder.addPropertyValue("contextScan", factoryBean.getContextScan());
        if (factoryBean.getPackages().length > 0) {
          builder.addPropertyValue("packages", factoryBean.getPackages());
        }
        builder.addPropertyValue(
            "camelPropertyPlaceholder", factoryBean.getCamelPropertyPlaceholder());
        builder.addPropertyValue("camelJMXAgent", factoryBean.getCamelJMXAgent());
        builder.addPropertyValue(
            "camelStreamCachingStrategy", factoryBean.getCamelStreamCachingStrategy());
        builder.addPropertyValue("threadPoolProfiles", factoryBean.getThreadPoolProfiles());
        // add any depends-on
        addDependsOn(factoryBean, builder);
      }

      NodeList list = element.getChildNodes();
      int size = list.getLength();
      for (int i = 0; i < size; i++) {
        Node child = list.item(i);
        if (child instanceof Element) {
          Element childElement = (Element) child;
          String localName = child.getLocalName();
          if (localName.equals("endpoint")) {
            registerEndpoint(childElement, parserContext, contextId);
          } else if (localName.equals("routeBuilder")) {
            addDependsOnToRouteBuilder(childElement, parserContext, contextId);
          } else {
            BeanDefinitionParser parser = parserMap.get(localName);
            if (parser != null) {
              BeanDefinition definition = parser.parse(childElement, parserContext);
              String id = childElement.getAttribute("id");
              if (ObjectHelper.isNotEmpty(id)) {
                parserContext.registerComponent(new BeanComponentDefinition(definition, id));
                // set the templates with the camel context
                if (localName.equals("template")
                    || localName.equals("consumerTemplate")
                    || localName.equals("proxy")
                    || localName.equals("export")) {
                  // set the camel context
                  definition
                      .getPropertyValues()
                      .addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
                }
              }
            }
          }
        }
      }

      // register as endpoint defined indirectly in the routes by from/to types having id explicit
      // set
      registerEndpointsWithIdsDefinedInFromOrToTypes(element, parserContext, contextId, binder);

      // register templates if not already defined
      registerTemplates(element, parserContext, contextId);

      // lets inject the namespaces into any namespace aware POJOs
      injectNamespaces(element, binder);

      // inject bean post processor so we can support @Produce etc.
      // no bean processor element so lets create it by our self
      injectBeanPostProcessor(element, parserContext, contextId, builder);
    }