osgi & blueprint

36
OSGi & Blueprint Apache Karaf OSGi Container and Apache Aries

Upload: kara-satish-kumar

Post on 19-Jan-2017

289 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: OSGi & Blueprint

OSGi & Blueprint

Apache Karaf OSGi Container and Apache Aries

Page 2: OSGi & Blueprint

Agenda• OSGi in a Nutshell• Apache Felix Karaf• Blueprint

Page 3: OSGi & Blueprint

OSGi in a Nutshell• What is OSGi?• Why OSGi?• OSGi Architecture

• Modular Layer• Lifecycle Layer• Service Layer

• Bundles in depth• OSGi Service Registry

Page 4: OSGi & Blueprint

What is OSGi?•Modularity layer for the Java platformModularity, where the code of application is divided into logical parts representing separate concerns

•From OSGi Alliance (http://www.osgi.org)standard for service-oriented, component-based Java applications

Page 5: OSGi & Blueprint

Why OSGi?• Reduced Complexity & Reuse - Developing with OSGi technology means developing bundles: the OSGi components.

Bundles are modules. They hide their internals from other bundles and communicate through well defined services .• Dynamic Updates - The OSGi component model is a dynamic model. Bundles can be installed, started, stopped, updated, and

uninstalled without bringing down the whole system.• Adaptive - The OSGi service registry is a dynamic registry where bundles can register, get, and listen to services. This dynamic

service model allows bundles to find out what capabilities are available on the system and adapt the functionality they can provide. This makes code more flexible and resilient to changes.

• Versioning - OSGi technology solves JAR hell. JAR hell is the problem that library A works with library B;version=2, but library C can only work with B;version=3. In standard Java, you're out of luck. In the OSGi environment, all bundles are carefully versioned and only bundles that can collaborate are wired together in the same class space. This allows both bundle A and C to function with their own library.

• Small - The OSGi Release 4 Framework can be implemented in about a 300KB JAR file. This is a small overhead for the amount of functionality that is added to an application by including OSGi. OSGi therefore runs on a large range of devices: from very small, to small, to mainframes. It only asks for a minimal Java VM to run and adds very little on top of it.

• Fast - One of the primary responsibilities of the OSGi framework is loading the classes from bundles. In traditional Java, the JARs are completely visible and placed on a linear list. Searching a class requires searching through this (often very long, 150 is not uncommon) list. In contrast, OSGi pre-wires bundles and knows for each bundle exactly which bundle provides the class. This lack of searching is a significant speed up factor at startup.

Page 6: OSGi & Blueprint

OSGi Architecture• Bundles - Bundles are the OSGi components made by the developers.

• Services - The services layer connects bundles in a dynamic way by offering a publish-find-bind model for plain old Java objects. This layer concerned with interaction and communication among modules.

• Life-Cycle - The API to install, start, stop, update, and uninstall bundles. This provides execution time module management and access to the underlying OSGi framework.

• Modules - The layer that defines how a bundle can import and export code. Basically concerned with packaging and sharing the code.

• Security - The layer that handles the security aspects.

• Execution Environment - Defines what methods and classes are available in a specific platform.

Page 7: OSGi & Blueprint

Modular Layer• The module layer defines the OSGi module concept,

called a bundle, which is a JAR file with extra metadata and resources.

• Bundles typically aren’t an entire application packaged into a single JAR file; rather, they’re the logical modules that combine to form a given application.

• Bundles are more powerful than standard JAR files, because you can explicitly declare which contained packages are externally visible (that is, exported packages).

• Another important advantage of bundles over standard JAR files is the fact that you can explicitly declare on which external packages the bundles depend (that is, imported packages).

• The main benefit of explicitly declaring your bundles’ exported and imported packages is that the OSGi framework can manage and verify their consistency automatically.

Page 8: OSGi & Blueprint

Lifecycle Layer• The lifecycle layer defines how bundles are dynamically

installed and managed in the OSGi framework.• The lifecycle layer serves two different purposes. External

to your application, the lifecycle layer precisely defines the bundle lifecycle operations (install, update, start, stop, and uninstall). These lifecycle operations allow you to dynamically administer, manage, and evolve your application in a well-defined way. This means bundles can be safely added to and removed from the framework without restarting the application process.

• Internal to your application, the lifecycle layer defines how your bundles gain access to their execution context, which provides them with a way to interact with the OSGi framework and the facilities it provides during execution. This overall approach to the lifecycle layer is powerful because it lets you create externally (and remotely) managed applications or completely self-managed applications (or any combination).

Page 9: OSGi & Blueprint

Service Layer• Service layer supports and promotes a flexible

application programming model incorporating concepts popularized by service-oriented computing.

• The main concepts revolve around the service-oriented publish, find, and bind interaction pattern: service providers publish their services into a service registry, while service clients search the registry to find available services to use.

Page 10: OSGi & Blueprint

BundlesWhat is (in) an OSGi bundle?• JAR file containing classes and resources• Extra manifest headers

• Human-readable Information• Bundle Identification• Code Visibility• Class-search order

Page 11: OSGi & Blueprint

BundlesOSGi bundle manifest headers

• Human-readable Information − Bundle-Name− Bundle-Name− Bundle-Description− Bundle-DocURL− Bundle-Category− Bundle-Vendor− Bundle-ContactAddress− Bundle-Copyright

• Bundle Identification− Bundle-ManifestVersion− Bundle-SymbolicName− Bundle-Version

• Code Visibility− Bundle-ClassPath− Export-Package− Import-Package

Syntax Meaning

"[min,max)" min <= x < max"[min,max]" min <= x <= max"(min,max)" min < x < max"(min,max]" min < x <= max"min" min <= x

Versions and version ranges• minimum version• version range

• include version with [ and ]• exclude version with ( and )

Page 12: OSGi & Blueprint

Bundle Class-Search Order

• If the class is from a package starting with java., the parent class loader is asked for the class. If the class is found, it’s used. If there is no such class, the search ends with an exception.

• If the class is from a package imported by the bundle, the framework asks the exporting bundle for the class . If the class is found, it’s used. If there is no such class, the search ends with an exception.

• At last the bundle class path is searched for the class. If it’s found, it’s used. If there is no such class, the search ends with an exception.

Note : Each OSGi bundle has a class loader associated with it. Example: manifest information

Page 13: OSGi & Blueprint

Dependency Resolution

• Highest priority is given to already-resolved candidates, where multiple matches of resolved candidates are sorted according to version and then installation order.• Next priority is given to unresolved candidates, where multiple matches of unresolved candidates are sorted according to version and then installation order.

Page 14: OSGi & Blueprint

Bundle States

Page 15: OSGi & Blueprint

OSGi Service Registry• Provider Bundle

• implement interface• register service

• Client Bundle• find in registry• react when registered/unregistered

• Core interfaces• ServiceRegistration• ServiceReference• ServiceTracker

• Often registered/used using• Spring DM• Declarative Services• Blueprint

Page 16: OSGi & Blueprint

Limitations of OSGi• Runs on a single VM• Hardware Limitations (processor arch and memory)• Does not support non java services or applications• Inefficient Concurrency Programming• Limit to Grow

Page 17: OSGi & Blueprint

Apache Felix Karaf• Introduction• Installation• Command shell• Hot-deployment• Web console

Page 18: OSGi & Blueprint

IntroductionApache Felix Karaf• A flexible OSGi-based server runtime• Choice of OSGi Framework implementation:

• Equinox• Apache Felix

• Manage the container using• Command shell• Web console• JMX

• Some other features• Provisioning through feature descriptors• Applications

• Spring DM and Blueprint• Hot-deployment

Page 19: OSGi & Blueprint

Installation• Download Apache Karaf and Maven:

• Apache Karaf : http://karaf.apache.org/index/community/download.html

• Apache Maven : http://maven.apache.org/download.cgi

• Path Setup:

• $KARAF_HOME• M2 Setup:

• $M2_HOME• Maven configuration for Karaf

• Maven repository setup in karaf config

Page 20: OSGi & Blueprint

Command ShellStarting Karaf Command Shell

• start - Start the karaf container• karaf - Start the karaf container and connect console• client - Connect to the karaf console

• Unix-like TAB-completion, |, grep, cat, ...

Page 21: OSGi & Blueprint

Command Shell : osgiCommands to interact with OSGi Framework

• osgi:shutdown to stop container• osgi:list to show bundles• osgi:headers $id to show bundle metadata• osgi:ls $id to show bundle services

Page 22: OSGi & Blueprint

Command Shell : osgiCommands to interact with bundles

• osgi:install• Install from URL• file:$bundlepath, http:, mvn:$group.id/$aartifact.id/$version

• osgi:start $id, osgi:stop $id• osgi:update $id• osgi:uninstall $id• osgi:diag $id (Apache Karaf 3.0.0)

Page 23: OSGi & Blueprint

Command Shell : packagesAllows interacting with OSGi PackageAdmin• packages:exports shows lists of exported packages• packages:imports shows

• wired imports• bundles providing the matching export

• Difference between osgi:headers and packages:

Page 24: OSGi & Blueprint

Command Shell : configInteract with ConfigAdmin service • config:list• for changing the runtime config

• config:edit• config:propset, config:propdel, config:propappend• config:update or config:cancel

Page 25: OSGi & Blueprint

Command Shell Some other examples

• dev: shell holds some developer tools• log: shell interacts with log service• ssh: shell to work with SSH client and server

Type “help” to see all the commands and their description in karaf.Type “man <command>” to see the details and usage of a command.

Page 26: OSGi & Blueprint

Hot DeploymentKaraf supports deployment of

• Bundles• XML files (Blueprint and Features)

Page 27: OSGi & Blueprint

Web Console

Page 28: OSGi & Blueprint

Blueprint with Apache Aries• Introduction• Service Registration• Service Reference• Listeners• Feature

Page 29: OSGi & Blueprint

Introduction

• OSGi standard for IoC/DI• Inspired by Spring DM (is also the RI)• Aries implementation• XML configuration files• Register beans as services in OSGi Service• Reference other services in OSGi Service Registry

Page 30: OSGi & Blueprint

Service Registration

• Create a bean with class reference • Expose the service with interface reference

Page 31: OSGi & Blueprint

Service Reference

• Create reference for the implementation • Inject to your service

reference-list

reference

Page 32: OSGi & Blueprint

Listeners

• Listener• Registration listener• Reference listener

Registration Listener

Reference Listener

Page 33: OSGi & Blueprint

Feature

• Group of other features and bundles• Easy to maintain dependency in large project• Easy to deploy all the application bundles and related features at one go• Uses maven as repository of bundles and features• Other options apart from maven is file and url as bundle installation

features:addurl mvn:$groupid/$artifactid/$version/xml/featuresfeatures:install $artifactid

Page 34: OSGi & Blueprint

References• http://karaf.apache.org/• http://www.osgi.org/Main/HomePage• https://github.com/karasatishkumar/osgi-labs• https://github.com/seijoed/osgi-starter• http://www.hascode.com/2010/07/how-to-create-a-simple-osgi-web-application-using-maven/• http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.osgifep.multiplatform.doc%2Ftopics

%2Fca_blueprint_dynamism.html• http://fusesource.com/docs/esb/4.2/deploy_osgi/DeployFeatures-Create.html

Page 35: OSGi & Blueprint

Questions???

Page 36: OSGi & Blueprint

Thanks

Kara Satish Kumarhttps://www.linkedin.com/in/techgeeksatish