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());
}
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;
}
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);
}
}
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);
}
}
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;
}
Aggregations