nprg044: osgi framework
DESCRIPTION
NPRG044: OSGi framework. michal malohlava michal.malohlava @d3s.mff.cuni.cz. Step #1: Download Eclipse RCP http://www.eclipse.org/downloads/. Do you use JARs?. JAR advantages and disadvantages. + Includes class files and additional resources + Deployment No information hiding - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/1.jpg)
CHARLES UNIVERSITY IN PRAGUE
http://d3s.mff.cuni.cz
faculty of mathematics and physics
NPRG044:OSGi framework
michal [email protected]
![Page 3: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/3.jpg)
3
Do you use JARs?
![Page 4: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/4.jpg)
4
JAR advantages and disadvantages
Michal Malohlava, NPRG044, OSGi framework
+ Includes class files and additional resources+ Deployment
- No information hiding- No dynamicity- Cannot specify required JARs- No versioning
OK, it’s not completely true, but JARs classpath
is almost entirely useless
![Page 5: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/5.jpg)
5
Common Java classloading
Michal Malohlava, NPRG044, OSGi framework
Cannot handle different
versions of same class
The picture was taken from the book “OSGi in Practice” written by Neil Bartlett See http://njbartlett.name/osgibook.html
![Page 6: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/6.jpg)
6
J2EE classloading
Michal Malohlava, NPRG044, OSGi framework
The classloader has precedence over
classloaders below => all applications
have to use the same version of the shared
library
The picture was taken from the book “OSGi in Practice” written by Neil Bartlett See http://njbartlett.name/osgibook.html
![Page 7: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/7.jpg)
7
OSGi
Michal Malohlava, NPRG044, OSGi framework
OSGi is a specificationOpen Service Gateway InitiativeCurrent version R4.3 (see http://www.osgi.org/)3 parts + Java API + execution environment specification
CoreCompendiumEnterprise
SpecifiesFrameworkModulesProvided servicesAdditional functionality
![Page 8: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/8.jpg)
8
OSGi framework
Michal Malohlava, NPRG044, OSGi framework
Framework to build modular applications“LEGO principle”Fine-grained modules which are
ReusableScalableIsolatedBringing separation of concepts
Modules should be “easily” testable,manageable, maintainable, repairable, exchangeable
Bringing abstraction
![Page 9: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/9.jpg)
9
OSGi applications
Michal Malohlava, NPRG044, OSGi framework
Existing applicationsBMW service platformEclipseVirgo server (Spring dm Server)GlassFish J2EE application serverIBM WebSphere J2EE application serverNewtonJBoss, JOnASApache Karaf
UsersBombardier, Volvo, Siemens, BMW, IBM, Red Hat, Siemens AG, NEC, Oracle
![Page 10: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/10.jpg)
10
OSGi implementations
Michal Malohlava, NPRG044, OSGi framework
Open sourceEclipse Equinox
Many extensions of OSGi, e.g. bundle aspects, extension points
Apache FelixBased on Oscar (implementation of OSGi R3)Compliant to OSGi specification R4.2
KnopflerfishConcierge
Implementation of OSGi R3, optimized for embedded devices
CommercialProSyst, Knopflerfish Pro
![Page 11: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/11.jpg)
11
OSGi framework conceptual architecture
Michal Malohlava, NPRG044, OSGi framework
The picture was taken from the book “OSGi in Practice” written by Neil Bartlett See http://njbartlett.name/osgibook.html
![Page 12: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/12.jpg)
12
OSGi Basic concepts
Michal Malohlava, NPRG044, OSGi framework
BundleModuleUnit of deployment
ServiceCommunication between components
![Page 13: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/13.jpg)
13
Bundle
Michal Malohlava, NPRG044, OSGi framework
Unit of deploymentClassical JAR with meta-information
Class files,Additional resources (images, videos, source code, …)META-INF, OSG-INF directories containing meta-information
Bundle is versionedMajor, minor, micro, qualifier (1.0.3_rc2)Multiple versions at runtime are allowed
Bundle can export/hide packagesRecommended practice: ”Exposing only API not implementation”
Declarative dependenciesBundlesPackages
Range of version [1.0, 2.0)
![Page 14: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/14.jpg)
14
Bundle meta-information
Michal Malohlava, NPRG044, OSGi framework
Manifest META-INF/MANIFEST.MFManifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: LogTargetBundleBundle-Activator: LogTargetActivatorBundle-SymbolicName: cz.cuni.mff.d3s.LogTargetBundleBundle-Version: 1.0.0.qualifierBundle-Vendor: D3S MFF UKBundle-RequiredExecutionEnvironment: JavaSE-1.6Import-Package: cz.mff.cuni.d3s.nprg044.tut1.test01.api, org.osgi.framework;version="1.5.0", org.osgi.service.component;version="1.1.0", org.osgi.service.log;version="1.3.0“Service-Component: OSGI-INF/componentOne.xml, OSGI-INF/factory.xml
Each line is limited 72bytes
by design of JVM
![Page 15: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/15.jpg)
15
Bundle dependencies
Michal Malohlava, NPRG044, OSGi framework
Export packagesList all of packages + versions + attributesFine grained package filtering
Exclude,include, parameters
Import packageRequire specific version(s)
e.g. [1.0, 2.0)
Resolution: optional/mandatory
Require bundleNot recommended because it restricts further changes in API
Export-Package: cz.*;exclude=”*Impl”
Import-Package: cz.mff.*;version=”[1.0,1.3.1)”;resolution=optional
Require-Bundle: logger-api-bundle
![Page 16: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/16.jpg)
16
MANIFEST.FM headers
Michal Malohlava, NPRG044, OSGi framework
Bundle-ClassPathWay to bundle 3rd party JAR libraries
Bundle-ActivatorName of class implementing BundleActivatorThe class is called when the bundle is activated
Bundle-SymbolicNameBundle ID
Bundle-Version1.0.3.qualifier (qualifier corresponds to timestamp)
Bundle-RequiredExecutionEnvironmentEnforces the execution context
DynamicImport-PackageOn-the-fly import
Bundle-NativeCodeImport .so, .dll
![Page 17: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/17.jpg)
17
Bundle lifecycle
Michal Malohlava, NPRG044, OSGi framework
The picture was taken from the book “OSGi in Practice” written by Neil Bartlett See http://njbartlett.name/osgibook.html
![Page 18: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/18.jpg)
18
Bundle lifecycle - activation
Michal Malohlava, NPRG044, OSGi framework
Manage bundle lifecycle
class BundleActivatorvoid start(BundleContext ctx)
Register service, listeners, look for servicesvoid stop(BundleContext)
Stop trackers, listeners, …
class BundleContextPropertiesServicesBundlesFiltersListeners
public class SimpleLogTargetActivator extends Activator {
@Override public void start(BundleContext context){ /* ... */ }
@Override public void stop(BundleContext context) { /* ... */ }}
![Page 19: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/19.jpg)
19
OSGi classloading
Michal Malohlava, NPRG044, OSGi framework
The picture was taken from the OSGi wikiSee http://wiki.osgi.org/wiki/Main_Page
![Page 20: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/20.jpg)
20
OSGi classloading
Michal Malohlava, NPRG044, OSGi framework
Separated class loader per bundleClassloaders do not compose a tree, but a general graph
Lookup orderParent
only for classes from java.* package
Imported packagesRequired bundlesLocal bundleclasspath
The picture was taken from the book “OSGi in Practice” written by Neil Bartlett See http://njbartlett.name/osgibook.html
![Page 21: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/21.jpg)
21
Bundle classpath
Michal Malohlava, NPRG044, OSGi framework
Bundle classpath is composed of classes fromImported packagesProvisions of required bundlesLocal classpath specified via Bundle-Classpath
![Page 22: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/22.jpg)
22
Demo #01
Michal Malohlava, NPRG044, OSGi framework
Download Eclipse RCPhttp://www.eclipse.org/downloads/
Create a simple bundle with activatorVia wizard in “New > Project > …”
Bundle the bundle
Deploy bundleCreate new OSGi launch configuration & launch it
Package org.eclipse.osgi is required to be selectedUse “Add required bundles”
Observe its state in the console
![Page 23: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/23.jpg)
23
OSGi Console
Michal Malohlava, NPRG044, OSGi framework
Important commandshelpss
Display installed bundlesservices
Display published servicesstatusexit
Shutdown OSGi frameworkstart/stop <bundle-id>Update <bundle-id>packages
Shows exported packagesdiag
Run diagnostic
![Page 24: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/24.jpg)
24
Service
Michal Malohlava, NPRG044, OSGi framework
Communication layer for bundlesWell-defined communication pointsInherent dynamic nature
Can appear/disappear any time at runtime
Multiple providers can provide the same servicesThe service has additional properties which can for example specify its priority
ServiceService is an object registered by a bundle in a ServiceRegistry
ProgrammaticallyDeclaratively
Service has associated propertiesE.g., service.ranking
![Page 25: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/25.jpg)
25
Registering service (1)
Michal Malohlava, NPRG044, OSGi framework
Programmatically in BundleActivator
ProblemsCode
Services semantics is spread over codedependencies, properties, implementation v. provided interface
public void start(BundleContext context) { SimpleLogTargetImpl logTargetImpl = new SimpleLogTargetImpl();
registration = context.registerService(ILogTarget.class.getName(),logTargetImpl,null);
}
![Page 26: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/26.jpg)
26
Registering service (2)
Michal Malohlava, NPRG044, OSGi framework
Declarative services (DS)
DeclarativelyServices provided by components
Automated service management by DS framework
Dependency injection of required servicesLife-cycle management
<scr:component name="logger-component“ enable=“true”
activate=“activate”> <implementation class="cz.cuni...LoggerImpl"/> <service> <provide interface="cz...ILogger"/> </service></scr:component>
![Page 27: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/27.jpg)
27
Service components
Michal Malohlava, NPRG044, OSGi framework
Component is a normal Java class contained within a bundle
Defined in separated XML file in OSGI-INFdirectory
MANIFEST.FMHas tocontain component file reference: Service-Component: OSGI-INF/component.xml
ActivationDeclared method
ComponentContext, BundleContext, Map as parameters
Service providerSpecify name of provided service
<scr:component name="logger-component“ activate=“activate”> <implementation class="cz.cuni...LoggerImpl"/> <service> <provide interface="cz...ILogger"/> </service></scr:component>
![Page 28: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/28.jpg)
28
Demo #02
Michal Malohlava, NPRG044, OSGi framework
Implement a bundle with API
Implement two bundles implementing API
Register API servicesProgrammaticallyDeclaratively
Launch configuration has to contain ‘org.eclipse.equinox.ds’ bundle
Observe provided services in console (command services)
![Page 29: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/29.jpg)
29
Service consumption (1)
Michal Malohlava, NPRG044, OSGi framework
Bundle can search for service which implements specific interfaceSeveral bad solution
context.getService(...)Nasty code with active waiting
Service registry listeners
Recommended solutions (thread-safe)Service trackerComponents
Service reference ref =context.getServiceRef(“cz.bar”);
if (ref!=null) { Bar bar = (Bar) context.getService(ref); if (bar != null) { ... context.ungetService(ref) }}
![Page 30: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/30.jpg)
30
Service tracker – white board pattern
Michal Malohlava, NPRG044, OSGi framework
Services dependenciesContent provider v. consumers
e.g., consume a new service if and only if the specified service appears
“Don't look for content providers, let them to register as services and track for the services”
ServiceTracker capture service life-cyclevia ServiceTrackerCustomizer
Capture process of adding/removing/modifying service
![Page 31: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/31.jpg)
31
Service tracker
Michal Malohlava, NPRG044, OSGi framework
Service TrackerTracking for service
Filters (name, id, property, owning bundle, ...)LDAP syntax (e.g. (&(objectName=”foo”)(property1=”Xyz”)) )
//In Bundle Activator - starttracker = new ServiceTracker(context, ILogger.class.getName(), null);
tracker.open();
// get the service(s)ILogger log = (ILogger)tracker.getService();ILogger log = (Ilogger) tracker.waitForService(1000);
// stop trackingtracker.close();
![Page 32: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/32.jpg)
32
Service tracker
Michal Malohlava, NPRG044, OSGi framework
Construction determines attributes
ServiceTracker methodsopen()/close() – start/stop tracking for a servicegetService()addingService/removedService/modifiedService(ServiceReference rf)
Interface ServiceTrackerCustomizerCan be overridden by user
ServiceTracker(BundleContext context,
java.lang.String clazz, ServiceTrackerCustomizer customizer)
![Page 33: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/33.jpg)
33
Service consumption (2)
Michal Malohlava, NPRG044, OSGi framework
Declaratively via service components
Service referenceNameInterfacesBind/unbind methodTargetPolicy: static/dynamicCardinality: M..N
1..1 – if multiple services is accessible then the one with the highest service.ranking is used
<scr:component name=”getServiceComp”> <implementation class=”GetLoggerService”> <reference name=”log” interface=”org.osgi...LogService” bind=”setLog” unbind=”unsetLog”</scr:component>
![Page 34: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/34.jpg)
34
Services lookup
Michal Malohlava, NPRG044, OSGi framework
Lookup strategyLook for service during component activation
Event strategyLet the DS framework to inject the service via defined methodsBind/unbind attributes of reference declaration
<?xml version="1.0" encoding="UTF-8"?><scr:component name="example.listen" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"> <implementation class="com.acme.LogLookupImpl"/> <reference name="LOG" interface="org.osgi.service.log.LogService"/></scr:component>
public class LogLookupImpl { private void activate(ComponentContext ctxt) { LogService log = (LogService) ctxt.locateService("LOG"); }}
![Page 35: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/35.jpg)
35
Component factories
Michal Malohlava, NPRG044, OSGi framework
A component can be declared as a factoryComponentFactory service is registered
newInstance(Dictionary d) method
The user tracks for Component Factory service and create a new instance
A component factory can provide a serviceRegistered for each created instance
![Page 36: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/36.jpg)
36
Demo #03
Michal Malohlava, NPRG044, OSGi framework
Write a new bundle with a tester component consuming declared services in demo #02
Possiblescenario
Try to callupdate commandin console
![Page 37: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/37.jpg)
37
OSGi services
Michal Malohlava, NPRG044, OSGi framework
Logging (LogService)Http (HttpService)
Exposing registered servletsEvent
Messaging Producer <-> ConsumerDevice managerDiagnostics/Monitoring
JMXApplication manager
Application package – set of resource (bundles, data,...)Can be deployed/install
Location/measurement servicesRemote services
![Page 38: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/38.jpg)
38
Demo #04
Michal Malohlava, NPRG044, OSGi framework
Use HttpService as an additional implementation of ILogTargetIntroduce a new bundle with a component exposing ILogTarget and requiring HttpService
Register HTTP servlet
Launch configuration has to introduce web-server bundles (e.g., Jetty). Don’t forget on
javax.servletorg.eclipse.equinox.http.servletorg.eclipse.equinox.http.jetty
Specify JVM property: -Dorg.osgi.service.http.port=8080
![Page 39: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/39.jpg)
39
OSGi 4.2 features
Michal Malohlava, NPRG044, OSGi framework
Framework launchingRemote servicesBlueprint servicesBundle trackerService hooksConditional permissions
Enterprise features – bundling (WAR), JPA, JNDI, JDBC integration
![Page 40: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/40.jpg)
40
OSGi 4.3 features
Michal Malohlava, NPRG044, OSGi framework
Introduction of generics into OSGi APICapabilitiesRequirementsAdapt concept
Bundle can be adapted to another typeWeaving hook
Bytecode modificationResolver, bundle, service events hooks
![Page 41: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/41.jpg)
41
Bundles repositories
Michal Malohlava, NPRG044, OSGi framework
OBRhttp://bundles.osgi.orgOSGi compendium implementation
Springhttp://sigil.codecauldron.org/spring-external.obrhttp://sigil.codecauldron.org/spring-release.obr
Knopflerfishhttp://www.knopflerfish.org/repo/bindex.xml
![Page 42: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/42.jpg)
42
Resources
Michal Malohlava, NPRG044, OSGi framework
OSGi specificationhttp://www.osgi.org/
OSGi tooling http://en.wikipedia.org/wiki/OSGi-Tooling
NPRG044 source codehttp://code.google.com/a/eclipselabs.org/p/nprg044-eclipse-platform/
![Page 43: NPRG044: OSGi framework](https://reader036.vdocument.in/reader036/viewer/2022062814/568166e9550346895ddb2f5c/html5/thumbnails/43.jpg)
43
OSGi tools
Michal Malohlava, NPRG044, OSGi framework
Pax
Bnd
Scribids