Changes between Version 22 and Version 23 of PluginDevelopmentGuide


Ignore:
Timestamp:
21.04.2013 00:33:45 (12 years ago)
Author:
jri
Comment:

Consuming more than one service

Legend:

Unmodified
Added
Removed
Modified
  • PluginDevelopmentGuide

    v22 v23  
    864864=== Consuming a service === 
    865865 
    866 Your plugin can consume the services provided by other plugins. To do so your plugin must get hold of the //service object// of the other plugin. Your plugin typically stores that service object in a private instance variable. Through the service object your plugin can call all the service methods declared in the other's plugin service interface. 
    867  
    868 Behind the scenes the DeepaMehta Core handles a plugin service as an OSGi service. Because of the dynamic nature of an OSGi environment DeepaMehta plugin services can arrive and go away in an unpredictable fashion. Your plugin must deal with that. However, you as a plugin developer must not care about DeepaMehta's OSGi foundation. The DeepaMehta Core hides that from you and provides an easy-to-use API for consuming plugin services. 
     866Your plugin can consume the services provided by other plugins. To do so your plugin must get hold of the //service object// of the other plugin. Through the service object your plugin can call all the service methods declared in the other's plugin service interface. 
     867 
     868Behind the scenes the DeepaMehta Core handles a plugin service as an OSGi service. Because of the dynamic nature of an OSGi environment DeepaMehta plugin services can arrive and go away at any time. Your plugin must deal with that. However, you as a plugin developer must not care about DeepaMehta's OSGi foundation. The DeepaMehta Core hides the details from you and provides an easy-to-use API for consuming plugin services. 
    869869 
    870870To consume a plugin service your plugin must override 2 hooks: `serviceArrived` and `serviceGone`. Through the former your plugin gets hold of a service object and in the latter your plugin must release that service object. These 2 hooks are called by the DeepaMehta Core as soon as a desired plugin becomes available resp. goes away. 
    871871 
    872 To tell the DeepaMehta Core which plugin services your plugin wants to consume use the `@ConsumesService` annotation at the `serviceArrived` hook. State the fully qualified names (strings) of all the respective service interfaces as the annotation argument. For more than one service use the array notation: `{"...", "...", ...}`. 
    873  
    874 The single argument of the 2 `serviceArrived` and `serviceGone` hooks is the respective service object, declared generically just as `PluginService`. (Remember, `PluginService` is the common base interface for all plugin services.) So, casting is required. In `serviceArrived` you typically store the service object in a private instance variable. In `serviceGone` you typically set the instance variable to `null` in order to release the service object. 
     872To tell the DeepaMehta Core which plugin service your plugin wants to consume use the `@ConsumesService` annotation at the `serviceArrived` hook. State the fully qualified name (string) of the respective service interface as the annotation argument. To consume more than one service use the array notation: `{"...", "..."}`. 
     873 
     874The single argument of the 2 `serviceArrived` and `serviceGone` hooks is the respective service object, declared generically just as `PluginService`. (Remember, `PluginService` is the common base interface for all plugin services.) So casting is required. In `serviceArrived` you typically store the service object in a private instance variable. In `serviceGone` you typically set the instance variable to `null` in order to release the service object. 
    875875 
    876876As an example, see how the //Workspaces// plugin (part of the DeepaMehta Standard Distribution) consumes the //Facets// service: 
     
    906906}}} 
    907907 
    908 You see the Workspaces plugin consumes just one plugin service: the //Facets// service. So, the `@ConsumesService`argument is just one string. The `PluginService` object passed to the 2 hooks needs not being further investigated. 
     908You see the Workspaces plugin consumes just one plugin service: the //Facets// service. Its service interface is specified as the `@ConsumesService` argument. The `PluginService` object passed to the 2 hooks needs not being further investigated. 
     909 
     910In contrast lets see how it looks like when a plugin consumes more than one service. As an example here is an extraction of the //Geomaps// plugin (part of the DeepaMehta Standard Distribution): 
     911 
     912{{{ 
     913#!java 
     914public class GeomapsPlugin extends PluginActivator { 
     915 
     916    private TopicmapsService topicmapsService; 
     917    private FacetsService facetsService; 
     918 
     919    // *** Hook Implementations *** 
     920 
     921    @Override 
     922    @ConsumesService({ 
     923        "de.deepamehta.plugins.topicmaps.service.TopicmapsService", 
     924        "de.deepamehta.plugins.facets.service.FacetsService" 
     925    }) 
     926    public void serviceArrived(PluginService service) { 
     927        if (service instanceof TopicmapsService) { 
     928            topicmapsService = (TopicmapsService) service; 
     929        } else if (service instanceof FacetsService) { 
     930            facetsService = (FacetsService) service; 
     931        } 
     932    } 
     933 
     934    @Override 
     935    public void serviceGone(PluginService service) { 
     936        if (service == topicmapsService) { 
     937            topicmapsService = null; 
     938        } else if (service == facetsService) { 
     939            facetsService = null; 
     940        } 
     941    } 
     942}}} 
     943 
     944You see the Geomaps plugin consumes 2 plugin services: the //Topicmaps// service and the //Facets// service. Both service interfaces are specified as the `@ConsumesService` argument. Note the array notation with the curly braces `{...}`. Note also the `if` statements in the `serviceArrived` and `serviceGone` hooks.