Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

OSGi World Congress Workshop Exercise - P Kriens

464 visualizaciones

Publicado el

OSGi World Congress 2002

Publicado en: Tecnología
  • Sé el primero en comentar

  • Sé el primero en recomendar esto

OSGi World Congress Workshop Exercise - P Kriens

  1. 1. app2 app3 app1 app4 Browser app 1 app 2 app 3 app 4 SP 2 SP 3 SP 4 SP 1 multicast
  2. 2. ActivatorDistributor 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
  3. 3. Activator <<interface>> BundleActivator
  4. 4. Activator <<interface>> BundleActivator LogService Logs messages
  5. 5. 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 ); }
  6. 6. Manifest-Version: 1.0 Bundle-Activator: aQute.world.congress.Activator Import-Package: org.osgi.framework, org.osgi.service.log
  7. 7. HttpTracker Tracks http servers <<interface>> Http Service Service Tracker Registers page Portal Registers /portal page Activator <<interface>> BundleActivator LogService Logs messages
  8. 8. <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>
  9. 9. Activator activator; public HttpTracker( Activator activator, BundleContext context ) { super( context, HttpService.class.getName(),null); this.activator = activator; }
  10. 10. 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; }
  11. 11. public URL getResource(String name) { try { URL url = getClass().getResource( name.substring(1) ); return url; } catch( Exception e ) { e.printStackTrace(); } return null; }
  12. 12. public boolean handleSecurity( HttpServletRequest request, HttpServletResponse response) throws IOException { return true; }
  13. 13. http = new HttpTracker( this, context ); http.open(); HttpTracker http;
  14. 14. // Add to stop method http.close();
  15. 15. 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
  16. 16. 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
  17. 17. boolean active = true; DatagramSocket socket; byte outgoing[]; Activator activator;
  18. 18. Distributor( Activator activator, String outgoing ) { super("distributor"); this.activator = activator; this.outgoing = outgoing.getBytes(); }
  19. 19. 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); }
  20. 20. 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 ); }
  21. 21. 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); }
  22. 22. void close() { active = false; socket.close(); }
  23. 23. 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() ); }
  24. 24. void register( BundleContext context ) { registration = context.registerService( Link.class.getName(), this, properties ); } void modify( Link link ) { registration.setProperties(link.properties); }
  25. 25. void unregister() { registration.unregister(); } Object getUrl(){ return properties.get( "url" ); }
  26. 26. 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; } }
  27. 27. // 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();
  28. 28. // Add to stop method distributor.close();
  29. 29. aQute www.aQute.se +46 300 39800, Peter.Kriens@aQute.se

×