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. |
| 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. 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 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. |
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. |
| 872 | To 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 | |
| 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. |
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. |
| 908 | You 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 | |
| 910 | In 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 |
| 914 | public 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 | |
| 944 | You 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. |