Post on 10-May-2015
description
JCR in Action
Content-based Applications with Apache Jackrabbit
Carsten Ziegelercziegeler@apache.org
Day Software
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
Agenda
• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions
3
Content Repository
• Generic application data store• Structured and unstructured content• Support small and large-scale data• Locking, transactions, versioning,
observation and searching
4
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
Sample Application
• Digital Asset Management– Hierarchical storage of pictures– Upload– Tagging– Searching– Automatic thumbnail generation
6
Poor man's flickr...
Content Repository Features
• Hierarchical content– Nodes and properties
• Structured– Nodetypes and typed properties
• And/or unstructured• Fine and coarse-grained
7
Sample Content Structure
8
Basel
City
Europe
Travel Family
Amsterdam
2007
Weddings
2008
Photo
Photo
Photo
Photo
Content Repository Features
• Query (XPath, SQL)• Export/Import (XML)• Referential Integrity• Authentication• Access Control• Versioning• Observation• Locking and Transactions (JTA)
9
Comparison
• File System– Hierarchival, unstructured
• Database– Structured– Referential integrity, transactions
• Content Repository– Advantages of a FS and a database– Plus observation, versioning etc.
10
11
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
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
Connecting to the Repository
14
Working with the Repository
15
Traverse the Hierarchy
16
Retrieve a Property
17
Interaction Summary
• Get the repository• Login to a workspace
– Provides a session
• Use the session to– access nodes and properties– change and save them
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-
subscribe@jackrabbit.apache.org– Components
• Core, API, RMI, WebDAV, webapp, JCA, …
19
http://jackrabbit.apache.org/
19
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
Agenda
• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions
21
Starting point: Leverage the standard node types
• Type hierarchy • Content hierarchy
22
nt:hierarchyNode
nt:folder
nt:file
nt:linkedFile
nt:resource
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)
Top-down modeling: Content hierarchies
24
Basel
City
Europe
Travel Family
Amsterdam
2007
Weddings
2008
Photo
Photo
Photo
Photo
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
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
Content Modeling: Words of advice
• Checkout Jackrabbit wiki and mailing lists– "Davids Model"
• Look at existing node types• Mixin node types possible
27
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
Agenda
• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions
29
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)
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
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
Sample - Alternative Views
33
my:photo > nt:file- description (string)- location (string)- tags (string[])
my:tag- name (string)- description (string)
Agenda
• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample Application• Summary and Questions
34
Staged Publishing: Versioning
35
Version store
LiveStaging
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
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
Authentication and Authorization
• Repository-level authentication and authorization applies to all clients– Better than application-level authorization
38
Jackrabbit Configuration
• Workspace Configuration– XML configuration– Persistence Managers– Query index configuration (Lucene)– File system configuration
• Check out the Documentation
39
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
Event Types
• Events can be of 5 different types– NODE_ADDED– NODE_REMOVED– PROPERTY_ADDED– PROPERTY_REMOVED– PROPERTY_CHANGED
Advanced JCR Features 41
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
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
Agenda
• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample application• Summary and Questions
44
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
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
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
Agenda
• JCR and Apache Jackrabbit• Basic Content Modeling• References and Search• Advanced Features• Demo: Sample application• Summary and Questions
48
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
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
Q&A
51