Created
September 23, 2010 19:52
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.springframework.scripting.js; | |
public interface HelloService { | |
String hello(String name); | |
String helloParameterized(String name, Date effectiveDate, int age, Locale locale); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hello(name) { | |
return messages.getMessage("hello", [name, city], locale); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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