61
& OSGi, Scripting REST Web Development with Apache Sling Carsten Ziegeler | Day Software [email protected]

Apache Sling : JCR, OSGi, Scripting and REST

Embed Size (px)

Citation preview

Page 1: Apache Sling : JCR, OSGi, Scripting and REST

& OSGi, Scripting RESTWeb Development with Apache SlingCarsten Ziegeler | Day [email protected]

Page 2: Apache Sling : JCR, OSGi, Scripting and REST

2

About Carsten Ziegeler

2

• Member of the Apache Software Foundation– Sling, Felix, Cocoon, Portals, Sanselan, Excalibur,

Incubator– PMC: Felix, Portals, Cocoon, Incubator, Excalibur (Chair)

• RnD Team at Day Software• Article/Book Author, Technical Reviewer, Speaker• JSR 286 Spec Group (Portlet API 2.0)

Page 3: Apache Sling : JCR, OSGi, Scripting and REST

3

Agenda– ApacheSling1 Motivation2 Content and JCR3 Web and REST4 Scripting5 OSGi6 Outlook

3

Page 4: Apache Sling : JCR, OSGi, Scripting and REST

4

1 Motivation1 Motivation

4

Page 5: Apache Sling : JCR, OSGi, Scripting and REST

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

5

WebChallenges

Page 6: Apache Sling : JCR, OSGi, Scripting and REST

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

6

WebChallenges

JCR

RESTROA

OSGi

Scripting

Page 7: Apache Sling : JCR, OSGi, Scripting and REST

7

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

– http://incubator.apache.org/sling

7

5th Winner JAX Innovation Award 2008

ApacheSling – TheFun isBack

Page 8: Apache Sling : JCR, OSGi, Scripting and REST

8

: Example ApacheSling Powered Site

Page 9: Apache Sling : JCR, OSGi, Scripting and REST

9

2 Content/JCR2 Content/JCR

9

Page 10: Apache Sling : JCR, OSGi, Scripting and REST

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

10

Page 11: Apache Sling : JCR, OSGi, Scripting and REST

11

Content Repository

11

• Generic application data store• Structured and unstructured content• Support small and large-scale data• Locking, transactions, versioning, observation

and searching

Page 12: Apache Sling : JCR, OSGi, Scripting and REST

12

JCR – JSR 170• 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?

12

JSR 283 – Version 2.0 – scheduled for 2009

Page 13: Apache Sling : JCR, OSGi, Scripting and REST

13

SampleApplication• Digital Asset Management

– Hierarchical storage of pictures– Upload– Tagging– Searching– Automatic thumbnail generation

13

Poor man's flickr...

Page 14: Apache Sling : JCR, OSGi, Scripting and REST

14

Content Repository Features• Hierarchical content

– Nodes and properties

• Structured– Nodetypes and typed properties

• And/or unstructured• Fine and coarse-grained

14

Page 15: Apache Sling : JCR, OSGi, Scripting and REST

15

SampleContent Structure

15

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 16: Apache Sling : JCR, OSGi, Scripting and REST

16

Content Repository Features• Query (XPath, SQL)• Export/Import (XML)• Referential Integrity• Authentication• Access Control• Versioning• Observation• Locking and Transactions (JTA)

16

Page 17: Apache Sling : JCR, OSGi, Scripting and REST

17

Comparison• File System

– Hierarchical, unstructured

• Database– Structured– Referential integrity, transactions

• Content Repository– Advantages of a FS and a database– Plus observation, versioning etc.

17

Page 18: Apache Sling : JCR, OSGi, Scripting and REST

18

) (Content Repository JCR – JSR 170

18

Page 19: Apache Sling : JCR, OSGi, Scripting and REST

19

TheRepository 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!

Implementation of JCR 19

Page 20: Apache Sling : JCR, OSGi, Scripting and REST

20

C o n te n t R e p o s i to r y

W o r k s p a c e A

ab

c

i

gh

j k

= Node

= Property

Root

ed

6.02x1023

„Once upon a time..“ -25

true

Nodesand Properties

Implementation of JCR

20

Page 21: Apache Sling : JCR, OSGi, Scripting and REST

21

ApacheJackrabbit• JSR 170 reference implementation• Apache TLP since 2006• Several releases• JCR 2.0 RI later this year• Additional components

Page 22: Apache Sling : JCR, OSGi, Scripting and REST

22

Leverage thestandard node types• Type hierarchy • Content hierarchy

22

nt:hierarchyNode

nt:folder

nt:file

nt:linkedFile

nt:resource

Page 23: Apache Sling : JCR, OSGi, Scripting and REST

23

: Modeling Content Types

23

my:resource > nt:resource- width, height (long)- format (string)

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

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

my:tag- name (string)- description (string)

Page 24: Apache Sling : JCR, OSGi, Scripting and REST

24

: Modeling Content Hierarchies

24

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 25: Apache Sling : JCR, OSGi, Scripting and REST

25

3 ROA and REST3 ROA and REST

25

Page 26: Apache Sling : JCR, OSGi, Scripting and REST

26

ResourceOriented 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

26

Page 27: Apache Sling : JCR, OSGi, Scripting and REST

27

REST with ApacheSling• Default behaviour for GET• Creating/Updating content through POST

– Default behaviour

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

27

Page 28: Apache Sling : JCR, OSGi, Scripting and REST

28

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

28

Page 29: Apache Sling : JCR, OSGi, Scripting and REST

29

- Resource first Request Processing

/products/sling.print.a4.html

Resource Path Selectors Extension

29

• URI Decomposition••

– Resource and representation

• Content retrieved from repository• Rendering based on content type

Page 30: Apache Sling : JCR, OSGi, Scripting and REST

30

Basic Request Processing Steps• URI decomposition• Resolve the resource

– Source: request URI

• Resolve rendering script– Source: resource type– Scripts are wrapped by a generic script servlet

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

• Invoke rendering chain30

Page 31: Apache Sling : JCR, OSGi, Scripting and REST

31

ResourceResolver• Gateway for resources• Abstracts the path resolution• Abstracts access to the persistence layer(s)• Configurable

– Mappings (Multi-site mgmt, beautify paths)

• Tasks:– Finding Resources– Getting Resources– Simplification of Query Execution

31

Page 32: Apache Sling : JCR, OSGi, Scripting and REST

32

- Resource First Request Processing

Resource Resolver

Script Resolver

Script

request

response

Resource (resource type)

32

Page 33: Apache Sling : JCR, OSGi, Scripting and REST

33

4 Scripting4 Scripting

33

Page 34: Apache Sling : JCR, OSGi, Scripting and REST

34

Scripting Inside• It's your choice

– JSP, servlet, ESP– 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

34

Page 35: Apache Sling : JCR, OSGi, Scripting and REST

35

Script Resolving I• Path to script is built from ...

– Configured search paths ( /apps, /libs )– Resource type converted to path ( my/photo )– Selector string ( print/a4)– Request method & MIME Type

• GET --> Request URL Extension ( html )• else --> Method Name ( POST, PUT, ... )

35

Page 36: Apache Sling : JCR, OSGi, Scripting and REST

36

Script Resolving Example• URI: /products/sling.print.a4.html• Resource: /products/sling• Resource Type: myapp:product• Script for GET:

– /apps/myapp/product/print/a4/html.*

• Script for POST:– /libs/myapp/product/print/a4/POST.*

36

Page 37: Apache Sling : JCR, OSGi, Scripting and REST

37

Script Resolving II• Scripts are searched by best matching

– /apps/myapp/product/print/a4/html.*– /libs/myapp/product/print/a4/html.*– /apps/myapp/product/html.*– /libs/myapp/product/html.*

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

37

Page 38: Apache Sling : JCR, OSGi, Scripting and REST

38

Powerful Scripting with Includes I

38

Resource Resolver

Script Resolver

Script

Request: /albums/travel.html

response

Resource, resource type: my:Album

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

Generates main htmlsling:include with selectors

Page 39: Apache Sling : JCR, OSGi, Scripting and REST

39

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

Powerful Scripting with Includes II

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

Script Resolver

Request: /albums/travel.html

response

Script

Script: /libs/my/Album/tree.html.jsp

Page 40: Apache Sling : JCR, OSGi, Scripting and REST

40

5 OSGi5 OSGi

40

Page 41: Apache Sling : JCR, OSGi, Scripting and REST

41

: RuntimeFramework Requirements• Modularization – Modularity is key

– Manage growing complexity– Support dynamic extensibility

• Lifecycle Management• Configuration Management• Dependency Management

– Modules– Services

• Dynamic System Changes41

Page 42: Apache Sling : JCR, OSGi, Scripting and REST

42

.. .. OSGi in 5 ehm 1Minute• 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

42

Page 43: Apache Sling : JCR, OSGi, Scripting and REST

43

AnOSGi Bundle• Leverages the Java packaging mechanism:

JAR files• Contains Java classes and resources• Additional meta-data

– Implicit dependencies to other bundles– Package imports/exports

43

Page 44: Apache Sling : JCR, OSGi, Scripting and REST

44

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

44

Page 45: Apache Sling : JCR, OSGi, Scripting and REST

45

TheOSGi 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

45

Page 46: Apache Sling : JCR, OSGi, Scripting and REST

Dynamic Services• OSGi Declarative Services Specification

– XML Configuration • Contained in 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 46

Page 47: Apache Sling : JCR, OSGi, Scripting and REST

Config Admin andMetatype• 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 47

Page 48: Apache Sling : JCR, OSGi, Scripting and REST

ApacheFelix• Top-level project (March 2007)

• Healthy and diverse community• OSGi R4 (R4.1) implementation

– Framework (frequent releases)– Services (continued development)– Moving towards upcoming R4.2

• Tools– Maven Plugins, Web Console, iPojo

• New sub project– Karaf : Runtime Environment (Service Mix)

48

Page 49: Apache Sling : JCR, OSGi, Scripting and REST

ApacheSling 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!

49

Page 50: Apache Sling : JCR, OSGi, Scripting and REST

50

StandaloneJavaApplication• One single executable JAR file• Small Launcher• Starts OSGi Framework (Apache Felix)• Uses Jetty in an OSGi Bundle

50

Page 51: Apache Sling : JCR, OSGi, Scripting and REST

51

WebApplication• Extends Standalone Application

– Replaces Command Line Support with a Servlet

• Uses a Bridge to connect Sling to the Servlet Container

51

Page 52: Apache Sling : JCR, OSGi, Scripting and REST

52

Factsabout Sling• Sling API

– No reference to JCR API– Uses resource abstraction

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

• ConfigAdmin, Declarative Services, Metatype• Felix Web Console

52

Page 53: Apache Sling : JCR, OSGi, Scripting and REST

53

Modules

scripting/*

scripting/api

jcr/resource

jcr/jackrabbit

jcr/api sling/core

Sling API

sling/servlets-* sling/threads

sling/i18n

sling/adapter

sling/servlet-resolver

sling/event

sling/scheduler

sling/bundleresource

commons/* Maven 2 Plugins osgi/*

53

Page 54: Apache Sling : JCR, OSGi, Scripting and REST

54

6 Outlook6 Outlook

54

Page 55: Apache Sling : JCR, OSGi, Scripting and REST

55

Current State• Apache Sling is in the Apache Incubator• Second Release: RSN! :)• Demo Applications• Increasing interest -> Increasing community• Graduating from the incubator to a TLP

– sling.apache.org

55

Page 56: Apache Sling : JCR, OSGi, Scripting and REST

56

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 :)

56

Page 57: Apache Sling : JCR, OSGi, Scripting and REST

57

...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.

57

Page 58: Apache Sling : JCR, OSGi, Scripting and REST

58

• 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

58

...Manage theWebChallenges

Page 59: Apache Sling : JCR, OSGi, Scripting and REST

59

... : with ApacheSling Stateof theArt• Modularity and dynamics (OSGi)• Content Management (JCR)• Resource oriented architecture (REST)• Scripting inside

59

Page 60: Apache Sling : JCR, OSGi, Scripting and REST

60

!ApacheSling – TheFun isBack• Web Framework• Java Content Repository• REST• Scripting inside• OSGi• Apache Open Source project• Check it out today!

60

Page 61: Apache Sling : JCR, OSGi, Scripting and REST

61

!Thanks for your attention

Q&A