OSGi framework overview

Preview:

Citation preview

Balduran chang2008/9/30Balduran.cs96g@g2.nctu.edu.twEC719(03)5712121#54819

Bundle management portion GUI Bundle list un/install bundle Start/stop bundle Update/configre bundle

EV bundle portion one trigger function to broadcast “Emergency event!” once receive EV message, response it and show

incoming message. Port number and message should be able to modify

PPT slice

Architectures

System design

Used module

Source code

Java code

Jar file

Knopflerfish

http://www.knopflerfish.org/download.html

Apache Felix

http://felix.apache.org/site/downloads.cgi

Equinox

http://download.eclipse.org/equinox/

Prosyst mBedded Server Equinox Edition

http://dz.prosyst.com/oss/

Install Use startinstall.bat

Start framework C:\mbs_equinox\bin\v

ms\jdk\server.bat

install uninstall start stop refresh update status ss services

bundles bundle headers log gc sl exit init shutdown

install file:../../../demo/bundles/httpdemo.jar start <bundle id> Check the bundle status is active

Visit http://<mbs_host>/images/imagination.jpg

Stop <bundle id> Check the bundle status is resolved

Uninstall <bundle id>

JDK 1.6.07 mBS Equinox edition 3.4 Eclipse

Configure build path…

Add external JARs… bundles/org.eclipse.osgi.jar

bundles/org.eclipse.osgi.services.jar

bundles/org.eclipse.osgi.util.jar

1. Bundle implementation2. Bundle activator3. Compile and generate class file4. Manifest.mf file5. Generate bundle JAR file6. Install and start bundle

HelloWorldThread.javapublic class HelloWorldThread extends Thread {

private boolean running = true;

public HelloWorldThread() {}

public void run() {

while (running) {

System.out.println("Hello World!");

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

System.out.println("ERROR: " + e);

}

}

}

public void stopThread() {

this.running = false;

}

}

Activator.javaimport org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

public static BundleContext bc = null;

private HelloWorldThread thread = null;

public void start(BundleContext bc) throws Exception {

System.out.println("Bundle starting...");

Activator.bc = bc;

this.thread = new HelloWorldThread();

this.thread.start();

}

public void stop(BundleContext bc) throws Exception {

System.out.println("Bundle stopping...");

this.thread.stopThread();

this.thread.join();

Activator.bc = null;

}

}

Manifest.mfManifest-Version: 1.0

Bundle-Name: first bundle

Bundle-SymbolicName: firstbundle

Bundle-Version: 1.0.0

Bundle-Description: first Demo Bundle

Bundle-Vendor: vendorABC

Bundle-Activator: demo.Activator

Bundle-Category: demo

Import-Package: org.osgi.framework

Compile CMD

javac -classpath .;../bundles/org.eclipse.osgi.jar; *.java

Jar CMD

jar cmf ./manifest firstbundle.jar *.class

Build.xml<?xml version="1.0"?>

<project name="simplebundle" default="all">

<target name="all" depends="init,compile,jar" />

<target name="init">

<mkdir dir="./classes" />

<mkdir dir="./bin" />

</target>

<target name="compile">

<javac destdir="./classes" debug="on" srcdir="./src">

</javac>

</target>

<target name="jar">

<jar basedir="./classes" jarfile="./bin/simplebundle.jar" compress="true"

includes="**/*" manifest="./meta-inf/MANIFEST.MF" />

</target>

<target name="clean">

<delete dir="./classes" />

<delete dir="./bin" />

</target>

</project>

GetService.javaimport org.osgi.framework.ServiceReference;

import org.osgi.framework.BundleException;

public class GetService implements BundleActivator {

ServiceReference timeRef = null;

TimeService timeService = null;

public void start(BundleContext bc) throws BundleException {

timeRef = bc.getServiceReference(TimeService.class.getName());

if (timeRef != null) {

timeService = (TimeService) bc.getService(timeRef);

}

if (timeService == null) {

return;

}

}

public void stop(BundleContext bc) throws BundleException {

if (timeRef != null) {

bc.ungetService(timeRef);

timeRef = null;

timeService = null;

}

}

}

public class PMPDemo implements BundleActivator {

static String SYSTEM = "com.prosyst.util.system.SystemService";

public void start(BundleContext bx) throws BundleException {

try {

ServiceReference sr = bx.getServiceReference(PMPService.class.getName());

if(sr == null) throw new BundleException("Can't get\\PMPService");

PMPService pmp = (PMPService)bx.getService(sr);if(pmp == null) throw new BundleException("Can't get\\

PMPService");

Hashtable props = new Hashtable (20);props.put("username", "admin");props.put("password", "admin");PMPConnection con = pmp.connect("socket://127.0.0.1:1449",\\

props);

RemoteObject rObject = con.getReference(SYSTEM, "");

String [] str = {Byte.TYPE.getName()};

RemoteMethod rMethod = rObject.getMethod("getProperties", str);

rMethod.changeReturnType(ExternalizableDictionary.class);Dictionary obj = (ExternalizableDictionary)rMethod.invoke(new\\

Object[] {new Byte((byte) 3)}, true);Enumeration enum = obj.keys();while(enum.hasMoreElements()) {Object tmp = enum.nextElement();System.out.println(tmp + " " + obj.get(tmp));

}} catch (Exception e) {System.out.println(e);

}}

public void stop(BundleContext bx) {}

}

Bundle Management portion

Use PMP (prosyst message protocol) to get a connection to remote framework.

1. request PMPService

2. obtain PMPConnection

3. call remote service method

4. To make a service PMP-accessible, you should additionally implement the com.prosyst.util.io.Remoteinterface in the service class.

public class example implements Remote {

public Class[] remoteInterfaces() {

}

}

Ref: https://dz.prosyst.com/pdoc/mbs_equinox_3.4/um/framework/bundles/prosyst/rpc/pmp/pmpbundle.html

See also:

com.prosyst.util.parser

▪ Parser Service

▪ https://dz.prosyst.com/pdoc/mbs_prof_6.2/um/framework/bundles/system/putil/putil_parser.html

mConsole APIs

▪ https://dz.prosyst.com/pdoc/mbs_prof_6.2/um/framework/admins/mConsole/mConsole_programmer.html

EV bundle portion

Java.net.DatagramSocket

▪ SO_BROADCAST

▪ setBroadcast(boolean on)

▪ getBroadcast()

Java.net.DatagramPacket

▪ Use DatagramPacket(int port)

https://dz.prosyst.com/pdoc/mbs_equinox_3.4/um/index.html

http://www.knopflerfish.org/tutorials.html

Recommended