Java 类org.apache.camel.spring.CamelContextFactoryBean 实例源码

项目:fabric8-forge    文件:CamelXmlHelper.java   
protected static List<ContextDto> parseCamelContexts(CamelCatalog camelCatalog, File xmlFile) throws Exception {
    List<ContextDto> camelContexts = new ArrayList<>();

    RouteXml routeXml = new RouteXml();
    XmlModel xmlModel = routeXml.unmarshal(xmlFile);

    // TODO we don't handle multiple contexts inside an XML file!
    CamelContextFactoryBean contextElement = xmlModel.getContextElement();
    String name = contextElement.getId();
    List<RouteDefinition> routeDefs = contextElement.getRoutes();
    ContextDto context = new ContextDto(name);
    camelContexts.add(context);
    String key = name;
    if (Strings.isNullOrBlank(key)) {
        key = "_camelContext" + camelContexts.size();
    }
    context.setKey(key);
    List<NodeDto> routes = createRouteDtos(camelCatalog, routeDefs, context);
    context.setChildren(routes);
    return camelContexts;
}
项目:Camel    文件:CamelNamespaceHandler.java   
protected void addDependsOn(CamelContextFactoryBean factoryBean, BeanDefinitionBuilder builder) {
    String dependsOn = factoryBean.getDependsOn();
    if (ObjectHelper.isNotEmpty(dependsOn)) {
        // comma, whitespace and semi colon is valid separators in Spring depends-on
        String[] depends = dependsOn.split(",|;|\\s");
        if (depends == null) {
            throw new IllegalArgumentException("Cannot separate depends-on, was: " + dependsOn);
        } else {
            for (String depend : depends) {
                depend = depend.trim();
                LOG.debug("Adding dependsOn {} to CamelContext({})", depend, factoryBean.getId());
                builder.addDependsOn(depend);
            }
        }
    }
}
项目:Camel    文件:CamelContextModelErrorHandlerIssueTest.java   
@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"));
}
项目:switchyard    文件:RouteFactory.java   
private static List<RouteDefinition> processCamelContextElement(CamelContextFactoryBean camelContextFactoryBean, SwitchYardCamelContext camelContext) throws Exception {
    if (camelContext != null) {
        if (camelContextFactoryBean.getEndpoints() != null) {
            // processing camelContext/endpoint
            for (CamelEndpointFactoryBean epBean : camelContextFactoryBean.getEndpoints()) {
                epBean.setCamelContext(camelContext);
                camelContext.getWritebleRegistry().put(epBean.getId(), epBean.getObject());
            }
        }
        if (camelContextFactoryBean.getDataFormats() != null) {
            // processing camelContext/dataFormat
            for (DataFormatDefinition dataFormatDef : camelContextFactoryBean.getDataFormats().getDataFormats()) {
                camelContext.getDataFormats().put(dataFormatDef.getId(), dataFormatDef);
            }
        }
    }
    return camelContextFactoryBean.getRoutes();
}
项目:fabric8-forge    文件:XmlModel.java   
public XmlModel(CamelContextFactoryBean contextElement, Document doc, Map<String, String> beans, Node node, String ns, boolean justRoutes, boolean routesContext) {
    this.contextElement = contextElement;
    this.doc = doc;
    this.beans = beans;
    this.node = node;
    this.ns = ns;
    this.justRoutes = justRoutes;
    this.routesContext = routesContext;
}
项目:fabric8-forge    文件:RouteXml.java   
public void marshal(File file, final CamelContextFactoryBean context) throws Exception {
    marshal(file, new Model2Model() {
        @Override
        public XmlModel transform(XmlModel model) {
            model.update(context);
            return model;
        }
    });
}
项目:fabric8-forge    文件:RouteXml.java   
public void copyRoutesToElement(CamelContext context, CamelContextFactoryBean contextElement) {
    if (context instanceof ModelCamelContext) {
        copyRoutesToElement(((ModelCamelContext) context).getRouteDefinitions(), contextElement);
    } else {
        LOG.error("Invalid camel context! ({})", context.getClass().getName());
    }
}
项目:switchyard    文件:CamelContextFactoryBeanDelegate.java   
/**
 * Constructor.
 * @param context SwitchYard CamelContext
 * @param bean CamelContextFactoryBean
 */
public CamelContextFactoryBeanDelegate(SwitchYardCamelContext context, CamelContextFactoryBean bean) {
    _camelContext = context;
    _factoryBean = bean;
    String id = bean.getId();
    if (id != null && context instanceof DefaultCamelContext) {
        ((DefaultCamelContext)context).setName(id);
    } else {
        id = context.getName();
    }
    setId(id);
}
项目:switchyard    文件:RouteFactory.java   
/**
 * Loads a set of route definitions from an XML file.
 * 
 * @param xmlPath
 *            path to the file containing one or more route definitions
 * @param camelContext
 *            CamelContext
 * @param propertyResolver
 *            The property Resolver
 * @return list of route definitions
 */
public static List<RouteDefinition> loadRoute(String xmlPath, SwitchYardCamelContext camelContext,
        PropertyResolver propertyResolver) {
    List<RouteDefinition> routes = null;

    try {
        Object obj = CamelModelFactory.createCamelModelObjectFromXML(xmlPath);

        // Look for top-level element - camelContext, routes or route
        if (obj instanceof CamelContextFactoryBean) {
            routes = processCamelContextElement((CamelContextFactoryBean)obj, camelContext);
        } else if (obj instanceof RoutesDefinition) {
            routes = ((RoutesDefinition)obj).getRoutes();
        } else if (obj instanceof RouteDefinition) {
            routes = new ArrayList<RouteDefinition>(1);
            routes.add((RouteDefinition)obj);
        }

        // If we couldn't find a route definition, throw an error
        if (routes == null) {
            CamelComponentMessages.MESSAGES.noRoutesFoundInXMLFile(xmlPath);
        }


        return routes;
    } catch (Exception e) {
        throw new SwitchYardException(e);
    }
}
项目:fabric8-forge    文件:XmlModel.java   
public CamelContextFactoryBean getContextElement() {
    return contextElement;
}
项目:fabric8-forge    文件:RouteXml.java   
public void copyRoutesToElement(List<RouteDefinition> routeDefinitionList, CamelContextFactoryBean contextElement) {
    List<RouteDefinition> routes = contextElement.getRoutes();
    routes.clear();
    routes.addAll(routeDefinitionList);
}
项目:Camel    文件:CamelNamespaceHandler.java   
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("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);
    addBeanDefinitionParser("hystrixConfiguration", HystrixConfigurationDefinition.class, false, false);
    addBeanDefinitionParser("consulConfiguration", ConsulConfigurationDefinition.class, false, false);
    addBeanDefinitionParser("dnsConfiguration", DnsConfigurationDefinition.class, false, false);
    addBeanDefinitionParser("etcdConfiguration", EtcdConfigurationDefinition.class, false, false);
    addBeanDefinitionParser("kubernetesConfiguration", KubernetesConfigurationDefinition.class, false, false);
    addBeanDefinitionParser("ribbonConfiguration", RibbonConfigurationDefinition.class, false, false);

    // errorhandler 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;
    // These code will try to detected if we are in the OSGi environment.
    // 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));
}
项目:switchyard    文件:CamelContextConfigurator.java   
private static void configureCamelContextXML(CamelContext context, Object value) throws Exception {
    Object object = CamelModelFactory.createCamelModelObjectFromXML(value.toString());
    if (object instanceof CamelContextFactoryBean) {
        CamelModelFactory.importCamelContextFactoryBean((SwitchYardCamelContext)context, (CamelContextFactoryBean)object);
    }
}
项目:fabric8-forge    文件:XmlModel.java   
/**
 * Creates a new model using the given context
 *
 * @param newContext
 */
public void update(CamelContextFactoryBean newContext) {
    this.contextElement = newContext;
}
项目:switchyard    文件:CamelModelFactory.java   
/**
 * Imports CamelContext configuration  into existing SwitchYardCamelContext instance
 * from CamelContextFactoryBean JAXB object.
 * 
 * @param context SwitchYardCamelContext
 * @param bean CamelContextFactoryBean JAXB object
 * @throws Exception failed to import configurations
 */
public static void importCamelContextFactoryBean(SwitchYardCamelContext context, CamelContextFactoryBean bean) throws Exception {
    new CamelContextFactoryBeanDelegate(context, bean).importConfiguration();
}