I am confused.
I am currently developing an application using OSGi declarative services (DS). I have an API for my application and the application works fine. Now I want to add a User Interface. So I decided on a separation between a Controller and View Elements. Each view is it's own component.
Now my confusion starts. When I am using DS, I create a Service Interface which gets implementet by its Implementation and consumed by a Consumer. I do not understand how this fits in with UI Elements? I do not want to register a complete new API just for my UI into the OSGi framework, and I do not think it should be necessary. On the other hand, if I want to leverage OSGi to dynamically load my components if and only if they are needed, to save resources, I cannot seem to reference them properly, because they are not Services and therefore not registered with the framework....?
I tried to manually register Services with
FrameworkUtil.getBundle(getClass()).getBundleContext().registerService(Class.class, new Class(), null)
and later retrieving them with
context.getService(context.getServiceReference(Class.class)
But first, this does not seem to work, as I get an error stating that Null Service References are not allowed, and secondly I do want to make this kind of properly. According to Peter Kriens in this SO discussion registering and retrieveing services manually is really really really not recommended.
So I feel like I am overseeing something relevant. In conclusion these are my most important questions:
When I have multiple Components inside one package and I do not want to call each others constructors like plain java, how does one component activate another, using the OSGi SCR and use their methods? For example the MainView has a button to Edit something, which is its own dialog and Component. So when the user presses the button, the MainView component activate the EditView component, which is only then loaded and thrown away afterwards.
How does registering components in a framework work? Is it even possible? I feel like, as well thought through as the OSGi spec exists, this either exists already or I should not do it and think through my application structure.
Using the Controller-View pattern the UI only talks to a controller, which fetches data from the Service over a standard OSGi Reference. Does this setup even make sense implemented in OSGi?
Thanks for any help or discussion. I really appreciate it.