JCR (Java Content Repository) is a standardized API for accessing content repositories. Specifications:
- JCR 1.0 — JSR-170 (2004)
- JCR 2.0 — JSR-283 (2009)
The implementation used in Sling is Apache Jackrabbit Oak.
- Functional data: application domain-specific content — articles, blog posts, comments, assets
- Structured data: address records, spreadsheet-like data
- Unstructured data: PDFs, Word Docs, XML, CSVs
- Other: workflow definitions, ACLs, code
JCR uses a hierarchical content model:
- Repository — top-level container
- Workspace — logical partition (default: "crx.default", typically only one used)
- Node — hierarchical tree structure (like folders and files)
- Property — holds data on a node
- Session — opened/closed per request. Should be short-lived, never shared across threads.
Every node has a Primary Nodetype which defines mandatory/optional properties, data types, and subnodes:
nt:unstructured— free data structure, no constraints (default)nt:folder— comparable to file system foldernt:file+nt:resource— files/binary data
Additionally, a node can have any number of Mixins (e.g. mix:title, mix:referenceable).
Properties hold the data of nodes. Supported data types:
- STRING, LONG, DOUBLE, DECIMAL, BOOLEAN, DATE, BINARY, REFERENCE
- All types support multi-valued variants (arrays): STRING[], LONG[], etc.
Rule of thumb: don't use it. "Content first, structure later, maybe."
- Exception: Use built-in JCR node types where appropriate (especially for files)
- For technical reasons, querying by nodetype is very efficient
- Often existing nodetypes are sufficient
JCR:
nt:base— base type for all primary node typesnt:unstructured— most flexible, no constraintsnt:folder— represents foldersnt:file— represents files (requiresjcr:contentchild)nt:resource— holds binary data (requiresjcr:data)
Sling:
sling:OrderedFolder— default for folder structuressling:OsgiConfig— OSGi config stored in repositorysling:Mapping— URL mappings
Main package: javax.jcr. The API defines different support levels:
- Level 1 — read access, export
- Level 2 — write access, import
- Advanced — observation, locking, versioning, access control
Jackrabbit Oak supports all levels/all features.
Note: The API is a bit outdated (designed 2002) — makes heavy use of checked exceptions and is cumbersome compared to modern Java.
Rule of thumb: don't use it. All normal read/write operations should use the Sling Resource API.
Use JCR API only for features not supported by Sling:
- Versioning and locking nodes
- Managing security and ACLs
- Observation use cases not supported by Sling eventing
- Copy or move whole subtrees
- Queries with special features like limiting