You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by js...@apache.org on 2023/01/18 10:14:13 UTC

[unomi] branch UNOMI-732-add-documentation created (now e56941e7c)

This is an automated email from the ASF dual-hosted git repository.

jsinovassinnaik pushed a change to branch UNOMI-732-add-documentation
in repository https://gitbox.apache.org/repos/asf/unomi.git


      at e56941e7c UNOMI-732 : add example how to use OSGI service in groovy actions

This branch includes the following new commits:

     new e56941e7c UNOMI-732 : add example how to use OSGI service in groovy actions

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[unomi] 01/01: UNOMI-732 : add example how to use OSGI service in groovy actions

Posted by js...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jsinovassinnaik pushed a commit to branch UNOMI-732-add-documentation
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit e56941e7c4bd5a742ad076e0c1d0e736b26c991f
Author: jsinovassin <js...@jahia.com>
AuthorDate: Wed Jan 18 11:13:55 2023 +0100

    UNOMI-732 : add example how to use OSGI service in groovy actions
---
 manual/src/main/asciidoc/configuration.adoc | 54 +++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/manual/src/main/asciidoc/configuration.adoc b/manual/src/main/asciidoc/configuration.adoc
index 6594f5954..7795d56d4 100644
--- a/manual/src/main/asciidoc/configuration.adoc
+++ b/manual/src/main/asciidoc/configuration.adoc
@@ -529,6 +529,60 @@ curl -X DELETE 'http://localhost:8181/cxs/rules/scriptGroovyActionRule' \
 --user karaf:karaf
 ----
 
+===== Inject an OSGI service in a groovy script
+
+It's possible to use the services provided by unomi directly in the groovy actions.
+
+In the following example, we are going to create a groovy action that displays the number of existing profiles by using the profile service provided by unomi.
+
+----
+import org.osgi.framework.Bundle
+import org.osgi.framework.BundleContext
+import org.osgi.framework.FrameworkUtil
+import org.apache.unomi.groovy.actions.GroovyActionDispatcher
+import org.osgi.framework.ServiceReference
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+
+final Logger logger = LoggerFactory.getLogger(GroovyActionDispatcher.class.getName());
+
+@Action(id = "displayNumberOfProfilesAction", actionExecutor = "groovy:DisplayNumberOfProfilesAction", description = "Display the number of existing profiles")
+def execute() {
+
+    // Use OSGI function to get the bundleContext
+    Bundle bundle = FrameworkUtil.getBundle(GroovyActionDispatcher.class);
+    BundleContext context = bundle.getBundleContext();
+
+    // Get the service reference
+    ServiceReference<ProfileService> serviceReference = context.getServiceReference(ProfileService.class);
+
+    // Get the service you are looking for
+    ProfileService profileService = context.getService(serviceReference);
+
+    // Example of displaying the number of profile
+    logger.info("Display profile count")
+    logger.info(profileService.getAllProfilesCount().toString())
+
+    return EventService.NO_CHANGE
+}
+----
+
+===== Known limitation
+
+Only the services accessible by the class loader of the GroovyActionDispatcher class can be used in the groovy actions.
+That includes the services in the following packages:
+----
+org.apache.unomi.api.actions
+org.apache.unomi.api.services
+org.apache.unomi.api
+org.apache.unomi.groovy.actions
+org.apache.unomi.groovy.actions.annotations
+org.apache.unomi.groovy.actions.services
+org.apache.unomi.metrics
+org.apache.unomi.persistence.spi
+org.apache.unomi.services.actions;version
+----
+
 ==== Scripting roadmap
 
 Scripting will probably undergo major changes in future versions of Apache Unomi, with the likely retirement of MVEL in favor of Groovy Actions detailed above.