Views
2 weeks ago

tornadofx-guide

OSGi OSGi This chapter

OSGi OSGi This chapter is geared primarily towards folks who already have familiarity with OSGi, which stands for Open Services Gateway Initiative. The idea behind OSGi is adding and removing modules to a Java application without the need for restarting. TornadoFX supports OSGi and allows highly modular and dynamic applications. If you have no interest in OSGi currently, you are welcome to skip this chapter. However, it is highly recommended to at least know what it is so you can identify moments in the future that make it handy. TornadoFX comes with the metadata needed for an OSGi runtime to detect and enable it. When the tornadofx.jar is loaded in an OSGi container, a number of services are automatically installed in the runtime. These services enable some very interesting features which we will discuss. OSGi Introduction Please be familiar with the basics of OSGi before you continue this chapter. To get a quick overview of OSGi technology you can check out the tutorials on the OSGi Alliance website. The Apache Felix tutorials are also a good starting point reference for basic OSGi patterns. Services When the TornadoFX JAR is loaded, you can create your own TornadoFX bundle and create your application any way you like. However, some usage patterns are so typical and useful that TornadoFX has built-in support for them. Dynamic Applications 206

OSGi The dynamic nature of OSGi lends itself well to GUI applications in general. The ability to have certain functionality come and go as the environment changes can be powerful. JavaFX itself is unfortunately written in a way that prevents you from starting another JavaFX application after the initial application shuts down. To circumvent this shortcoming and enable you to stop and start your application as many times as you want, TornadoFX provides a way to register your App class with an application proxy which will keep the JavaFX environment running even when your application shuts down. To get started, implement a BundleActivator that provides a means to start() and stop() an App . Registering your application for this functionality can be done by calling context.registerApplication with your App class as the single parameter in your bundle Activator : class Activator : BundleActivator { override fun start(context: BundleContext) { context.registerApplication(MyApp::class) } } override fun stop(context: BundleContext) { } If you prefer OSGi declarative services instead, this will have the same effect provided that you have the OSGi DS bundle loaded: @Component class AppRegistration : ApplicationProvider { override val application = MyApp::class } Provided that the TornadoFX bundle is available in your container, this is enough to start your application automatically once the bundle is activated. You can now stop and start it as many times as you like by stopping and starting the bundle. Dynamic Stylesheets 207

Guide
guide
GUIDE
GUIDE
GUIDE
Guide
Guide
GUIDE
guide
guide
Guide
Guide
GUIDE
GUIDE
GUIDE
Guide
Guide
The Guide
guide
Guide
guide
Guide
GUIDE TO
guide
GUIDE
GUIDE
GUIDE
GUIDE
guide
guide to