Download - JCR In Action (ApacheCon EU 2008)

Transcript
Page 1: JCR In Action (ApacheCon EU 2008)

JCR in Action

Content-based Applications with Apache Jackrabbit

Carsten [email protected]

Day Software

Page 2: JCR In Action (ApacheCon EU 2008)

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: JCR In Action (ApacheCon EU 2008)

Agenda

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

3

Page 4: JCR In Action (ApacheCon EU 2008)

Content Repository

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

observation and searching

4

Page 5: JCR In Action (ApacheCon EU 2008)

JSR 170: Content Repository for JavaTM 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?

5

JSR 283 – Version 2.0 – scheduled for 2008

Page 6: JCR In Action (ApacheCon EU 2008)

Sample Application

• Digital Asset Management– Hierarchical storage of pictures– Upload– Tagging– Searching– Automatic thumbnail generation

6

Poor man's flickr...

Page 7: JCR In Action (ApacheCon EU 2008)

Content Repository Features

• Hierarchical content– Nodes and properties

• Structured– Nodetypes and typed properties

• And/or unstructured• Fine and coarse-grained

7

Page 8: JCR In Action (ApacheCon EU 2008)

Sample Content Structure

8

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 9: JCR In Action (ApacheCon EU 2008)

Content Repository Features

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

9

Page 10: JCR In Action (ApacheCon EU 2008)

Comparison

• File System– Hierarchival, unstructured

• Database– Structured– Referential integrity, transactions

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

10

Page 11: JCR In Action (ApacheCon EU 2008)

11

Page 12: JCR In Action (ApacheCon EU 2008)

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 12

Page 13: JCR In Action (ApacheCon EU 2008)

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

13

Page 14: JCR In Action (ApacheCon EU 2008)

Connecting to the Repository

14

Page 15: JCR In Action (ApacheCon EU 2008)

Working with the Repository

15

Page 16: JCR In Action (ApacheCon EU 2008)

Traverse the Hierarchy

16

Page 17: JCR In Action (ApacheCon EU 2008)

Retrieve a Property

17

Page 18: JCR In Action (ApacheCon EU 2008)

Interaction Summary

• Get the repository• Login to a workspace

– Provides a session

• Use the session to– access nodes and properties– change and save them

Page 19: JCR In Action (ApacheCon EU 2008)

Apache Jackrabbit– JSR 170 reference implementation– Apache TLP since 2006– Looking back

• 1.0: April 2006• 1.1: October 2006• 1.4: January 2008

– Looking forward (tentative)• 1.5: Q2/2008• 2.0: 2008 (JCR 2.0 RI)• announce-

[email protected]– Components

• Core, API, RMI, WebDAV, webapp, JCA, …

19

http://jackrabbit.apache.org/

19

Page 20: JCR In Action (ApacheCon EU 2008)

Words of Advice

• Read (or browse) the JCR specification– jcr-1.0.jar included

• Getting started with Jackrabbit– jackrabbit-webapp: Drop-in deployment– First Hops: Embedded repository– Take your time

• (Have a look at Apache Sling when it's released...)

20

Page 21: JCR In Action (ApacheCon EU 2008)

Agenda

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

21

Page 22: JCR In Action (ApacheCon EU 2008)

Starting point: Leverage the standard node types

• Type hierarchy • Content hierarchy

22

nt:hierarchyNode

nt:folder

nt:file

nt:linkedFile

nt:resource

Page 23: JCR In Action (ApacheCon EU 2008)

Bottom-up 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: JCR In Action (ApacheCon EU 2008)

Top-down modeling: Content hierarchies

24

Basel

City

Europe

Travel Family

Amsterdam

2007

Weddings

2008

Photo

Photo

Photo

Photo

Page 25: JCR In Action (ApacheCon EU 2008)

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

25

Page 26: JCR In Action (ApacheCon EU 2008)

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

26

Page 27: JCR In Action (ApacheCon EU 2008)

Content Modeling: Words of advice

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

• Look at existing node types• Mixin node types possible

27

Page 28: JCR In Action (ApacheCon EU 2008)

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

28

Page 29: JCR In Action (ApacheCon EU 2008)

Agenda

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

29

Page 30: JCR In Action (ApacheCon EU 2008)

Alternative Views: References

30

Birds

Tags Album

Photo

Photo

Trees Top 10

Favorites

Picks

linklinklink

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

Page 31: JCR In Action (ApacheCon EU 2008)

Alternative Views: SearchLooking for

XPath SQL

Latest photos

/jcr:root/my:albums//element(*,my:photo)

[@data > xs:dateTime(‘…’)]

SELECT * FROM my:photo

WHERE

jcr:path LIKE ‘/my:albums/%’

AND date > DATE ‘…’

Favorites with keywords

/jcr:root/my:albums//element(*,my:favorite)

/jcr:content[jcr:contains(.,’…’)]

SELECT * FROM my:favorites

WHERE

jcr:path LIKE ‘/my:albums/%’

AND CONTAINS(*,‘…’)

31

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

Page 32: JCR In Action (ApacheCon EU 2008)

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

32

Page 33: JCR In Action (ApacheCon EU 2008)

Sample - Alternative Views

33

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

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

Page 34: JCR In Action (ApacheCon EU 2008)

Agenda

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

34

Page 35: JCR In Action (ApacheCon EU 2008)

Staged Publishing: Versioning

35

Version store

LiveStaging

Page 36: JCR In Action (ApacheCon EU 2008)

Authentication and Authorization

• Java Authentication and Authorization Services (JAAS)– Mostly the authentication part is

currently used by Jackrabbit– Pluggable authentication components– Support for single sing-on

36

Page 37: JCR In Action (ApacheCon EU 2008)

Authentication and Authorization

• Custom AccessManager interface in Jackrabbit– Pluggable authorization components– The default implementation supports

only global read, write, and admin access

– More advanced implementations are proprietary

37

Page 38: JCR In Action (ApacheCon EU 2008)

Authentication and Authorization

• Repository-level authentication and authorization applies to all clients– Better than application-level authorization

38

Page 39: JCR In Action (ApacheCon EU 2008)

Jackrabbit Configuration

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

• Check out the Documentation

39

Page 40: JCR In Action (ApacheCon EU 2008)

Observation

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

in events• Monitors events• Responds 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)

40

Page 41: JCR In Action (ApacheCon EU 2008)

Event Types

• Events can be of 5 different types– NODE_ADDED– NODE_REMOVED– PROPERTY_ADDED– PROPERTY_REMOVED– PROPERTY_CHANGED

Advanced JCR Features 41

Page 42: JCR In Action (ApacheCon EU 2008)

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

Advanced JCR Features 42

Page 43: JCR In Action (ApacheCon EU 2008)

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

43

Page 44: JCR In Action (ApacheCon EU 2008)

Agenda

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

44

Page 45: JCR In Action (ApacheCon EU 2008)

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

45

Page 46: JCR In Action (ApacheCon EU 2008)

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

46

Page 47: JCR In Action (ApacheCon EU 2008)

Advanced Development

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

versa– Similar to database ORMs

• Apache Sling– New project in incubation– REST based web framework– Leverages OSGi

47

Page 48: JCR In Action (ApacheCon EU 2008)

Agenda

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

48

Page 49: JCR In Action (ApacheCon EU 2008)

Conclusion

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

49

Page 50: JCR In Action (ApacheCon EU 2008)

Conclusion

• Web-friendly– Trivial URI mapping– WebDAV access for free– Dispatch on node type, not on URI path

• In your application?• Look at Jackrabbit OCM and Apache

Sling

50

Page 51: JCR In Action (ApacheCon EU 2008)

Q&A

51