Search in sources :

Example 1 with StaticScriptSource

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);
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 2 with StaticScriptSource

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);
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) Script(groovy.lang.Script) HashMap(java.util.HashMap) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ScriptSource(org.springframework.scripting.ScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) Test(org.junit.Test)

Example 3 with StaticScriptSource

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);
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) GString(groovy.lang.GString)

Example 4 with StaticScriptSource

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);
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) Test(org.junit.Test)

Example 5 with StaticScriptSource

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()));
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) HashMap(java.util.HashMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ScriptExecutor(org.springframework.integration.scripting.ScriptExecutor) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)18 Test (org.junit.Test)8 Test (org.junit.jupiter.api.Test)8 HashMap (java.util.HashMap)6 ScriptEvaluator (org.springframework.scripting.ScriptEvaluator)4 StandardScriptEvaluator (org.springframework.scripting.support.StandardScriptEvaluator)4 ScriptExecutor (org.springframework.integration.scripting.ScriptExecutor)3 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)2 GString (groovy.lang.GString)1 Script (groovy.lang.Script)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)1 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1 RefreshableResourceScriptSource (org.springframework.integration.scripting.RefreshableResourceScriptSource)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1