Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine

  • 1,289 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,289
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
27
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine™ Rob Harrop VP Interface21 http://www.interface21.com TS-9294 2007 JavaOneSM Conference | Session TS-9294 |
  • 2. Goal of This Talk What you will gain Learn how to create and exploit domain-specific languages for the Java Virtual Machine™ (JVM™) using JRuby. The terms “Java Virtual Machine” and “JVM” mean a Virtual Machine for the Java™ platform. 2007 JavaOneSM Conference | Session TS-9294 | 2
  • 3. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 3
  • 4. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 4
  • 5. Introduction to DSLs What is a DSL? • Domain-specific language • Custom language designed for a specific purpose • Not just about business-specific domains • General purpose examples • Build language (rake) • Application management (MScript—coming soon!) • Business examples • Derivative calculation • Corporate action entitlement calculation 2007 JavaOneSM Conference | Session TS-9294 | 5
  • 6. Introduction to DSLs Why bother? • Greatly simplify repetitive tasks • Java Management Extensions (JMX™)? • Encapsulate boilerplate code • Provide an API that expresses the intent of the code clearly • Invest a little up front to save a lot in the future • DSLs should deliver clear value 2007 JavaOneSM Conference | Session TS-9294 | 6
  • 7. Introduction to DSLs Use cases for DSLs • Simplify/encapsulate some API • JMX API, for example • Abstract complex business problems • Corporate action processing • Support operational control of an application • Script maintenance tasks 2007 JavaOneSM Conference | Session TS-9294 | 7
  • 8. Introduction to DSLs Classifying DSLs • Type • External—XML • Internal—JRuby, Groovy • Target • General purpose—MScript • Business-specific—CAScript 2007 JavaOneSM Conference | Session TS-9294 | 8
  • 9. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 9
  • 10. DEMO DSLs in Action—Rake and MScript 2007 JavaOneSM Conference | Session TS-9294 | 10
  • 11. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 11
  • 12. Techniques for DSLs in JRuby Operator overloading • Operator overloading allows natural syntax for common functions • Concatenation, addition • Attribute/index access Example from MScript: puts mbean[:SomeAttribute] mbean[:SomeAttribute] = quot;Hello World!quot; 2007 JavaOneSM Conference | Session TS-9294 | 12
  • 13. Techniques for DSLs in JRuby Hashes and symbols • Symbols are a great way to identify objects in your DSL • Tasks in rake • Hashes are great for expressing relationships • Task dependency in rake task :run-application => :setup 2007 JavaOneSM Conference | Session TS-9294 | 13
  • 14. Techniques for DSLs in JRuby Blocks • Encapsulate executable logic • Paired with symbols to identify this logic • Task actions in rake task :run-application => :setup do ruby quot;application.rbquot; end 2007 JavaOneSM Conference | Session TS-9294 | 14
  • 15. Techniques for DSLs in JRuby Method missing • Great advantage over Java platform • No need to know all operations in advance— but get natural call syntax • JMX technology operations on a MBean proxy in MScript 2007 JavaOneSM Conference | Session TS-9294 | 15
  • 16. Techniques for DSLs in JRuby Dynamic type extension • Another brilliant advantage over Java platform • Dynamically add methods to classes and objects • Attribute access in MScript 2007 JavaOneSM Conference | Session TS-9294 | 16
  • 17. Techniques for DSLs in JRuby Java technology integration • Manipulate Java platform domain logic using JRuby • Take full advantage of existing investment • Add in DSLs where they make sense • Get the best of both worlds 2007 JavaOneSM Conference | Session TS-9294 | 17
  • 18. Techniques for DSLs in JRuby Options for Java technology integration • Access Java platform from Ruby using JRuby • include_class etc • Access Ruby from Java platform using JRuby API • This is reasonably complex • Bi-directional access • Critical for DSLs that aim to simplify Java technology usage • Spring integration • Create beans using JRuby 2007 JavaOneSM Conference | Session TS-9294 | 18
  • 19. DEMO Techniques for DSLs in JRuby 2007 JavaOneSM Conference | Session TS-9294 | 19
  • 20. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 20
  • 21. Decomposing DSLs • All DSLs are composed of these common building blocks • As expected a higher level of abstraction is provided so as not to appear like basic method calls • Let’s dive into the code and look at these techniques in action 2007 JavaOneSM Conference | Session TS-9294 | 21
  • 22. DEMO Decomposing DSLs 2007 JavaOneSM Conference | Session TS-9294 | 22
  • 23. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 23
  • 24. Elements of DSL Style Identify the problem, express the solution • DSLs are not simply Java code expressed in a dynamic language • Identify the problem and create a syntax that clearly expresses the solution task :run_application do ruby “application.rb” end 2007 JavaOneSM Conference | Session TS-9294 | 24
  • 25. Elements of DSL Style Metadata and behaviour • DSL expressions typically contain metadata and behaviour as part of a single expression corporate_action :bonus_issue do |position, payout| entitlement :stock => position.quantity * payout.rate end Don’t let DSLs become another configuration file 2007 JavaOneSM Conference | Session TS-9294 | 25
  • 26. Elements of DSL Style Thinking the Ruby way • Use type extension in favour of if/else • Use blocks rather than anonymous types • Use methods on objects rather than statics 2007 JavaOneSM Conference | Session TS-9294 | 26
  • 27. Elements of DSL Style Characteristics of a DSL • Limited in scope • Address a small part of your domain • Limited in functionality • Only the features needed to address that part 2007 JavaOneSM Conference | Session TS-9294 | 27
  • 28. For More Information • Books • Programming Ruby (Thomas, Fowler and Hunt) • The Ruby Way (Fulton) • Web • http://jruby.codehaus.org • http://blog.interface21.com 2007 JavaOneSM Conference | Session TS-9294 | 28
  • 29. Q&A 2007 JavaOneSM Conference | Session TS-9294 | 29
  • 30. Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine™ Rob Harrop VP Interface21 http://www.interface21.com TS-9294 2007 JavaOneSM Conference | Session TS-9294 |