Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scripting Languages in OSGi

549 views

Published on

A talk at ApacheCon 2012 about using JSR223 Scripting languages from within OSGi

Published in: Software
  • Be the first to comment

  • Be the first to like this

Scripting Languages in OSGi

  1. 1. Scripting Languages in OSGi
  2. 2. Frank Lyaruu CTO Dexels Project lead Navajo Framework Amsterdam www.dexels.com Twitter: @lyaruu
  3. 3. Navajo Framework • TSL • XML based script language • Compiled to Java • Recently ported to OSGi
  4. 4. Scripting languages in OSGi • The combination has a pretty bad reputation • Different approach to dynamism
  5. 5. 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
  6. 6. Advantages of scripting languages • Dynamic. Changing code at runtime is completely normal • No complex builds necessary • Effective languages available for certain problems
  7. 7. Disadvantages of scripting languages • Errors might reveal themselves only at runtime • Limited modularity • Performance can be a problem
  8. 8. Calling a script from Java
  9. 9. JSR-223 Scripting API • More or less language independent • Many supporting languages • Some have a proprietary API • Some have both
  10. 10. Define a java interface In the simplest case:
  11. 11. Using scripts in plain Java
  12. 12. 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
  13. 13. Generating bundles • Make up symbolic name • Make up a version • Determine Java dependencies
  14. 14. Generate an implementation class • We need an actual implementation class in the generated bundle. • Usually it can be pretty simple
  15. 15. Generated java file:
  16. 16. Base class* *The actual implementation is a bit messier
  17. 17. Generate a MANIFEST
  18. 18. 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)
  19. 19. Expose a service to OSGi • Register manually from an activator • Use a dependency injection framework
  20. 20. Declarative Services • Easier to generate XML than Java code • Tooling (Felix WebConsole) • Dependency management for free
  21. 21. Generate DS file
  22. 22. 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)
  23. 23. Finding dependencies • The scripting language supports this :-) (TSL/Navajo) • Annotate script :-| • Use a descriptor file :-(
  24. 24. 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
  25. 25. Compiling Java in OSGi • JSR 199 Compiler API
  26. 26. Compiling Java in OSGi • What is the classpath?
  27. 27. Compiling Java in OSGi • Create a custom javax.tools.JavaFileManager • Use the OSGi Bundle Wiring API to locate exported packages • Listen to bundle events
  28. 28. 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.
  29. 29. Manage changes • Manual • Monitor file system (Felix FileInstall ArtifactTransformer) • On demand (For example git hooks)
  30. 30. DEMO • JRuby
  31. 31. Generated Java
  32. 32. Generated DS
  33. 33. 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
  34. 34. 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
  35. 35. Frank Lyaruu frank@dexels.com Twitter: @lyaruu Demo code: github.com/Dexels/apachecon Navajo code: github.com/Dexels/navajo Questions?

×