1 / 106

OSGi Tutorial

OSGi Tutorial. Matthias lübken, akquinet www.akuinet.de & Peter Kriens, aQute www.aQute.biz. Content. I – Setting up Eclipse and the OSGi Platform II – OSGi Background III – OSGi Technology IV – OSGi Concepts V – Hello world VI - Launching a Framework

ghita
Télécharger la présentation

OSGi Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OSGi Tutorial • Matthias lübken, akquinet • www.akuinet.de • & • Peter Kriens, aQute • www.aQute.biz

  2. Content • I – Setting up Eclipse and the OSGi Platform • II – OSGi Background • III – OSGi Technology • IV – OSGi Concepts • V – Hello world • VI - Launching a Framework • VII - Component Interaction and Collaboration • IIX – Service Components • IX – Using the HTTP Service • X – Making a Portal • XI – Creating a content provider • XII – Final

  3. Section I – Setting up Eclipse and the OSGi Platform

  4. Setup: Your Infrastructure • Create a new workspace • Import all the projects and make a copy • Find where your Eclipse installation resides • Copy the bnd.jar file to the eclipse/dropins directory • Restart Eclipse • Select your new workspace

  5. Your Workspace

  6. Verify bnd is installed

  7. Section II - OSGi Background

  8. What is the OSGi Service Platform? • A Java framework for developing (remotely) deployed service applications, that require: • Reliability • Large scale distribution • Wide range of devices • Collaborative • Created through a collaboration of industry leaders • IBM, Ericsson, Nokia, Sony, Telcordia, Samsung, ProSyst, Gatespace, BenQ, Nortel, Oracle, Sybase, Espial, and many more • Spec 4.+ publicly available at www.osgi.org … • Cool!

  9. Why the OSGi Service Platform? • What problems does the OSGi Service Platform address? • Modularity! • The limited (binary) software portability problem • The complexity of building heterogeneous software systems • Supporting the myriad of configuration, variations, and customizations required by today’s devices • Managing the software on the device

  10. Problems: Limited Binary Software Portability • Lack of portability causes • Market friction: No large market of reusable components and applications • Reduced quality • Unnecessary constraints on hardware and software architectures • CPUs differ widely in cost and performance • Linux is nice, but it is sub-optimal for smaller devices • Benefits of the OSGi Platform • Applications run unmodified on different hardware and software architectures due to a formal specification of all relevant aspects • The OSGi Specifications gives you choice!

  11. SERVICE ORIENTED PROGRAMMING STRUCTURED PROGRAMMING OBJECT ORIENTED PROGRAMMING Problems: Complexity of Software PRODUCTIVITY ASSEMBLY COMPLEXITY AND SIZE

  12. Problems: Limits of Object Oriented Technology • Objects are great, but oh, the tangled webs we weaves … • Coupling severely limits reusability • Using a generic object, can drag in a large number of other objects • Creates overly large systems after a certain complexity is reached • Flexibility must be built in by the programmer • Plug-in architectures • Component architectures like OSGi minimize the coupling that is created by OO

  13. Software complexity

  14. Section III - OSGi Technology

  15. Architecture • Applications / modules share a single Java VM • Better class loading e.g. versioning • Isolation/security between applications • Communication & collaboration between applications • Life cycle management • install, start, stop, update, etc.

  16. Layering • Defined in a number of layers S E C U R I T Y Applications (bundles) Service Life Cycle Module Java Execution Env OS + Hardware

  17. OSGI MIN. CLDC/ MIDP CDC/FP J2SE Layers: Execution Environment • Provides a defined context for applications • Defines the available class library • Not bound to a concrete JRE • OSGi Minimum EE • Matches most Java profiles • Framework need OSGi Minimum • Implementations can use more

  18. BUNDLE BUNDLE BUNDLE BUNDLE BUNDLE BUNDLE BUNDLE Layers: Module Layer • Specifies class loading and packaging • Modularization with classloaders • Protection • Versioning

  19. BUNDLE X SYSTEM BUNDLE BUNDLE X-V2 BUNDLE B BUNDLE M BUNDLE A Layers: Life Cycle Layer 1 • System Bundle represents the Framework • Provides an API for managing bundles • Install • Resolve • Start • Stop • Refresh • Update • Uninstall STATE (ACTIVE OR NOT) MANAGES

  20. INSTALLED RESOLVED STARTING STOPPING ACTIVE UNINSTALLED Layers: Life Cycle Layer 2 • Bundle Activator is called at startup • Implements two methods: • start(): Initialize and return quickly • stop(): Cleanup • Bundle Context to access framework • Start Level service to control the start/stop of groups of applications

  21. Layers: Service Layer • Inside-VM service model • Discover • based on interface or properties • Bind • to one or more services by • program control, • default rules, or • deployment configuration • Service Oriented Architectures (SOA) confusion • Web services bind and discover over the net • OSGi binds and discovers inside a Java VM

  22. Why services? • Collaboration model • Separate contract from implementation • Dynamically discover and bind available implementations • Components are reusable • Allows alternate implementations SERVICE CONTRACT PROVIDES COMPONENT USES 18

  23. APPLICATION MANAGER FOREIGN APP. ACCESS SIGNED BUNDLES DECLARATIVE SERVICES DEVICE MANAGEMENT SECURITY POLICIES FRAMEWORK LAYERING INITIAL PROVISIONING UPNP CONDITIONAL PERMISSIONS … STANDARD SERVICES EVOLUTION R4 MOBILE R3 UPNP INITIAL PROVISIONING NAME SPACE JINI START LEVEL IO CONNECTOR WIRE ADMIN XML PARSER MEASUREMENT & STATE POSITION EXECUTION ENV. VEHICLE R2 PACKAGE ADMIN CONFIGURATION ADMIN PERMISSION ADMIN USER ADMIN PREFERENCES METATYPE SERVICE TRACKER HOME AUTOMATION R1 FRAMEWORK HTTP LOG DEVICE ACCESS 2006 2000 2001 2003

  24. Layers: The Security Layer • Based on Java 2 security • Permissions • Bundle signing • And a number of extra rules • Dynamic • Fully under control of the operator • Fully controlled systems • Fully open systems

  25. The OSGi Implementations • Major Framework vendors are • ProSyst • Gatespace Telematics • IBM • Siemens • Espial • Open source implementations • Apache Felix • Eclipse Equinox • Gatespace Knopflerfish • See http://www.aqute.biz/osgi for an overview

  26. Benefits • Components are small • Easier to make, maintain and understand • Components are minimally coupled to other components • Gives reusability • Collaborative model • Allows reuse • Wide Adoption • Bigger market, more components available • Excellent model for customizations and variations

  27. What Did We Learn • The OSGi Service Platform is kind of a Java Operating System • Execution Environment • Module layer • Service Layer • Security • It simplifies: • Deployment problems • Software composition • Software management • Implemented by many vendors and open source groups

  28. Section IV - OSGi concepts

  29. BUNDLE A {…} BUNDLE B {…} BUNDLE C {…} Framework Entities OSGI FRAMEWORK = SERVICE, DEFINED BY JAVA INTERFACE = BUNDLE

  30. BUNDLE A {…} BUNDLE B {…} Bundles & Services • A bundle is the deliverable application • Like a Windows EXE file • Content is a JAR file • A bundle registers 0..n services • A service ... • is specified by an interface • may be implemented by multiple bundles • is bound to the bundle life-cycle

  31. What is in a Bundle? • A bundle contains (normally in a JAR file): • Manifest (bundle meta data) • Code (classes in packages) • Resources (other files in the JAR file)

  32. BUNDLE A {…} What Happens With a Bundle? • The framework: • Reads the manifest • Installs code and resources • Resolves dependencies • Controls life cycle • During runtime the framework: • Calls the Bundle Activator start() & stop() • Manages the class path • Handles the service dependencies

  33. SECTION V - HELLO WORLD

  34. First Project:Create • Create a new Java Project • Name, up to you • (examples use my.project) • Defaults are OK • Next

  35. First Project: Classpath • Source: ok

  36. First Project: Classpath • Projects: nothing • Libraries, we need to addthe osgi.jar. This can be found in the aQute.tutorial.runtimeproject, in the repo directory • Add JARs -> select • Choose the repo directory

  37. Create a Hello (and Goodbye) World Bundle

  38. Create a Hello (and Goodbye) World Bundle • Create a Java source called World.java • Select src folder * > New > Class

  39. Create a Hello (and Goodbye) World Bundle • Set package name: my.project • Set Class Name: World • Add Interfaces: Add … • BundleActivator • Finish

  40. Source code for Hello/Goodbye World • The source should look similar to ... • fill in the // TODO blocks • start – Called when the bundle is started • stop – When the bundle is stopped • Some tips • F3 > Will jump to the selected type (click BundleActivator, and then F3) • Control-Shift-T > Will show a source of a type (class) • Control-Shift-R > Will show a source of a resource package my.project;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class World implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println(“Hello World”); } public void stop(BundleContext context) throws Exception { System.out.println(“Goodbye World”); }}

  41. Manifest code for Hello/Goodbye World • Each JAR file has a manifest • http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html • A manifest is a text file that provides meta information about the JAR file. • Used for versioning and JAR file signing. • A manifest consists of a set of headers and values. • For example, Manifest-Version: 1 • Unrecognized headers are ignored by the Java VM, this makes it possible to extend the manifest • The OSGi specifications have declared manydifferent headers • Vendor information • Imported and Exported packages • Native code

  42. Hello World (and Goodbye) • A bundle needs a manifest. The bnd plugin can create this for you (and verify its headers for validity!) • You have to create a bnd file in the project directory. * > New > File • bnd.bnd • Likely opens a separate applications • Open with Properties Editor • You can make this an association for *.bnd files (see Eclipse Preferences)

  43. Real code! Hello World (and Goodbye) • Add: • Private-Package. Defines the packages that should be included in your bundle You can use wildcards • my.project.* • Bundle-Activator, my.project.World • Create the bundle by selecting the bnd file, then * > Make Bundle • This creates a JAR file, which is your first bundle! Private-Package: my.project.*Bundle-Activator: \ my.project.World

  44. Bnd Translation • The bnd plugin translates the bnd.bnd file to the manifest, and embeds in the JAR file • uppercase HEADERS ARE copied to the manifest Private-Package: my.project Bundle-Activator: \ my.project.World BND Manifest-Version 1.0 Bnd-LastModified 1220347055030 Bundle-Activator my.project.World Bundle-ManifestVersion 2 Bundle-Name my.project Bundle-SymbolicName my.project Bundle-Version 0 Created-By 1.5.0_13 (Apple Inc.) Import-Package org.osgi.framework;version="1.3" Private-Package my.project Tool Bnd-0.0.265

  45. BND • Import-Package is calculated from the referred MATCHED classes: • * (all) is default, !com.acme.* means do not import • VERY GOOD DEFAULTS! • VERY LITTLE HEADERS NEED SPECIFYING • Export package expressions are matched against all packages on the class path • SIMPLIFIED SYNTAX FOR DECLARATIVE SERVICES • USEFUL IN: • ANT, ECLIPSE, COMMAND LINE, MAVEN • Full manual: • http://www.aqute.biz/Code/Bnd

  46. See What We Created • Double click on the my.project.jar shows contexts of Manifest.MF.

  47. Section VI - Launch a Framework

  48. Launch a Framework • Debug -> Open Debug Dialog … • Select EquinoxFw (in Java Applications) • This launch file is provided by the aQute.tutorial.runtime project. • This starts an Equinox Framework with the Apache Felix fileinstall.jar bundle as only bundle. • The fileinstall.jar bundle watches the work directory for bundles • Any bundle in this directory will be installed and started • Removed bundles will be uninstalled • This is not an OSGi “standard”, it is just a Felix bundle • Installing and uninstalling is therefore simply managing the aQute.tutorial.runtime/work directory • Copy & Paste a file: gets installed • Delete a file: gets uninstalled

  49. Launch a Framework • Debug -> Debug Dialog … • -> Debug

  50. Run the Hello World bundle • The launch runs a Framework console • Just copy (Copy and Paste) the my.project.jar to the work directory • See “Hello World” in the console • Type “ss” (show status) • Look at the active bundles • the number for the my.project bundle. This is the bundle-id.

More Related