osgi world congress workshop exercise - p kriens

62

Upload: mfrancis

Post on 11-Aug-2015

111 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: OSGi World Congress Workshop Exercise - P Kriens
Page 2: OSGi World Congress Workshop Exercise - P Kriens
Page 3: OSGi World Congress Workshop Exercise - P Kriens

app2

app3 app1

app4

Browser

app 1 app 2 app 3 app 4

SP 2

SP 3

SP 4

SP 1

multicast

Page 4: OSGi World Congress Workshop Exercise - P Kriens

Activator Distributor

Link

HttpTracker Tracks http servers

finds buddies

<<interface>> Http Service

Service Tracker Thread <<interface>>

BundleActivator

Tracks buddies Registers page

Portal Tracks portal entries

Registers /portal page

LogService

Logs messages

Page 5: OSGi World Congress Workshop Exercise - P Kriens

Activator

<<interface>> BundleActivator

Page 6: OSGi World Congress Workshop Exercise - P Kriens
Page 7: OSGi World Congress Workshop Exercise - P Kriens
Page 8: OSGi World Congress Workshop Exercise - P Kriens
Page 9: OSGi World Congress Workshop Exercise - P Kriens
Page 10: OSGi World Congress Workshop Exercise - P Kriens
Page 11: OSGi World Congress Workshop Exercise - P Kriens
Page 12: OSGi World Congress Workshop Exercise - P Kriens
Page 13: OSGi World Congress Workshop Exercise - P Kriens
Page 14: OSGi World Congress Workshop Exercise - P Kriens
Page 15: OSGi World Congress Workshop Exercise - P Kriens
Page 16: OSGi World Congress Workshop Exercise - P Kriens
Page 17: OSGi World Congress Workshop Exercise - P Kriens
Page 18: OSGi World Congress Workshop Exercise - P Kriens
Page 19: OSGi World Congress Workshop Exercise - P Kriens
Page 20: OSGi World Congress Workshop Exercise - P Kriens
Page 21: OSGi World Congress Workshop Exercise - P Kriens
Page 22: OSGi World Congress Workshop Exercise - P Kriens
Page 23: OSGi World Congress Workshop Exercise - P Kriens

Activator

<<interface>> BundleActivator LogService

Logs messages

Page 24: OSGi World Congress Workshop Exercise - P Kriens
Page 25: OSGi World Congress Workshop Exercise - P Kriens

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 ); }

Page 26: OSGi World Congress Workshop Exercise - P Kriens
Page 27: OSGi World Congress Workshop Exercise - P Kriens
Page 28: OSGi World Congress Workshop Exercise - P Kriens

Manifest-Version: 1.0 Bundle-Activator: aQute.world.congress.Activator Import-Package: org.osgi.framework, org.osgi.service.log

Page 29: OSGi World Congress Workshop Exercise - P Kriens
Page 30: OSGi World Congress Workshop Exercise - P Kriens
Page 31: OSGi World Congress Workshop Exercise - P Kriens

HttpTracker Tracks http servers

<<interface>> Http Service

Service Tracker

Registers page

Portal

Registers /portal page

Activator

<<interface>> BundleActivator LogService

Logs messages

Page 32: OSGi World Congress Workshop Exercise - P Kriens

<html> <head> <title>My Demo HTML page</title> </head> <body> <h1>My Demo</h1> This is aQute's (use your own name!) demo page </body> </html>

Page 33: OSGi World Congress Workshop Exercise - P Kriens
Page 34: OSGi World Congress Workshop Exercise - P Kriens
Page 35: OSGi World Congress Workshop Exercise - P Kriens
Page 36: OSGi World Congress Workshop Exercise - P Kriens

Activator activator;

public HttpTracker( Activator activator, BundleContext context ) { super( context, HttpService.class.getName(),null); this.activator = activator; }

Page 37: OSGi World Congress Workshop Exercise - P Kriens

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; }

Page 38: OSGi World Congress Workshop Exercise - P Kriens

public URL getResource(String name) { try { URL url = getClass().getResource( name.substring(1) ); return url; } catch( Exception e ) { e.printStackTrace(); } return null; }

Page 39: OSGi World Congress Workshop Exercise - P Kriens

public boolean handleSecurity( HttpServletRequest request, HttpServletResponse response) throws IOException { return true; }

Page 40: OSGi World Congress Workshop Exercise - P Kriens

http = new HttpTracker( this, context ); http.open();

HttpTracker http;

Page 41: OSGi World Congress Workshop Exercise - P Kriens

// Add to stop method http.close();

Page 42: OSGi World Congress Workshop Exercise - P Kriens

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

Page 43: OSGi World Congress Workshop Exercise - P Kriens
Page 44: OSGi World Congress Workshop Exercise - P Kriens

HttpTracker Tracks http servers

<<interface>> Http Service

Service Tracker

Registers page

Portal

Registers /portal page

Activator

<<interface>> BundleActivator LogService

Logs messages

Distributor

Link

finds buddies

Thread

Tracks buddies

Tracks portal entries

Page 45: OSGi World Congress Workshop Exercise - P Kriens
Page 46: OSGi World Congress Workshop Exercise - P Kriens

boolean active = true; DatagramSocket socket; byte outgoing[]; Activator activator;

Page 47: OSGi World Congress Workshop Exercise - P Kriens

Distributor( Activator activator, String outgoing ) { super("distributor"); this.activator = activator; this.outgoing = outgoing.getBytes(); }

Page 48: OSGi World Congress Workshop Exercise - P Kriens

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); }

Page 49: OSGi World Congress Workshop Exercise - P Kriens

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 ); }

Page 50: OSGi World Congress Workshop Exercise - P Kriens

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); }

Page 51: OSGi World Congress Workshop Exercise - P Kriens

void close() { active = false; socket.close(); }

Page 52: OSGi World Congress Workshop Exercise - P Kriens

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() ); }

Page 53: OSGi World Congress Workshop Exercise - P Kriens

void register( BundleContext context ) { registration = context.registerService( Link.class.getName(), this, properties ); }

void modify( Link link ) { registration.setProperties(link.properties); }

Page 54: OSGi World Congress Workshop Exercise - P Kriens

void unregister() { registration.unregister(); }

Object getUrl(){ return properties.get( "url" ); }

Page 55: OSGi World Congress Workshop Exercise - P Kriens

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; } }

Page 56: OSGi World Congress Workshop Exercise - P Kriens

// 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();

Page 57: OSGi World Congress Workshop Exercise - P Kriens

// Add to stop method distributor.close();

Page 58: OSGi World Congress Workshop Exercise - P Kriens
Page 59: OSGi World Congress Workshop Exercise - P Kriens
Page 60: OSGi World Congress Workshop Exercise - P Kriens
Page 61: OSGi World Congress Workshop Exercise - P Kriens
Page 62: OSGi World Congress Workshop Exercise - P Kriens

aQute

www.aQute.se

+46 300 39800, [email protected]