Writing a script is simple. Create a file (let's call it populate-books.tsl)
and place it somewhere you can easily access from your Felix installation. I've put it in a directory under the Felix home directory called scripts/.
The population script taken as an example here will have the same books as we've used previously. Each command is on a line (below they wrap around because of page width limitations):
echo Now adding books to inventory. . . add admin admin 9789079350018 "OSGi Service Platform, Core Specification, Release 4, Version 4.1" "OSGi Alliance" Reference 8 add admin admin 9789079350025 "OSGi Service Platform, Service Compendium, Release 4, Version 4.1" "OSGi Alliance" Reference 6 echo Done.
This program can now be executed on framework restart to add those two books to the inventory.
To execute the program, we use the source
command (introduced in Chapter 3). I've refreshed the bundle to clear the books that were added earlier:
g! refresh 6
g!
Stoping Book Inventory Mock Impl
Starting Book Inventory Mock Impl
Use the cd
command to change directory and the ls
command to double-check that the file is there:
g! cd scripts
Name scripts
CanonicalPath C:felixscripts
Parent C:felix
Path C:felixscripts
AbsoluteFile C:felixscripts
AbsolutePath C:felixscripts
CanonicalFile C:felixscripts
ParentFile C:felix
g! ls
C:felixscriptspopulate-books.tsl
Now run the following script:
g! source populate-books.tsl
Now adding books to inventory. . .
Done.
A quick search confirms the books were correctly added.
g! search admin admin author %
Reference: OSGi Service Platform, Core Specification, Release 4, Version 4.1 from OSGi Alliance [8]
Reference: OSGi Service Platform, Service Compendium, Release 4, Version 4.1 from OSGi Alliance [6]
This scripting capability is very useful. For example, I've constructed a script that helps me restart with a fresh Felix installation in just a few steps as follows:
# update obr repos echo Adding releases obr repository. . . repos add file:///C:/projects/felixbook/releases/repository.xml # deploy tier 3 echo Deploying tier 3 (inventory) bundles. . . bundlelevel -i 2 frameworklevel 2 deploy -s "Bookshelf Inventory Impl - Mock" # deploy tier 2 echo Deploying tier 2 (business logic) bundles. . . bundlelevel -i 3 frameworklevel 3 deploy -s "Bookshelf Service" # deploy tier 1 echo Deploying tier 1 (presentation) bundles. . . bundlelevel -i 5 frameworklevel 5 deploy -s "Bookshelf Service Gogo commands" # populate books source populate-books.tsl
After starting with a clean Felix install (or deleting the felix-cache
directory), change to the scripts/
directory:
g! cd scripts
Name scripts
CanonicalPath C:felixscripts
Parent C:felix
Path C:felixscripts
AbsoluteFile C:felixscripts
AbsolutePath C:felixscripts
CanonicalFile C:felixscripts
ParentFile C:felix
And execute the install.tsl
script. It registers the releases repository with the OBR service and installs the bundles (with the right start levels). It then calls the populate-books.tsl
script to add the books to the inventory:
g! source install.tsl
Adding releases obr repository. . .
Deploying tier 3 (inventory) bundles. . .
Target resource(s):
-------------------
Bookshelf Inventory Impl - Mock (1.5.0)
Required resource(s):
---------------------
Bookshelf Inventory API (1.5.0)
Deploying...
Starting Book Inventory Mock Impl
done.
Deploying tier 2 (business logic) bundles. . .
Target resource(s):
-------------------
Bookshelf Service (1.8.0)
Deploying...done.
Deploying tier 1 (presentation) bundles. . .
Target resource(s):
-------------------
Bookshelf Service Gogo commands (1.8.1)
Deploying...done.
Now adding books to inventory. . .
Done.
Of course, this script is now as far as we have come in this chapter; it will require a step-by-step update as you go along.