Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Gliffy Diagram
borderfalse
sizeL
namesingleton
pageFELIX:Apache Felix Dependency Manager - OSGi Design Patterns
pageid9374247
spacealignFELIXcenter

Code Example

...

Code Block

public class Activator extends DependencyActivatorBase {
    public void init(BundleContext context, DependencyManager manager) throws Exception {
        manager.add(createComponent()
            .setInterface(UserStore.class, new Properties() {{ put("store.id", "users"); }})
            .setImplementation(UserStoreImpl.class)
            .add(createServiceDependency()
                .setService(Store.class)
                .setRequired(true)
            )
            .add(createServiceDependency()
                .setService(LogService.class)
                .setRequired(false)
            )
        );
    }
    
    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
}

Aspect Service

Provides an aspect on top of a specific type of service.

...

Gliffy Diagram
FELIX
borderfalse
sizeL
nameaspect
pageFELIX:Apache Felix Dependency Manager - OSGi Design Patterns
pageid9374247
aligncenter
space
spaceFELIX

Code Example

Code Block

public class Activator extends DependencyActivatorBase {
    public void init(BundleContext context, DependencyManager manager) throws Exception {
        manager.add(createAspectService(Manageable.class, "(monitor=true)", 50)
            .setImplementation(ManageableMonitor.class)
        );
    }
    
    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
}

public interface Manageable {
    public void setProperty(String key, String value);
}

public class ManageableMonitor implements Manageable {
    private volatile Manageable m_manageable;

    public void setProperty(String key, String value) {
        System.out.println("Someone set " + key + " to " + value);
        m_manageable.setProperty(key, value);
    }
}

Adapter Service

Provides an adapter for a specific type of service.

...

Gliffy Diagram
FELIX
borderfalse
sizeL
nameadapter
pageFELIX:Apache Felix Dependency Manager - OSGi Design Patterns
pageid9374247
aligncenter
space
spaceFELIX

Code Example

Code Block

public class Activator extends DependencyActivatorBase {
    public void init(BundleContext context, DependencyManager manager) throws Exception {
        manager.add(createAdapterService(Manageable.class, "(publish=servlet)")
            .setInterface(HttpServlet.class.getName(), null)
            .setImplementation(ManageableServlet.class)
        );
    }
    
    public void destroy(BundleContext context, DependencyManager manager) throws Exception {}
}

public interface Manageable {
    public void setProperty(String key, String value);
}

public class ManageableServlet implements HttpServlet {
    private volatile Manageable m_manageable;

    public void doPost(HttpRequest req, HttpResponse response) {
        String key = req.getProperty("key");
        String value = req.getProperty("value");
        m_manageable.setProperty(key, value);
    }
}

Resource Adapter Service

Provides an adapter for a specific type of resource.

...

Resource adapters are similar to normal adapters, but instead of requiring a service, they require a resource and provide a service on top of it. Resources are an abstraction that is introduced by the dependency manager, represented as a URL. They can be implemented to serve resources embedded in bundles, somewhere on a file system or in a content repository or database.

...

Gliffy Diagram
borderfalse
sizeL
nameresource-adapter
pageFELIX:Apache Felix Dependency Manager - OSGi Design Patterns
pageid9374247aligncenter
spaceFELIX

Temporal Dependency

Provides a proxy that hides the service dynamics of a dependency, even if it disappears for a short time.

Motivation

As a service consumer, you sometimes do not want to deal with the dynamics of services and the fact that they tend to go away for short periods of time whilst their hosting bundle gets updated. A temporal dependency provides you with a proxy that hides these dynamics and blocks your calls if you try to invoke a method on a service that is currently "updating". The maximum time to wait is configurable and you will get an exception if no new service becomes available before that time.

Structure

Null Object

Provides an implementation of an object that does nothing and can be used in the absence of the real object.

Motivation

When a component depends on a service, but the dependency is optional, it means that it will use this service when available, but it can still operate if it's not. Constantly checking in your code if a service is actually available tends to lead to code with a lot of "if (service != null) service.invoke();" constructions which do not help with code readability. Instead, the dependency manager offers you a mechanism where it will inject null objects for services that are currently not available so you can simply invoke methods on them that "do nothing".

Structure

Whiteboard

Handles listeners by leveraging the OSGi service registry to publish and look them up.

Motivation

The traditional model for dealing with listeners in Java needlessly complicates things in an OSGi context. Instead of having listeners registering themselves with the component that will invoke them on any change, a listener simply registers itself in the service registry and the component will do a lookup of all relevant services. This is explained in more detail on the OSGi.org wiki in the "Listeners considered harmful: the 'whiteboard' pattern" article.

Structure