Search in sources :

Example 1 with NamedNode

use of org.apache.camel.NamedNode in project camel by apache.

the class DefaultExecutorServiceManager method onThreadPoolCreated.

/**
     * Invoked when a new thread pool is created.
     * This implementation will invoke the {@link LifecycleStrategy#onThreadPoolAdd(org.apache.camel.CamelContext,
     * java.util.concurrent.ThreadPoolExecutor, String, String, String, String) LifecycleStrategy.onThreadPoolAdd} method,
     * which for example will enlist the thread pool in JMX management.
     *
     * @param executorService the thread pool
     * @param source          the source to use the thread pool
     * @param threadPoolProfileId profile id, if the thread pool was created from a thread pool profile
     */
private void onThreadPoolCreated(ExecutorService executorService, Object source, String threadPoolProfileId) {
    // add to internal list of thread pools
    executorServices.add(executorService);
    String id;
    String sourceId = null;
    String routeId = null;
    // extract id from source
    if (source instanceof NamedNode) {
        id = ((OptionalIdentifiedDefinition<?>) source).idOrCreate(this.camelContext.getNodeIdFactory());
        // and let source be the short name of the pattern
        sourceId = ((NamedNode) source).getShortName();
    } else if (source instanceof String) {
        id = (String) source;
    } else if (source != null) {
        if (source instanceof StaticService) {
            // the source is static service so its name would be unique
            id = source.getClass().getSimpleName();
        } else {
            // fallback and use the simple class name with hashcode for the id so its unique for this given source
            id = source.getClass().getSimpleName() + "(" + ObjectHelper.getIdentityHashCode(source) + ")";
        }
    } else {
        // no source, so fallback and use the simple class name from thread pool and its hashcode identity so its unique
        id = executorService.getClass().getSimpleName() + "(" + ObjectHelper.getIdentityHashCode(executorService) + ")";
    }
    // id is mandatory
    ObjectHelper.notEmpty(id, "id for thread pool " + executorService);
    // extract route id if possible
    if (source instanceof ProcessorDefinition) {
        RouteDefinition route = ProcessorDefinitionHelper.getRoute((ProcessorDefinition<?>) source);
        if (route != null) {
            routeId = route.idOrCreate(this.camelContext.getNodeIdFactory());
        }
    }
    // let lifecycle strategy be notified as well which can let it be managed in JMX as well
    ThreadPoolExecutor threadPool = null;
    if (executorService instanceof ThreadPoolExecutor) {
        threadPool = (ThreadPoolExecutor) executorService;
    } else if (executorService instanceof SizedScheduledExecutorService) {
        threadPool = ((SizedScheduledExecutorService) executorService).getScheduledThreadPoolExecutor();
    }
    if (threadPool != null) {
        for (LifecycleStrategy lifecycle : camelContext.getLifecycleStrategies()) {
            lifecycle.onThreadPoolAdd(camelContext, threadPool, id, sourceId, routeId, threadPoolProfileId);
        }
    }
    // now call strategy to allow custom logic
    onNewExecutorService(executorService);
}
Also used : SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService) RouteDefinition(org.apache.camel.model.RouteDefinition) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) NamedNode(org.apache.camel.NamedNode) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) StaticService(org.apache.camel.StaticService)

Example 2 with NamedNode

use of org.apache.camel.NamedNode in project camel by apache.

the class ModelHelper method modelToXml.

private static <T extends NamedNode> T modelToXml(CamelContext context, InputStream is, String xml, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = getJAXBContext(context);
    XmlConverter xmlConverter = newXmlConverter(context);
    Document dom = null;
    try {
        if (is != null) {
            dom = xmlConverter.toDOMDocument(is, null);
        } else if (xml != null) {
            dom = xmlConverter.toDOMDocument(xml, null);
        }
    } catch (Exception e) {
        throw new TypeConversionException(xml, Document.class, e);
    }
    if (dom == null) {
        throw new IllegalArgumentException("InputStream and XML is both null");
    }
    Map<String, String> namespaces = new LinkedHashMap<>();
    extractNamespaces(dom, namespaces);
    Binder<Node> binder = jaxbContext.createBinder();
    Object result = binder.unmarshal(dom);
    if (result == null) {
        throw new JAXBException("Cannot unmarshal to " + type + " using JAXB");
    }
    // Restore namespaces to anything that's NamespaceAware
    if (result instanceof RoutesDefinition) {
        List<RouteDefinition> routes = ((RoutesDefinition) result).getRoutes();
        for (RouteDefinition route : routes) {
            applyNamespaces(route, namespaces);
        }
    } else if (result instanceof RouteDefinition) {
        RouteDefinition route = (RouteDefinition) result;
        applyNamespaces(route, namespaces);
    }
    return type.cast(result);
}
Also used : TypeConversionException(org.apache.camel.TypeConversionException) Node(org.w3c.dom.Node) NamedNode(org.apache.camel.NamedNode) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with NamedNode

use of org.apache.camel.NamedNode in project camel by apache.

the class CustomIdFactoryTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // use our own id factory so we can generate the keys we like to use
            context.setNodeIdFactory(new NodeIdFactory() {

                public String createId(NamedNode definition) {
                    return "#" + definition.getShortName() + ++counter + "#";
                }
            });
            // add our debugger so we can debug camel routes when we send in messages
            context.addInterceptStrategy(new MyDebuggerCheckingId());
            // a little content based router so we got 2 paths to route at runtime
            from("direct:start").choice().when(body().contains("Hello")).to("mock:hello").otherwise().log("Hey").to("mock:other").end();
        }
    };
}
Also used : NodeIdFactory(org.apache.camel.spi.NodeIdFactory) RouteBuilder(org.apache.camel.builder.RouteBuilder) NamedNode(org.apache.camel.NamedNode)

Example 4 with NamedNode

use of org.apache.camel.NamedNode in project camel by apache.

the class DefaultCamelContext method explainEipJson.

public String explainEipJson(String nameOrId, boolean includeAllOptions) {
    try {
        // try to find the id within all known routes and their eips
        String eipName = nameOrId;
        NamedNode target = null;
        for (RouteDefinition route : getRouteDefinitions()) {
            if (route.getId().equals(nameOrId)) {
                target = route;
                break;
            }
            for (FromDefinition from : route.getInputs()) {
                if (nameOrId.equals(from.getId())) {
                    target = route;
                    break;
                }
            }
            Iterator<ProcessorDefinition> it = ProcessorDefinitionHelper.filterTypeInOutputs(route.getOutputs(), ProcessorDefinition.class);
            while (it.hasNext()) {
                ProcessorDefinition def = it.next();
                if (nameOrId.equals(def.getId())) {
                    target = def;
                    break;
                }
            }
            if (target != null) {
                break;
            }
        }
        if (target != null) {
            eipName = target.getShortName();
        }
        String json = getEipParameterJsonSchema(eipName);
        if (json == null) {
            return null;
        }
        // overlay with runtime parameters that id uses at runtime
        if (target != 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[]>();
            // extract options from the node
            Map<String, Object> options = new LinkedHashMap<String, Object>();
            IntrospectionSupport.getProperties(target, options, "", false);
            // remove outputs which we do not want to include
            options.remove("outputs");
            // 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 description = row.get("description");
                // find the configured option
                Object o = options.get(name);
                if (o != null) {
                    value = o.toString();
                }
                value = URISupport.sanitizePath(value);
                if (includeAllOptions || o != null) {
                    // add as selected row
                    if (!selected.containsKey(name)) {
                        selected.put(name, new String[] { name, kind, label, required, type, javaType, deprecated, 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 value = row[7];
                String defaultValue = row[8];
                String description = row[9];
                // 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 (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();
        }
        return json;
    } catch (Exception e) {
        // ignore and return empty response
        return null;
    }
}
Also used : FromDefinition(org.apache.camel.model.FromDefinition) CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) NamedNode(org.apache.camel.NamedNode) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MalformedObjectNameException(javax.management.MalformedObjectNameException) VetoCamelContextStartException(org.apache.camel.VetoCamelContextStartException) IOException(java.io.IOException) LoadPropertiesException(org.apache.camel.util.LoadPropertiesException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) FailedToStartRouteException(org.apache.camel.FailedToStartRouteException) LinkedHashMap(java.util.LinkedHashMap) RouteDefinition(org.apache.camel.model.RouteDefinition) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

NamedNode (org.apache.camel.NamedNode)4 LinkedHashMap (java.util.LinkedHashMap)2 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)2 RouteDefinition (org.apache.camel.model.RouteDefinition)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)1 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)1 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)1 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 StaticService (org.apache.camel.StaticService)1