Author: jsinovassinnaik Date: Wed Jan 18 16:06:13 2023 New Revision: 1906771
URL: http://svn.apache.org/viewvc?rev=1906771&view=rev Log: Site checkin for project Apache Unomi :: Root Project Modified: unomi/website/manual/2_1_x/index.html unomi/website/manual/latest/index.html Modified: unomi/website/manual/2_1_x/index.html URL: http://svn.apache.org/viewvc/unomi/website/manual/2_1_x/index.html?rev=1906771&r1=1906770&r2=1906771&view=diff ============================================================================== --- unomi/website/manual/2_1_x/index.html (original) +++ unomi/website/manual/2_1_x/index.html Wed Jan 18 16:06:13 2023 @@ -2870,6 +2870,68 @@ def execute() { </div> </div> </div> +<div class="sect4"> +<h5 id="_inject_an_osgi_service_in_a_groovy_script">Inject an OSGI service in a groovy script</h5> +<div class="paragraph"> +<p>It’s possible to use the services provided by unomi directly in the groovy actions.</p> +</div> +<div class="paragraph"> +<p>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.</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre>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 +}</pre> +</div> +</div> +</div> +<div class="sect4"> +<h5 id="_known_limitation">Known limitation</h5> +<div class="paragraph"> +<p>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:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre>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</pre> +</div> +</div> +</div> </div> <div class="sect3"> <h4 id="_scripting_roadmap">4.8.5. Scripting roadmap</h4> @@ -11265,7 +11327,7 @@ They allow to modify an item, that would </div> <div id="footer"> <div id="footer-text"> -Last updated 2022-12-14 10:31:11 +0100 +Last updated 2023-01-06 14:47:30 +0100 </div> </div> </body> Modified: unomi/website/manual/latest/index.html URL: http://svn.apache.org/viewvc/unomi/website/manual/latest/index.html?rev=1906771&r1=1906770&r2=1906771&view=diff ============================================================================== --- unomi/website/manual/latest/index.html (original) +++ unomi/website/manual/latest/index.html Wed Jan 18 16:06:13 2023 @@ -2870,6 +2870,68 @@ def execute() { </div> </div> </div> +<div class="sect4"> +<h5 id="_inject_an_osgi_service_in_a_groovy_script">Inject an OSGI service in a groovy script</h5> +<div class="paragraph"> +<p>It’s possible to use the services provided by unomi directly in the groovy actions.</p> +</div> +<div class="paragraph"> +<p>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.</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre>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 +}</pre> +</div> +</div> +</div> +<div class="sect4"> +<h5 id="_known_limitation">Known limitation</h5> +<div class="paragraph"> +<p>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:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre>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</pre> +</div> +</div> +</div> </div> <div class="sect3"> <h4 id="_scripting_roadmap">4.8.5. Scripting roadmap</h4> @@ -11265,7 +11327,7 @@ They allow to modify an item, that would </div> <div id="footer"> <div id="footer-text"> -Last updated 2022-12-14 10:31:11 +0100 +Last updated 2023-01-06 14:47:30 +0100 </div> </div> </body>
