polyglot plugin programming

95
1

Upload: atlassian

Post on 10-May-2015

1.039 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Polyglot Plugin Programming

1

Page 2: Polyglot Plugin Programming

How to write plugins in a language other than Java

Stefan SaasenConfluence Team Lead, Atlassian

Polyglot Plugin Development

2

Page 3: Polyglot Plugin Programming

• Written an Atlassian plugin in Java• Interested in other programming languages• Eager to see what is possible with Atlassian plugins

Audience

3

Page 4: Polyglot Plugin Programming

• Who has actually written a Atlassian plugin?• In any other language than Java?

Show of hands

4

Page 5: Polyglot Plugin Programming

5

Page 6: Polyglot Plugin Programming

6

Rhino

Page 7: Polyglot Plugin Programming

7

Page 9: Polyglot Plugin Programming

• Why?• How?

• Example 1: Scala

• Example 2: JRuby

• Go and do it!

Polyglot Plugins

9

Page 10: Polyglot Plugin Programming

Why not?

10

Page 11: Polyglot Plugin Programming

Why ?

11

Page 12: Polyglot Plugin Programming

• 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?

Page 13: Polyglot Plugin Programming

• 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?

Page 14: Polyglot Plugin Programming

14

Page 15: Polyglot Plugin Programming

15

“Succinctness”

“No time to write in Java”

“Less bugs”

Page 16: Polyglot Plugin Programming

16

Page 17: Polyglot Plugin Programming

• Why?• How?

• Example 1: Scala

• Example 2: JRuby

• Go and do it!

Polyglot Plugins

17

Page 18: Polyglot Plugin Programming

• 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

Page 19: Polyglot Plugin Programming

19

Page 20: Polyglot Plugin Programming

20

Page 21: Polyglot Plugin Programming

21

Page 22: Polyglot Plugin Programming

22

Page 23: Polyglot Plugin Programming

How?

23

Page 24: Polyglot Plugin Programming

24

Overview

Java

JVM

Java Standard Library

Scala Runtime Library

java

csc

alac

Java Bytecode*.class

Scala

Page 25: Polyglot Plugin Programming

25

Page 26: Polyglot Plugin Programming

26

Page 27: Polyglot Plugin Programming

• Start with Maven to get the AMPS goodness

• Use the maven-scala-plugin

• Add SBT if necessary

Build process

27

Page 28: Polyglot Plugin Programming

• Compile time is a concern!• FSC• Useful Maven goals:

mvn scala:ccmvn scala:console

Build process f.

28

Page 29: Polyglot Plugin Programming

Bundle runtime

29

Page 30: Polyglot Plugin Programming

Size might be a problem :(

30

Page 31: Polyglot Plugin Programming

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

Page 32: Polyglot Plugin Programming

Simply install using the plugin manager!

32

Page 33: Polyglot Plugin Programming

• 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

Page 34: Polyglot Plugin Programming

Scala: Working with Java APIs

34

Page 35: Polyglot Plugin Programming

• Collections• Method naming conventions• Annotations• Null vs. Option

Scala gotchas (the 80%)

35

Page 36: Polyglot Plugin Programming

• 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

Page 37: Polyglot Plugin Programming

37

Page 38: Polyglot Plugin Programming

38

Page 39: Polyglot Plugin Programming

39

Page 40: Polyglot Plugin Programming

• Libraries rely on bean style naming conventionse.g. XWork, Velocity

• Scala: uniform access principle• Solution: Use @BeanProperty and

@BooleanBeanProperty

Method naming conventions

40

Page 41: Polyglot Plugin Programming

41

Page 42: Polyglot Plugin Programming

42

Page 43: Polyglot Plugin Programming

43

Page 44: Polyglot Plugin Programming

• How to use @JavaAnnotation on a getter method when defining fields?

• Scala meta annotations @beanGetter, @beanGetter, @field, @getter, @setter

(Meta)-Annotations

44

Page 45: Polyglot Plugin Programming

45

Page 46: Polyglot Plugin Programming

46

Page 47: Polyglot Plugin Programming

47

Page 48: Polyglot Plugin Programming

• 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

Page 49: Polyglot Plugin Programming

49

Page 50: Polyglot Plugin Programming

50

Page 51: Polyglot Plugin Programming

51

Page 52: Polyglot Plugin Programming

52

Page 53: Polyglot Plugin Programming

Show me the code!

53

Page 54: Polyglot Plugin Programming
Page 55: Polyglot Plugin Programming
Page 56: Polyglot Plugin Programming
Page 57: Polyglot Plugin Programming

57

Page 58: Polyglot Plugin Programming

58

Page 59: Polyglot Plugin Programming

59

Page 60: Polyglot Plugin Programming

60

Page 61: Polyglot Plugin Programming

• 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

Page 62: Polyglot Plugin Programming

• 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

Page 63: Polyglot Plugin Programming

• Why?• How?

• Example 1: Scala

• Example 2: JRuby

• Go and do it!

Agenda

63

Page 64: Polyglot Plugin Programming

• 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

Page 65: Polyglot Plugin Programming

65

Page 66: Polyglot Plugin Programming

How?

66

Page 67: Polyglot Plugin Programming

67

Overview

Java

JVM

Java Standard Library

Ruby Runtime

Ruby Standard Library

RubyGems Your Ruby Code

java

cjr

ubyc

Ruby files *.rb

Java Bytecode*.class

Page 68: Polyglot Plugin Programming

68

Page 69: Polyglot Plugin Programming

69

Page 70: Polyglot Plugin Programming

70

Page 71: Polyglot Plugin Programming

71

Plugins can’t be written in Ruby

Page 72: Polyglot Plugin Programming

72

But we can still use Ruby in a plugin!

Page 73: Polyglot Plugin Programming

• 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

Page 74: Polyglot Plugin Programming

• javax.scripting - JSR 223• Apache Bean Scripting Framework (BSF)• JRuby Embed - lower level JRuby API

74

Embedding Strategies

Page 75: Polyglot Plugin Programming

• 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

Page 76: Polyglot Plugin Programming

76

Page 77: Polyglot Plugin Programming

• 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

Page 78: Polyglot Plugin Programming

Show me the code!

78

Page 79: Polyglot Plugin Programming

79

Page 80: Polyglot Plugin Programming

80

Page 81: Polyglot Plugin Programming

81

1 minute

Page 82: Polyglot Plugin Programming

82

Page 83: Polyglot Plugin Programming

83

Page 84: Polyglot Plugin Programming

84

Page 85: Polyglot Plugin Programming

85

Page 86: Polyglot Plugin Programming

86

Page 87: Polyglot Plugin Programming

87

Page 88: Polyglot Plugin Programming

88

Page 89: Polyglot Plugin Programming

89

Page 90: Polyglot Plugin Programming

• 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

90https://bitbucket.org/ssaasen/atlassian-jruby-example-plugin

Page 91: Polyglot Plugin Programming

• 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

Page 92: Polyglot Plugin Programming

• 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

Page 93: Polyglot Plugin Programming

#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

Page 94: Polyglot Plugin Programming

Thank you!

94

Page 95: Polyglot Plugin Programming

95

• “Happy Foods” by swanksalothttp://www.flickr.com/photos/swanksalot/5021262869/

• “Construction” by Daniel Morrishttp://www.flickr.com/photos/danielmorris/275438405/

• “70 80 90” by roujohttp://www.flickr.com/photos/tekmagika/437989361