osgi, scripting and rest, building webapps with apache sling

60
OSGi, Scripting, REST Webapps with Apache Sling Carsten Ziegeler | Day Management AG [email protected]

Upload: carsten-ziegeler

Post on 10-May-2015

6.593 views

Category:

Technology


1 download

DESCRIPTION

My Apache Sling presentation from the WJAX 2009 in Munich

TRANSCRIPT

Page 1: OSGi, Scripting and REST, Building Webapps With Apache Sling

OSGi, Scripting, RESTWebapps with Apache Sling

Carsten Ziegeler | Day Management AG

[email protected]

Page 2: OSGi, Scripting and REST, Building Webapps With Apache Sling

2

About• Member of the ASF– Sling, Felix, Cocoon, Portals, Sanselan,

Excalibur, Incubator– PMC: Felix, Portals, Sling, Incubator,

Excalibur (Chair)• RnD Team at Day Software• Article/Book Author, Technical

Reviewer• JSR 286 Spec Group (Portlet API 2.0)

Page 3: OSGi, Scripting and REST, Building Webapps With Apache Sling

3

Agenda – Apache Sling1 Motivation2 Content and JCR3 Web and REST4 Scripting5 OSGi6 Outlook

Page 4: OSGi, Scripting and REST, Building Webapps With Apache Sling

4

1 Motivation1 Motivation

Apache Sling

Page 5: OSGi, Scripting and REST, Building Webapps With Apache Sling

5

• Publish huge amount of information– Different types– Highly dynamic– Different output formats

• Web 2.0 ready– Collaboration and integration

• Fast changing requirements– Rapid prototyping and development– Dynamic, extensible but maintainable

Web Challenges

Page 6: OSGi, Scripting and REST, Building Webapps With Apache Sling

6

• Publish huge amount of information– Different types– Highly dynamic– Different output formats

• Web 2.0 ready– Collaboration and integration

• Fast changing requirements– Rapid prototyping and development– Dynamic, extensible but maintainable

Web Challenges

JCR

RESTROA

OSGi

Scripting

Page 7: OSGi, Scripting and REST, Building Webapps With Apache Sling

7

• Web framework• Java content repository (JCR)• ROA / REST• Scripting inside• OSGi• Apache Open Source top level project

– http://sling.apache.org• Driving force behind several OSGi

related projects at Apache

5th Winner JAX Innovation Award 2008

Apache Sling – The Fun Is Back

Page 8: OSGi, Scripting and REST, Building Webapps With Apache Sling

8

Apache Sling Powered Site

Page 9: OSGi, Scripting and REST, Building Webapps With Apache Sling

9

2 Content/JCR2 Content/JCR

Apache Sling

Page 10: OSGi, Scripting and REST, Building Webapps With Apache Sling

10

Content Is Important• Apache Sling makes handling content

easy• Java content repository (JCR)

– Several different sources possible• Direct mapping: URL -> content

– Configurable– http://myserver.domain/products/sling

.html• Flexible rendering

Page 11: OSGi, Scripting and REST, Building Webapps With Apache Sling

11

Content Repository I• Features of an RDBMS

– Structure, integrity– Query, transactions

• Features of a filesystem– Hierarchy, binaries– Locking, access control

• All the other good stuff– Observation, versioning– Unstructured– Multi values, sort order, full text

• Single repository for all content!

Page 12: OSGi, Scripting and REST, Building Webapps With Apache Sling

12

JCR – JSR 170, JSR 283• Content Repository for Java

technology API• (Java) Standard– Supported by many vendors– Used by many products and projects– Several open source solutions

• How do you connect to a CR?• How do you interact with a CR?

JSR 283 – Version 2.0 – Final since September 09

Page 13: OSGi, Scripting and REST, Building Webapps With Apache Sling

13

Sample Application: Slingshot• Digital Asset Management– Hierarchical storage of pictures– Upload– Tagging– Searching– Automatic thumbnail generation

13

Poor man's flickr...

Page 14: OSGi, Scripting and REST, Building Webapps With Apache Sling

14

Slingshot Content Structure

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 15: OSGi, Scripting and REST, Building Webapps With Apache Sling

15

Content Repository II• Hierarchical content– Nodes and properties

• Structured– Nodetypes and typed properties

• And/or unstructured• Fine and coarse-grained• Single repository for all content!

Page 16: OSGi, Scripting and REST, Building Webapps With Apache Sling

16

The Repository Model• Repository: one (or more) workspaces• Workspace contains a tree of items• Item: Node or property• Nodes provide the content structure– May have children

• Actual data is stored as values of properties

• Types and namespaces!

Page 17: OSGi, Scripting and REST, Building Webapps With Apache Sling

17

Root

„Once upon a time..“

Nodes and Properties

Implementation of JCR

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

“Images from Europe“

Properties

Page 18: OSGi, Scripting and REST, Building Webapps With Apache Sling

18

Apache Jackrabbit• JSR 170, JSR 283 reference

implementation• Apache TLP since 2006• Several releases• JCR 2.0 implementation end of 2009• Additional components

Page 19: OSGi, Scripting and REST, Building Webapps With Apache Sling

19

Leverage the standard node types

• Type hierarchy • Content hierarchy

nt:hierarchyNode

nt:folder

nt:file

nt:linkedFile

nt:unstructured

Page 20: OSGi, Scripting and REST, Building Webapps With Apache Sling

20

Bottom-up modeling: Node types

slingshot:photo > nt:file- slingshot:description (string)- slingshot:location (string)- slingshot:tags (string[])

slingshot:album > nt:folder- slingshot:description (string)- slingshot:date (date)

slingshot:tag- slingshot:description (string)

Page 21: OSGi, Scripting and REST, Building Webapps With Apache Sling

21

Top-down modeling: Hierarchy

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 22: OSGi, Scripting and REST, Building Webapps With Apache Sling

22

3 ROA and REST3 ROA and REST

Apache Sling

Page 23: OSGi, Scripting and REST, Building Webapps With Apache Sling

23

Resource Oriented Architecutre• Piece of information is a resource

– News entry, product, photo...– (Descriptive) URI

• Stateless– Request contains all relevant

information• Use HTTP

– Methods (GET, POST) for operations

Page 24: OSGi, Scripting and REST, Building Webapps With Apache Sling

24

REST with Apache Sling• Default behaviour for GET• Creating/Updating content through

POST– Default behaviour

• Additional operations/method• Resource-first request processing!

Page 25: OSGi, Scripting and REST, Building Webapps With Apache Sling

25

Resource• Sling's abstraction of the thing

addressed by the request URI– Usually mapped to a JCR node– File system, database...

• Properties of resources– Path, e.g. JCR Item path– Type, e.g. JCR node type– Metadata, e.g. last modification date

Page 26: OSGi, Scripting and REST, Building Webapps With Apache Sling

26

• URI Decomposition– Resource and representation

• Content retrieved from repository• Rendering based on content type

Resource-first Request Processing

/slingshout/albums/travel.print.a4.html

Resource Path Selectors Extension

Page 27: OSGi, Scripting and REST, Building Webapps With Apache Sling

27

Basic Request Processing Steps• URI decomposition• Resolve the resource (using URI)• Resolve rendering script– Source: resource type– Scripts wrapped by generic servlet

• Create rendering chain– Configurable (servlet) filters– Rendering servlet

• Invoke rendering chain

Page 28: OSGi, Scripting and REST, Building Webapps With Apache Sling

28

Resource Resolver I• Tasks:– Finding Resources– Getting Resources– Simplification of Query Execution

Page 29: OSGi, Scripting and REST, Building Webapps With Apache Sling

29

Resource Resolver II• Gateway for resources• Abstracts the path resolution• Abstracts access to the persistence

layer(s)• Configurable

– Mappings (Multi-site mgmt, beautify paths)

Page 30: OSGi, Scripting and REST, Building Webapps With Apache Sling

30

Resource-First Request Processing

Resource Resolver

Script Resolver

Script

request

response

Resource (resource type)

Page 31: OSGi, Scripting and REST, Building Webapps With Apache Sling

31

4 Scripting4 Scripting

Apache Sling

Page 32: OSGi, Scripting and REST, Building Webapps With Apache Sling

32

Scripting Inside• It's your choice– JSP, servlet, ESP, Scala– javax.script (Apache BSF)– own script handlers

• Scripts stored in OSGi bundles or repository• Scripts are searched at configurable locatiosn• Default servlets (or scripts)

– JSON, XML– Registerable– Fallback / Last Ressort

Page 33: OSGi, Scripting and REST, Building Webapps With Apache Sling

33

Script Resolving I• Path to script is built from ...– Configured search paths ( /apps, /libs )– Resource type converted to path

( slingshot/Album )– Selector string ( print/a4)– Request method & extension• GET --> Request URL Extension ( html )• else --> Method Name ( POST, PUT, ... )

Page 34: OSGi, Scripting and REST, Building Webapps With Apache Sling

34

Script Resolving Example• URI:

/slingshot/albums/travel.print.a4.html• Resource: /slingshot/albums/travel• Resource Type: slingshot:Album• Script for GET:– /apps/slingshot/Album/print/a4/html.*

• Script for POST:– /libs/slingshot/Album/print/a4/POST.*

Page 35: OSGi, Scripting and REST, Building Webapps With Apache Sling

35

Script Resolving II• Scripts are searched by best matching

– /apps/slingshot/Album/print/a4/html.*– /libs/slingshot/Album/print/a4/html.*– /apps/slingshot/Album/html.*– /libs/slingshot/Album/html.*

• Resource has a type and a super type– Script inheritance– Default script (JSON...)

Page 36: OSGi, Scripting and REST, Building Webapps With Apache Sling

36

Powerful Scripting with Includes I

Resource Resolver

Script Resolver

Script

Request: /slingshot/albums/travel.html

response

Resource, resource type: slingshot:Album

Script: /libs/slingshot/Album/html.jsp

Generates main htmlsling:include with selectors

Page 37: OSGi, Scripting and REST, Building Webapps With Apache Sling

37

Script (/libs/slingshot/Album/html.jsp)

Powerful Scripting with Includes II

<sling:include resource="<%= current %>" addSelectors="main"/>…

Script Resolver

Request: /slingshot/albums/travel.html

response

Script

Script: /libs/slingshot/Photo/tree.html.jsp

for(current : ChildResources)

Page 38: OSGi, Scripting and REST, Building Webapps With Apache Sling

38

• ROA• URI decomposition• Resource resolving• Script resolving

– Recursion• Flexible script search algorithm

Resource-first Request Processing

Page 39: OSGi, Scripting and REST, Building Webapps With Apache Sling

39

5 OSGi5 OSGi

Apache Sling

Page 40: OSGi, Scripting and REST, Building Webapps With Apache Sling

40

Runtime Framework: Requirements• Modularization – Modularity is key

– Manage growing complexity– Support (dynamic) extensibility

• Lifecycle management• Configuration management• Dependency management– Modules, services– Different distributions

• Dynamic System Changes

Page 41: OSGi, Scripting and REST, Building Webapps With Apache Sling

41

OSGi in 5..ehm..1 Minute

• Specification of a framework• Module concept (bundles) with lifecycle• Simple but powerful component model

– Lifecycle management– Publish/Find/Bind service registration

• Dynamic!• Uses the concept of bundles

Page 42: OSGi, Scripting and REST, Building Webapps With Apache Sling

42

An OSGi Bundle• Leverages the Java packaging

mechanism: JAR files• Contains Java classes and resources• Additional meta-data– Implicit dependencies to other bundles– Package imports/exports

Page 43: OSGi, Scripting and REST, Building Webapps With Apache Sling

43

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

Page 44: OSGi, Scripting and REST, Building Webapps With Apache Sling

44

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

Page 45: OSGi, Scripting and REST, Building Webapps With Apache Sling

Dynamic Services• OSGi Declarative Services Spec– XML Configuration (in a bundle)– Publishing services– Consuming services• Policy (static,dynamic), cardinality (0..1, 1..1,

0..n)– Default configuration– Service Lifecycle management

• Various Implementations– Apache Felix SCR

Page 46: OSGi, Scripting and REST, Building Webapps With Apache Sling

Config Admin and Metatype• OSGi Config Admin– Configuration Manager– Persistence storage– API to retrieve/update/remove configs– Works with Declarative Services

• OSGi Metatype Service– Description of bundle metadata– Description of service configurations

• Various Implementations– Apache Felix (Reference Implementation)

Page 47: OSGi, Scripting and REST, Building Webapps With Apache Sling

Apache Felix• Top-level project (March 2007)• Healthy and diverse community• OSGi R4 (R4.2) implementation– Framework (frequent releases)– Services (continued development)

• Tools– Maven Plugins, Web Console, iPojo

• New sub project– Karaf : Runtime Environment (OSGi Kernel)– Sigil: OSGi tooling

Page 48: OSGi, Scripting and REST, Building Webapps With Apache Sling

Contributions to Apache Felix• Declarative service• Config admin• Metatype service• Preferences service• Web console (!)• Maven SCR Plugin (!)

Page 49: OSGi, Scripting and REST, Building Webapps With Apache Sling

Apache Sling Runtime• Uses Apache Felix• Runtime: Apache Sling Launchpad

– Might be merged with Felix Karaf• Two flavours

– Standalone Java Application– Web application

• But Sling can be deployed in any OSGi framework!

Page 50: OSGi, Scripting and REST, Building Webapps With Apache Sling

50

Standalone Java Application• One single executable JAR file• Small Launcher• Starts OSGi Framework (Apache

Felix)• Uses Jetty in an OSGi Bundle

Page 51: OSGi, Scripting and REST, Building Webapps With Apache Sling

51

Web Application• Extends Standalone Application– Replaces Main with a Servlet

• Uses a bridge to connect Sling to the Servlet Container

Page 52: OSGi, Scripting and REST, Building Webapps With Apache Sling

52

Facts about Sling• Sling API– No reference to JCR API– Uses resource abstraction

• 36 maintained bundles (many optional)– Over 25 additional bundles

• Highly modular and runtime configurable– OSGi framework– Everything is a OSGi bundle

• ConfigAdmin, Declarative Services, Metatype• Felix Web Console

Page 53: OSGi, Scripting and REST, Building Webapps With Apache Sling

53

6 Outlook6 Outlook

Apache Sling

Page 54: OSGi, Scripting and REST, Building Webapps With Apache Sling

54

Current State• Apache Sling is a top level project• New convenience Release

– Frequent releases of various bundles• Demo Applications• Increasing interest -> Increasing

community

Page 55: OSGi, Scripting and REST, Building Webapps With Apache Sling

55

Development• Easy to get started

– Add content, add script, add more content

• Very flexible and dynamic• Tooling– Maven Plugins– Apache Felix SCR Plugin– Additional Sling Plugins– But not tied to Maven of course :)

Page 56: OSGi, Scripting and REST, Building Webapps With Apache Sling

56

And remember...• (Nearly) Everything is content– Application content– HTML pages, CSS and JavaScript files,

static images– Documentation, resource bundles, etc.– With versioning, export/import, full text

search, etc.

Page 57: OSGi, Scripting and REST, Building Webapps With Apache Sling

57

• Publish huge amount of information– Different types– Highly dynamic– Different output formats

• Web 2.0 ready– Collaboration and integration

• Fast changing requirements– Rapid prototyping and development– Dynamic, extensible but maintainable

Manage the Web Challenges...

Page 58: OSGi, Scripting and REST, Building Webapps With Apache Sling

58

with Apache Sling: State of the Art• Modularity and dynamics (OSGi)• Content Management (JCR)• Resource oriented architecture

(REST)• Scripting inside

Page 59: OSGi, Scripting and REST, Building Webapps With Apache Sling

59

Apache Sling – The Fun is Back!• Web Framework• Java Content Repository• REST• Scripting inside• OSGi• Apache Open Source project• Check it out today!

Page 60: OSGi, Scripting and REST, Building Webapps With Apache Sling

60

Thanks for your attention!

Q&A

Apache Sling