Search in sources :

Example 1 with CamelContextFactoryBean

use of org.apache.camel.spring.CamelContextFactoryBean in project camel by apache.

the class CamelContextModelErrorHandlerIssueTest method testCamelContextModel.

@Test
public void testCamelContextModel() throws Exception {
    JAXBContext jaxbContext = new SpringModelJAXBContextFactory().newJAXBContext();
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object obj = unmarshaller.unmarshal(new File("src/test/resources/org/apache/camel/spring/issues/CamelContextModelErrorHandlerIssueTest.xml"));
    assertNotNull(obj);
    CamelContextFactoryBean context = (CamelContextFactoryBean) obj;
    assertEquals("myCamel", context.getId());
    assertEquals("dlc", context.getErrorHandlerRef());
    assertEquals(1, context.getRoutes().size());
    Marshaller marshaller = jaxbContext.createMarshaller();
    StringWriter writer = new StringWriter();
    marshaller.marshal(context, writer);
    String s = writer.getBuffer().toString();
    LOG.info(s);
    assertTrue("Should have error handler", s.contains("<errorHandler"));
    assertTrue("Should have redelivery policy", s.contains("<redeliveryPolicy"));
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) CamelContextFactoryBean(org.apache.camel.spring.CamelContextFactoryBean) SpringModelJAXBContextFactory(org.apache.camel.spring.SpringModelJAXBContextFactory) Test(org.junit.Test)

Example 2 with CamelContextFactoryBean

use of org.apache.camel.spring.CamelContextFactoryBean in project camel by apache.

the class CamelNamespaceHandler method init.

public void init() {
    // register restContext parser
    registerParser("restContext", new RestContextDefinitionParser());
    // register routeContext parser
    registerParser("routeContext", new RouteContextDefinitionParser());
    // register endpoint parser
    registerParser("endpoint", endpointParser);
    addBeanDefinitionParser("keyStoreParameters", KeyStoreParametersFactoryBean.class, true, true);
    addBeanDefinitionParser("secureRandomParameters", SecureRandomParametersFactoryBean.class, true, true);
    registerBeanDefinitionParser("sslContextParameters", new SSLContextParametersFactoryBeanBeanDefinitionParser());
    addBeanDefinitionParser("proxy", CamelProxyFactoryBean.class, true, false);
    addBeanDefinitionParser("template", CamelProducerTemplateFactoryBean.class, true, false);
    addBeanDefinitionParser("fluentTemplate", CamelFluentProducerTemplateFactoryBean.class, true, false);
    addBeanDefinitionParser("consumerTemplate", CamelConsumerTemplateFactoryBean.class, true, false);
    addBeanDefinitionParser("export", CamelServiceExporter.class, true, false);
    addBeanDefinitionParser("threadPool", CamelThreadPoolFactoryBean.class, true, true);
    addBeanDefinitionParser("redeliveryPolicyProfile", CamelRedeliveryPolicyFactoryBean.class, true, true);
    // jmx agent, stream caching, hystrix, service call configurations and property placeholder cannot be used outside of the camel context
    addBeanDefinitionParser("jmxAgent", CamelJMXAgentDefinition.class, false, false);
    addBeanDefinitionParser("streamCaching", CamelStreamCachingStrategyDefinition.class, false, false);
    addBeanDefinitionParser("propertyPlaceholder", CamelPropertyPlaceholderDefinition.class, false, false);
    // error handler could be the sub element of camelContext or defined outside camelContext
    BeanDefinitionParser errorHandlerParser = new ErrorHandlerDefinitionParser();
    registerParser("errorHandler", errorHandlerParser);
    parserMap.put("errorHandler", errorHandlerParser);
    // camel context
    boolean osgi = false;
    Class<?> cl = CamelContextFactoryBean.class;
    // If so, camel will use the OSGi version of CamelContextFactoryBean to create the CamelContext.
    try {
        // Try to load the BundleActivator first
        Class.forName("org.osgi.framework.BundleActivator");
        Class<?> c = Class.forName("org.apache.camel.osgi.Activator");
        Method mth = c.getDeclaredMethod("getBundle");
        Object bundle = mth.invoke(null);
        if (bundle != null) {
            cl = Class.forName("org.apache.camel.osgi.CamelContextFactoryBean");
            osgi = true;
        }
    } catch (Throwable t) {
        // not running with camel-core-osgi so we fallback to the regular factory bean
        LOG.trace("Cannot find class so assuming not running in OSGi container: " + t.getMessage());
    }
    if (osgi) {
        LOG.info("OSGi environment detected.");
    }
    LOG.debug("Using {} as CamelContextBeanDefinitionParser", cl.getCanonicalName());
    registerParser("camelContext", new CamelContextBeanDefinitionParser(cl));
}
Also used : Method(java.lang.reflect.Method) CamelContextFactoryBean(org.apache.camel.spring.CamelContextFactoryBean)

Example 3 with CamelContextFactoryBean

use of org.apache.camel.spring.CamelContextFactoryBean in project fabric8 by jboss-fuse.

the class RouteXml method unmarshal.

public XmlModel unmarshal(Document doc, String message) throws Exception {
    Unmarshaller unmarshaller = jaxbContext().createUnmarshaller();
    // ("bean", springNamespace)
    Map<String, String> beans = new HashMap<String, String>();
    // lets pull out the spring beans...
    // TODO: shouldn't we use http://www.springframework.org/schema/beans namespace instead??
    List<Node> beanElems = nodesByNamespace(doc, springNS, "bean");
    for (Node n : beanElems) {
        if (n instanceof Element) {
            String id = ((Element) n).getAttributeValue("id");
            String cn = ((Element) n).getAttributeValue("class");
            if (id != null && cn != null) {
                beans.put(id, cn);
            }
        }
    }
    // now lets pull out the jaxb routes...
    List<String[]> search = Arrays.asList(new String[] { springNS, "routeContext" }, new String[] { springNS, "camelContext" }, new String[] { springNS, "routes" }, new String[] { blueprintNS, "routeContext" }, new String[] { blueprintNS, "camelContext" }, new String[] { blueprintNS, "routes" });
    List<Node> found = new LinkedList<Node>();
    for (String[] pair : search) {
        List<Node> nodes = nodesByNamespace(doc, pair[0], pair[1]);
        int n = nodes.size();
        if (n != 0) {
            if (n > 1) {
                LOG.warn(message + " contains " + n + " <" + pair[1] + "> elements. Only the first one will be used");
            }
            Node node = nodes.get(0);
            found.add(node);
        }
    }
    if (found.size() > 0) {
        Node n = found.get(0);
        if (n != null) {
            String ns = getNamespaceURI(n);
            Node parseNode;
            if (!ns.equals(springNS)) {
                parseNode = cloneAndReplaceNamespace(n, ns, springNS);
            } else {
                parseNode = n;
            }
            boolean justRoutes = false;
            boolean routesContext = false;
            String xmlText = nodeWithNamespacesToText(parseNode, (Element) n);
            Object object = unmarshaller.unmarshal(new StringReader(xmlText));
            CamelContextFactoryBean sc;
            if (object instanceof CamelContextFactoryBean) {
                LOG.debug("Found a valid CamelContextFactoryBean! {}", object);
                sc = (CamelContextFactoryBean) object;
            } else if (object instanceof RoutesDefinition) {
                justRoutes = true;
                sc = new CamelContextFactoryBean();
                sc.setRoutes(((RoutesDefinition) object).getRoutes());
            } else if (object instanceof CamelRouteContextFactoryBean) {
                routesContext = true;
                sc = new CamelContextFactoryBean();
                sc.setRoutes(((CamelRouteContextFactoryBean) object).getRoutes());
            } else if (object instanceof org.apache.camel.blueprint.CamelRouteContextFactoryBean) {
                routesContext = true;
                sc = new CamelContextFactoryBean();
                sc.setRoutes(((org.apache.camel.blueprint.CamelRouteContextFactoryBean) object).getRoutes());
            } else {
                LOG.warn("Unmarshalled not a CamelContext: {}", object);
                sc = new CamelContextFactoryBean();
            }
            return new XmlModel(sc, doc, beans, n, ns, justRoutes, routesContext);
        } else {
            LOG.info(message + " does not contain a CamelContext. Maybe the XML namespace is not spring: '{}' or blueprint: '{}'?", springNS, blueprintNS);
            // lets create a new collection
            return new XmlModel(new CamelContextFactoryBean(), doc, beans, null, CamelNamespaces.springNS, false, false);
        }
    }
    // ?
    return null;
}
Also used : CamelRouteContextFactoryBean(org.apache.camel.spring.CamelRouteContextFactoryBean) HashMap(java.util.HashMap) Node(de.pdark.decentxml.Node) Element(de.pdark.decentxml.Element) LinkedList(java.util.LinkedList) StringReader(java.io.StringReader) RoutesDefinition(org.apache.camel.model.RoutesDefinition) Unmarshaller(javax.xml.bind.Unmarshaller) CamelContextFactoryBean(org.apache.camel.spring.CamelContextFactoryBean)

Aggregations

CamelContextFactoryBean (org.apache.camel.spring.CamelContextFactoryBean)3 Unmarshaller (javax.xml.bind.Unmarshaller)2 Element (de.pdark.decentxml.Element)1 Node (de.pdark.decentxml.Node)1 File (java.io.File)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 RoutesDefinition (org.apache.camel.model.RoutesDefinition)1 CamelRouteContextFactoryBean (org.apache.camel.spring.CamelRouteContextFactoryBean)1 SpringModelJAXBContextFactory (org.apache.camel.spring.SpringModelJAXBContextFactory)1 Test (org.junit.Test)1