use of org.apache.camel.model.RoutesDefinition in project camel by apache.
the class XmlCdiBeanFactory method beansFrom.
Set<SyntheticBean<?>> beansFrom(URL url) throws JAXBException, IOException {
try (InputStream xml = url.openStream()) {
Object node = XmlCdiJaxbContexts.CAMEL_CDI.instance().createUnmarshaller().unmarshal(xml);
if (node instanceof RoutesDefinition) {
RoutesDefinition routes = (RoutesDefinition) node;
return singleton(routesDefinitionBean(routes, url));
} else if (node instanceof ApplicationContextFactoryBean) {
ApplicationContextFactoryBean app = (ApplicationContextFactoryBean) node;
Set<SyntheticBean<?>> beans = new HashSet<>();
for (CamelContextFactoryBean factory : app.getContexts()) {
SyntheticBean<?> bean = camelContextBean(factory, url);
beans.add(bean);
beans.addAll(camelContextBeans(factory, bean, url));
}
for (ErrorHandlerDefinition definition : app.getErrorHandlers()) {
beans.add(errorHandlerBean(definition, url));
}
for (ImportDefinition definition : app.getImports()) {
// Get the base URL as imports are relative to this
String path = url.getFile().substring(0, url.getFile().lastIndexOf('/'));
String base = url.getProtocol() + "://" + url.getHost() + path;
beans.addAll(beansFrom(base + "/" + definition.getResource()));
}
for (RestContextDefinition factory : app.getRestContexts()) {
beans.add(restContextBean(factory, url));
}
for (RouteContextDefinition factory : app.getRouteContexts()) {
beans.add(routeContextBean(factory, url));
}
for (AbstractCamelFactoryBean<?> factory : app.getBeans()) {
if (hasId(factory)) {
beans.add(camelContextBean(null, factory, url));
}
}
return beans;
} else if (node instanceof CamelContextFactoryBean) {
CamelContextFactoryBean factory = (CamelContextFactoryBean) node;
Set<SyntheticBean<?>> beans = new HashSet<>();
SyntheticBean<?> bean = camelContextBean(factory, url);
beans.add(bean);
beans.addAll(camelContextBeans(factory, bean, url));
return beans;
} else if (node instanceof RestContextDefinition) {
RestContextDefinition factory = (RestContextDefinition) node;
return singleton(restContextBean(factory, url));
} else if (node instanceof RouteContextDefinition) {
RouteContextDefinition factory = (RouteContextDefinition) node;
return singleton(routeContextBean(factory, url));
}
}
return emptySet();
}
use of org.apache.camel.model.RoutesDefinition 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;
}
use of org.apache.camel.model.RoutesDefinition in project syncope by apache.
the class SyncopeCamelContext method loadRouteDefinitions.
private void loadRouteDefinitions(final List<String> routes) {
try {
RoutesDefinition routeDefs = camelContext.loadRoutesDefinition(IOUtils.toInputStream("<routes xmlns=\"http://camel.apache.org/schema/spring\">" + StringUtils.join(routes) + "</routes>", StandardCharsets.UTF_8));
camelContext.addRouteDefinitions(routeDefs.getRoutes());
} catch (Exception e) {
LOG.error("While adding route definitions into Camel Context {}", camelContext, e);
throw new CamelException(e);
}
}
use of org.apache.camel.model.RoutesDefinition in project ddf by codice.
the class MetacardStorageRoute method stop.
public void stop(int code) {
try {
List<RouteDefinition> routesToRemove = new ArrayList<>();
ModelCamelContext context = getContext().adapt(ModelCamelContext.class);
for (RouteDefinition routeDefinition : context.getRouteDefinitions()) {
if (getRouteIds().contains(routeDefinition.getId())) {
context.getRouteController().stopRoute(routeDefinition.getId());
routesToRemove.add(routeDefinition);
setRouteCollection(new RoutesDefinition());
}
}
for (RouteDefinition routeDefinition : routesToRemove) {
context.removeRouteDefinition(routeDefinition);
}
} catch (Exception e) {
LOGGER.error("Could not stop route: {}", e);
}
}
use of org.apache.camel.model.RoutesDefinition in project syndesis by syndesisio.
the class IntegrationRouteBuilder method mandatoryConvertToRoutesDefinition.
@SuppressWarnings("PMD")
private RoutesDefinition mandatoryConvertToRoutesDefinition(String resource, Object instance) {
final RoutesDefinition definitions;
if (instance instanceof RoutesDefinition) {
definitions = (RoutesDefinition) instance;
} else if (instance instanceof RouteDefinition) {
definitions = new RoutesDefinition();
definitions.route((RouteDefinition) instance);
} else if (instance instanceof RouteBuilder) {
RouteBuilder builder = (RouteBuilder) instance;
try {
builder.configure();
definitions = builder.getRouteCollection();
} catch (Exception e) {
LOGGER.warn("Unable to configure resource: " + resource, e);
throw ObjectHelper.wrapRuntimeCamelException(e);
}
} else {
throw new IllegalArgumentException("Unable to convert instance: " + instance);
}
return definitions;
}
Aggregations