ThR37
ThR37

Reputation: 4085

How to externalize the configuration of bundles?

I am developing a OSGi program composed of several bundles that I run sometimes on my local windows dev computer, sometimes on a classic linux. Currently, several bundles dedicated to resource connection have their own configuration file (properties file) containing some informations like the path to access some important files (present on both environments).

However, since the paths are different on the two execution-environments, I have to manually change the configuration before compilation, depending on which environment I am going to run my program in.

Is there a way for bundles to refer to an external configuration file? A solution could be to create a fragment for each environment that I generate only once, but I won't be able to change the configuration file easily since it will be in the jar of the fragment.

Are there some "best practices" that I should know to solve my "simple" problem ?

Upvotes: 1

Views: 2772

Answers (2)

Chris Dolan
Chris Dolan

Reputation: 8963

Expanding on @earcam's excellent suggestion, I recommend binding your configuration via Declarative Services and Metatype. It makes it REALLY easy, particularly with the Felix annotations. Below is a simplified example of a service that uses JAAS for authentication, and it has a configurable JAAS realm name. The "ConfigurationPolicy.OPTIONAL" is the awesome part. If you set it to REQUIRE then the service will not be registered until its configured.

@Component(
        name = "com.example.authprovider",
        label = "Example authentication interceptor",
        description = "Blocks unauthenticated access to REST endpoints",
        specVersion = "1.1",
        metatype = true,
        policy = ConfigurationPolicy.OPTIONAL
)
@Service
@References({
        ...
})
@Properties({
        @Property(name="jaasRealm", value = "default", label="JAAS Realm",
                description = "the JAAS realm to use to find LoginModules to authenticate this login"),
        ...
})
public class Foo implements ... {
    ...
}

If you take this approach and use a Metatype-friendly container like Apache Karaf, then you'll get an auto-generated configuration UI for free in the admin web console.

Upvotes: 5

earcam
earcam

Reputation: 6682

Take a look at OSGi's ConfigurationAdmin [1],[2] - this will suit your needs exactly (and is yet another example of OSGi's elegance).

Basically you'll implement a ManagedService or ManagedServiceFactory and the ConfigurationAdmin service take care of the rest.

The default setup for the Felix implementation used in concert with File Install (see Angelo's comment) will scan a directory of configuration files (filename is the service ID and file suffix .cfg). But ConfigurationAdmin is pluggable, so the backend for config could be a database etc.

The great thing about externalizing your config in this way is that you can keep it with the app/environment - so your bundles become agnostic of their environment.

Upvotes: 6

Related Questions