Let's talk about Launchers, and how they are used to start JVx applications.
Starting an application
We've previously outlined a simple way to start a JVx application, and now we're going to look at how to do it with a launcher to benefit from everything JVx has to offer. From a technical point of view, there are two prerequisites which must be fulfilled before a JVx application can run:
- the JVM must have started.
- the technology specific system must have started.
Then, and only then, the JVx application can run. Depending on the implementation that is used, that can be as easily as instancing the factory (Swing, JavaFX), but can also mean that a servlet server has to start (Vaadin). Because we do not wish to encumber our applications with technology specific code, we have to entrust all this to an encapsulated entity, meaning the implementations of ILauncher and IApplication.
Following the chain
The steps for getting an application to start are as follows:
- The first thing that must run is obviously the JVM, without it we won't have much luck starting anything.
- The launcher must be created and it must start the Technology.
- The launcher than creates the application which the user is seeing.
So we need two classes, the ILauncher implementation which knows how to start the Technology and the IApplication implementation. That we already knew, so let's try to put this into code. For simplicity reasons (and because I don't want to write a complete factory from scratch for this example) we will reuse the Swing implementation and write a new launcher and application for it.
Entry point
The Main class that we will use as example is very straightforward:
-
public class Main
-
{
-
public static void main(String[] pArgs)
-
{
-
// All we have to do here is kickoff the creation of the launcher.
-
// The launcher will do everything that is required to start for us.
-
//
-
// In a real world scenario and/or application there might be more
-
// setup or groundwork required, for example processing the arguments,
-
// but we don't need any of that here.
-
new SwingLauncher();
-
}
-
}
All we have to do there is start the launcher itself. As the comment suggests, there might be work required for a "real" application startup. For this example, it is all we need to do. Of course we could also directly embed this little function into the launcher implementation itself, to save us one class.
The launcher
The ILauncher implementation on the other hand contains quite some logic, but nothing not manageable:
-
public class SwingLauncher extends SwingFrame
-
implements ILauncher
-
{
-
// We have to extend from SwingFrame because there is no factory
-
// instantiated at that point, so we can't use UI components.
-
-
private IApplication application;
-
-
public SwingLauncher()
-
{
-
super();
-
-
try
-
{
-
SwingUtilities.invokeAndWait(this::startup);
-
}
-
catch (InvocationTargetException | InterruptedException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
-
@Override
-
public void dispose()
-
{
-
try
-
{
-
// We must notify the application that we are being disposed.
-
application.notifyDestroy();
-
}
-
catch (SecurityException e)
-
{
-
e.printStackTrace();
-
}
-
-
super.dispose();
-
-
// We have to make sure that the application is exiting when
-
// the frame is disposed of.
-
System.exit(0);
-
}
-
-
private void startup()
-
{
-
// We create a new SwingFactory and it is directly registered as global
-
// instance, that means it will be used by all components which are
-
// created from now on.
-
UIFactoryManager.getFactoryInstance(SwingFactory.class);
-
-
// Also we set it as our factory instance.
-
setFactory(UIFactoryManager.getFactory());
-
-
// Because the IApplication implementation we use is based upon
-
// UI components (which is advisable) we have to wrap this launcher
-
// in an UILauncher.
-
UILauncher uiLauncher = new UILauncher(this);
-
-
// Now we create the main application.
-
// Note that the ExampleApplication is already based upon
-
// UI components.
-
application = new ExampleApplication(uiLauncher);
-
-
// Then we add the application as content to the launcher.
-
uiLauncher.add(application);
-
-
// Perform some setup work and start everything.
-
uiLauncher.pack();
-
uiLauncher.setVisible(true);
-
-
// We also have to notify the application itself.
-
application.notifyVisible();
-
}
-
-
// SNIP
-
}
In short, the launcher is kicking off the Swing thread by invoking the startup method on the main Swing thread. This startup method will instantiate the factory and then create the application. From there we only need to set it visible and then our application has started.
The launcher extends from SwingFrame, that is required because there hasn't been a factory created yet which could be used by UI components to create themselves. If we'd try to use an UI component before creating/setting a factory, we would obviously see the constructor of the component fail with a NullPointerException.
The method startup() is invoked on the main Swing thread, which also happens to be the main UI thread for JVx in this application. Once we are on the main UI thread we can create the application, add it and then set everything to visible.
The application
The IApplication implementation is quite short, because we extend com.sibvisions.rad.application.Application, an IApplication implementation created with UI components.
-
public class ExampleApplication extends Application
-
{
-
public ExampleApplication(UILauncher pParamUILauncher)
-
{
-
super(pParamUILauncher);
-
}
-
-
@Override
-
protected IConnection createConnection() throws Exception
-
{
-
// Not required for this example.
-
return null;
-
}
-
-
@Override
-
protected String getApplicationName()
-
{
-
return "Example Application";
-
}
-
}
Because the launcher has previously started the technology and created the factory we can from here on now use UI components, which means we are already independent of the underlying technology. So the IApplication implementation can already be used with different technologies and is completely independent.
Notes on the launcher
As you might have noticed, in our example the launcher is a (window) frame, that makes sense for nearly every desktop GUI toolkit as they all depend upon a window as main method to display their applications. But the launcher could also be simpler, for example just a call to start the GUI thread. Or it could be something completely different, for example an incoming HTTP request.
Also don't forget that the launcher is providing additional functionality to the application, like saving file handles, reading and writing the configuration and similar platform and toolkit dependent operations, see the launcher for Swing for further details.
Conclusion
This example demonstrates how a simple launcher is implemented and why it is necessary to have a launcher in the first place. Compared with the "just set the factory" method this seems like a lot of work, but the launchers used by JVx are of course a lot more complex than these examples, that is because they implement all the required functionality and also take care of a lot of boiler plate operations. It is taking care of all technology specific code and allows to keep your application free from knowing about the platform it runs on.