use of java.util.LinkedHashMap in project camel by apache.
the class DefaultCamelContext method doStartOrResumeRoutes.
/**
* Starts or resumes the routes
*
* @param routeServices the routes to start (will only start a route if its not already started)
* @param checkClash whether to check for startup ordering clash
* @param startConsumer whether the route consumer should be started. Can be used to warmup the route without starting the consumer.
* @param resumeConsumer whether the route consumer should be resumed.
* @param addingRoutes whether we are adding new routes
* @throws Exception is thrown if error starting routes
*/
protected void doStartOrResumeRoutes(Map<String, RouteService> routeServices, boolean checkClash, boolean startConsumer, boolean resumeConsumer, boolean addingRoutes) throws Exception {
isStartingRoutes.set(true);
try {
// filter out already started routes
Map<String, RouteService> filtered = new LinkedHashMap<String, RouteService>();
for (Map.Entry<String, RouteService> entry : routeServices.entrySet()) {
boolean startable = false;
Consumer consumer = entry.getValue().getRoutes().iterator().next().getConsumer();
if (consumer instanceof SuspendableService) {
// consumer could be suspended, which is not reflected in the RouteService status
startable = ((SuspendableService) consumer).isSuspended();
}
if (!startable && consumer instanceof StatefulService) {
// consumer could be stopped, which is not reflected in the RouteService status
startable = ((StatefulService) consumer).getStatus().isStartable();
} else if (!startable) {
// no consumer so use state from route service
startable = entry.getValue().getStatus().isStartable();
}
if (startable) {
filtered.put(entry.getKey(), entry.getValue());
}
}
// the context is in last phase of staring, so lets start the routes
safelyStartRouteServices(checkClash, startConsumer, resumeConsumer, addingRoutes, filtered.values());
} finally {
isStartingRoutes.remove();
}
}
use of java.util.LinkedHashMap in project camel by apache.
the class DefaultCamelContext method explainDataFormatJson.
public String explainDataFormatJson(String dataFormatName, DataFormat dataFormat, boolean includeAllOptions) {
try {
String json = getDataFormatParameterJsonSchema(dataFormatName);
if (json == null) {
// the model may be shared for multiple data formats such as bindy, json (xstream, jackson, gson)
if (dataFormatName.contains("-")) {
dataFormatName = ObjectHelper.before(dataFormatName, "-");
json = getDataFormatParameterJsonSchema(dataFormatName);
}
if (json == null) {
return null;
}
}
List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true);
// selected rows to use for answer
Map<String, String[]> selected = new LinkedHashMap<String, String[]>();
Map<String, String[]> dataFormatOptions = new LinkedHashMap<String, String[]>();
// extract options from the data format
Map<String, Object> options = new LinkedHashMap<String, Object>();
IntrospectionSupport.getProperties(dataFormat, options, "", false);
for (Map.Entry<String, Object> entry : options.entrySet()) {
String name = entry.getKey();
String value = "";
if (entry.getValue() != null) {
value = entry.getValue().toString();
}
value = URISupport.sanitizePath(value);
// find type and description from the json schema
String type = null;
String kind = null;
String label = null;
String required = null;
String javaType = null;
String deprecated = null;
String secret = null;
String defaultValue = null;
String description = null;
for (Map<String, String> row : rows) {
if (name.equals(row.get("name"))) {
type = row.get("type");
kind = row.get("kind");
label = row.get("label");
required = row.get("required");
javaType = row.get("javaType");
deprecated = row.get("deprecated");
secret = row.get("secret");
defaultValue = row.get("defaultValue");
description = row.get("description");
break;
}
}
// remember this option from the uri
dataFormatOptions.put(name, new String[] { name, kind, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
}
// include other rows
for (Map<String, String> row : rows) {
String name = row.get("name");
String kind = row.get("kind");
String label = row.get("label");
String required = row.get("required");
String value = row.get("value");
String defaultValue = row.get("defaultValue");
String type = row.get("type");
String javaType = row.get("javaType");
String deprecated = row.get("deprecated");
String secret = row.get("secret");
value = URISupport.sanitizePath(value);
String description = row.get("description");
boolean isDataFormatOption = dataFormatOptions.containsKey(name);
// always include from uri or path options
if (includeAllOptions || isDataFormatOption) {
if (!selected.containsKey(name)) {
// add as selected row, but take the value from uri options if it was from there
if (isDataFormatOption) {
selected.put(name, dataFormatOptions.get(name));
} else {
selected.put(name, new String[] { name, kind, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
}
}
}
}
json = ObjectHelper.before(json, " \"properties\": {");
StringBuilder buffer = new StringBuilder(" \"properties\": {");
boolean first = true;
for (String[] row : selected.values()) {
if (first) {
first = false;
} else {
buffer.append(",");
}
buffer.append("\n ");
String name = row[0];
String kind = row[1];
String label = row[2];
String required = row[3];
String type = row[4];
String javaType = row[5];
String deprecated = row[6];
String secret = row[7];
String value = row[8];
String defaultValue = row[9];
String description = row[10];
// add json of the option
buffer.append(StringQuoteHelper.doubleQuote(name)).append(": { ");
CollectionStringBuffer csb = new CollectionStringBuffer();
if (kind != null) {
csb.append("\"kind\": \"" + kind + "\"");
}
if (label != null) {
csb.append("\"label\": \"" + label + "\"");
}
if (required != null) {
csb.append("\"required\": \"" + required + "\"");
}
if (type != null) {
csb.append("\"type\": \"" + type + "\"");
}
if (javaType != null) {
csb.append("\"javaType\": \"" + javaType + "\"");
}
if (deprecated != null) {
csb.append("\"deprecated\": \"" + deprecated + "\"");
}
if (secret != null) {
csb.append("\"secret\": \"" + secret + "\"");
}
if (value != null) {
csb.append("\"value\": \"" + value + "\"");
}
if (defaultValue != null) {
csb.append("\"defaultValue\": \"" + defaultValue + "\"");
}
if (description != null) {
csb.append("\"description\": \"" + description + "\"");
}
if (!csb.isEmpty()) {
buffer.append(csb.toString());
}
buffer.append(" }");
}
buffer.append("\n }\n}\n");
// insert the original first part of the json into the start of the buffer
buffer.insert(0, json);
return buffer.toString();
} catch (Exception e) {
// ignore and return empty response
return null;
}
}
use of java.util.LinkedHashMap in project camel by apache.
the class DefaultCamelContext method explainEndpointJson.
// CHECKSTYLE:OFF
public String explainEndpointJson(String uri, boolean includeAllOptions) {
try {
URI u = new URI(uri);
String json = getComponentParameterJsonSchema(u.getScheme());
if (json == null) {
return null;
}
List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true);
// selected rows to use for answer
Map<String, String[]> selected = new LinkedHashMap<String, String[]>();
Map<String, String[]> uriOptions = new LinkedHashMap<String, String[]>();
// insert values from uri
Map<String, Object> options = EndpointHelper.endpointProperties(this, uri);
// extract consumer. prefix options
Map<String, Object> consumerOptions = IntrospectionSupport.extractProperties(options, "consumer.");
// and add back again without the consumer. prefix as that json schema omits that
options.putAll(consumerOptions);
for (Map.Entry<String, Object> entry : options.entrySet()) {
String name = entry.getKey();
String value = "";
if (entry.getValue() != null) {
value = entry.getValue().toString();
}
value = URISupport.sanitizePath(value);
// find type and description from the json schema
String type = null;
String kind = null;
String group = null;
String label = null;
String required = null;
String javaType = null;
String deprecated = null;
String secret = null;
String defaultValue = null;
String description = null;
for (Map<String, String> row : rows) {
if (name.equals(row.get("name"))) {
type = row.get("type");
kind = row.get("kind");
group = row.get("group");
label = row.get("label");
required = row.get("required");
javaType = row.get("javaType");
deprecated = row.get("deprecated");
secret = row.get("secret");
defaultValue = row.get("defaultValue");
description = row.get("description");
break;
}
}
// remember this option from the uri
uriOptions.put(name, new String[] { name, kind, group, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
}
// include other rows
for (Map<String, String> row : rows) {
String name = row.get("name");
String kind = row.get("kind");
String group = row.get("group");
String label = row.get("label");
String required = row.get("required");
String value = row.get("value");
String defaultValue = row.get("defaultValue");
String type = row.get("type");
String javaType = row.get("javaType");
String deprecated = row.get("deprecated");
String secret = row.get("secret");
value = URISupport.sanitizePath(value);
String description = row.get("description");
boolean isUriOption = uriOptions.containsKey(name);
// always include from uri or path options
if (includeAllOptions || isUriOption || "path".equals(kind)) {
if (!selected.containsKey(name)) {
// add as selected row, but take the value from uri options if it was from there
if (isUriOption) {
selected.put(name, uriOptions.get(name));
} else {
selected.put(name, new String[] { name, kind, group, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
}
}
}
}
// skip component properties
json = ObjectHelper.before(json, " \"componentProperties\": {");
// and rewrite properties
StringBuilder buffer = new StringBuilder(" \"properties\": {");
boolean first = true;
for (String[] row : selected.values()) {
if (first) {
first = false;
} else {
buffer.append(",");
}
buffer.append("\n ");
String name = row[0];
String kind = row[1];
String group = row[2];
String label = row[3];
String required = row[4];
String type = row[5];
String javaType = row[6];
String deprecated = row[7];
String secret = row[8];
String value = row[9];
String defaultValue = row[10];
String description = row[11];
// add json of the option
buffer.append(StringQuoteHelper.doubleQuote(name)).append(": { ");
CollectionStringBuffer csb = new CollectionStringBuffer();
if (kind != null) {
csb.append("\"kind\": \"" + kind + "\"");
}
if (group != null) {
csb.append("\"group\": \"" + group + "\"");
}
if (label != null) {
csb.append("\"label\": \"" + label + "\"");
}
if (required != null) {
csb.append("\"required\": \"" + required + "\"");
}
if (type != null) {
csb.append("\"type\": \"" + type + "\"");
}
if (javaType != null) {
csb.append("\"javaType\": \"" + javaType + "\"");
}
if (deprecated != null) {
csb.append("\"deprecated\": \"" + deprecated + "\"");
}
if (secret != null) {
csb.append("\"secret\": \"" + secret + "\"");
}
if (value != null) {
csb.append("\"value\": \"" + value + "\"");
}
if (defaultValue != null) {
csb.append("\"defaultValue\": \"" + defaultValue + "\"");
}
if (description != null) {
csb.append("\"description\": \"" + description + "\"");
}
if (!csb.isEmpty()) {
buffer.append(csb.toString());
}
buffer.append(" }");
}
buffer.append("\n }\n}\n");
// insert the original first part of the json into the start of the buffer
buffer.insert(0, json);
return buffer.toString();
} catch (Exception e) {
// ignore and return empty response
return null;
}
}
use of java.util.LinkedHashMap in project camel by apache.
the class ModelHelper method loadRoutesDefinition.
/**
* Marshal the xml to the model definition
*
* @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use
* @param node the xml node
* @throws Exception is thrown if an error is encountered unmarshalling from xml to model
*/
public static RoutesDefinition loadRoutesDefinition(CamelContext context, Node node) throws Exception {
JAXBContext jaxbContext = getJAXBContext(context);
Map<String, String> namespaces = new LinkedHashMap<>();
Document dom = node instanceof Document ? (Document) node : node.getOwnerDocument();
extractNamespaces(dom, namespaces);
Binder<Node> binder = jaxbContext.createBinder();
Object result = binder.unmarshal(node);
if (result == null) {
throw new JAXBException("Cannot unmarshal to RoutesDefinition using JAXB");
}
// can either be routes or a single route
RoutesDefinition answer;
if (result instanceof RouteDefinition) {
RouteDefinition route = (RouteDefinition) result;
answer = new RoutesDefinition();
applyNamespaces(route, namespaces);
answer.getRoutes().add(route);
} else if (result instanceof RoutesDefinition) {
answer = (RoutesDefinition) result;
for (RouteDefinition route : answer.getRoutes()) {
applyNamespaces(route, namespaces);
}
} else {
throw new IllegalArgumentException("Unmarshalled object is an unsupported type: " + ObjectHelper.className(result) + " -> " + result);
}
return answer;
}
use of java.util.LinkedHashMap in project camel by apache.
the class ModelHelper method dumpModelAsXml.
/**
* Dumps the definition as XML
*
* @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use
* @param definition the definition, such as a {@link org.apache.camel.NamedNode}
* @return the output in XML (is formatted)
* @throws JAXBException is throw if error marshalling to XML
*/
public static String dumpModelAsXml(CamelContext context, NamedNode definition) throws JAXBException {
JAXBContext jaxbContext = getJAXBContext(context);
final Map<String, String> namespaces = new LinkedHashMap<>();
// gather all namespaces from the routes or route which is stored on the expression nodes
if (definition instanceof RoutesDefinition) {
List<RouteDefinition> routes = ((RoutesDefinition) definition).getRoutes();
for (RouteDefinition route : routes) {
extractNamespaces(route, namespaces);
}
} else if (definition instanceof RouteDefinition) {
RouteDefinition route = (RouteDefinition) definition;
extractNamespaces(route, namespaces);
}
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter buffer = new StringWriter();
marshaller.marshal(definition, buffer);
XmlConverter xmlConverter = newXmlConverter(context);
String xml = buffer.toString();
Document dom;
try {
dom = xmlConverter.toDOMDocument(xml, null);
} catch (Exception e) {
throw new TypeConversionException(xml, Document.class, e);
}
// Add additional namespaces to the document root element
Element documentElement = dom.getDocumentElement();
for (String nsPrefix : namespaces.keySet()) {
String prefix = nsPrefix.equals("xmlns") ? nsPrefix : "xmlns:" + nsPrefix;
documentElement.setAttribute(prefix, namespaces.get(nsPrefix));
}
// We invoke the type converter directly because we need to pass some custom XML output options
Properties outputProperties = new Properties();
outputProperties.put(OutputKeys.INDENT, "yes");
outputProperties.put(OutputKeys.STANDALONE, "yes");
try {
return xmlConverter.toStringFromDocument(dom, outputProperties);
} catch (TransformerException e) {
throw new IllegalStateException("Failed converting document object to string", e);
}
}
Aggregations