Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AtlasCamp 2012: Polyglot Plugin Programming

AtlasCamp 2012: Polyglot Plugin Programming

Stefan Saasen

March 22, 2012
Tweet

More Decks by Stefan Saasen

Other Decks in Programming

Transcript

  1. 1

  2. How to write plugins in a language other than Java

    Stefan Saasen Confluence Team Lead, Atlassian Polyglot Plugin Development 2
  3. • Written an Atlassian plugin in Java • Interested in

    other programming languages Audience 3
  4. • Written an Atlassian plugin in Java • Interested in

    other programming languages • Eager to see what is possible with Atlassian plugins Audience 3
  5. • Who has actually written a Atlassian plugin? • In

    any other language than Java? Show of hands 4
  6. 5

  7. 7

  8. • Why? • How? • Example 1: Scala • Example

    2: JRuby • Go and do it! Polyglot Plugins 9
  9. • Leverage existing knowledge in the team • Libraries/applications already

    written in language X • Time to market/devspeed 12 What to tell the boss?
  10. • Leverage existing knowledge in the team • Libraries/applications already

    written in language X • Time to market/devspeed • Explore technologies in a sandboxed environment 12 What to tell the boss?
  11. • Leverage existing knowledge in the team • Libraries/applications already

    written in language X • Time to market/devspeed • Explore technologies in a sandboxed environment • Grass looks always greener on the other side 12 What to tell the boss?
  12. • Leverage existing knowledge in the team • Libraries/applications already

    written in language X • Time to market/devspeed • Explore technologies in a sandboxed environment • Grass looks always greener on the other side 13 What to tell the boss?
  13. 14

  14. 16

  15. • Why? • How? • Example 1: Scala • Example

    2: JRuby • Go and do it! Polyglot Plugins 17
  16. • Hybrid language: Object oriented & functional • Concise, statically

    typed (with type inference) • Higher order/first class functions, pattern matching • Powerful type system • Rich standard library 18
  17. 19

  18. 20

  19. 21

  20. 22

  21. 24 Overview Java JVM Java Standard Library Scala Runtime Library

    javac scalac Java Bytecode *.class Scala
  22. 25

  23. 26

  24. • Start with Maven to get the AMPS goodness •

    Use the maven-scala- plugin • Add SBT if necessary Build process 27
  25. • Compile time is a concern! • FSC • Useful

    Maven goals: mvn scala:cc mvn scala:console Build process f. 28
  26. 31 Option Scope Package Size Bundle Bundle & Shrink w/ProGuard

    Install separately compile mvn package -Pbundle 8.5 MB Simple compile mvn package -Pproduction 680 KB Deploy externally provided mvn package 50 KB Deploy internally Deployment options
  27. 31 Option Scope Package Size Bundle Bundle & Shrink w/ProGuard

    Install separately compile mvn package -Pbundle 8.5 MB Simple compile mvn package -Pproduction 680 KB Deploy externally provided mvn package 50 KB Deploy internally Deployment options
  28. 31 Option Scope Package Size Bundle Bundle & Shrink w/ProGuard

    Install separately compile mvn package -Pbundle 8.5 MB Simple compile mvn package -Pproduction 680 KB Deploy externally provided mvn package 50 KB Deploy internally Deployment options
  29. 31 Option Scope Package Size Bundle Bundle & Shrink w/ProGuard

    Install separately compile mvn package -Pbundle 8.5 MB Simple compile mvn package -Pproduction 680 KB Deploy externally provided mvn package 50 KB Deploy internally Deployment options
  30. • Dependencies: Scala libraries are jar files and often available

    from Maven repositories • Compile time: mvn scala:cc for continuous compilation • Workflow: scala:cc & atlas-cli pi or mvn package && atlas-install-plugin • Profit: mvn scala:console to get the Scala REPL 33 Development checklist
  31. • Immutable vs Mutable, serial vs. parallel • Rich API:

    Prefer Scala collections • Convert between Java and Scala collections when calling Java methods • Solution: JavaConverters (explicit, preferred) vs. JavaConversions (implicit) • Watch out for serialization: Bandana/XStream Collections 36
  32. 37

  33. 38

  34. 39

  35. • Libraries rely on bean style naming conventions e.g. XWork,

    Velocity • Scala: uniform access principle • Solution: Use @BeanProperty and @BooleanBeanProperty Method naming conventions 40
  36. 41

  37. 42

  38. 43

  39. • How to use @JavaAnnotation on a getter method when

    defining fields? • Scala meta annotations @beanGetter, @beanGetter, @field, @getter, @setter (Meta)-Annotations 44
  40. 45

  41. 46

  42. 47

  43. • In Scala Option represents an optional value. • Don’t

    let the billion-dollar mistake leak into your Scala code! • Solution: wrap nullable method calls in an Option Option vs. null 48
  44. 49

  45. 50

  46. 51

  47. 52

  48. 57

  49. 58

  50. 59

  51. 60

  52. • Servlet & XWork action • Confluence macro • REST

    via Jersey/Jackson • Build setup • Use as a starting point Example Scala plugin 61 https://bitbucket.org/ssaasen/atlassian-scala-example-plugin
  53. • Replace Java entirely with Scala or mix and match

    • Runtime library: 3 deployment options Let’s recap 62
  54. • Replace Java entirely with Scala or mix and match

    • Runtime library: 3 deployment options • Fits into the AMPS development workflow Let’s recap 62
  55. • Replace Java entirely with Scala or mix and match

    • Runtime library: 3 deployment options • Fits into the AMPS development workflow • Scala Gotchas: watch out for common problems Let’s recap 62
  56. • Replace Java entirely with Scala or mix and match

    • Runtime library: 3 deployment options • Fits into the AMPS development workflow • Scala Gotchas: watch out for common problems • “Scala is a great fit to write Atlassian plugins in” Let’s recap 62
  57. • Why? • How? • Example 1: Scala • Example

    2: JRuby • Go and do it! Agenda 63
  58. • Dynamic, object-oriented language • Everything is an object (no

    “primitives”) • Meta-programming and open, executable classes allow for easy creation of DSLs • JRuby: Ruby runtime written in Java (J)Ruby 64
  59. 65

  60. 67 Overview Java JVM Java Standard Library Ruby Runtime Ruby

    Standard Library RubyGems Your Ruby Code javac jrubyc Ruby files *.rb Java Bytecode *.class
  61. 68

  62. 68

  63. 69

  64. 70

  65. • Embed the Ruby Runtime • Use Java to execute

    Ruby scripts and classes 73 Recipe for using JRuby
  66. • Embed the Ruby Runtime • Use Java to execute

    Ruby scripts and classes • Make components of the host application available 73 Recipe for using JRuby
  67. • Embed the Ruby Runtime • Use Java to execute

    Ruby scripts and classes • Make components of the host application available • Use the script execution result in Java 73 Recipe for using JRuby
  68. • javax.scripting - JSR 223 • Apache Bean Scripting Framework

    (BSF) • JRuby Embed - lower level JRuby API 74 Embedding Strategies
  69. • javax.scripting - JSR 223 Only configurable via system properties

    :( • Apache Bean scripting framework (BSF) No point in adding a separate API • JRuby Embed - lower level JRuby API 75 Embedding Strategies
  70. 76

  71. • Package Ruby files • Download and package Ruby gems

    • Make Ruby gems available on the Ruby LOAD_PATH 77 Building the plugin
  72. • Package Ruby files • Download and package Ruby gems

    • Make Ruby gems available on the Ruby LOAD_PATH • Bundle everything into a plugin jar file 77 Building the plugin
  73. • Package Ruby files • Download and package Ruby gems

    • Make Ruby gems available on the Ruby LOAD_PATH • Bundle everything into a plugin jar file • A lot of Maven XML :) 77 Building the plugin
  74. • Package Ruby files • Download and package Ruby gems

    • Make Ruby gems available on the Ruby LOAD_PATH • Bundle everything into a plugin jar file • A lot of Maven XML :) • Caveat: JRuby runtime is even larger! 77 Building the plugin
  75. 79

  76. 80

  77. 82

  78. 83

  79. 84

  80. 85

  81. 86

  82. 87

  83. 88

  84. 89

  85. • How to use Rubygem based libraries in a plugin

    • How to access host components in Ruby • How to use the execution result • Sinatra in Confluence ;-) Example JRuby plugin 90 https://bitbucket.org/ssaasen/atlassian-jruby-example-plugin
  86. • The plugin can’t be written in Ruby! • Use

    the Ruby runtime • Development workflow bonus: dynamic reloading! • The overhead of adding the runtime and providing Java glue code makes this only feasible for high value use cases :( 91 Let’s recap
  87. • Scala is a great alternative when developing an Atlassian

    plugin • JRuby is powerful, makes developing in a dynamic language possible but requires more work and custom Java adapter code • Java interoperability needs to be well understood in both cases 92 Conclusion
  88. #atlascamp TAKE-AWAYS The power of the Atlassian plugin framework allows

    you to build your plugins the way you want and Java doesn’t have to be the limit. “ ” 93
  89. 95 • “Happy Foods” by swanksalot http://www.flickr.com/photos/swanksalot/5021262869/ • “Construction” by

    Daniel Morris http://www.flickr.com/photos/danielmorris/275438405/ • “70 80 90” by roujo http://www.flickr.com/photos/tekmagika/437989361