Configure Your OSGi Services with Apache Felix File Install

A recent post about Managed Services reminded me of a feature in Apache Felix File Install that I found worth mentioning. While working on a project with Holger, I learned from him that File Install cannot only be used to manage bundles. It also monitors configuration files and updates Managed Services when their respective configuration changes.

File Install can also configure Manged Service Factories and more. Yet, I found it particularly useful for Managed Services whose configuration data is usually environment-specific (e.g. printer URLs, database URLs, TCP ports). And the best thing is: it frees you from programmatically fiddling with the Configuration Admin.

How it works

File Install can be told to watch a certain folder (usually through a system property). You can then place configuration files (plain Java property file) into that folder. Configuration files are associated to Managed Services by a naming convention. The file name must have the form <pid>.cfg. If there is a Managed Service with a matching PID, it is updated whenever the file changes.


I put a minimal self-contained example on GitHub. It has a single bundle that provides an EchoService implemented like this:

public class EchoService implements ManagedService {

  public void updated( Dictionary<String, ?> properties ) {
    if( properties != null ) {
      System.out.println( "port = " + properties.get( "port" ) );


Easy to spot: its whole purpose is to print out the configuration whenever it changes.

The EchoService is registered with a PID of echoservice. Besides the bundle is a project that contains the configuration file. It is named echoservice.cfg and holds a single property: port = 7.

The launch configuration puts together the neccessary bundles: OSGi framework (Equinox here), the example bundle, Felix Gogo Shell (to diagnose problems if any) and File Install along with the Configuration Admin. And these VM arguments


tell File Install to watch the project folder and check every Second for changes. Thanks to the project_loc variable the launch configuration is portable across development environments.

Now that we start the OSGi framework, the console output will look like this:

port = 7

If we make a change to echoservice.cfg file and wait a little, we will see that the EchoService is updated. The new configuration appears in the console log.

More of this

If this drew your interest you may want to have a deeper look at File Install. It also supports Managed Service Factories, can write back configuration changes, can substitue property values, and watch multiple folders. The documentation page has all the details.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

− 1 = seven

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: