Scripting your Java Application
         with BSF 3.0
        Felix Meschberger
        ApacheCon EU 09
About
    Senior Developer at Day
•
    fmeschbe@apache.org
•
•   http://blog.meschberger.ch
•   Apache Projects:
    – Sl...
Contents
    Scope
•
    BSF 3.0
•
                          TM
•   Scripting for the Java Platform
•   OSGi Framework
•  ...
Scope
                        TM
•   Scripting for the Java Platform
•   Using BSF 3
•   Not using BSF 2 API
•   Example: ...
Contents
    Scope
•
    BSF 3.0
•
                          TM
•   Scripting for the Java Platform
•   OSGi Framework
•  ...
About BSF
• Bean Scripting Framework
• http://jakarta.apache.org/bsf/
• Timeline
  – 1999 Sanjiva Weerawarana, IBM
  – 200...
BSF 3.0
• Java Scripting API (JSR-223)
• Stable
• Beta due to TCK issues
Contents
    Scope
•
    BSF 3.0
•
                          TM
•   Scripting for the Java Platform
•   OSGi Framework
•  ...
TM
Scripting for the Java            Platform
    JSR-223
•
    Approved November 2006
•
•   Builds on BSF 2.4 and BeanShe...
Three Steps for Scripting
1. Get the ScriptEngineManager
  ScriptEngineManager mgr = new
    ScriptEngineManager();
2. Get...
Demo 1
• Scripting in Three Steps
  – Sample0.java
  – Call Class from Command Line
Main API
• javax.script.ScriptEngineManager
  – Manages ScriptEngineFactory
  – Provides access to ScriptEngine
  – Manage...
Helper API
• javax.script.Bindings
  – Variable Binding between Scripts and App.
• javax.script.ScriptContext
  – Context ...
Advanced API
• javax.script.Invocable
  – Optionally implemented by ScriptEngine
  – Allows calling functions in scripts
•...
Issues
• Missing Lifecycle Support
  – ScriptEngineFactory can only be added
  – Cleanup of ScriptEngineManager only on GC...
Script Language Support
• Implement 2 Interfaces
  – ScriptEngineFactory
  – ScriptEngine
• Register
  – Manually
    Scri...
„Demo“ Script Engine Factory
public class DemoScriptEngineFactory
        implements ScriptEngineFactory {

     public Sc...
„Demo“ Script Engine
public class DemoScriptEngine
        extends AbstractScriptEngine {

    public Object eval(String s...
„Demo“ Registration
META-INF/services/javax.script.ScriptEngineFactory
  ch.meschberger.demo.engine.DemoScriptEngineFactor...
Demo 2
• Automatic Registration of „Demo“ Engine
  – Sample1.java
  – Call Class from Commandline
• Manual Registration of...
Interaction
• Variable Bindings
  – Global Scope
  – Engine Scope
  – Runtime Scope
• Return Values
Demo 3
• Simple Script Executor
  – Sample4
  – Reads and executes <lang>: <script>
• Return Value From Script
• Global Sc...
Contents
    Scope
•
    BSF 3.0
•
                          TM
•   Scripting for the Java Platform
•   OSGi Framework
•  ...
OSGi Quick Shot

The Framework forms the core of the
OSGi Service Platform Specifications. It
provides a general-purpose, ...
OSGi Layers
• Security Layer
  – Java 2 Security based
• Module Layer
  – Bundles and Classloaders
• Life Cycle Layer
  – ...
Sling and Java Scripting
• Provide BSF 3.0 API (Java 5 only)
• Manage ScriptEngineFactory
  – Create ScriptEngineManager
 ...
ScriptEngine for Sling
• Create a Bundle
  – Export-Package: None required
  – Import-Package: javax.script plus required
...
Sling, Java Scripting: Lifecycle
• Problem:
  – Lifecycle required for Cleanup
  – META-INF/services required for Interope...
Contents
    Scope
•
    BSF 3.0
•
                          TM
•   Scripting for the Java Platform
•   OSGi Framework
•  ...
How Sling finds Scripts

/content/cars/audi/s4.details.html
Demo: Sleep
• http://sleep.dashnine.org/
• Perl-like
• Provides ScriptEngineFactory with
  automatic registration
• ch.mes...
Demo 3
• Sling Running
• Web Console shows known Engines
• Show Scripts
Links
    http://jakarta.apache.org/bsf/
•
    http://www.jcp.org/en/jsr/detail?id=223
•
•   http://incubator.apache.org/s...
Questions ?
Thank You !
Famous Last Words
• Rate this talk at
  – http://apacheconus2008.crowdvine.com/talks/s
    how/1348
• Join the Sling Commu...
Upcoming SlideShare
Loading in …5
×

Scripting Yor Java Application with BSF3

2,505 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,505
On SlideShare
0
From Embeds
0
Number of Embeds
94
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scripting Yor Java Application with BSF3

  1. 1. Scripting your Java Application with BSF 3.0 Felix Meschberger ApacheCon EU 09
  2. 2. About Senior Developer at Day • fmeschbe@apache.org • • http://blog.meschberger.ch • Apache Projects: – Sling – Felix – Jackrabbit
  3. 3. Contents Scope • BSF 3.0 • TM • Scripting for the Java Platform • OSGi Framework • Demo
  4. 4. Scope TM • Scripting for the Java Platform • Using BSF 3 • Not using BSF 2 API • Example: Apache Sling
  5. 5. Contents Scope • BSF 3.0 • TM • Scripting for the Java Platform • OSGi Framework • Demo
  6. 6. About BSF • Bean Scripting Framework • http://jakarta.apache.org/bsf/ • Timeline – 1999 Sanjiva Weerawarana, IBM – 2002 Subproject of Jakarta (Version 2.3) – 2006 BSF 2.4 – 2007 BSF 3.0 beta2 – 2009 BSF 3.0 beta3 (right now!)
  7. 7. BSF 3.0 • Java Scripting API (JSR-223) • Stable • Beta due to TCK issues
  8. 8. Contents Scope • BSF 3.0 • TM • Scripting for the Java Platform • OSGi Framework • Demo
  9. 9. TM Scripting for the Java Platform JSR-223 • Approved November 2006 • • Builds on BSF 2.4 and BeanShell • Included in Java 6 • BSF 3.0 for Java 1.4 and Java 5
  10. 10. Three Steps for Scripting 1. Get the ScriptEngineManager ScriptEngineManager mgr = new ScriptEngineManager(); 2. Get the ScriptEngine ScriptEngine eng = mgr.getEngineByExtension(„js“); 3. Evaluate the Script Object result = eng.eval(„'Hello World'“);
  11. 11. Demo 1 • Scripting in Three Steps – Sample0.java – Call Class from Command Line
  12. 12. Main API • javax.script.ScriptEngineManager – Manages ScriptEngineFactory – Provides access to ScriptEngine – Manages Global Scope • javax.script.ScriptEngineFactory – Registered with ScriptEngineManager – Creates ScriptEngine • javax.script.ScriptEngine – Evaluates Scripts
  13. 13. Helper API • javax.script.Bindings – Variable Binding between Scripts and App. • javax.script.ScriptContext – Context for evaluating Scripts – Bindings (Scopes) – Input/Output • javax.script.ScriptException – Thrown on Script Execution Errors
  14. 14. Advanced API • javax.script.Invocable – Optionally implemented by ScriptEngine – Allows calling functions in scripts • javax.script.Compilable – Optionally implemented by ScriptEngine – Allows precompiling scripts – Generates CompiledScript • javax.script.CompiledScript – Generated by Compilable.compile()
  15. 15. Issues • Missing Lifecycle Support – ScriptEngineFactory can only be added – Cleanup of ScriptEngineManager only on GC • Missing API – ScriptEngineManager.unregisterXXX() – ScriptEngineManager.destroy() – ScriptEngineFactory.destroy() • META-INF/services – ClassLoader Dependency
  16. 16. Script Language Support • Implement 2 Interfaces – ScriptEngineFactory – ScriptEngine • Register – Manually ScriptEngineManager.registerEngineExtension() ScriptEngineManager.registerEngineMimeType() ScriptEngineManager.registerEngineName() – Automatically META-INF/services/javax.scripting.ScriptEngineFactory
  17. 17. „Demo“ Script Engine Factory public class DemoScriptEngineFactory implements ScriptEngineFactory { public ScriptEngine getScriptEngine() { return new DemoScriptEngine(this); } public List<String> getExtensions() { return Arrays.asList(quot;demoquot;); } // more methods not shown }
  18. 18. „Demo“ Script Engine public class DemoScriptEngine extends AbstractScriptEngine { public Object eval(String script, ScriptContext context) { return script; } // more methods not shown }
  19. 19. „Demo“ Registration META-INF/services/javax.script.ScriptEngineFactory ch.meschberger.demo.engine.DemoScriptEngineFactory ScriptEngineManager.registerEngine*() ScriptEngineManager mgr = new ScriptEngineManager(); mgr.registerEngineName(engineName, new DemoScriptEngineFactory());
  20. 20. Demo 2 • Automatic Registration of „Demo“ Engine – Sample1.java – Call Class from Commandline • Manual Registration of „Demo“ Engine – Sample2.java – Call Class from Commandline
  21. 21. Interaction • Variable Bindings – Global Scope – Engine Scope – Runtime Scope • Return Values
  22. 22. Demo 3 • Simple Script Executor – Sample4 – Reads and executes <lang>: <script> • Return Value From Script • Global Scope – Shared Bindings • Runtime Scope – Non-shared Bindings
  23. 23. Contents Scope • BSF 3.0 • TM • Scripting for the Java Platform • OSGi Framework • Demo
  24. 24. OSGi Quick Shot The Framework forms the core of the OSGi Service Platform Specifications. It provides a general-purpose, secure, and managed Java framework that supports the deployment of extensible and downloadable applications known as bundles. OSGi Service Platform Core Specification, Release 4, Version 4.1, The OSGi Alliance, April 2007
  25. 25. OSGi Layers • Security Layer – Java 2 Security based • Module Layer – Bundles and Classloaders • Life Cycle Layer – Installation, Start, Stop, Uninstallation, ... • Service Layer – Service Registry • Actual Services
  26. 26. Sling and Java Scripting • Provide BSF 3.0 API (Java 5 only) • Manage ScriptEngineFactory – Create ScriptEngineManager – Update ScriptEngineManager • Automatic Registration – META-INF/services/j.s.ScriptEngineFactory – ScriptEngineFactory services
  27. 27. ScriptEngine for Sling • Create a Bundle – Export-Package: None required – Import-Package: javax.script plus required – DynamicImport-Package: * • ClassLoader Issues – Create Bridging ClassLoader http://wiki.eclipse.org/BundleProxyClassLoader_recipe – Set Thread's context ClassLoader
  28. 28. Sling, Java Scripting: Lifecycle • Problem: – Lifecycle required for Cleanup – META-INF/services required for Interoperability • Solution: – BundleActivator
  29. 29. Contents Scope • BSF 3.0 • TM • Scripting for the Java Platform • OSGi Framework • Demo
  30. 30. How Sling finds Scripts /content/cars/audi/s4.details.html
  31. 31. Demo: Sleep • http://sleep.dashnine.org/ • Perl-like • Provides ScriptEngineFactory with automatic registration • ch.meschberger.demo.sleep – Downloads and Bundles Sleep
  32. 32. Demo 3 • Sling Running • Web Console shows known Engines • Show Scripts
  33. 33. Links http://jakarta.apache.org/bsf/ • http://www.jcp.org/en/jsr/detail?id=223 • • http://incubator.apache.org/sling/ • http://felix.apache.org/ • http://scripting.dev.java.net/
  34. 34. Questions ?
  35. 35. Thank You !
  36. 36. Famous Last Words • Rate this talk at – http://apacheconus2008.crowdvine.com/talks/s how/1348 • Join the Sling Community at – http://incubator.apache.org/sling – mailto:sling-dev@incubator.apache.org

×