Practical OSGi

  • 1,732 views
Uploaded on

Presentation on Practical OSGi from Sun Community One North 2009 event in Oslo.

Presentation on Practical OSGi from Sun Community One North 2009 event in Oslo.

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • What does the Private-Package manifest header do?
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
1,732
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
150
Comments
1
Likes
7

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide






























































Transcript

  • 1. Practical OSGi Sten Roger Sandvik Enonic AS srs@enonic.com
  • 2. What is OSGi? • A module system for Java The Dynamic Module System for Java™. • Dynamic modules (bundles) Installing, starting, stopping, updating and uninstalling modules at runtime. • Service oriented Services can be registered and consumed at runtime. • A worldwide standard OSGi Alliance. Release R1 in 2000. Currenty at release R4.1.
  • 3. Key Benefits • No more “JAR” hell Can have multiple versions of same library. • Reuse packaged modules Allows 3rd party prepackaged modules to be installed. • Less server restarts Update modules at runtime. • Simplifies multi-team projects Can partition the application into smaller modules.
  • 4. Key Benefits (2) • Enables smaller systems By breaking it into smaller pieces (some optional). • Multiple runtime options Standalone, Client, Server, Embedded. • Multiple implementations Eclipse Equinox, Apache Felix, Knopflerfish. • Very high adaption rate Apache, Sun, Oracle, IBM, BMW and Enonic are all using it.
  • 5. Theory
  • 6. Framework Layers • L3 - Service L3 Publish/find/bind service model to decouple bundles. L2 • L2 - Lifecycle L1 Independent lifecycle of bundles without JVM restarts. • L1 - Module L0 A module (or bundle) uses classes from other bundles in a controlled way. • L0 - Execution environment Well defined profiles (JavaSE, CDC) that define the JVM environment.
  • 7. Module Layer • Unit of deployment L3 A module is called a bundle in OSGi. L2 • Standard JAR file with metadata L1 Metadata in META-INF/MANIFEST.MF • Seperate classloader per bundle L0 Class sharing at the Java package level. • Share only “interface” packages Share only API between bundles.
  • 8. Module Layer (2) • Multi-version support L3 Different bundles can see different versions. L2 • Manifest example L1 Bundle-Name: Example Bundle-SymbolicName: net.foo.common Bundle-Version: 1.0.0 L0 Import-Package: org.osgi.framework;version=”1.3”, net.foo.api;version=”1.0.0” Export-Package: net.foo.common;version=”1.0.0” Private-Package: net.foo.common.internal, net.foo.common.internal.codec Bundle-ManifestVersion: 2
  • 9. Lifecycle Layer • Managed lifecycle L3 Managed states for each bundle. L2 • Update existing bundles L1 Dynamically install, start, update and uninstall. L0
  • 10. Service Layer • Service registry L3 Publish/find/bind services at runtime. L2 • Bind interfaces L1 Bind interfaces - not implementations. L0 Registry publish find Provider Requester interact
  • 11. Practice
  • 12. Password Encoder • Password encoder application Simple JVM client that uses password encoders. • Encoders are pluggable Multiple implementations. Can be stopped/started at runtime.
  • 13. Project Structure • API Bundle API Holds all public avaliable APIs. Encoder • Encoder Bundle Client Provides a password encoder implementation. Parent • Client Bundle Client bundle that uses one or more password encoders.
  • 14. API Bundle • Provides the API API Simple PasswordEncoder interface. Encoder • Exports package Client Export the API (net.foo.api) package so other bundles can see it.
  • 15. Password Encoder package net.foo.api; API /** * Simple interface that defines the password Encoder * encoder. */ public interface PasswordEncoder Client { /** * Returns the name of this encoder. */ public String getName(); /** * Encode the password and return the encoded password. */ public String encode(String password); }
  • 16. Manifest • Exports the package API Exports package (net.foo.api) with right version. Encoder ... Bundle-Name: Example - API Client Bundle-SymbolicName: net.foo.api Export-Package: net.foo.api;version=”1.0.0” ...
  • 17. Encoder Bundle • Uses the API API Imports proper API package (net.foo.api). Encoder • Registers a provider Client Registers a PasswordEncoder provider implementation as a service. • Hides the implementation Hides implementation from other bundles by using Private-Package.
  • 18. Register a Service • Use a BundleActivator API Implement bundle activator that handle lifecycle. Encoder • Register on start Client Register service on when bundle is started. • Unregister at stop Unregister the service when bundle is stopped. • Add proper metadata Do not forget to add Bundle-Activator metadata in manifest.
  • 19. Bundle Activator package net.foo.encoder; API import net.foo.api.*; import org.osgi.framework.*; Encoder public class Activator implements BundleActivator Client { private ServiceRegistration reg; public void start(BundleContext context) { this.reg = context.registerService( PasswordEncoder.class.getName(), new ReversePasswordEncoder(), null); } public void stop(BundleContext context) { this.reg.unregister(); } }
  • 20. Manifest • Declare BundleActivator API Set the bundle activator classname in manifest. Encoder • Import used packages Client Import net.foo.api and org.osgi.framework packages with right version. • Hide implementation package Declare net.foo.encoder package as a private package.
  • 21. Manifest (2) ... API Bundle-Name: Example - Encoder Bundle-SymbolicName: net.foo.encoder Bundle-Activator: Encoder net.foo.encoder.Activator Import-Package: net.foo.api;version=”1.0.0”, org.osgi.framework;version=”1.3” Client Private-Package: net.foo.encoder ...
  • 22. Client Bundle • Uses the API API Does not see the encoder implementation. Encoder • Track PasswordEncoder’s Client Track available password encoder providers. • Encodes password based on encoder Finds right encoder and encodes the password.
  • 23. Locating Services • Services are dynamic API All services can be added and removed runtime. Encoder • Not always available on init Client Do not assume that you can always obtain a service during initialization. • Use ServiceTracker Use ServiceTracker to track available services. Remember to open() and close() the tracker.
  • 24. Using ServiceTracker public class Activator implements BundleActivator API { private ServiceTracker tracker; Encoder public void start(BundleContext context) { tracker = new ServiceTracker( Client context, PasswordEncoder.class.getName(), null); tracker.open(); } public void stop(BundleContext context) { tracker.close(); } public class MyConsumer } { private ServiceTracker tracker; ... public void consumeServices() { Object[] services = tracker.getServices(); } }
  • 25. Client Implementation public class Client API { private ServiceTracker tracker; Encoder ... public String encode(String method, String password) Client { for (Object service : tracker.getServices()) { PasswordEncoder enc = (PasswordEncoder)service; if (enc.getName().equals(method)) { return enc.encode(password); } } throw new IllegalArgumentException(“Provider not found”); } }
  • 26. Manifest ... API Bundle-Name: Example - Client Bundle-SymbolicName: net.foo.client Bundle-Activator: Encoder net.foo.client.Activator Import-Package: net.foo.api;version=”1.0.0”, Client org.osgi.framework;version=”1.3”, org.osgi.util.tracker;version=”1.3” Private-Package: net.foo.client ...
  • 27. Improvements • Use a declarative service model Hides OSGi details. Makes service tracking easy. Spring DM is one implementation. • Use automatic manifest creation Use Maven Bundle Plugin (Apache Felix Project) to create and validate manifest entries at build time.
  • 28. Demo
  • 29. Tools Used • Maven 2 Build the project using Maven 2 using standard layout. • Maven Bundle Plugin Creates and validates the MANIFEST.MF. Based on BND. • Maven Pax Plugin Running the OSGi application using Pax Runner. • Felix OSGi Container Implementation of R4.1 specification.
  • 30. 3rd Party Bundles • Pax Web Web container based on Jetty. Implementation of HttpService. • Pax Logging Logging abstraction and LogService implementation. Possible to use SLF4J, Commons Logging and Log4J in bundles. • Felix WebConsole Web Console to visualize bundles and services.
  • 31. Q? srs@enonic.com