jcr in action (apachecon us 2009)

55
JCR in Action Content-based Applications with Apache Jackrabbit Carsten Ziegeler [email protected] Apache Con US Presentation – November 2009 - Oakland

Upload: carsten-ziegeler

Post on 10-May-2015

6.697 views

Category:

Technology


2 download

DESCRIPTION

My presentation about JCR and Apache Jackrabbit from the ApacheCon US 2009, November, in Oakland, CA.

TRANSCRIPT

Page 1: JCR In Action (ApacheCon US 2009)

JCR in Action

Content-based Applications with Apache Jackrabbit

Carsten [email protected]

Apache Con US Presentation – November 2009 - Oakland

Page 2: JCR In Action (ApacheCon US 2009)

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)

2

Page 3: JCR In Action (ApacheCon US 2009)

Motivation• Tried and trusted NOSQL solution• Standard Java API

– First spec released in May 2005• Open Source Implementation

– 1.0 Release in April 2006• Think about your data storage use

cases– JCR might help

3

Page 4: JCR In Action (ApacheCon US 2009)

Agenda• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions

4

Page 5: JCR In Action (ApacheCon US 2009)

Content Repository

5

Page 6: JCR In Action (ApacheCon US 2009)

Content Storage• Hierarchical content

– Nodes and properties• Structured

– Nodetypes and typed properties• And/or unstructured• Fine and coarse-grained

6

Page 7: JCR In Action (ApacheCon US 2009)

Sample Application• Digital Asset Management

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

7

Poor man's flickr...

Page 8: JCR In Action (ApacheCon US 2009)

Sample Content Structure

8

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 9: JCR In Action (ApacheCon US 2009)

Sample Application• Java web application• Uses Apache Sling as web framework• Content repository managed by

Apache Jackrabbit• Interaction through the JCR API

9

Page 10: JCR In Action (ApacheCon US 2009)

JSR 170: Content Repository for JavaTM technology API

• (Java) Standard – Version 1.0– 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?

10

Page 11: JCR In Action (ApacheCon US 2009)

JSR 283 : JCR 2.0 is final• New features• Improved specification

– Cleaned up API (deprecation)– Revised specification document

• Binary compatible– JSR 170 apps run without modification

• More later...

11

Page 12: JCR In Action (ApacheCon US 2009)

12

CR : Combines Advantages

Page 13: JCR In Action (ApacheCon US 2009)

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

13

Page 14: JCR In Action (ApacheCon US 2009)

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!

Implementation of JCR 14

Page 15: JCR In Action (ApacheCon US 2009)

Content Repository

Workspace A

ab c

i

gh

j k

= Node

= Property

Root

ed

6.02x1023

„Once upon a time..“

-25

true

Nodes and Properties

Implementation of JCR

15

Page 16: JCR In Action (ApacheCon US 2009)

Connecting to the Repository

16

JCR 1.0

Page 17: JCR In Action (ApacheCon US 2009)

17

Connecting to the Repository• JCR 2.0 provides RepositoryFactory• Uses Service Provider Mechanism

• META-INF/services/javax.jcr.RepositoryFactory

• Just use• RepositoryFactory.getRepository(null)

• Or specify connection parameters• RepositoryFactory.getRepository(Map)

Page 18: JCR In Action (ApacheCon US 2009)

Working with the Repository

18

Page 19: JCR In Action (ApacheCon US 2009)

Traverse the Hierarchy

19

Page 20: JCR In Action (ApacheCon US 2009)

Retrieve a Property

20

Page 21: JCR In Action (ApacheCon US 2009)

Interaction Summary• Get the repository• Login to a workspace

– Provides a session• Use the session to

– Access nodes and their properties– Change and save them

21

Page 22: JCR In Action (ApacheCon US 2009)

Apache Jackrabbit– JSR 170 and 283 reference

implementation– Apache TLP since 2006– Releases

• 1.6 (JSR 170 based)• 2.0 Beta 2 is out (nearly)• 2.0 Final (end of 2009)• announce-

[email protected]– Components

• Commons, API• RMI, WebDAV, WebApp, JCA• OCM• And more...

22

http://jackrabbit.apache.org/

22

Page 23: JCR In Action (ApacheCon US 2009)

Words of Advice• Read (or browse) the JCR specification

– jcr-1.0.jar/jcr-2.0.jar included• Getting started with Jackrabbit

– jackrabbit-webapp: Drop-in deployment– First Hops: Embedded repository– Take your time– Think about your use cases

• Have a look at Apache Sling

23

Page 24: JCR In Action (ApacheCon US 2009)

Agenda• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions

24

Page 25: JCR In Action (ApacheCon US 2009)

Starting point: Leverage the standard node types

• Type hierarchy • Content hierarchy

25

nt:hierarchyNode

nt:folder

nt:file

nt:linkedFile

nt:unstructured

Page 26: JCR In Action (ApacheCon US 2009)

Bottom-up modeling: Content types

26

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 27: JCR In Action (ApacheCon US 2009)

Top-down modeling: Content hierarchies

27

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 28: JCR In Action (ApacheCon US 2009)

Content Modeling: Words of advice

• Namespaces– Use a single namespace per company or

application– Use a reasonably unique namespace prefix– Prefixed names for structured content– Default namespace for unstructured

content

28

Page 29: JCR In Action (ApacheCon US 2009)

Content Modeling: Words of advice

• Use an application root node– /my:content– Good for searching, backup, and migration

• Avoid flat hierarchies– User interface complexity

• Content-driven design– Design your content before your application

29

Page 30: JCR In Action (ApacheCon US 2009)

Content Modeling: Words of advice

• Checkout Apache Jackrabbit wiki and mailing lists– "Davids Model"

• Look at existing node types• Mixin node types possible

30

Page 31: JCR In Action (ApacheCon US 2009)

David‘s Model• Rule #1: Data First, Structure Later.

Maybe• Rule #2: Drive the content hierarchy,

don‘t let it happen• Rule #6: Files are Files are Files• Look at http://wiki.apache.org/jackrabbit/

DavidsModel

31

Page 32: JCR In Action (ApacheCon US 2009)

Agenda• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions

32

Page 33: JCR In Action (ApacheCon US 2009)

Alternative Views: References

33

Bird

Tags Album

Photo

Photo

Tree Top 10

Favorites

Picks

linklinklink

API:Node.getReferences():PropertyIteratorProperty.getNode():NodeNode.setProperty(String name, Node)

Page 34: JCR In Action (ApacheCon US 2009)

Sample – Alternative Views• Reference by name

34

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

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

Page 35: JCR In Action (ApacheCon US 2009)

Sample - Alternative Views

35

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

bird

• Reference by path /

Tags

/Tags/bird

Page 36: JCR In Action (ApacheCon US 2009)

Alternative Views: SearchLooking for

XPath SQL

Latest photos

/jcr:root/my:albums//element(*,my:photo)[@data > xs:dateTime(‘…’)]

SELECT * FROM my:photoWHEREjcr:path LIKE ‘/my:albums/%’AND date > DATE ‘…’

Favorites with keywords

/jcr:root/my:albums//element(*,my:favorite)/jcr:content[jcr:contains(.,’…’)]

SELECT * FROM my:favoritesWHEREjcr:path LIKE ‘/my:albums/%’AND CONTAINS(*,‘…’)

36

API:Session.getWorkspace().getQueryManager():QueryManagerQueryManager.createQuery(String stmt, String language):Query;Query.execute():QueryResult

Page 37: JCR In Action (ApacheCon US 2009)

Alternative Views: Words of advice

• Moderate use of references– Circular references only within a subtree– Plan for backup and content migration

• Best search performance when selecting a small subset of content

• References, path or name property

37

Page 38: JCR In Action (ApacheCon US 2009)

Agenda• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions

38

Page 39: JCR In Action (ApacheCon US 2009)

Observation• Optional feature of the JCR specification• Enables applications to register interest

in events• Monitor events• Respond to events

Advanced JCR Features

API:ObservationManager:addEventListener(EventListener listener, int eventTypes, java.lang.String absPath, boolean isDeep, java.lang.String[] uuid, java.lang.String[] nodeTypeName, boolean noLocal)

39

Page 40: JCR In Action (ApacheCon US 2009)

Event Types• Events can be of 5 different types

– Node added– Node removed– Property added– Property removed– Property changed

Advanced JCR Features 40

Page 41: JCR In Action (ApacheCon US 2009)

Observation Events• Describe changes to a workspace• Dispatched on persistent change• Provide the path of the item• Provide the user ID• Only provided to sessions with

sufficient access privileges• Events may not be complete!

– Example: removal of a tree of nodes

Advanced JCR Features 41

Page 42: JCR In Action (ApacheCon US 2009)

Event Listeners• Registered with a workspace• Registration with optional filters

– Like node types, paths• Receive events for every change

Advanced JCR Features

API:public void onEvent(EventIterator events);

42

Page 43: JCR In Action (ApacheCon US 2009)

Events Advice• Events occur after a save

– Modification based on events is a new operation

– Such events can be filtered• Events may not tell the complete story

– Tree removal, copy, move• Most common pattern

– Node added, removed, changed

Advanced JCR Features 43

Page 44: JCR In Action (ApacheCon US 2009)

Staged Publishing: Versioning

44

Version store

LiveStaging

Page 45: JCR In Action (ApacheCon US 2009)

Jackrabbit Configuration• Workspace configuration

– XML configuration– Persistence Managers– Query index configuration (Lucene)– File system configuration

• Check out the documentation

45

Page 46: JCR In Action (ApacheCon US 2009)

Agenda• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions

46

Page 47: JCR In Action (ApacheCon US 2009)

Getting Content• Get the repository• Login to a workspace

– Returns a session• Starting with the session

– Navigate to the desired content– Get the query manager and search

47

Page 48: JCR In Action (ApacheCon US 2009)

Writing Content• Get the repository• Login to a workspace

– Returns a session• Navigate to the correct node

– To change it– To add new nodes/properties– To remove nodes/properties

• Persist changes

48

Page 49: JCR In Action (ApacheCon US 2009)

Advanced Development• Apache Sling

– REST based web framework– Powered by OSGi– Scripting Inside

• Apache Jackrabbit OCM– Map content to Java objects and vice

versa– Similar to database ORMs

49

Page 50: JCR In Action (ApacheCon US 2009)

JCR 2.0 Features• Abstract Query Model

– Java Query Object Model / SQL• ACL and Access Control Policies

– Plus Retention Policies and Hold• Nodetype Registry• New Property Types and Nodetypes• Shareable Nodes (Graph)• Journalling Observation

50

Page 51: JCR In Action (ApacheCon US 2009)

Agenda• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions

51

Page 52: JCR In Action (ApacheCon US 2009)

(Nearly) Everthing is Content• Application Domain Specific Content• Presentation Support (HTML, CSS,

JavaScript, Images)• Documentation, Translations• ...

52

Page 53: JCR In Action (ApacheCon US 2009)

Conclusion• Content Repositories

– Combine advantages from FS and DB– Add important features– Structure/Access your data the way your

domain requires it• JCR – The Java API• Apache Jackrabbit – The Implementation

53

Page 54: JCR In Action (ApacheCon US 2009)

Famous Last Words• Read the specification• JCR in your application?• Join the Jackrabbit community!• Seriously consider Apache Sling for

web applications• Check for additional stuff like OCM

54

Page 55: JCR In Action (ApacheCon US 2009)

Q&A

55