use of org.springframework.scripting.support.StaticScriptSource in project spring-integration by spring-projects.
the class AbstractScriptParser method doParse.
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String scriptLocation = element.getAttribute(LOCATION_ATTRIBUTE);
String scriptText = DomUtils.getTextValue(element);
if (StringUtils.hasText(scriptLocation) == StringUtils.hasText(scriptText)) {
parserContext.getReaderContext().error("Either the 'location' attribute or inline script text must be provided, but not both.", element);
return;
}
List<Element> variableElements = DomUtils.getChildElementsByTagName(element, "variable");
String scriptVariableGeneratorName = element.getAttribute("script-variable-generator");
if (StringUtils.hasText(scriptVariableGeneratorName) && variableElements.size() > 0) {
parserContext.getReaderContext().error("'script-variable-generator' and 'variable' sub-elements are mutually exclusive.", element);
return;
}
if (StringUtils.hasText(scriptLocation)) {
builder.addConstructorArgValue(this.resolveScriptLocation(element, parserContext.getReaderContext(), scriptLocation));
} else {
builder.addConstructorArgValue(new StaticScriptSource(scriptText));
}
BeanMetadataElement scriptVariableGeneratorDef = null;
if (!StringUtils.hasText(scriptVariableGeneratorName)) {
BeanDefinitionBuilder scriptVariableGeneratorBuilder = BeanDefinitionBuilder.genericBeanDefinition(DefaultScriptVariableGenerator.class);
ManagedMap<String, Object> variableMap = buildVariablesMap(element, parserContext, variableElements);
if (!CollectionUtils.isEmpty(variableMap)) {
scriptVariableGeneratorBuilder.addConstructorArgValue(variableMap);
}
scriptVariableGeneratorDef = scriptVariableGeneratorBuilder.getBeanDefinition();
} else {
scriptVariableGeneratorDef = new RuntimeBeanReference(scriptVariableGeneratorName);
}
builder.addConstructorArgValue(scriptVariableGeneratorDef);
postProcess(builder, element, parserContext);
}
use of org.springframework.scripting.support.StaticScriptSource in project spring-integration by spring-projects.
the class GroovyScriptExecutingMessageProcessorTests method testInt3166GroovyScriptExecutingMessageProcessorPerformance.
@Test
public void testInt3166GroovyScriptExecutingMessageProcessorPerformance() throws Exception {
final Message<?> message = new GenericMessage<Object>("test");
final AtomicInteger var1 = new AtomicInteger();
final AtomicInteger var2 = new AtomicInteger();
String script = "var1.incrementAndGet(); Thread.sleep(100); var2.set(Math.max(var1.get(), var2.get())); var1.decrementAndGet()";
ScriptSource scriptSource = new StaticScriptSource(script, Script.class.getName());
final MessageProcessor<Object> processor = new GroovyScriptExecutingMessageProcessor(scriptSource, message1 -> {
Map<String, Object> variables = new HashMap<String, Object>(2);
variables.put("var1", var1);
variables.put("var2", var2);
return variables;
});
ExecutorService executor = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
executor.execute(() -> processor.processMessage(message));
}
executor.shutdown();
assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
assertTrue(var2.get() > 1);
}
use of org.springframework.scripting.support.StaticScriptSource in project spring-integration by spring-projects.
the class GroovyCommandMessageProcessor method getScriptSource.
@Override
protected ScriptSource getScriptSource(Message<?> message) {
Object payload = message.getPayload();
Assert.isInstanceOf(String.class, payload, "Payload must be a String containing a Groovy script.");
String className = generateScriptName(message);
return new StaticScriptSource((String) payload, className);
}
use of org.springframework.scripting.support.StaticScriptSource in project spring-integration by spring-projects.
the class PythonScriptExecutorTests method test2.
@Test
public void test2() {
Object obj = executor.executeScript(new StaticScriptSource("def foo(y):\n\tx=y\n\treturn y\nz=foo(2)"));
assertEquals(2, obj);
}
use of org.springframework.scripting.support.StaticScriptSource in project spring-integration by spring-projects.
the class Jsr223ScriptExecutorTests method test.
@Test
public void test() {
ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("jruby");
executor.executeScript(new StaticScriptSource("'hello, world'"));
executor.executeScript(new StaticScriptSource("'hello, again'"));
Map<String, Object> variables = new HashMap<String, Object>();
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("one", 1);
headers.put("two", "two");
headers.put("three", 3);
variables.put("payload", "payload");
variables.put("headers", headers);
Resource resource = new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb");
String result = (String) executor.executeScript(new ResourceScriptSource(resource), variables);
assertEquals("payload modified", result.substring(0, "payload modified".length()));
}
Aggregations