You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Getting Started

When developing an OSGi bundle that has dependencies and possibly registers services, there are two classes in particular we need to implement:

  1. The bundle activator which controls the life-cycle of the bundle.
  2. The actual service implementation, which can be a POJO.

When using the dependency manager, your bundle activator is a subclass of DependencyActivatorBase. It needs to implement two life cycle methods: init and destroy. Both methods take two arguments: BundleContext and DependencyManager. The latter is your interface to the declarative API you can use to define your services and dependencies.

The following paragraphs will show various examples that explain how to do this. Subsequently, some more advanced scenarios will be covered that involve listening to dependency and service state changes and interacting with the OSGi framework from within your service implementation.

Registering a service

public class Activator extends DependencyActivatorBase {
    public void init(BundleContext context, DependencyManager manager) throws Exception {
        manager.add(createService()
            .setInterface(Store.class.getName(), null)
            .setImplementation(MemoryStore.class)
        );
    }
    
    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
}

public interface Store {
    public void put(String key, Object value);
    public Object get(String key);
}

public class MemoryStore implements Store {
    private Map m_map = new HashMap();

    public Object get(String key) {
        return m_map.get(key);
    }

    public void put(String key, Object value) {
        m_map.put(key, value);
    }
}

Depending on a service

Tracking services with callbacks

Depending on a configuration

...

  • No labels