Skip to content

Programmatic Service Registration_124059889

nxi edited this page Apr 9, 2015 · 1 revision

Gumtree : Programmatic Service Registration

Created by Tony Lam, last modified on Jul 07, 2009
GumTree promotes the use of service oriented programming style to make application more maintainable. A service in the GumTree Platform is simply a POJO (Plain Old Java Object) that has a well definied interface. Each service is usually inject a single instance into the runtime, and make it available for use to the rest of the system. This is considered to be a better programming practice than using the singleton pattern. There are many ways to inject a service into OSGi (the runtime kernel of GumTree), they included OSGi, Declarative Service, Spring DM IoC, etc. GumTree provides a convenience way to inject services programmatically, using the helper class org.gumtree.core.util.ServiceRegistrationManager. For example, to register a service in the Activator, try:
public class Activator extends Plugin {

    private ServiceRegistrationManager serviceRegistrationManager;

    public void start(BundleContext context) throws Exception {
        ...
        // Register scripting manager
        serviceRegistrationManager = new ServiceRegistrationManager(context);
        serviceRegistrationManager.registerService(IScriptingManager.class, new ScriptingManager());
        ...
    }

    public void stop(BundleContext context) throws Exception {
        ...
        // Require manual dispose to unregister
        if (serviceRegistrationManager != null) {
            serviceRegistrationManager.dispose();
            serviceRegistrationManager = null;
        }
        ...
    }
Strickly speaking all registered services should be disposed automatically by OSGi, but it is a good practice to remove them manually to avoid memory leak. References:
Document generated by Confluence on Apr 01, 2015 00:11
Clone this wiki locally