A (very) quick introduction to OSGi for Java developers. These slides are meant to be a quick overview of the technology and make you understand how useful it can be.
2. Why ?
• Most mature Java “plug‐in” technology
• Allows hot deployment/un‐deployment
• Manages dependencies between plug‐ins (aka
OSGi bundles)
• Offers (opKonal) addiKonal services (HTTP,
configuraKon, etc...)
3. What is an OSGi Bundle ?
JAR
• Basically a JAR with:
MANIFEST.MF
–AddiKonal stuff in it’s Bundle-Name: Hello World
manifest file
Bundle-SymbolicName: org.example.helloworld
Bundle-Description: A Hello World bundle
Bundle-ManifestVersion: 2
Bundle-Version: 1.0.0
–A class called an Bundle-Activator: org.example.Activator
Export-Package:
org.example.helloworld;version="1.0.0"
acKvator to register Import-Package: org.osgi.framework;version="1.3.0"
services Classes
–Your code and resources org.example.AcKvator
...
–(OpKonally)Other JARs
for legacy (migraKon) (OpKonal) JARs
legacy‐1.0.jar
purposes ...
4. OSGi Services
Services are all implemented using one or more OSGi bundles
Logging Component RunKme Wire Admin
ConfiguraKon Admin Deployment Admin XML Parser
Device Access Event Admin Measurement and State
User Admin ApplicaKon Admin My Service 1
IO Connector HTTP Service My Service 2
Preferences UPnP Device Service
5. Bundle Activator
• Simply iniKalizes
and shuts down the package org.example;
bundle (if needed)
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
• Can register with
private BundleContext context;
public void start(BundleContext context) throws Exception {
System.out.println("Starting: Hello World");
exisKng services }
this.context = context;
public void stop(BundleContext context) throws Exception {
• Helper classes are }
System.out.println("Stopping: Goodbye Cruel World");
this.context = null;
available to make
}
things easier
6. OSGi and Maven
• Very good match as both know about
dependencies
• Maven project == OSGi bundle
• Maven Felix Plugin does the packaging for you !
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>org.osgi.service.log</Export-Package>
<Private-Package>org.apache.felix.log.impl</Private-Package>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Activator>${pom.artifactId}.impl.Activator</Bundle-Activator>
<Export-
Service>org.osgi.service.log.LogService,org.osgi.service.log.LogReaderService</
Export-Service>
</instructions>
</configuration>
</plugin>
7. Learn more...
• OSGi implementaKons
–Eclipse Equinox : hfp://eclipse.org/equinox/ (most
mature, was born out of Eclipse, integrated in
Websphere and Eclipse)
–Apache Felix : hfp://felix.apache.org (best integraKon
with Maven, evolving rapidly, used in Apache Sling and
other Apache projects