scripting languages in osgi

35
Scripting Languages in OSGi

Upload: frank-lyaruu

Post on 16-Apr-2017

388 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Scripting Languages in OSGi

Scripting Languages in OSGi

Page 2: Scripting Languages in OSGi

Frank LyaruuCTO Dexels

Project lead Navajo FrameworkAmsterdam

www.dexels.comTwitter: @lyaruu

Page 3: Scripting Languages in OSGi

Navajo Framework• TSL

• XML based script language

• Compiled to Java

• Recently ported to OSGi

Page 4: Scripting Languages in OSGi

Scripting languages in OSGi

• The combination has a pretty bad reputation

• Different approach to dynamism

Page 5: Scripting Languages in OSGi

Scripting languages in OSGi

• Use any (?) scripting language

• Dynamically add, modify and remove scripts

• Expose it as an OSGi service

• Can declaratively depend on other services

• No external depedencies

• Behave like any other bundle

Page 6: Scripting Languages in OSGi

Advantages of scripting languages

• Dynamic. Changing code at runtime is completely normal

• No complex builds necessary

• Effective languages available for certain problems

Page 7: Scripting Languages in OSGi

Disadvantages of scripting languages

• Errors might reveal themselves only at runtime

• Limited modularity

• Performance can be a problem

Page 8: Scripting Languages in OSGi

Calling a script from Java

Page 9: Scripting Languages in OSGi

JSR-223 Scripting API

• More or less language independent

• Many supporting languages

• Some have a proprietary API

• Some have both

Page 10: Scripting Languages in OSGi

Define a java interfaceIn the simplest case:

Page 11: Scripting Languages in OSGi

Using scripts in plain Java

Page 12: Scripting Languages in OSGi

Scripting languages in OSGi

• Use any (?) scripting language Use JSR-223

• Dynamically add, modify and remove scripts Expose it as an OSGi service

• Can declaratively depend on other services

• No external depedencies

• Behave like any other bundle

Page 13: Scripting Languages in OSGi

Generating bundles

• Make up symbolic name

• Make up a version

• Determine Java dependencies

Page 14: Scripting Languages in OSGi

Generate an implementation class

• We need an actual implementation class in the generated bundle.

• Usually it can be pretty simple

Page 15: Scripting Languages in OSGi

Generated java file:

Page 16: Scripting Languages in OSGi

Base class*

*The actual implementation is a bit messier

Page 17: Scripting Languages in OSGi

Generate a MANIFEST

Page 18: Scripting Languages in OSGi

Scripting languages in OSGi

• Use any (?) scripting language Use JSR-223

• Dynamically add, modify and remove scripts (Generate bundles)

• Expose it as an OSGi service

• Can declaratively depend on other services

• No external depedencies (Generate bundles)

Page 19: Scripting Languages in OSGi

Expose a service to OSGi

• Register manually from an activator

• Use a dependency injection framework

Page 20: Scripting Languages in OSGi

Declarative Services

• Easier to generate XML than Java code

• Tooling (Felix WebConsole)

• Dependency management for free

Page 21: Scripting Languages in OSGi

Generate DS file

Page 22: Scripting Languages in OSGi

Scripting languages in OSGi

• Use any (?) scripting language Use JSR-223

• Dynamically add, modify and remove scripts (Generate bundles)

• Expose it as an OSGi service (DS)

• Can declaratively depend on other services (DS)

• No external depedencies (Generate bundles)

Page 23: Scripting Languages in OSGi

Finding dependencies

• The scripting language supports this :-) (TSL/Navajo)

• Annotate script :-|

• Use a descriptor file :-(

Page 24: Scripting Languages in OSGi

Declarative Services (DS)

• Determine service dependencies

• Generate a Declarative Service descriptor

• Service will only be activated and exposed when all dependencies are met.

• Generate bind/unbind methods for component class

Page 25: Scripting Languages in OSGi

Compiling Java in OSGi

• JSR 199 Compiler API

Page 26: Scripting Languages in OSGi

Compiling Java in OSGi

• What is the classpath?

Page 27: Scripting Languages in OSGi

Compiling Java in OSGi

• Create a custom javax.tools.JavaFileManager

• Use the OSGi Bundle Wiring API to locate exported packages

• Listen to bundle events

Page 28: Scripting Languages in OSGi

Compiling Java in OSGi

• Record all requested packages during compilation

• We’ve found our package dependencies.. Sort of..

• Only works for scripts that are compiled :-(

• Use Dynamic-ImportPackage if all else fails.

Page 29: Scripting Languages in OSGi

Manage changes

• Manual

• Monitor file system (Felix FileInstall ArtifactTransformer)

• On demand (For example git hooks)

Page 30: Scripting Languages in OSGi

DEMO

• JRuby

Page 31: Scripting Languages in OSGi

Generated Java

Page 32: Scripting Languages in OSGi

Generated DS

Page 33: Scripting Languages in OSGi

Current limitations

• Try a language with a byte code compiler (like Scala)

• Class.forName / ThreadContext classloaders call still cause trouble

• Determining dependencies (both code depedencies and service dependencies) is really script dependent

• Split packages will probably fail

Page 34: Scripting Languages in OSGi

Conclusions

• Different views on dynamic code

• Creating bundles on the fly is possible and not that hard*

• Navajo runs at high volume with about 5000 bundles

• ‘Intelligent’ configuration files

Page 35: Scripting Languages in OSGi

Frank Lyaruu

[email protected]: @lyaruu

Demo code:github.com/Dexels/apachecon

Navajo code:github.com/Dexels/navajo

Questions?