Search in sources :

Example 26 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project camel by apache.

the class MvelEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    String path = getResourceUri();
    ObjectHelper.notNull(path, "resourceUri");
    String newResourceUri = exchange.getIn().getHeader(MvelConstants.MVEL_RESOURCE_URI, String.class);
    if (newResourceUri != null) {
        exchange.getIn().removeHeader(MvelConstants.MVEL_RESOURCE_URI);
        log.debug("{} set to {} creating new endpoint to handle exchange", MvelConstants.MVEL_RESOURCE_URI, newResourceUri);
        MvelEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
        newEndpoint.onExchange(exchange);
        return;
    }
    CompiledTemplate compiled;
    ParserContext mvelContext = ParserContext.create();
    Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
    String content = exchange.getIn().getHeader(MvelConstants.MVEL_TEMPLATE, String.class);
    if (content != null) {
        // use content from header
        if (log.isDebugEnabled()) {
            log.debug("Mvel content read from header {} for endpoint {}", MvelConstants.MVEL_TEMPLATE, getEndpointUri());
        }
        // remove the header to avoid it being propagated in the routing
        exchange.getIn().removeHeader(MvelConstants.MVEL_TEMPLATE);
        compiled = TemplateCompiler.compileTemplate(content, mvelContext);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Mvel content read from resource {} with resourceUri: {} for endpoint {}", new Object[] { getResourceUri(), path, getEndpointUri() });
        }
        // getResourceAsInputStream also considers the content cache
        Reader reader = getEncoding() != null ? new InputStreamReader(getResourceAsInputStream(), getEncoding()) : new InputStreamReader(getResourceAsInputStream());
        String template = IOConverter.toString(reader);
        if (!template.equals(this.template)) {
            this.template = template;
            this.compiled = TemplateCompiler.compileTemplate(template, mvelContext);
        }
        compiled = this.compiled;
    }
    // let mvel parse and execute the template
    log.debug("Mvel is evaluating using mvel context: {}", variableMap);
    Object result = TemplateRuntime.execute(compiled, mvelContext, variableMap);
    // now lets output the results to the exchange
    Message out = exchange.getOut();
    out.setBody(result.toString());
    out.setHeaders(exchange.getIn().getHeaders());
    out.setAttachments(exchange.getIn().getAttachments());
}
Also used : InputStreamReader(java.io.InputStreamReader) Message(org.apache.camel.Message) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ParserContext(org.mvel2.ParserContext) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 27 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project fabric8 by jboss-fuse.

the class Auditor method getTemplate.

private CompiledTemplate getTemplate(String event, Exchange exchange) {
    String source = getTemplateSource(event, exchange);
    CompiledTemplate template = templates.get(source);
    if (template == null) {
        template = TemplateCompiler.compileTemplate(source, context);
        templates.put(source, template);
    }
    return template;
}
Also used : CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 28 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project fabric8 by jboss-fuse.

the class Auditor method toJson.

protected String toJson(AbstractExchangeEvent event) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(Auditor.class.getClassLoader());
        String eventType = event.getClass().getSimpleName();
        eventType = eventType.substring("Exchange".length());
        eventType = eventType.substring(0, eventType.length() - "Event".length());
        CompiledTemplate template = getTemplate(eventType, event.getExchange());
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("event", eventType);
        vars.put("host", System.getProperty("runtime.id"));
        vars.put("timestamp", new Date());
        vars.put("exchange", event.getExchange());
        return TemplateRuntime.execute(template, context, vars).toString();
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) EventObject(java.util.EventObject) Date(java.util.Date) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 29 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project drools by kiegroup.

the class DefaultGenerator method generate.

/*
     * (non-Javadoc)
     * 
     * @see org.kie.decisiontable.parser.Generator#generate(java.lang.String,
     *      org.kie.decisiontable.parser.Row)
     */
public void generate(String templateName, Row row) {
    try {
        CompiledTemplate template = getTemplate(templateName);
        VariableResolverFactory factory = new MapVariableResolverFactory();
        Map<String, Object> vars = new HashMap<String, Object>();
        initializePriorCommaConstraints(vars);
        initializeHasPriorJunctionConstraint(vars);
        vars.put("row", row);
        for (Cell cell : row.getCells()) {
            cell.addValue(vars);
        }
        String drl = String.valueOf(TemplateRuntime.execute(template, vars, factory, registry));
        rules.add(drl);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) IOException(java.io.IOException) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 30 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project drools by kiegroup.

the class AccumulateTemplateTest method getInvokerTemplateRegistry.

private TemplateRegistry getInvokerTemplateRegistry() {
    TemplateRegistry invokerRegistry = new SimpleTemplateRegistry();
    CompiledTemplate compiled = TemplateCompiler.compileTemplate(JavaRuleBuilderHelper.class.getResourceAsStream("javaInvokers.mvel"), (Map<String, Class<? extends Node>>) null);
    TemplateRuntime.execute(compiled, null, invokerRegistry);
    return invokerRegistry;
}
Also used : TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) Node(org.mvel2.templates.res.Node) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) JavaRuleBuilderHelper(org.drools.mvel.java.JavaRuleBuilderHelper) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

CompiledTemplate (org.mvel2.templates.CompiledTemplate)32 HashMap (java.util.HashMap)8 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)6 Node (org.mvel2.templates.res.Node)5 InputStream (java.io.InputStream)4 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)4 TemplateRegistry (org.mvel2.templates.TemplateRegistry)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ParserContext (org.mvel2.ParserContext)3 TemplateCompiler (org.mvel2.templates.TemplateCompiler)3 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 JavaRuleBuilderHelper (org.drools.mvel.java.JavaRuleBuilderHelper)2 ImmutableDefaultFactory (org.mvel2.integration.impl.ImmutableDefaultFactory)2 TemplateError (org.mvel2.templates.TemplateError)2 TemplateRuntime (org.mvel2.templates.TemplateRuntime)2 TestPluginNode (org.mvel2.tests.templates.tests.res.TestPluginNode)2 StringAppender (org.mvel2.util.StringAppender)2 VertxInternal (io.vertx.core.impl.VertxInternal)1