Upload
autumn-webb
View
213
Download
0
Embed Size (px)
Citation preview
Bean Scripting Framework.
曾俊雄
What is Scripting Language
Broader definition: all languages
Narrower definition: those programming languages requiring
no compilation or with implicit compilation
For example: JavaScript
source: http://zh.wikipedia.org/wiki/JavaScript
Why Scripting Languages?
simplicity real time most scripting languages have good UI
integration plenty of choices
Where to Use Scripting Languages?
integrated with UI languages, e.x., html vs. JavaScript
event-based scenarios, e.x., XSLT vs. xpath
programming with less ability but more simplicity, e.x., JSP
Drawbacks
Performance!!
Java + Scripting Language
Background Java performs better, but more complicat
ed in syntax and requires compilation Scripting languages (e.x., JavaScript) are
much slower (almost 1000x), but much easier in syntax and requires no compilation
Java + Scripting language is a good compromise use java in major part use scripting language for some
customizable parts but how?
JVM
byte code
script
Scripting Engine
load scripting engine with JVM, and pass scripting parts into the scripting engine.
However, different scripting engines have different interfaces……
Bean Scripting Framework
Actually, there is a plan to add common scripting interface into JVM with JavaSE 7.0 but when can we have JavaSE 7.0 ?!
Now, we have apache bean scripting framework http://jakarta.apache.org/bsf/
BSFManager
BSFEngine
JRubyEngine JavaScriptEngine PythonEngine ……
Usage
Register script engine to BSFManager BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupp
ort.bsf.JRubyEngine", new String[]{"rb"});
Create an instance of BSFManager BSFManager manager=new BSFManager();
Load registered script engine from the manager engine=manager.loadScriptingEngine("ruby");
Execute the scripting code, so it is kept in the memory engine.exec("ruby", 0, 0, “……script code……”);
Call target method declared in the scripting code engine.call(null, "actionPerformed", new java.lang.Object[]{self, arg0});
This is Ruby!!
Why use Ruby?
Simple yet powerful exactly the same expressive power with j
ava good bridging with jvm
http://jruby.codehaus.org/
Language Constructs
require 'java'
module Java include_package "java.lang" include_package "javax.swing" include_package "util.ws" include_package "java.util"end
def actionPerformed(caller, actionEvent) if actionEvent.getActionCommand()=="HelloWorld" Java::JOptionPane.showMessageDialog(caller, "HelloWorld") elsif actionEvent.getActionCommand()=="HelloWS" response=Java::WebServiceClient.send("http://localhost:8084/SpringWSTest/services", "<sayHello xmlns=\"HelloWorld\"/>") Java::JOptionPane.showMessageDialog(caller, response) elsif actionEvent.getActionCommand()=="HelloSoapHeaders" props=Java::HashMap.new props.put("{ns1}key1","value1") props.put("{ns2}key2",['_value1','_value2','_value3'].to_java) response=Java::WebServiceClient.send("http://localhost:8084/SpringWSTest/services", "<sayHello xmlns=\"HelloWorld\"/>", props) Java::JOptionPane.showMessageDialog(caller, response) endend
load java supportimport java.lang.*;
import javax.swing.*;
import util.ws.*;
import java.util.*; define function
require 'java'
module Java include_package "java.lang" include_package "javax.swing" include_package "util.ws" include_package "java.util"end
def actionPerformed(caller, actionEvent) if actionEvent.getActionCommand()=="HelloWorld" Java::JOptionPane.showMessageDialog(caller, "HelloWorld") elsif actionEvent.getActionCommand()=="HelloWS" response=Java::WebServiceClient.send("http://localhost:8084/SpringWSTest/services", "<sayHello xmlns=\"HelloWorld\"/>") Java::JOptionPane.showMessageDialog(caller, response) elsif actionEvent.getActionCommand()=="HelloSoapHeaders" props=Java::HashMap.new props.put("{ns1}key1","value1") props.put("{ns2}key2",['_value1','_value2','_value3'].to_java) response=Java::WebServiceClient.send("http://localhost:8084/SpringWSTest/services", "<sayHello xmlns=\"HelloWorld\"/>", props) Java::JOptionPane.showMessageDialog(caller, response) endend
load java supportimport java.lang.*;
import javax.swing.*;
import util.ws.*;
import java.util.*; define function