Kraken API

  • Kraken API defines script extension API.

Kraken API diagram

Kraken API diagram

ScriptContext usage

  • You can read input from console and print out messages.
    • read() reads a character.
    • readLine() reads a line.
    • print() prints a character.
    • println() prints a line.
  • You can turn echo off when you want to read password.
    • turnEchoOn()
    • turnEchoOff()

Steps to run a kraken script

  1. ConsoleHandler finds an OSGi service reference that implements org.krakenapps.api.ScriptFactory and has a specified "alias" property.
  2. ConsoleHandler gets a ScriptFactory instance by using the first returned service reference.
  3. ConsoleHandler creates a script through ScriptFactory.createScript().
  4. ConsoleHandler finds current ScriptContext and calls ScriptContext.setCurrentScript().
  5. ConsoleHandler creates a new thread and starts ScriptRunner.run().
  6. ScriptRunner calls script's method using reflection.

Common script extension implementation pattern

  • This scenario assumes that you use iPOJO (1.2.0+)
  • Implement ScriptFactory concrete class:
    • ScriptFactory concrete class should be in private package.
    • ScriptFactory constructor can receive a BundleContext instance.
      • You can find other OSGi services using BundleContext.getServiceReference() and can pass them to Script's constructor.
  • Implement Script concrete class:
    • All script's methods called by reflection must have an String[] argument.
  • Config metadata.xml
    • Example:
      <?xml version="1.0" encoding="UTF-8"?>
      <iPOJO>
      	<component className="org.krakenapps.filter.impl.FilterScriptFactory" name="filterScriptFactory" factory="false">
      		<provides>
      			<property name="alias" type="string" value="filter" />
      		</provides>
      	</component>
      	<instance component="filterScriptFactory" />
      </iPOJO>
      
    • Set "alias" property. This will be used when you call script in shell. (e.g. "filter.bind" for bind method call.)
    • iPOJO will instanciate an FilterScriptFactory instance as an iPOJO component and OSGi service.
      • This component instance will be identified by "instance.name" property. ("instance.name" will be "filterScriptFactory" for this example)

Attachments