Carbon and OSGi Deep Dive
Sameera JayasomaSoftware Architect
WSO2 Meetup18/12/2014
Overview
● What is Carbon?
● OSGi, P2.
● OSGI, P2 and Carbon integration.
● How carbon starts up?
● Carbon and OSGi bundles.
● Bundle pooling and multi-profiles support.
● Carbon and OSGi services.
● Axis2 OSGi integration in Carbon.
● Carbon, Components, Features,
● Carbon as a server building framework.
?
WSO2 Carbon
● Server building framework ○ offers a lean, modular platform to build servers/middleware products
● All WSO2 middleware products are developed based on this core
framework.
● Provides all the key ingredients to build enterprise-class servers.
● Servers built on top of this Carbon are designed as a set of reusable
modules/components.
Pre-Carbon Era
● Before 2008.
● We had,○ Three separate products○ Three code bases○ Three teams○ Similar features were available in these products but NO code has been
shared.
● This model wasn’t a scalable one.
● Therefore we decided to build our products in a
modular/componentized way to solve above issues.
● Realized the need of a solid foundation to achieve this.
OSGi to the rescue!!!
OSGi
● OSGi framework is a dynamic module system and a services platform for Java programming language.
● OSGi defines a set of rules to build modules, to manage their lifecycles and to communicate among these modules at runtime (in-VM SOA).
● Modules(Bundles) in OSGi can be installed, updated and uninstalled without restarting the JVM.
● Carbon has achieved modularity and dynamism through the concepts defined in the OSGi framework.
Bundle
● The unit of modularization in OSGi
● Traditional Java app. is a collection of Jars. But an OSGi based
system is a collection of bundles.
● Bundle is just a JAR with some additional metadata(manifest
headers) in the MANIFEST.MF file
● A java package is the unit of information hiding.
Bundle and Java packages
● Bundles can share packages with other bundles or hide packages.
● Bundles can declare dependencies on java packages exported by
other bundles.
Life Cycle of a Bundle
OSGi Services
● An OSGi service is a java object, registered into OSGi runtime with a
set of properties.
● Any java object can be registered as a service, but typically it should
implements a well-known interface.
● Its a mechanism for bundles to collaborate at runtime.
● OSGi services provides in-VM SOA model for bundles.
Equinox P2
● A provision platform for OSGi based systems.
● p2 stands for “Provisioning Platform”
● Provides a well defined model for provisioning OSGi bundles
● p2 allows us to manage feature in a controlled manner
○ Install, Uninstall, Revert
○ Based on a web or file based repository
■ Can be hosted internally for an organization
○ Three approaches:
■ Command line
■ Web console
■ Secure remote API
OSGi, P2 and Carbon
● Carbon is tightly coupled with OSGi and P2, if you look at it in a deep
technical level.
● Carbon has achieved modularity and dynamism through the module
system and service platform concepts defined in OSGi spec.
● Everything in Carbon is a bundle.
● These bundles share Java packages among each other, some of
these bundles share Java objects as OSGi services to communicate
with each other.
OSGi, P2 and Carbon
● P2 has enabled the provisioning aspect in Carbon.
● P2 brings the concept of a "Feature" which means a logically related
set of bundles. In other words you can think of it as a functionality in
the system. E.g.
○ Service Management Feature.
○ Registry Feature.
○ JMS Transport Feature.
● You can combine a set of these features into a "Repository" and host
it online or as a distribution to download.
● With P2 you can easily install, update, uninstall features from
repository and customize products.
How Carbon Starts up?...
How Carbon Starts up?
● Carbon Kernel contains many different OSGi bundles which needs to be
loaded up and activated in a particular order to complete Carbon
Initialization.
○ e.g. opening ports only after the server initialization is completed.
● We cannot rely on the OSGi bundle startup order as the startup order is
unpredictable.
How Carbon Starts up?...
OSGi Repository in Carbon
${CARBON.HOME}/repository/components
Everything in Carbon is an OSGi Bundle
● Static resources (java packages/classes) are shared with the bundles using the Export-Package manifest header.
● Carbon APIs classes shared while hiding implementation classes.
● Import-Package is used to declared dependencies to java packages exposed from other bundles.
● Private packages (which are not supposed to be shared) are grouped under the package called “internal”○ e.g. org.wso2.carbon.caching.internal
● Usage of “maven-bundle-plugin” to build OSGi bundles.
maven-bundle-plugin
● Allows you to generate OSGi bundles from Maven project. ○ Generate manifest.mf file with the required OSGi manifest headers
● It automatically generate imports and exports if you haven’t specified explicitly. ○ We recommend you to specify the list of of import and export packages
with versions explicitly in the pom.xml.
Jars to Bundles Converter
● Legacy jar files are covered the OSGi bundles on the fly.
● A Useful feature in Carbon which gives a quick and easy way for developers to expose their legacy libraries to OSGi runtime. ○ Installing DB drivers into OSGi runtime.
● But we do not recommend this. ○ Because these converted bundles do not export or import packages
with versions. No way to specify bundle activators etc.
● This conversion happens during the server startup. just before launching the OSGi runtime in Carbon.
Orbit Bundles
● Some third-party libraries are not available as OSGi bundles.
● Hence Carbon orbit bundles project was initiated to wrap these
libraries as OSGi bundles.
● Now it hosts many different orbit bundles required for many different
components in the Carbon platform.
○ Apache Tomcat OSGi bundle,
○ Apache Axis2 bundles,
○ Hazelcast bundle etc.
https://github.com/wso2/orbit
Bundle pooling
● Ability to share a common plugins directory by multiple applications.
● No duplication of content
● No duplicated downloads when upgrading your applications.
● Allows multiple modes/profiles of a single application to co-exists in single distribution
http://wiki.eclipse.org/Equinox_p2_Getting_Started#Bundle_pooling
Multi-profile support
● One download, multiple servers.e.g. sh wso2carbon.sh -Dprofile=profile_name
● Enables a product to run in multiple modes/profiles
● A profile of a product describes the runtime behaviour.
● Each profile contains only the required set of components
○ E.g. A single API Manager distribution can play following roles.
API Publisher
API Subscriber
API Gateway
Auth Server
Multi-profile support...
Demo
Role of OSGi Services in Carbon
● Shares state among modules. ○ e.g. carbon.xml configuration is shared among bundles as an OSGi
service
● Orders the initialization of modules during the server startup.
● Decouples to APIs from implementation classes.
● Means of extending the behaviour dynamically.○ e.g. Registering a Servlet as an OSGi service
Apache Axis2
● A Web Services Engine.
● Java runtime for hosting services.
● Runs standalone or in conjunction Application servers such as
Apache Tomcat.
● Supports both SOAP and REST
● It is extensible.
○ Axis2 Modules.
○ Axis2 Deployers
○ Transports
○ Message Formatters and Message Builders.
○ etc..
Apache Axis2 and Carbon Integration: Motivation
● Facilitate using Apache Axis2 in OSGI based environments
● Improved class loading mechanism for deployed services and other
Axis2 extensions such as Modules.
● Deploying Web services reside in Bundles
● Deploying Axis2 Extensions(Modules, Service Deployers, etc.) reside
in Bundles.
Problems faced
● Existence of splitted packages among axis2 jar files
● Dependencies of Axis2 are not yet available as OSGi bundles.
● Modules are required to be deployed before Web services in Axis2.
One Big Bundle
● Axis2 consists of several application jar files and third party libraries.
○ axis2-kernal.jar
○ axis2-adb.jar and many more..
● Some Java packages are splitted among these jar files.
○ org.apache.axis2.util package is available in both axis2-kernal.jar
and axis2-adb.jar.
● A package can only be exported by a single bundle, if the version is
same.
● Solution: Bundled all these axis2 jars files into single Big Bundle.
Web Services in Bundles
● Implemented a custom Service Deployer to handle Web services
reside in Bundles.
● Used OSGi extender pattern to detect the list of bundles which has
Web services.
● BundleListeners are used listen to bundle events.
● Only the bundles in ACTIVE state are taken into consideration.
● Bundle classloader is set as the Service classloader to give a better
class loading mechanism for Web Services.
Axis2 Extension in Bundles
● Extensions such as Axis2 Modules, Service Deployers, etc. in
bundles are required to be deployed in Axis2.
● Here also, OSGi extender pattern is used.
● Bundles specify a custom manifest header.
○ Axis2Deployer: POJODeployer
○ Axis2Module: rampart
● Filter the list of bundles having Axis2 extensions using these
manifest headers.
Modules before Services
● Problem: In Axis2, modules should be deployed before services.
Otherwise services become faulty and also these faulty services
cannot be recovered.
● In OSGi, bundle starting order is unpredictable.Hence bundles with
Axis2 Services may become ACTIVE before bundles with Axis2
Modules.
● Possibility of services being deployed before modules.
WSO2 Carbon based products
● Product is a distribution artifact which combines Carbon kernel and a set of logically related features.
● Following is a list of products offered by WSO2.
What is a Carbon Component?
● A set of OSGi Bundles.
● Lives in the Carbon Platform. Hence should conform to rules define
in the Carbon Platform.
● Uses Core Carbon Services
○ Via OSGi service registry
○ e.g. Registry Service, UserManager Service, etc.
What is a Carbon Feature?
● In Eclipse world
○ Feature is a grouping of set of logically related plug-ins/ OSGi
bundles
○ Feature can be installed into Eclipse platform using its Update
Manager
What is a Carbon Feature?
● In Carbon world
○ Feature can be thought of as an installable form of one or more
logically related Carbon component
■ Service Management Component
● What you develop
■ Service Management Feature
● What you install
○ Feature is a grouping of one or more logically related Carbon
components
○ Features can be installed into Carbon based product using
Feature Manager.
WSO2 Carbon: Component Architecture
WSO2 Carbon Kernel
WSO2 Carbon: Feature Library
WSO2 Carbon: Distribution Artifacts
WSO2 Carbon: Adaptability
Key Features offered by Carbon
● User Management API
● Registry/Repository API
● Caching API
● Clustering
● Pluggable Transports
● Artifact Deployment Engine
● Ghost Deployment of Artifacts
● Deployment Synchronization
● UI Framework
● Configuration and Context model (CarbonContext API)
● Multi-tenancy
Server Building Framework
http://awaytogarden.com/questions-anyone-or-careful-what-you-ask/
http://www.funlava.com/thank-you-quotes/