Skip to content

Instantly share code, notes, and snippets.

@nurkiewicz
Created September 23, 2010 19:52
Show Gist options
  • Save nurkiewicz/594236 to your computer and use it in GitHub Desktop.
Save nurkiewicz/594236 to your computer and use it in GitHub Desktop.
JavaScript dynamic language support in Spring framework article examples (http://nurkiewicz.blogspot.com/2010/09/javascript-dynamic-language-support-in.html)
package org.springframework.scripting.js;
public interface HelloService {
String hello(String name);
String helloParameterized(String name, Date effectiveDate, int age, Locale locale);
}
function hello(name) {
return messages.getMessage("hello", [name, city], locale);
}
package org.springframework.scripting.js;
public class JavaScriptScriptFactory implements ScriptFactory {
public boolean requiresConfigInterface() {
return true;
}
public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces) throws IOException, ScriptCompilationException {
return //It's a kind of magic
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
<lang:js id="javaScriptHelloService" script-interfaces="org.springframework.scripting.js.HelloService">
<lang:inline-script>
function hello(name) {
return "Hello, " + name + "!"
}
function helloParameterized(name, effectiveDate, age, locale) {
return "" + effectiveDate + ": " + name + " (" + (age + 1) + ", " +
locale.getDisplayCountry(java.util.Locale.US) + ")"
}
</lang:inline-script>
</lang:js>
</beans>
package org.springframework.scripting.js;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class JavaScriptScriptFactoryHelloTest {
@Resource
private HelloService helloService;
@Test
public void shouldReturnHelloStringFromJs() throws Exception {
//given
final String name = "Tomek";
//when
final String result = helloService.hello(name);
//then
assertThat(result).isEqualTo("Hello, Tomek!");
}
//other tests
}
<bean id="resourceBundleMessageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="org.springframework.scripting.js.i18n.hello"/>
</bean>
<bean id="greatBritainLocale" class="java.util.Locale">
<constructor-arg value="en_GB"/>
</bean>
<lang:js id="javaScriptHelloService"
script-interfaces="org.springframework.scripting.js.HelloService"
script-source="/org/springframework/scripting/js/HelloService.js">
<lang:property name="city" value="Warsaw"/>
<lang:property name="locale" ref="greatBritainLocale"/>
<lang:property name="messages" ref="resourceBundleMessageSource"/>
</lang:js>
package org.springframework.scripting.config;
public class LangNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerScriptBeanDefinitionParser("groovy", "org.springframework.scripting.groovy.GroovyScriptFactory");
registerScriptBeanDefinitionParser("jruby", "org.springframework.scripting.jruby.JRubyScriptFactory");
registerScriptBeanDefinitionParser("bsh", "org.springframework.scripting.bsh.BshScriptFactory");
registerScriptBeanDefinitionParser("js", "org.springframework.scripting.js.JavaScriptScriptFactory");
registerBeanDefinitionParser("defaults", new ScriptingDefaultsParser());
}
}
<lang:js id="javaScriptUserService"
script-interfaces="org.springframework.scripting.js.UserService"
script-source="http://somehost:8080/scripts/UserService.js"
refresh-check-delay="15000"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment