aQute Bundle Programming By Peter Kriens CEO aQute OSGi Director of Technology & OSGi Fellow.

Slides:



Advertisements
Similar presentations
Programming with Android: SDK install and initial setup Luca Bedogni Marco Di Felice Dipartimento di Scienze dellInformazione Università di Bologna.
Advertisements

1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Copyright © 2003 Pearson Education, Inc. Slide 3-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
1 Developing Applications in Protégé: The Protégé Plugin Architecture Timothy Redmond Ontolog Forum 29-Jun-2006.
©2003 aQute, All Rights Reserved Tokyo, August 2003 : 1 OSGi Service Platform Tokyo August 28, 2003 Peter Kriens CEO aQute, OSGi Fellow
AQute R4 By Peter Kriens CEO aQute OSGi Technology Officer and OSGi Fellow.
AQute Bundle Programming By Peter Kriens CEO aQute OSGi Technology Officer and OSGi Fellow.
AQute Eclipse Environment By Peter Kriens CEO aQute OSGi Director of Technology and OSGi Fellow.
18 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Introduction to Web Services.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Introducing the Java and Oracle Platforms Objectives 1-2 What Is Java? 1-3 Key Benefits of Java.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
XP New Perspectives on Microsoft Office Word 2003 Tutorial 7 1 Microsoft Office Word 2003 Tutorial 7 – Collaborating With Others and Creating Web Pages.
View-Based Application Development Lecture 1 1. Flows of Lecture 1 Before Lab Introduction to the Game to be developed in this workshop Comparison between.
1 NatQuery 3/05 An End-User Perspective On Using NatQuery To Extract Data From ADABAS Presented by Treehouse Software, Inc.
INTERNET PROTOCOLS Class 9 CSCI 6433 David C. Roberts Entire contents copyright 2011, David C. Roberts, all rights reserved.
Services Course Office Web Apps Participant Guide.
Troubleshooting Startup Problems
1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.
Chapter 24 Lists, Stacks, and Queues
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Creating Tables in a Web Site
August 2012 This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
1 Displaying Open Purchase Orders (F/Y 11). 2  At the end of this course, you should be able to: –Run a Location specific report of all Open Purchase.
4 Oracle Data Integrator First Project – Simple Transformations: One source, one target 3-1.
31242/32549 Advanced Internet Programming Advanced Java Programming
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Services Course Windows Live SkyDrive Participant Guide.
© Tomáš Kozel, Pavel Čech Java Basics Class structure, declaration, primitive data types, statements.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Wiki Tutorial. 2 Outline of Wiki Tutorial 1) Welcome and Introductions 2) What is a wiki, and why is it useful for our work in moving forward the program.
Copyright 2001 Advanced Strategies, Inc. 1 Data Bridging An Overview Prepared for DIGIT By Advanced Strategies, Inc.
Copyright © 2003 by Prentice Hall Computers: Tools for an Information Age Chapter 15 Programming and Languages: Telling the Computer What to Do.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
Chapter 11 Component-Level Design
A1.1 Assignment 1 “Deploying a Simple Web Service” ITCS 4010/5010 Grid Computing, UNC-Charlotte B. Wilkinson, 2005.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. a lesson approach Microsoft® PowerPoint 2010 © 2011 The McGraw-Hill Companies,
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
Chapter 8 Improving the User Interface
Student Interface for Online Testing Training Module Copyright © 2014 American Institutes for Research. All rights reserved.
Student Interface for Online Testing Training Module Copyright © 2014 American Institutes for Research. All rights reserved.
Data Structures Using C++ 2E
Benchmark Series Microsoft Excel 2013 Level 2
Yoshi
OSGi.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Introduction to OSGi +ActorFrame Surya Bahadur Kathayat
Presentation transcript:

aQute Bundle Programming By Peter Kriens CEO aQute OSGi Director of Technology & OSGi Fellow

© aQute, All Rights Reserved slide #2 Contents Overview The Bundle Class loading Using Services Threads and synchronization Cleaning up Whiteboard pattern Life Cycle Operations XML Parsing Tools

© aQute, All Rights Reserved slide #3 Bundle Programming Overview So you have to start programming for OSGi? Assuming you have some experience with J2SE programming, what are the key differences? –Dynamic environment, everything changes all the time –You share code and resources with other applications –Many bundles run on resource constrained devices Programming in a dynamic environment is difficult The course first goes through the gory details and then shows some convenience models that are coming in R4

© aQute, All Rights Reserved slide #4 The bundle A bundle is a JAR file –Based on the ZIP format, contains Classes Resources (images,files) Manifest A bundle contains all that is necessary to run it, except some well defined dependencies

© aQute, All Rights Reserved slide #5 Bundle model Bundles are like the (old) Mac model, not like the PC model A single file contains all that is needed No requirement for application installers that modify data all over the place Subsystems access the bundle to provide the relevant information they need No uninstallers that leave remains Web Server Desk Top Data Base Mac Application PC Application

© aQute, All Rights Reserved slide #6 Bundle Model For example, a management system wants to use an icon for a bundle They agree on a common name for a gif resource in the JAR file (the icon) If the management system receives the JAR it can find the icon and display System is easy to extend independent from others, only the JAR namespace must be managed Management System Bundle META-INF MANIFEST.MF icon.gif com/acme/appl/… … Get META-INF/icon.gif Display icon

© aQute, All Rights Reserved slide #7 Bundle Model Most important resources are the classes Classes in Java are packed in packages Each package is a directory in the JAR file –Dots become / however Naming, by convention, is done with the reverse domain name –com.acme.application Package naming is important for versioning an dependencies Never program in the default package (i.e. no package name specified) com.acme.application javax.servlet javax.servlet.http META-INF MANIFEST.MF com/acme/application MyApplication.class smiley.gif Handler.class javax/servlet Servlet.class ServletRequest.class javax/servlet/http …

© aQute, All Rights Reserved slide #8 Manifest The Manifest contains information about the bundle and its resources The Manifest is standardized by SUN – ar/basics/manifest.htmlhttp://java.sun.com/docs/books/tutorial/j ar/basics/manifest.html – ar/jar.htmlhttp://java.sun.com/j2se/1.3/docs/guide/j ar/jar.html The basic format is –Header –: –Value (max 76 characters but is extended on the next line if the next line starts with a space) A valid manifest starts with –Manifest-Version: 1.0 Headers are not case sensitive Java 2 has a Manifest parser in the java.util.jar package

© aQute, All Rights Reserved slide #9 Interesting OSGi headers Bundle-Activator –The fully qualified name of a class that is used to start the bundle Bundle-Name –Human Readable name, used to in UIs Bundle-SymbolicName –Name of the application that this bundle implements. The symbolic name must remain the same, but the version may change Bundle-Version –The actual version of the bundle. [. [. [. ]]] Bundle-Vendor Bundle-Copyright Bundle-Category Bundle-Description

© aQute, All Rights Reserved slide #10 Starting the bundle There are two types of bundles –Applications –Libraries Applications require the activation of the bundle, libraries only export packages The manifest header Bundle-Activator points to a fully qualified class name –com.acme.application.Activator This class must implement the org.osgi.framework.BundleActivator interface org.osgi.framework.BundleActivator When using Eclipse, the browse button in the manifest header will analyze the classes to find out which classes implement this interface

© aQute, All Rights Reserved slide #11 Starting the bundle The start method from the BundleActivator interface must be implemented carefully The start method borrows a Framework thread that is shared between all bundles. The start method should therefore return quickly Programmers do heavy initialization in the start method and the start method is synchronous and sequential A start method does not get its own thread!

© aQute, All Rights Reserved slide #12 Initialization Number one problem in OSGi deployments is startup time … Initialization should be limited to setting up the minimum and doing the rest of the initialization when the functionality is really needed –Lazy initialization is best, maybe your function is never needed! –Heavy initialization should be done in a thread Dangerous functions are –Opening connections (name lookup can take serious time) –Allocating resources that are not yet needed –Waiting for events that can take time to happen

© aQute, All Rights Reserved slide #13 The BundleContext The start method hands over a org.osgi.framework.BundleConte xt object org.osgi.framework.BundleConte xt This object represents the Framework but each bundle will gets own private context BundleContext objects should not be shared –Cost allocation –Capability The Framework is the center of the OSGi service platform universe Bundle A Frame work Bundle B Bundle C Bundle Context

© aQute, All Rights Reserved slide #14 Bundle Context method categories Private File Access Service Registry Event Handling Life Cycle Management Status information More about this later …

© aQute, All Rights Reserved slide #15 Sharing packages The Hello World example was trivial because it was fully self contained –It did not use any external resources –Nor did it provide any Real world applications share code and interact with each other Bundles can come and go at will, this introduces difficult sharing problems –What will happen when shared package goes away? –What happens when a new version becomes available? The Framework handles these issues

© aQute, All Rights Reserved slide #16 Sharing packages All classes in the bundle are by default private Sharing of classes is done with packages Manifest headers are used to control the sharing Import-Package contains a list of packages that are needed before this bundle can run –Import-Package: com.acme.application, com.acme.event, javax.servlet Bundle B Framework Import-Package

© aQute, All Rights Reserved slide #17 Sharing packages Export-Package contains a list of packages that may be shared with other bundles –Export-Package: com.acme.xyz, javax. All packages that are referred to from a bundle must be imported except ones that start with java. This said, there is an issue that currently packages on the classpath must not be imported or they cannot be found. This may change in the future Bundle A Bundle B Framework Import-Package Export-Package

© aQute, All Rights Reserved slide #18 Class cast exception The Framework guarantees that each package has only a single provider by picking only one provider, even if there are multiple exporters Why is it important that all bundles use the same class object? If the same class occurs multiple times in the system, bundles will experience hard to trouble shoot ClassCastExceptions because the names of the classes are equal So the Framework prevents this case MyObject x = (MyObject) z; Bundle A Bundle B B Provides the MyObjectClass But z is from the class MyObject from A

© aQute, All Rights Reserved slide #19 Package sharing AB C Private classes Shared classes Framework guarantees single Exporter for each package

© aQute, All Rights Reserved slide #20 Package versions When a package is imported or exported, it may have a version –Import-Package: com.acme.application;specification-version=3.1 –Export-Package: com.acme.application;specification-version=3.5 You may both import and export a package with different versions –Try to explain why export should have a higher version number? Packages must be backward compatible with lower version numbers When the specification-version attribute is omitted, the Framework assumes a version that is less than 0.0.0

© aQute, All Rights Reserved slide #21 Standard libraries and versions Many standard libraries are not well versioned, if at all Even if they are, they do not have the OSGi headers This means they should normally be imported without version specifiers

© aQute, All Rights Reserved slide #22 R4 And Versions R4 will have a more rich version model as well as standardizing the version syntax –Supported by a Version object Import can be specified with ranges, no longer requiring the backward compatibility rules R4 will also allow sharing of bundles –Enables private package sharing between bundles –Means same package name can appear in the system in different versions

© aQute, All Rights Reserved slide #23 Resolving Before a bundle is started, it must be resolved Resolving tries to match imports and exports If a bundle is resolved, it may be started Resolving may happen at any time between the installation and start Starting the bundle must attempt to resolve the bundle

© aQute, All Rights Reserved slide #24 Resolving Bundle A Export org.osgi.service.log com.ibm.service.log com.ibm.j9 Import org.osgi.service.http javax.servlet.http Framework org.osgi.framework org.osgi.service.http Bundle B Export ericsson.osgi javax.servlet javax.servlet.http org.osgi.service.log Import org.osgi.service.http B resolved A resolved

© aQute, All Rights Reserved slide #25 Dynamic Importing Static import (Import-Package) allows tool to reason about the bundle before it is installed Sometimes the packages that need to be imported are not known a- priori This only possible with the Class.forName mechanism Many popular packages use Class.forName to bind to unknown providers –Ant tasks –Jave Media Framework plugins –Applets –Security Providers Normal compiled implies that the dependency is always known a priori Tools can take advantage of this a priori knowledge and create the appropriate import statements

© aQute, All Rights Reserved slide #26 Assignment: Playing with Import and Export Use the Hello World project to see the effects of importing and exporting Add an import clause to the manifest of hello world –Import-Package: org.osgi.framework Create the JAR and check the console The bundle should start OK

© aQute, All Rights Reserved slide #27 Cheating Note that we actually cheated in the Eclipse tutorial, we never imported org.osgi.framework, but we should have It works because the org.osgi.framework package is also on the system class path In officially is required to import org.osgi.framework though … in R4 code that does not import correctly will break

© aQute, All Rights Reserved slide #28 Assignment: Playing with Import and Export Modify the manifest to add a version of 99 to the import statement –Import-Package: org.osgi.framework;specification -version=99 Build and check again Now move back to version 1 Check again

© aQute, All Rights Reserved slide #29 Assignment: Playing with Import and Export

© aQute, All Rights Reserved slide #30 What happens when we use a class we do not import? Make a copy of HelloWorld, call it UsingImportedClass Extend HttpServlet (Use Control-Shift-O to find the imports) Change the manifest so it points to our new UsingImportedClass Create the bundle See the error message on the bottom of the console The framework cannot find the java.servlet.http package

© aQute, All Rights Reserved slide #31 How do we solve this? Create an import statement in the manifest for –javax.servlet –javax.servlet.http Both are needed because javax.servlet.http.HttpServlet extends javax.servlet.Servlet … Manifest-Version: 1.0 Bundle-Activator: aQute.workshop.hello.UsingImportedClass Import-Package: org.osgi.service.log;specification- version=1.2, javax.servlet;specificaton-version=2.1, javax.servlet.http; specificaton-version=2.1

© aQute, All Rights Reserved slide #32 How can we get the ClassCastException? This is something you (normally) want to avoid at all times! However, it is educational to see how it can be done. The ClassCastException can happen when: –You have a class in your JAR, say X –You get an object from elsewhere that is of class X –You assign this object to a variable of type X

© aQute, All Rights Reserved slide #33 How to get a class cast exception We can show this using services (which will be thoroughly discussed later) We will get the log service from the Framework We will also include the org.osgi.service.log package into our JAR By using 2 different manifests we select where the log comes from HelloWorld.jar osgi.jar...service.log aQute.hello...service.log …

© aQute, All Rights Reserved slide #34 Class Cast Exception package aQute.workshop.hello; import org.osgi.framework.*; import org.osgi.service.log.*; public class ClassCastException implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("ClassCastException Example"); ServiceReference ref = context.getServiceReference(LogService.class.getName()); Object object = context.getService(ref); try { LogService log = (LogService) object; System.out.println("We could succesfully cast the log service... "); } catch( Exception cce ) { System.out.println("We could not cast the log service... "); } System.out.println( "Classloader we see: " + LogService.class + " " + LogService.class.getClassLoader() ); Class [] interfaces = object.getClass().getInterfaces(); for ( int i = 0; i<interfaces.length; i++ ) { System.out.println( "Their classloader " + interfaces[i] + " " + interfaces[i].getClassLoader() ); } public void stop(BundleContext arg0) throws Exception { System.out.println("Goodbye ClassCastException");} }

© aQute, All Rights Reserved slide #35 HelloWorld Log Service Class Cast Exception Manifest 1 (different classloaders) Manifest-Version: 1.0 Bundle-Activator: aQute.workshop.hello.ClassCastException Import-Package: org.osgi.framework; specification- version=1 Manifest 2 (importing, assuring same classloader) Manifest-Version: 1.0 Bundle-Activator: aQute.workshop.hello.ClassCastException Import-Package: org.osgi.framework; specification- version=1, org.osgi.service.log;specification-version=1.2 org.osgi.service.log aQute.workshop.hello org.osgi.service.log org.osgi.impl.service.log … object variable Ouch… OK

© aQute, All Rights Reserved slide #36 What Did This Assignment Teach? Bundles have dependencies on other classes that must be fulfilled before the bundle can be resolved Resolved bundles can be started It is necessary to specify all imports except java.* Class cast exceptions are hard to debug!

© aQute, All Rights Reserved slide #37 Bundle Class path There is a special manifest header that is called Bundle- Classpath This contains a comma separated list of embedded JAR files –. is the JAR itself –Bundle-Classpath:.,servlet.jar The Framework will treat the JAR file according to this Bundle classpath –Including order Powerful way to include existing JAR files without having to break them open main.jar b.jar c.jar a.jar Bundle-Classpath:.,a.jar, b.jar,c.jar

© aQute, All Rights Reserved slide #38 Collaborative model The OSGi specifications are more than an Applet, MIDlet, Xlet runner These models access APIs through shared packages –Factories –Listeners This model is supported in OSGi, but there are some disadvantages to this model –Packages need to be resolved at bundle start time, hard, if not impossible, to add missing packages later –No notifications for packages that come and go –Difficult to model multiplicity –Java can eagerly bind linked packages –Classloading issues

© aQute, All Rights Reserved slide #39 Collaborative Model What is needed is a controlled facility to communicate with other bundles The OSGi Alliance therefore introduced the service registry The service registry allows bundles to register objects through which they can communicate Other bundles can find these objects, or get notifications when they are registered or unregistered Framework fully manages this collaboration –Dependencies –security

© aQute, All Rights Reserved slide #40 Bundle Collaborative model JAVA Operating System Hardware OSGi Framework Service registry packages

© aQute, All Rights Reserved slide #41 Collaborative model How does OSGi relate to MIDP, Java TV, Browsers JAVA Operating System Hardware Java Application Manager Service registry packages Midlet, Xlet, or Applet Midlet, Xlet, or Applet No native code No package management (versions!) No collaboration No management bundles

© aQute, All Rights Reserved slide #42 Service Specifics A service (in the OSGi sense) is an object registered with the Framework for communicationwith other bundles The semantics and syntax of a service are specified in a Java interface Services can be seen as ports on the bundle service

© aQute, All Rights Reserved slide #43 A Service Defined Object that implements a Java Interface register get events: REGISTERED, UNREGISTERING, MODIFIED

© aQute, All Rights Reserved slide #44 Real World Example

© aQute, All Rights Reserved slide #45 Services and Interfaces Bundle Service Registry listen Display public void show(String msg) { System.out.println(msg); } public void show(String msg) { } public void show(String msg) { file.write(msg.getBytes()); } B C register Display public interface Display { void show(String msg); } A

© aQute, All Rights Reserved slide #46 Services continued Different bundles (from different vendors) can implement the same interface –Implementation is not visible to clients –Allows operator to replace implementations without disrupting service Services are (optionally) registered with properties –Describe information about the service –The client can search (or listen) for services that have certain properties and values for properties Standard properties –objectclassThe interfaces of the service –service.idSystem generated unique id –service.pidPersistent id –service.rankingOrdering value

© aQute, All Rights Reserved slide #47 Services The service model enables a powerful component model Components can be plugged in, and removed, as needed The system adapts to these changing configurations

© aQute, All Rights Reserved slide #48 Services API Service objects should not be freely shared because of security and performance issues Therefore, services are normally manipulated with a ServiceReference object A ServiceReference can –Provide the properties of the service –Be freely passed as a parameter to other bundles –Can be used to retrieve the actual service via the BundleContext, which might be personalized Client Code Service Reference Real Service Object Bundle Context getService(ServiceReference) getServiceReference(…)

© aQute, All Rights Reserved slide #49 How to get Services First is to obtain a Service Reference Simplest way is the one that picks the highest ranking service –ServiceReference getServiceReference(String ifName) The parameter is the name of the interface that is required –A good practice is to use MyClass.class.getName() instead of com.acme.mypackage.MyClass because it will be automatically changed when you refactor The associated service is guaranteed to implement this interface (the package of the interface must be imported) Ranking is defined by properties –service.ranking –service.pid

© aQute, All Rights Reserved slide #50 Using getServiceReference A Service Reference is a handle to the service Example code LogServicelog; public void start(BundleContext context) throws Exception { ServiceReference ref = context.getServiceReference( LogService.class.getClass() ); if ( ref != null ) { log = (LogService) context.getService(ref); }

© aQute, All Rights Reserved slide #51 Access to Service Reference Properties The Service Reference object holds a number of properties –Object getProperty(String key) –String [] getPropertyKeys() Properties can be any standard Java object, including arrays, vectors, arrays of primitives, etc. Filter searches expand arrays. –X = [a,b], matches (x=a) and (x=b) It is also possible to find all the keys –String[] getPropertyKeys();

© aQute, All Rights Reserved slide #52 Assignment Write a bundle that displays the Log Service properties on the console –Get the reference to the log service –Get the keys from this references –For each key Get the value Print the key=value For nicety, printout arrays separately!

© aQute, All Rights Reserved slide #53 Access to the Associated Bundles Service References are associate with a registering bundle and a number of bundles that use it –Bundle getBundle(); –Bundle[] getUsingBundles(); Note null return for getUsingBundles() Bundle A Bundle B Bundle C getUsingBundles getBundle

© aQute, All Rights Reserved slide #54 Getting Services It is possible to get all Services –ServiceReference[] getServiceReferences( String ifName, String filter) Note potential null return The ifName is the same as the previous but in this case it may be null The second parameter is a filter specification The filter syntax is of small expressive language that does an assertion on the properties

© aQute, All Rights Reserved slide #55 Filter Language The syntax is based on simple to parse, recursive structure Supported expressions –And (& expr+ ) –Or (| expr+) –Not (!expr+) –Simple(property-keyopliteral )

© aQute, All Rights Reserved slide #56 Filter Syntax Operations –=equals (may contain * for wildcards) –~=looks like –>=Greater than or equal –<=Less than or equal –=*Is defined Examples –(vendor=*IBM*) –(&(service.pid=com.acme.bananas)(priority>=10)) –(url=*)

© aQute, All Rights Reserved slide #57 getServiceReferences Example code class MyService { void foo(); } public void start(BundleContext context) throws Exception { ServiceReference ref[] = context.getServiceReferences( MyService.class.getName(), (url=*) ); for ( int i= 0; ref!= null && i<ref.length; i++ ) { MyService mys = context.getService(ref[i]); mys.foo(); } }

© aQute, All Rights Reserved slide #58 Assignment: Printout all services Create a bundle that prints out all the services that have the property service.pid Print: –Their properties –The bundle that registered it –The bundles that are using it

© aQute, All Rights Reserved slide #59 Registering a service There are two calls on the Bundle Context to register a service, both return a Service Registration object –ServiceRegistration registerService(String ifName, Object service, Dictionary properties ) –ServiceRegistration registerService(String ifNames[], Object service, Dictionary properties ) The ifName must match an interface on the service object The service is immediately available to other bundles The service can be unregistered with the unregister method on the Service Registration The properties of the service can also be changed with the setModified(Dictionary) call

© aQute, All Rights Reserved slide #60 Registering a Service import org.osgi.framework.*; import org.osgi.service.cm.*; public class RegisterService implements BundleActivator { class MyManagedService implements ManagedService { public void updated(Dictionary properties) throws ConfigurationException { System.out.println("Updated " + properties); } public void start(BundleContext context) throws Exception { System.out.println("Register Service Example"); Hashtable properties = new Hashtable(); properties.put("service.pid", "biz.aQute.ExamplePid"); ServiceRegistration reg = context.registerService(ManagedService.class.getName(), new MyManagedService(), properties); } public void stop(BundleContext arg0) throws Exception { }

© aQute, All Rights Reserved slide #61 Registering a Service In the monitor you can see the registration of the service

© aQute, All Rights Reserved slide #62 Listening to services It is possible to get notifications of –REGISTERED Service registrations –MODIFIEDService modifications –UNREGISTEREDService unregistrations All you have to do is add a listener to the BundleContext, there are 2 versions –addServiceListener(ServiceListener) –addServiceListener(ServiceListener,String filter) The second version allows to filter for only the services that are needed Service Listening is synchronous –Potential for deadlocks

© aQute, All Rights Reserved slide #63 Listening to Services public class ServiceListening implements BundleActivator, ServiceListener { public void serviceChanged(ServiceEvent event) { Stringtype= "UNKNOWN"; switch (event.getType()) { case ServiceEvent.MODIFIED :type = "MODIFIED"; break; case ServiceEvent.REGISTERED : type = "REGISTERED"; break; case ServiceEvent.UNREGISTERING : type = "UNREGISTERING"; break; } ServiceReference ref = event.getServiceReference(); Long id = (Long) ref.getProperty("service.id"); String [] classes = (String[]) ref.getProperty("objectclass"); System.out.println( "Service Event "+type + ":" + id + ":" + classes[0]); } public void start(BundleContext context) throws Exception { System.out.println("Listening to Services Example"); context.addServiceListener(this); } public void stop(BundleContext arg0) throws Exception { }

© aQute, All Rights Reserved slide #64 Assignment: Listenening Listen to service events Print them out Use the monitor to stop/start the bundles to see that they generate service events by registering and unregistering services

© aQute, All Rights Reserved slide #65 Service Registry The Framework Service Registry is available to all bundles to collaborate with other bundles Requires permission –Under Operator control Services are associated with properties –Query language to find appropriate service Bundles can update the properties

© aQute, All Rights Reserved slide #66 Service Factories How does a bundle customize a service per bundle? The ServiceFactory interface allows a bundle to return a specific customized service object for each bundle Framework calls back the factory for service get and unget methods The Framework keeps a reference count to the service Get and Unget will be perfectly matched

© aQute, All Rights Reserved slide #67 Service Factories Framework Bundle Standard Bundle Factory Bundle A { } Bundle B { }

© aQute, All Rights Reserved slide #68 Assignment: ServiceFactory Copy HelloWorld to ServiceFactoryExample Register a ServiceFactory for a String object as service type Register it with the following attributes –type=grabme –times=new Integer(5) Let the ServiceFactoryExample also implement the ServiceFactory interface Implement the getService(Bundle, ServiceRegistration) to return a unique string that also includes the calling bundles location Print out the get operation Print out the unget operation

© aQute, All Rights Reserved slide #69 Create a test bundle Copy HelloWorld to Grabber Listen to all services that have the property –type=grabme If a REGISTERED event happens –Get the times propery (an Integer) –Get the service times times –Print out the service Copy hello.jardesc to grabber.jardesc Copy Manifest.mf to grabber.mf Modify the manifest to have the Bundle Activator point to the Grabber In the context menu of the grabber.jardesc file select Open JAR Packager Update the location of the jar descriptor file as well as the manifest Finish

© aQute, All Rights Reserved slide #70 Service Tracker Services register and unregister all the time Onus on the programmer to only use services that are registered ServiceTracker simplifies this task: –Maintains a list of active services –Events can be overridden in subclass Used in almost every bundle

© aQute, All Rights Reserved slide #71 Service Tracker Use "As is" Gets each service from the registry when it is available –Object getService() –Object[] getServices() –Object waitForService()

© aQute, All Rights Reserved slide #72 ServiceTracker Track all services of a certain type in the service registry Use subclassing a ServiceTracker or use the ServiceTrackerCustomizer interface Implement the methods: –Object addingService(ServiceReference r) –void modifiedService(ServiceReference r, Object service) –void removedService(ServiceReference r, Object service)

© aQute, All Rights Reserved slide #73 Create a service tracker A service tracker requires a Bundle Context Notice that after creation, it needs to be opened public void start(BundleContext context) throws Exception { System.out.println("ListServices"); tracker = new ServiceTracker( context, context.createFilter("(service.pid=*)"), null ) { public Object addingService(ServiceReference ref) { printServiceReference(ref); return ref; } public void removedService(ServiceReference ref, Object o) { } }; tracker.open(); }

© aQute, All Rights Reserved slide #74 ServiceTracker: create Framework Bundle A Bundle B Bundle C

© aQute, All Rights Reserved slide #75 ServiceTracker: open Framework Bundle A Bundle B Bundle C addServiceListener Object addingService(ServiceReference r){…}

© aQute, All Rights Reserved slide #76 ServiceTracker: adding Framework Bundle A Bundle B Bundle C addServiceListener Object addingService(ServiceReference r){…}

© aQute, All Rights Reserved slide #77 ServiceTracker: removing Framework Bundle A Bundle B Bundle C addServiceListener void removedService(ServiceReference r,Objecto){…}

© aQute, All Rights Reserved slide #78 ServiceTracker: modified Framework Bundle B Bundle C addServiceListener void modifiedService(ServiceReference r, Object o){…} Bundle A ServiceTrackerList

© aQute, All Rights Reserved slide #79 Multiple constructors First is used to create a tracker on a specific interface –ServiceTracker( BundleContext, String, ServiceTrackerCustomizer) Second constructor is used to create a tracker with a filter Filters can be created with the BundleContext –ServiceTracker( BundleContext, Filter, ServiceTrackerCustomizer) Either constructor can use a ServiceTrackerCustomier When the customizer is null, the ServiceTracker will call the methods on itself, allowing subclassing The service tracker only becomes active after it is opened It should be closed in the stop() method

© aQute, All Rights Reserved slide #80 Assignment In a previous assignment we listed all the services that had a service.pid attribute Change this example to use a service tracker instead

© aQute, All Rights Reserved slide #81 Dependencies The Framework manages the dependencies between bundles Bundles that are installed and started will register services Framework will automatically unregister services when a bundle stops Event notifications for all important events

© aQute, All Rights Reserved slide #82 Dependencies Framework Bundle C { } Bundle A { } Bundle B { } Install A start events: install events: register

© aQute, All Rights Reserved slide #83 Framework Dependencies Bundle C { } Bundle B { } Uninstall stop events: uninstall events: unregister Bundle A { }

© aQute, All Rights Reserved slide #84 Dependency Management When a bundle stops, it is cleaned up –Registered services are removed –References to other services removed Bundles can be notified when a service they depend on is unregistered Class path dependencies are managed Model allows long running applications with dynamic software updates

© aQute, All Rights Reserved slide #85 How To Use The Registry The Framework handles dependencies, but not all! Dont call us, we'll call you! Is Better. –Also know as the White Board approach Clients should register their objects, not the Servers. This is not intuitive! Significantly simplifies programming, therefore strongly advised

© aQute, All Rights Reserved slide #86 White Board Approach See whitepaper: "Listeners Are Considered Harmful" Framework Client Server Client Server Add listener

© aQute, All Rights Reserved slide #87 White Board Approach Registry Bundle A { } register WAP bundle Events: register, unregister Bundle C { } Bundle B { } Publisher get

© aQute, All Rights Reserved slide #88 Threads Many tasks need to be done in a background thread –IO –Time related Threads cannot be stopped in Java –Some communication is used to request to stop them –A flag is reset –The thread is interrupted –Any IO channels are closed The continuous running model of OSGi requires that the threads remain alive, even when there are failures Failure handling must be careful not to overload the system

© aQute, All Rights Reserved slide #89 Typical thread loop

© aQute, All Rights Reserved slide #90 Assignment Write an application that sends out alive message on the network with UDP –Use your IP address as unique identifier Listen to the alive messages and register an Alive service for each station that has been discovered this way Unregister the service when no message has been received in 1 minute Make

© aQute, All Rights Reserved slide #91 Native Code Native code allows a bundle to carry non-java code for different operation systems If a bundle has a Bundle-NativeCode manifest header, the bundle should contain native code libraries that must be available for the bundle to execute. When a bundle makes a request to load a native code library, the findLibrary method of the caller's classloader must be called to return the file path name in which the Framework has made the requested native library available. The bundle must have the required RuntimePermission[loadLibrary. ] in order to load native code in the OSGi Service Platform.

© aQute, All Rights Reserved slide #92 Native Code The Bundle-NativeCode manifest header must conform to the following syntax: –Bundle-NativeCode ::= nativecode-clause (, nativecode- clause ) * –nativecode-clause ::= nativepaths ( ; env-parameter )* –Nativepaths::=jar-path ( ; jar-path )* –env-parameter::= (processordef | osnamedef | osversiondef | languagedef ) –Processordef::= processor = value –osnamedef::= osname = value –osversiondef::= osversion = value –Languagedef::= language = value –value ::= token | quoted-string

© aQute, All Rights Reserved slide #93 Native Code The following is a typical example of a native code declaration in a bundles manifest: Bundle-NativeCode: /lib/http.DLL ; /lib/zlib.dll ; osname = Windows95 ; osname = Windows98 ; osname = WindowsNT ; processor = x86 ; language = en ; language = se, /lib/solaris/libhttp.so ; osname = Solaris ; osname = SunOS ; processor = sparc, /lib/linux/libhttp.so ; osname = Linux ; processor = mips

© aQute, All Rights Reserved slide #94 Native Code If a Bundle-NativeCode clause contains duplicate env-parameter entries, the corresponding values must be ORed together. This feature must be carefully used because the result is not always obvious. This is highlighted by the following example: // The effect of this header // is probably not the intended effect! Bundle-NativeCode: /lib/http.DLL ; osname = Windows95 ; osversion = 3.1 ; osname = WindowsXP ; osversion = 5.1

© aQute, All Rights Reserved slide #95 Native Code The previous example implies that the native library will load on Windows XP 3.1 and later, which was probably not intended. The single clause should be split up when the expected effect is desired: Bundle-NativeCode: /lib/http.DLL ; osname = Windows95 ; osversion = 3.1, /lib/http.DLL ; osname= WindowsXP ; osversion = 5.1 If multiple native code libraries need to be installed on one platform, they must be specified in the same clause for that platform.

© aQute, All Rights Reserved slide #96 Native Code: Use Table OS NameVersionProcessorLanguageFilterLibs win32x86ennativecodewin32.dll, nativecode1win32.dll linuxx86en(org.osgi.framework.windowing.sy stems=gtk) nativecodegtk.so linuxx86en(org.osgi.framework.windowing.sy stem=qt) nativecodeqt.so

© aQute, All Rights Reserved slide #97 Native Code Bundle-NativeCodenativecodewin32.dll; nativecode1win32.dll; processor=x86; osname=win32; language=en, nativecodegtk.so; processor=x86; osname=linux; selection-filter = (org.osgi.framework.windowing.system = gtk); language=en, nativecodeqt.so; processor=x86; osname=linux; selection-filter = (org.osgi.framework.windowing.system = qt); language=en If the native code algorithm fails to select a native code clause from the Bundle- NativeCode header, the bundle will fail to install and a BundleException will be thrown. The selection filter is used to select the appropriate clause based upon the windowing system.

© aQute, All Rights Reserved slide #98 aQute ,