Search code examples
javaeclipse-plugincomponentsosgi-bundlekura

Should every component(.xml) have its own eclipse-project?


So I learning how to use eclipse-kura to implement IoT-applications. My question is, should every Component have its own eclipse-project/bundle or if multiple Components can be in the same bundle (as in my OSGI-INF would contain 2 Component Definitions for 2 different classes).

I couldn't find any examples for this anywhere. As I am a beginner at this and am trying to understand the concepts any hint would be appreciated.


Solution

  • Short answer: it is up to you.

    Long answer: You can do it however you like ;)

    There is no rule in OSGi that forbids to put several components (classes that are declarative services and are annotated with @Component) in one bundle. It depends however a little on how you define the boundaries of your bundles, i.e., do you create a bundle per functionality or do you create a bundle per layer in a x-tiered application (e.g. one bundle for db, one bundle for logic and one bundle for GUI). Those examples were completely arbitrary, but you can see that depending on how you define the scopes of your bundles it sometimes would make sense to put several components within one bundle and sometimes not so much.

    Regarding on how I would recommend to structure bundles:

    • create one API bundle per functionality, e.g. a API that defines a database service, API that defines a login service or whatever the functionality of your application should be. API bundles usually only contain interfaces, abstract classes and data types.
    • create one implementation bundle per implementation that has classes that implement the service interfaces defined in the api bundle and use the data types in the API bundle to communicate with services in other bundles.
    • Per implementation bundle put as many components as you deem appropriate for the implementation of the service, but try to not implement functionality that exceeds the boundarys of the scope you defined for this bundle.

    Maybe a good example can be found in the examples of OSGi enRoute: https://enroute.osgi.org/Tutorial/

    Kind regards, Thomas