Search in sources :

Example 1 with RefreshableResourceScriptSource

use of org.springframework.integration.scripting.RefreshableResourceScriptSource in project spring-integration by spring-projects.

the class DslScriptExecutingMessageProcessor method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (StringUtils.hasText(this.location)) {
        this.script = this.applicationContext.getResource(this.location);
    }
    ScriptSource scriptSource = new RefreshableResourceScriptSource(this.script, this.refreshCheckDelay);
    if (!StringUtils.hasText(this.lang)) {
        String filename = this.script.getFilename();
        int index = filename.lastIndexOf(".") + 1;
        if (index < 1) {
            throw new BeanCreationException("'lang' isn't provided and there is 'file extension' for script " + "resource: " + this.script);
        }
        this.lang = filename.substring(index);
    }
    if (this.applicationContext.containsBean(ScriptExecutingProcessorFactory.BEAN_NAME)) {
        ScriptExecutingProcessorFactory processorFactory = this.applicationContext.getBean(ScriptExecutingProcessorFactory.BEAN_NAME, ScriptExecutingProcessorFactory.class);
        this.delegate = processorFactory.createMessageProcessor(this.lang, scriptSource, this.variableGenerator);
    } else {
        this.delegate = new ScriptExecutingMessageProcessor(scriptSource, this.variableGenerator, ScriptExecutorFactory.getScriptExecutor(this.lang));
    }
    this.delegate.setBeanFactory(this.applicationContext);
    this.delegate.setBeanClassLoader(this.applicationContext.getClassLoader());
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ScriptExecutingProcessorFactory(org.springframework.integration.scripting.config.ScriptExecutingProcessorFactory) ScriptSource(org.springframework.scripting.ScriptSource) RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) AbstractScriptExecutingMessageProcessor(org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor) ScriptExecutingMessageProcessor(org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor)

Example 2 with RefreshableResourceScriptSource

use of org.springframework.integration.scripting.RefreshableResourceScriptSource in project spring-integration by spring-projects.

the class GroovyScriptExecutingMessageProcessorTests method testRefreshableScriptExecutionWithAlwaysRefresh.

@Test
public void testRefreshableScriptExecutionWithAlwaysRefresh() throws Exception {
    String script = "return \"payload is $payload, header is $headers.testHeader\"";
    Message<?> message = MessageBuilder.withPayload("foo").setHeader("testHeader", "bar").build();
    TestResource resource = new TestResource(script, "simpleTest");
    ScriptSource scriptSource = new RefreshableResourceScriptSource(resource, 0);
    MessageProcessor<Object> processor = new GroovyScriptExecutingMessageProcessor(scriptSource);
    // process with the first script
    Object result = processor.processMessage(message);
    assertEquals("payload is foo, header is bar", result.toString());
    // change script, but since refresh-delay is less then 0 we should still se old script executing
    resource.setScript("return \"payload is $payload\"");
    // process and see assert that the old script is used
    result = processor.processMessage(message);
    assertEquals("payload is foo", result.toString());
    resource.setScript("return \"payload is 'hello'\"");
    // process and see assert that the old script is used
    result = processor.processMessage(message);
    assertEquals("payload is 'hello'", result.toString());
}
Also used : RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) 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 RefreshableResourceScriptSource

use of org.springframework.integration.scripting.RefreshableResourceScriptSource in project spring-integration by spring-projects.

the class RefreshableResourceScriptSourceTests method testIsModifiedInfiniteDelay.

@Test
public void testIsModifiedInfiniteDelay() throws Exception {
    RefreshableResourceScriptSource source = new RefreshableResourceScriptSource(new ByteArrayResource("foo".getBytes()) {

        @Override
        public long lastModified() throws IOException {
            return System.currentTimeMillis();
        }
    }, -1);
    assertEquals(false, source.isModified());
    assertEquals("foo", source.getScriptAsString());
}
Also used : RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with RefreshableResourceScriptSource

use of org.springframework.integration.scripting.RefreshableResourceScriptSource in project spring-integration by spring-projects.

the class RefreshableResourceScriptSourceTests method testGetScriptAsString.

@Test
public void testGetScriptAsString() throws Exception {
    RefreshableResourceScriptSource source = new RefreshableResourceScriptSource(new ByteArrayResource("foo".getBytes()), 1000);
    assertEquals("foo", source.getScriptAsString());
}
Also used : RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.Test)

Example 5 with RefreshableResourceScriptSource

use of org.springframework.integration.scripting.RefreshableResourceScriptSource in project spring-integration by spring-projects.

the class RefreshableResourceScriptSourceTests method testIsModifiedZeroDelay.

@Test
public void testIsModifiedZeroDelay() throws Exception {
    RefreshableResourceScriptSource source = new RefreshableResourceScriptSource(new ByteArrayResource("foo".getBytes()) {

        @Override
        public long lastModified() throws IOException {
            return System.currentTimeMillis();
        }
    }, 0);
    Thread.sleep(100L);
    assertEquals(true, source.isModified());
    assertEquals("foo", source.getScriptAsString());
}
Also used : RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

RefreshableResourceScriptSource (org.springframework.integration.scripting.RefreshableResourceScriptSource)9 Test (org.junit.Test)8 ByteArrayResource (org.springframework.core.io.ByteArrayResource)5 ScriptSource (org.springframework.scripting.ScriptSource)4 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)3 StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)3 IOException (java.io.IOException)2 Ignore (org.junit.Ignore)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 AbstractScriptExecutingMessageProcessor (org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor)1 ScriptExecutingProcessorFactory (org.springframework.integration.scripting.config.ScriptExecutingProcessorFactory)1 ScriptExecutingMessageProcessor (org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor)1