maven scr plugin (apachecon eu 2008 - fft)
DESCRIPTION
Presentation from the ApacheCon EU 2008 in Amsterdam. The SCR plugin for Maven from the Apache Felix project is a simple but powerful tool. In just 15min you'll learn how to develop services in Java to be used in an OSGi environment and how to package and deploy them. You should have knowledge about component oriented development for Java; the talk will also cover the bare minimum OSGi knowledge, but this is not an OSGi introduction.TRANSCRIPT
About
• Apache Software Foundation Member– Cocoon, Excalibur, Pluto, Felix, Incubator,
Sling, Sanselan– PMC: Cocoon, Incubator, Portals, Felix,
Excalibur (Chair)
• Senior Developer at Day Software• Article/Book Author, Technical Reviewer• JSR 286 spec group (Portlet API 2.0)
2
MotivationComponent Based (Web)Application
Bundle A
Public API
Private Classes
Components and Services
S1
S2C1
Bundle B
Private Classes
Components and Services
C2
C3
Modularity Requirements
• A bundle contains more than public classes/API– Well defined boundaries (packages)
• A bundle depends on other clasesses/frameworks etc.– Well defined dependencies (packages)
• Dynamic wiring of services• Install/Update/Uninstall• Support for versioning/multi-versions
4
OSGi in 5..ehm..1 Minute
• Specification of a framework• Simple component model• Component lifecycle management• Service registration• Dynamic!• Uses the concept of bundles
5
An OSGi Bundle
• Leverages the Java packaging mechanism: JAR files
• Contains Java classes and resources• Additional meta-data
– dependencies to other bundles– package imports/exports
6
An OSGi Bundle II
• Bundle activator concept– Custom object notified on bundle startup
• Access to service registry– register services/use services
• Automatical wiring of bundles– including classpath
• Solves many modularity problems of todays (web)apps
7
Services
• OSGi offers an API to register services– Service is registered by its interface name(s)– Implementation is bundle private– Several components for same service
possible (from different bundles)
• Bundles can query services– By interface names– With additional filters
8
The OSGi Core
• Minimal but sufficient API for services– Minimal overhead: Good for simple bundles– No support for component management– No support for configuration management– Requires sometimes a lot of Java coding
• Additional (optional) OSGi extensions– Declarative Service Specification – Configuration Admin Service Specification
9
Declarative Service Specification
• XML format for services– Services, implementation, and references
• Automatic registration on bundle startup– Deregistration on bundle stop
• Defines the Service Component Runtime• Usage is very straightforward
– Implementation• requires set/unset methods for references• might contain special (de)activation methods
10
Configuration Admin Service Spec
• Central service for– storing and delivering service configurations– persistent storage
• API for querying and changing configurations– services/refs are updated
• XML meta data description for component configuration
11
Apache Felix
• Open Source implementation of OSGi R4– Framework (Core)– Services (Compendium)
• Package Admin, Start Level, Configuration Admin, Declarative Services, Event Admin, Preferences
– Maven Plugins– Shell and other config tools– OSGi Bundle Repository (OBR)
12
The Maven Bundle Plugin• Creates a JAR with bundle meta data• Meta data
– is calculated (as far as possible)– can be specified in the pom
• Integrates nicely and seamlessly
13
The Maven SCR Plugin
• Generates descriptor files based on annotations– Component, service– References– Class enhancements for simpler usage
• Additional support for the configuration admin– Properties
14
Developing with Apache Felix
• Maven 2• Maven Bundle Plugin• Maven SCR Plugin
15
Example Service
• Registering a servlet in a running OSGi environment
• Using provided services– LogService for logging– HttpService for registering servlets
16
Servlet Service Ipublic class SimpleSlingServlet extends HttpServlet { private LogService log; private HttpService httpService; protected void doGet(....) { // nothing Sling/OSGi specific in this method // 1. Log log.log(LogService.LOG_DEBUG, "Processing request, path info=" + req.getPathInfo()); // 2. Create response ... } ... }
17
Servlet Service IIa/** * @scr.component */ public class SimpleSlingServlet extends HttpServlet { /** @scr.reference */ private LogService log;
/** @scr.reference */ private HttpService httpService;
protected void bindLog(LogService l) { .. }
protected void unbindLog(LogService l) { .. }
18
Servlet Service IIb/** * @scr.component */ public class SimpleSlingServlet extends HttpServlet { /** @scr.reference */ private LogService log;
/** @scr.reference */ private HttpService httpService;
..bind/unbind methods are generated by the plugin!
19
Servlet Service IIIpublic class SimpleSlingServlet extends HttpServlet {
protected void activate(ComponentContext ctx) {
httpService.registerServlet("/test", this,
null, null);
}
protected void deactivate(ComponentContext ctx) {
httpService.unregister("/test“);
}
}
20
Servlet Service IV/** * @scr.property name="path" value="/test" */ public class SimpleSlingServlet extends HttpServlet {
protected void activate(ComponentContext ctx){
String myPath = (String)ctx.getProperties().
get("path");
}
}
21
Registering a Service/** * @scr.component * @scr.service interface=“org.osgi.service.log.LogService“ */ public class SimpleLogService implements LogService { ...}
22
Maven Plugin Usage I<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals><goal>scr</goal></goals>
</execution>
</executions>
</plugin>
23
Maven Plugin Usage II<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Private-Package>
org.apache.sling.examples.impl
</Private-Package>
</instructions>
</configuration>
</plugin>
24
Advice/Summary
• Read the OSGi Spec• Have a look at Apache Felix• Use the provided Maven plugins• The overhead of using OSGi is minimal
– benefit from the advantages– be sure to understand the impact first!
25
Q&A
26