1.15k likes | 1.47k Views
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
E N D
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 • VII - Component Interaction and Collaboration • IIX – Service Components • IX – Using the HTTP Service • X – Making a Portal • XI – Creating a content provider • XII – Final
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
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!
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
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!
SERVICE ORIENTED PROGRAMMING STRUCTURED PROGRAMMING OBJECT ORIENTED PROGRAMMING Problems: Complexity of Software PRODUCTIVITY ASSEMBLY COMPLEXITY AND SIZE
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
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.
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
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
BUNDLE BUNDLE BUNDLE BUNDLE BUNDLE BUNDLE BUNDLE Layers: Module Layer • Specifies class loading and packaging • Modularization with classloaders • Protection • Versioning
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
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
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
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
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
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
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
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
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
BUNDLE A {…} BUNDLE B {…} BUNDLE C {…} Framework Entities OSGI FRAMEWORK = SERVICE, DEFINED BY JAVA INTERFACE = BUNDLE
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
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)
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
First Project:Create • Create a new Java Project • Name, up to you • (examples use my.project) • Defaults are OK • Next
First Project: Classpath • Source: ok
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
Create a Hello (and Goodbye) World Bundle • Create a Java source called World.java • Select src folder * > New > Class
Create a Hello (and Goodbye) World Bundle • Set package name: my.project • Set Class Name: World • Add Interfaces: Add … • BundleActivator • Finish
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”); }}
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
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)
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
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
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
See What We Created • Double click on the my.project.jar shows contexts of Manifest.MF.
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
Launch a Framework • Debug -> Debug Dialog … • -> Debug
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.