26
Maven SCR Plugin Developing OSGi with the Felix SCR Plugin Carsten Ziegeler [email protected] Day Software

Maven SCR Plugin (ApacheCon EU 2008 - FFT)

Embed Size (px)

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.

Citation preview

Page 1: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

Maven SCR Plugin

Developing OSGi with the Felix SCR Plugin

Carsten [email protected]

Day Software

Page 2: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 3: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

MotivationComponent Based (Web)Application

Bundle A

Public API

Private Classes

Components and Services

S1

S2C1

Bundle B

Private Classes

Components and Services

C2

C3

Page 4: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 5: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 6: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 7: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 8: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 9: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 10: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 11: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 12: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 13: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 14: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 15: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

Developing with Apache Felix

• Maven 2• Maven Bundle Plugin• Maven SCR Plugin

15

Page 16: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

Example Service

• Registering a servlet in a running OSGi environment

• Using provided services– LogService for logging– HttpService for registering servlets

16

Page 17: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 18: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 19: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 20: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 21: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 22: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

Registering a Service/** * @scr.component * @scr.service interface=“org.osgi.service.log.LogService“ */ public class SimpleLogService implements LogService { ...}

22

Page 23: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 24: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 25: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

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

Page 26: Maven SCR Plugin (ApacheCon EU 2008 - FFT)

Q&A

26