Presentation is loading. Please wait.

Presentation is loading. Please wait.

OSGi World Congress Workshop Exercise By Peter Kriens CEO aQute OSGi Technology Officer.

Similar presentations


Presentation on theme: "OSGi World Congress Workshop Exercise By Peter Kriens CEO aQute OSGi Technology Officer."— Presentation transcript:

1 OSGi World Congress Workshop Exercise By Peter Kriens CEO aQute OSGi Technology Officer

2 www.aQute.se - 2002 OSGi World Congress - 2 Functional Description Program See your buddies web page viewer See your buddies web page viewer At the end, each participant adds a web page to the portal of all other participants At the end, each participant adds a web page to the portal of all other participants Zero Administration … Zero Administration …

3 www.aQute.se - 2002 OSGi World Congress - 3 Architecture app2 app3app1 app4 Browser app 1 app 2 app 3 app 4 SP 2 SP 3 SP 4 SP 1 multicast

4 www.aQute.se - 2002 OSGi World Congress - 4 Architecture: Classes ActivatorDistributor Link HttpTracker Tracks http servers finds buddies > Http Service Service Tracker Thread > BundleActivator Tracks buddiesRegisters page Portal Tracks portal entries Registers /portal page LogService Logs messages

5 www.aQute.se - 2002 OSGi World Congress - 5 Start: Hello World Activator The Activator class will print Hello World when the bundle is started and Goodbye World when it stops. The Activator class will print Hello World when the bundle is started and Goodbye World when it stops. > BundleActivator

6 www.aQute.se - 2002 OSGi World Congress - 6 Getting Started Assumptions Assumptions –Eclipse 2.0.1 (check in Help:About, 2.0.0 does not work!) installed –unzipped fwx.zip fwx.exe fwx.exe load directory load directory osgi.jar osgi.jar servlet.jar servlet.jar –Started Eclipse

7 www.aQute.se - 2002 OSGi World Congress - 7 Eclipse

8 www.aQute.se - 2002 OSGi World Congress - 8 Getting Started Create new Java Project Create new Java Project –File:New:Project –Java Project, Next –Name is ws –Default location, Next –Libraries, Add external jars, servlet.jar, osgi.jar (from where you unzipped fwx.zip, e.g. c:\fwx\osgi.jar) servlet.jar, osgi.jar (from where you unzipped fwx.zip, e.g. c:\fwx\osgi.jar) –Attach source (select osgi.jar), Finish

9 www.aQute.se - 2002 OSGi World Congress - 9 Add External Jar

10 www.aQute.se - 2002 OSGi World Congress - 10 Setup Activator class Add Package Add Package –File:New:Package –Name, aQute.world.congress

11 www.aQute.se - 2002 OSGi World Congress - 11 Setup Activator class Add new class Add new class File:New:Class File:New:Class –Package, aQute.world.congress –Name, Activator –Interfaces Add BundleActivator Add BundleActivator Finish Finish Eclipse will open a generated source file Eclipse will open a generated source file

12 www.aQute.se - 2002 OSGi World Congress - 12 Activator class setup

13 www.aQute.se - 2002 OSGi World Congress - 13 Write Hello/Goodbye World Fill in the System.out.println(...) in the appropriate places Fill in the System.out.println(...) in the appropriate places Save the file (File:Save or control-S) Save the file (File:Save or control-S) –This automatically compiles, so correct any errors

14 www.aQute.se - 2002 OSGi World Congress - 14 Create The Manifest We only define what class to start: Activator We only define what class to start: Activator File:New:File, name it Manifest.mf File:New:File, name it Manifest.mf –Store in aQute.world.congress package/dir Manifest MUST end with 2 empty lines!! Manifest MUST end with 2 empty lines!! Fill in (and save): Fill in (and save): Manifest-Version: 1.0 Bundle-Activator: aQute.world.congress.Activator Import-Package: org.osgi.framework

15 www.aQute.se - 2002 OSGi World Congress - 15 Create Manifest

16 www.aQute.se - 2002 OSGi World Congress - 16 Create Bundle JAR file! Select ws in left pane Select ws in left pane Press right mouse button Press right mouse button Select Export Select Export Select JAR File Select JAR File Next Next

17 www.aQute.se - 2002 OSGi World Congress - 17 Create JAR File Export Destination Export Destination –Name it ws.jar –Save the JAR in the load directory where you unzipped fwx.zip, e.g. c:\fwx\load Next Next

18 www.aQute.se - 2002 OSGi World Congress - 18 Create JAR file Deselect with compile errors Deselect with compile errors Save the description in the ws workspace, under the name bundle.jardesc Save the description in the ws workspace, under the name bundle.jardesc Next Next

19 www.aQute.se - 2002 OSGi World Congress - 19 Create JAR file Set to use existing Manifest Set to use existing Manifest –Select Manifest.mf Finish Finish

20 www.aQute.se - 2002 OSGi World Congress - 20 Starting A Framework The fwx.exe is the OSGi Reference Framework with all the R2 services reference implementations The fwx.exe is the OSGi Reference Framework with all the R2 services reference implementations –This Framework is not optimized, nor industrialized –It expires December 1 –Contact a vendor for a real framework To start it, open a window on its directory and double click it To start it, open a window on its directory and double click it It will automatically start all bundles in the load directory It will automatically start all bundles in the load directory

21 www.aQute.se - 2002 OSGi World Congress - 21 Starting the Framework

22 www.aQute.se - 2002 OSGi World Congress - 22 Restarting You can restart the bundle by You can restart the bundle by –select the bundle.jardesc –Right mouse: Create Jar

23 www.aQute.se - 2002 OSGi World Congress - 23 Using The Log Service Activator We will now use a service: The Log Service We will now use a service: The Log Service > BundleActivator LogService Logs messages

24 www.aQute.se - 2002 OSGi World Congress - 24 Prepare for the Log Service Save the BundleContext parameter in a context instance variable Save the BundleContext parameter in a context instance variable n n BundleContext context; n n public void start(BundleContext context) { this.context = context;... }

25 www.aQute.se - 2002 OSGi World Congress - 25 The log() method Add a new method to the Activator class: Add a new method to the Activator class: void log( String msg, Throwable exception ) { ServiceReference r = context.getServiceReference( LogService.class.getName() ); LogService log =(LogService)context.getService(r); if ( log != null ) { if ( exception == null ) log.log( LogService.LOG_INFO, msg ); else log.log( LogService.LOG_ERROR, msg, exception ); } else System.out.println( msg + " : " + exception ); context.ungetService( r ); }

26 www.aQute.se - 2002 OSGi World Congress - 26 Use the log method Replace the calls to System.out with calls to the log method Replace the calls to System.out with calls to the log method n n public void start(BundleContext context) { this.context = context; log( "Hello world", null ); } public void stop(BundleContext context){ log( "Goodbye world", null ); }

27 www.aQute.se - 2002 OSGi World Congress - 27 Organize Imports Source:Organizer Imports Source:Organizer Imports This will automatically add the necessary import statements This will automatically add the necessary import statements

28 www.aQute.se - 2002 OSGi World Congress - 28 Modify The Manifest Edit Manifest.mf Edit Manifest.mf Add Import-Package for Add Import-Package for –org.osgi.service.log Manifest-Version: 1.0 Bundle-Activator: aQute.world.congress.Activator Import-Package: org.osgi.framework, org.osgi.service.log

29 www.aQute.se - 2002 OSGi World Congress - 29 Using the Log Service Save the source file (correct compile errors) Save the source file (correct compile errors) Select bundle.jardesc, right menu, Create JAR Select bundle.jardesc, right menu, Create JAR Check the console of fwx: Check the console of fwx: –Goodbye world –But no Hello world … This is now in the log –Where is the log????

30 www.aQute.se - 2002 OSGi World Congress - 30 Reading the Log Service Open a telnet session Open a telnet session –Start:Run:telnet localhost 2011 This opens a simple OSGi console/debugger This opens a simple OSGi console/debugger Type Type –log

31 www.aQute.se - 2002 OSGi World Congress - 31 Publishing a page HttpTracker Tracks http servers > Http Service Service Tracker Registers page Portal Registers /portal page Activator > BundleActivator LogService Logs messages

32 www.aQute.se - 2002 OSGi World Congress - 32 Write An Html Page File:New:File File:New:File –Directory, aQute/world/congress –Call it page.html Edit it (select page.html, Open With:Text Editor) Edit it (select page.html, Open With:Text Editor) My Demo HTML page My Demo This is aQute's (use your own name!) demo page

33 www.aQute.se - 2002 OSGi World Congress - 33 Create an HTML file

34 www.aQute.se - 2002 OSGi World Congress - 34 Create HttpTracker HttpTracker tracks Http Service objects and adds a page.html to every Http Service HttpTracker tracks Http Service objects and adds a page.html to every Http Service It extends ServiceTracker to simplify tracking It extends ServiceTracker to simplify tracking –Object addingService(ServiceReference) –void modifiedService(ServiceReference,Object) –void removedService(ServiceReference,Object) Create new class aQute.world.congress.HttpTracker that extends ServiceTracker, implements HttpContext Create new class aQute.world.congress.HttpTracker that extends ServiceTracker, implements HttpContext

35 www.aQute.se - 2002 OSGi World Congress - 35 Add HttpTracker class

36 www.aQute.se - 2002 OSGi World Congress - 36 Add HttpTracker class And it needs a variable, constructor, add: And it needs a variable, constructor, add: Activator activator; public HttpTracker( Activator activator, BundleContext context ) { super( context, HttpService.class.getName(),null); this.activator = activator; }

37 www.aQute.se - 2002 OSGi World Congress - 37 Add HttpTracker class addingService addingService public Object addingService( ServiceReference reference ) { HttpService http = (HttpService)super.addingService(reference); try { http.registerResources("/mydemo", "", this ); } catch( NamespaceException e ) { activator.log( "Cannot register because + name is already in use /mydemo", e ); } return http; }

38 www.aQute.se - 2002 OSGi World Congress - 38 Add HttpTracker class Modify the getResource method to return resources from the package Modify the getResource method to return resources from the package public URL getResource(String name) { try { URL url = getClass().getResource( name.substring(1) ); return url; } catch( Exception e ) { e.printStackTrace(); } return null; }

39 www.aQute.se - 2002 OSGi World Congress - 39 Add HttpTracker class handlSecurity returns false. This means deny access, change it to return true. handlSecurity returns false. This means deny access, change it to return true. public boolean handleSecurity( HttpServletRequest request, HttpServletResponse response) throws IOException { return true; }

40 www.aQute.se - 2002 OSGi World Congress - 40 Add HttpTracker class Organize Imports Organize Imports Save it Save it Add an instance variable for the Http Tracker to Activator Add an instance variable for the Http Tracker to Activator And add the following line to the start method And add the following line to the start method http = new HttpTracker( this, context ); http.open(); HttpTracker http ;

41 www.aQute.se - 2002 OSGi World Congress - 41 Clean-up // Add to stop method http.close();

42 www.aQute.se - 2002 OSGi World Congress - 42 Modify The Manifest Add Import-Package for Add Import-Package for –org.osgi.util.tracker –org.osgi.service.http –javax.servlet.http, javax.servlet Manifest-Version: 1.0 Bundle-Activator: aQute.world.congress.Activator Import-Package: org.osgi.framework, org.osgi.service.log,org.osgi.util.tracker, org.osgi.service.http,javax.servlet.http, javax.servlet

43 www.aQute.se - 2002 OSGi World Congress - 43 Add HttpTracker class Save open files Save open files Select bundle.jardesc Select bundle.jardesc –Create JAR Open a browser and surf into Open a browser and surf into –http://localhost/mydemo/page.html Ensure that your HTTP proxy is off! Ensure that your HTTP proxy is off! –IE : Tools : Internet Options : Connections : Lan settings

44 www.aQute.se - 2002 OSGi World Congress - 44 Finding Your Buddies HttpTracker Tracks http servers > Http Service Service Tracker Registers page Portal Registers /portal page Activator > BundleActivator LogService Logs messages Distributor Link finds buddies Thread Tracks buddies Tracks portal entries

45 www.aQute.se - 2002 OSGi World Congress - 45 Add Distributor class Add a new class Distribitor Add a new class Distribitor –Package aQute.world.congress –Extends Thread This class will open a UDP socket This class will open a UDP socket Will listen to all Datagrams on port 2014 Will listen to all Datagrams on port 2014 Forwards all datagrams to the activator Forwards all datagrams to the activator Sends an announce datagram every 5 seconds Sends an announce datagram every 5 seconds Simple mechanism to dynamically discover others Simple mechanism to dynamically discover others

46 www.aQute.se - 2002 OSGi World Congress - 46 Add Distributor class Add Instance variable declarations Add Instance variable declarations booleanactive = true; DatagramSocketsocket; byteoutgoing[]; Activatoractivator;

47 www.aQute.se - 2002 OSGi World Congress - 47 Add Distribitor class Add Constructor Add Constructor Distributor( Activator activator, String outgoing ) { super("distributor"); this.activator = activator; this.outgoing = outgoing.getBytes(); }

48 www.aQute.se - 2002 OSGi World Congress - 48 Add Distribitor class public void run() { while ( active ) try { socket = new DatagramSocket( 2014 ); socket.setSoTimeout( 5000 ); activator.log("Discovery starts.", null); … inner loop … see next slide socket.close(); socket = null; } catch(Exception e) { activator.log( "Main discover loop exit", e ); if ( active ) try { Thread.currentThread().sleep(1000); } catch( Exception x) {} } activator.log("Discovery quits.", null); }

49 www.aQute.se - 2002 OSGi World Congress - 49 Add Distribitor class while ( active ) try { DatagramPacket packet = new DatagramPacket(new byte[256], 256); socket.receive(packet); String incoming = new String( packet.getData(), 0, packet.getLength()); activator.process( new Link(incoming) ); } catch(InterruptedIOException e) { announce();} catch(Exception e) { if ( active ) activator.log( "Receving remote + service packets, ignoring ", e ); }

50 www.aQute.se - 2002 OSGi World Congress - 50 Add Distribitor class The announce method The announce method void announce() throws IOException { DatagramSocket announce = new DatagramSocket(); DatagramPacket packet = new DatagramPacket( outgoing, outgoing.length, InetAddress.getByName("255.255.255.255"), 2014 ); announce.send(packet); }

51 www.aQute.se - 2002 OSGi World Congress - 51 Add Distribitor class And the last close method And the last close method Organize imports Organize imports Save Save –Unresolved method in Activator: process void close() { active = false; socket.close(); }

52 www.aQute.se - 2002 OSGi World Congress - 52 Add Link class Add linke class Add linke class The link class just holds the buddy info together. The link class just holds the buddy info together. Hashtable properties = new Hashtable(); ServiceRegistration registration; Link( String msg ) { StringTokenizer tz = new StringTokenizer( msg, "|" ); properties.put( "title", tz.nextToken() ); properties.put( "description",tz.nextToken()); properties.put( "url", tz.nextToken() ); }

53 www.aQute.se - 2002 OSGi World Congress - 53 Add Link class Register method, for a new discovered buddy Register method, for a new discovered buddy Modify method, when old buddy, new message Modify method, when old buddy, new message void register( BundleContext context ) { registration = context.registerService( Link.class.getName(), this, properties ); } void modify( Link link ) { registration.setProperties(link.properties); }

54 www.aQute.se - 2002 OSGi World Congress - 54 Add Link class Unregister when link is gone Unregister when link is gone To get the unique key To get the unique key void unregister() { registration.unregister(); } Object getUrl(){ return properties.get( "url" ); }

55 www.aQute.se - 2002 OSGi World Congress - 55 Add process method in Activator The process method must add buddy to the portal The process method must add buddy to the portal The portal tracks url service property. The portal tracks url service property. Edit Activator, add process method Edit Activator, add process method Hashtable links = new Hashtable(); void process( Link link ) { Link existing = (Link) links.get( link.getUrl() ); if ( existing != null ) existing.modify( link ); else { link.register( context ); links.put( link.getUrl(), link ); existing = link; } }

56 www.aQute.se - 2002 OSGi World Congress - 56 Add and Start the Distributor // Add to start method String host= InetAddress.getLocalHost().getHostAddress(); if ( System.getProperty( "org.osgi.service.http.port" ) != null ) host += ":" + System.getProperty( "org.osgi.service.http.port" ); distributor = new Distributor( this, "My Title|My Own Description|http://" + host + "/mydemo/page.html" ); distributor.start();

57 www.aQute.se - 2002 OSGi World Congress - 57 Clean-up (stop method) // Add to stop method distributor.close();

58 www.aQute.se - 2002 OSGi World Congress - 58 Find the buddies Organize imports for all files Organize imports for all files Build JAR Build JAR Browse to: Browse to: –http://localhost/portal You should see your own page, and with any luck pages from your buddies You should see your own page, and with any luck pages from your buddies

59 www.aQute.se - 2002 OSGi World Congress - 59 Advanced Exercise Links are not cleaned up when the buddy stops sending Links are not cleaned up when the buddy stops sending Cleanup the links when no message has been received for 30 seconds Cleanup the links when no message has been received for 30 seconds

60 www.aQute.se - 2002 OSGi World Congress - 60 Conclusion Surprisingly little code is necessary to perform interesting functions Surprisingly little code is necessary to perform interesting functions Coupling to other parts is minimal because OSGi specified interfaces are intermediates Coupling to other parts is minimal because OSGi specified interfaces are intermediates Tools are available to simplify creation of manifest and bundle files Tools are available to simplify creation of manifest and bundle files This is only the beginning... This is only the beginning...

61 www.aQute.se - 2002 OSGi World Congress - 61 Vendors With Tools & Courses Acunia - www.acunia.com Acunia - www.acunia.com aQute - www.aQute.se aQute - www.aQute.se IBM - www.ibm.com/embedded IBM - www.ibm.com/embedded Gatespace - www.gatespace.com Gatespace - www.gatespace.com ProSyst - www.prosyst.com ProSyst - www.prosyst.com See for more www.osgi.org See for more www.osgi.org

62 www.aQute.se - 2002 OSGi World Congress - 62 aQute www.aQute.se +46 300 39800, Peter.Kriens@aQute.se


Download ppt "OSGi World Congress Workshop Exercise By Peter Kriens CEO aQute OSGi Technology Officer."

Similar presentations


Ads by Google