What is a JCR?

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.

What Is Content?

  • 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

Hierarchical Structure

JCR uses a hierarchical content model:

  1. Repository — top-level container
  2. Workspace — logical partition (default: "crx.default", typically only one used)
  3. Node — hierarchical tree structure (like folders and files)
  4. Property — holds data on a node
  5. Session — opened/closed per request. Should be short-lived, never shared across threads.

JCR Node

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 folder
  • nt:file + nt:resource — files/binary data

Additionally, a node can have any number of Mixins (e.g. mix:title, mix:referenceable).

JCR Property

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.

When to Use Nodetype Restrictions

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

Important Built-in Nodetypes

JCR:

  • nt:base — base type for all primary node types
  • nt:unstructured — most flexible, no constraints
  • nt:folder — represents folders
  • nt:file — represents files (requires jcr:content child)
  • nt:resource — holds binary data (requires jcr:data)

Sling:

  • sling:OrderedFolder — default for folder structures
  • sling:OsgiConfig — OSGi config stored in repository
  • sling:Mapping — URL mappings

JCR API Overview

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.

When to Use the JCR API

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