Example usage for org.springframework.scripting.support ResourceScriptSource ResourceScriptSource

List of usage examples for org.springframework.scripting.support ResourceScriptSource ResourceScriptSource

Introduction

In this page you can find the example usage for org.springframework.scripting.support ResourceScriptSource ResourceScriptSource.

Prototype

public ResourceScriptSource(Resource resource) 

Source Link

Document

Create a new ResourceScriptSource for the given resource.

Usage

From source file:com.acmemotors.filter.DataFilterTests.java

@Before
public void setUp() {
    processor = new GroovyScriptExecutingMessageProcessor(
            new ResourceScriptSource(new ClassPathResource("DataFilter.groovy")));
}

From source file:io.gravitee.repository.redis.ratelimit.RateLimitRepositoryConfiguration.java

@Bean(name = "rateLimitAsyncScript")
public RedisScript<List> script() {
    DefaultRedisScript<List> redisScript = new DefaultRedisScript<List>();
    redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/ratelimit-async.lua")));
    redisScript.setResultType(List.class);
    return redisScript;
}

From source file:org.iff.infra.util.spring.script.ScriptFactoryPostProcessor.java

/**
 * Convert the given script source locator to a ScriptSource instance.
 * <p>By default, supported locators are Spring resource locations
 * (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh")
 * and inline scripts ("inline:myScriptText...").
 * @param beanName the name of the scripted bean
 * @param scriptSourceLocator the script source locator
 * @param resourceLoader the ResourceLoader to use (if necessary)
 * @return the ScriptSource instance//from   w ww. java 2  s . co  m
 */
protected ScriptSource convertToScriptSource(String beanName, String scriptSourceLocator,
        ResourceLoader resourceLoader) {

    if (scriptSourceLocator.startsWith(INLINE_SCRIPT_PREFIX)) {
        return new StaticScriptSource(scriptSourceLocator.substring(INLINE_SCRIPT_PREFIX.length()), beanName);
    } else {
        return new ResourceScriptSource(resourceLoader.getResource(scriptSourceLocator));
    }
}

From source file:org.springframework.integration.groovy.GroovyExpressionTests.java

@Test
public void testScriptFactoryCustomizer() throws Exception {
    Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
    GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
    ResourceScriptSource scriptSource = new ResourceScriptSource(
            new NamedByteArrayResource("\"name=${name}\"".getBytes(), "InlineScript"));
    Object scriptedObject = factory.getScriptedObject(scriptSource, null);
    assertEquals("name=foo", scriptedObject.toString());
    customizer.setMap(Collections.singletonMap("name", (Object) "bar"));
    scriptedObject = factory.getScriptedObject(scriptSource, null);
    assertEquals("name=bar", scriptedObject.toString());
}

From source file:org.springframework.integration.groovy.GroovyExpressionTests.java

@Test
public void testScriptFactoryCustomizerThreadSafety() throws Exception {
    final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
    final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
    final ResourceScriptSource scriptSource = new ResourceScriptSource(
            new NamedByteArrayResource("\"name=${name}\"".getBytes(), "InlineScript"));
    Object scriptedObject = factory.getScriptedObject(scriptSource, null);
    assertEquals("name=foo", scriptedObject.toString());
    CompletionService<String> completionService = new ExecutorCompletionService<String>(
            Executors.newFixedThreadPool(10));
    for (int i = 0; i < 100; i++) {
        final String name = "bar" + i;
        completionService.submit(new Callable<String>() {
            public String call() throws Exception {
                Object scriptedObject;
                synchronized (customizer) {
                    customizer.setMap(Collections.singletonMap("name", (Object) name));
                    scriptedObject = factory.getScriptedObject(scriptSource, null);
                }/*www.ja  v a  2s.  com*/
                String result = scriptedObject.toString();
                logger.debug("Result=" + result + " with name=" + name);
                if (!("name=" + name).equals(result)) {
                    throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
                }
                return name;
            }
        });
    }
    Set<String> set = new HashSet<String>();
    for (int i = 0; i < 100; i++) {
        set.add(completionService.take().get());
    }
    assertEquals(100, set.size());
}

From source file:org.springframework.integration.groovy.GroovyExpressionTests.java

@Test
public void testScriptFactoryCustomizerStatic() throws Exception {
    final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
    final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
    final ResourceScriptSource scriptSource = new ResourceScriptSource(
            new NamedByteArrayResource("\"name=${name}\"".getBytes(), "InlineScript"));
    Object scriptedObject = factory.getScriptedObject(scriptSource, null);
    assertEquals("name=foo", scriptedObject.toString());
    CompletionService<String> completionService = new ExecutorCompletionService<String>(
            Executors.newFixedThreadPool(10));
    for (int i = 0; i < 100; i++) {
        final String name = "bar" + i;
        completionService.submit(new Callable<String>() {
            public String call() throws Exception {
                Object scriptedObject = factory.getScriptedObject(scriptSource, null);
                String result = scriptedObject.toString();
                logger.debug("Result=" + result + " with name=" + name);
                if (!("name=foo").equals(result)) {
                    throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
                }/* w  ww .  j  a v a 2s .c  o m*/
                return name;
            }
        });
    }
    Set<String> set = new HashSet<String>();
    for (int i = 0; i < 100; i++) {
        set.add(completionService.take().get());
    }
    assertEquals(100, set.size());
}

From source file:org.springframework.integration.groovy.GroovyExpressionTests.java

@Test
public void testScriptFactoryCustomizerThreadSafetyWithNewScript() throws Exception {
    final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
    final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
    CompletionService<String> completionService = new ExecutorCompletionService<String>(
            Executors.newFixedThreadPool(5));
    for (int i = 0; i < 100; i++) {
        final String name = "Bar" + i;
        completionService.submit(new Callable<String>() {
            public String call() throws Exception {
                Object scriptedObject;
                synchronized (customizer) {
                    customizer.setMap(Collections.singletonMap("name", (Object) name));
                    ResourceScriptSource scriptSource = new ResourceScriptSource(
                            new NamedByteArrayResource("\"name=${name}\"".getBytes(), "InlineScript" + name));
                    scriptedObject = factory.getScriptedObject(scriptSource, null);
                }/*w  w  w. j a  va  2 s .  c  o m*/
                String result = scriptedObject.toString();
                logger.debug("Result=" + result + " with name=" + name);
                if (!("name=" + name).equals(result)) {
                    throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
                }
                return name;
            }
        });
    }
    Set<String> set = new HashSet<String>();
    for (int i = 0; i < 100; i++) {
        set.add(completionService.take().get());
    }
    assertEquals(100, set.size());
}