Previous Topic

Next Topic

Book Contents

Book Index

Using Lua Scripts

This guide contains some general Lua usage scenarios for the Lua OSGi library.

Invoking Lua Scripts

Once the Lua bundle is installed on the framework, the following Lua support is provided:

For example:

lua.load D:\lua-scripts\go.lua arg1 arg2

For example:

lua.eval print(2+2)

4

Lua Programming in OSGi

How to access common Java OSGi objects via Lua:

Getting Bundle Context

Use the osgilib.bc() function to get the bundle context. Here is an example to find the system bundle using the context and to print its symbolic name.

local context = osgilib.bc()

print("Context:"..context:getBundle(0):getSymbolicName())

Getting Services

Use the osgilib.services(filter) and osgilib.service(classname) functions to get the OSGi services. The filter can be with a given LDAP filter, service PID, service ID or Java object class. The first example prints all PluggableCommand service group help messages on the console:

local services = osgilib.services("com.prosyst.util.pcommands.PluggableCommands")

for i=1,services.length do

      local current=services[i];

      print("Services:"..current:getGroupHelpMessage())

end

The second example prints just one PluggableCommand service group help message on the console. If there are more than one services available , they are ordered by increasing service ranking, and then by increasing service ID, and then the first one is taken.

local service = osgilib.service("com.prosyst.util.pcommands.PluggableCommands")

print("Service:"..service:getGroupHelpMessage())

Getting Bundles

Use the osgilib.bundles(filter) and osgilib.bundle(symbolicname) functions to get the OSGi bundles. The filter parameter can be a 'long' number – bundle ID, an LDAP filter or a pattern with the asterisk symbol – *. The first example prints all symbolic names of bundles matching the given com.prosyst.mbs.core.* filter.

local bundles = osgilib.bundles("com.prosyst.mbs.core.*")

for i=1,bundles.length do

      local current=bundles[i];

      print("Bundles:"..current:getSymbolicName())

end

The second example prints the symbolic name of the Core API bundle only, straight after getting it.

local bundle = osgilib.bundle("com.prosyst.mbs.core.api")

print("Bundle:"..bundle:getSymbolicName())