Download Bean Scripting Framework

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
complicated 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?
byte code
JVM
script
Scripting Engine
However, different scripting
engines have different
interfaces……
load scripting engine
with JVM, and pass
scripting parts into the
scripting engine.
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


Create an instance of BSFManager


BSFManager.registerScriptingEngine("ruby",
"org.jruby.javasupport.bsf.JRubyEngine", new String[]{"rb"});
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
java
good bridging with jvm

http://jruby.codehaus.org/
Language Constructs
require 'java'
load java support
module Java
include_package "java.lang"
include_package "javax.swing"
include_package "util.ws"
include_package "java.util"
end
import java.lang.*;
import javax.swing.*;
import util.ws.*;
import java.util.*;
define function
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)
end
end
require 'java'
load java support
module Java
include_package "java.lang"
include_package "javax.swing"
include_package "util.ws"
include_package "java.util"
end
import java.lang.*;
import javax.swing.*;
import util.ws.*;
import java.util.*;
define function
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)
end
end
Related documents