Sling RepoInit: a Handy Tool to Manage Content and Users in AEMSling RepoInit: a Handy Tool to Manage Content and Users in AEM

Tech Tips

9 min read

Sling RepoInit: a Handy Tool to Manage Content and Users in AEM

Tags

#AEM

#AEM Tips

#Apache Sling

Share

Managing the initial repository state can be hard because it involves setting up code, configuration, content, and more. This is a lot to cope with when we need to complete several tasks in advance, such as creating service users, creating groups, setting up ACL permissions, and pre-creating/updating content. Repoinit (Apache Sling Repository Initialization) is a tool to help configure all of this in one place.

The advantages of Repoinit include:

  • Keeping the environments in sync in terms of initial and basic configuration
  • Speeding up local environment setup for developers
  • Creating resources at startup so they’re built into the logic from the beginning

Enable Repoinit in AEM

To enable Repoinit, OSGi configuration for factory PID org.apache.sling.jcr.repoinit.RepositoryInitializer in one of the project’s configuration folders. Since it’s an OSGi configuration, it is easy to have a set of scripts for author and publish run modes, along with different environments such as Stage and Production. For example, config.author, config.publish, config.author.prod, and config.publish.stage, etc. use a descriptive name for the configuration like org.apache.sling.jcr.repoinit.RepositoryInitializer~init.

These configurations have two optional fields:

  • A multi-value references field with each value providing the URL (as a String) of raw Repoinit statements
  • A multi-value scripts field with each value providing Repoinit statements as plain text in a String
Apache Sling repository initializer factory

Note 1: this service presents a challenge when you’re resolving the references. Each reference is expected to be in the form of a URL, and OSGi supports exposing bundles as URLs. However, when Apache Felix resolves a bundle’s URL in the URLHandlersBundleStreamHandler, it expects the URL host to be the UUID of the bundle, not a stable ID such as the Bundle’s Symbolic Name. You can find out how to resolve this issue here.

Note 2: When configuring script properties, it is easier to define them in the .config file than it is to define in the JSON-based .cfg.json format, since scripts are typically multiline.

Configuration file example: apps > my-app > config.author > org.apache.sling.jcr.repoinit.RepositoryInitializer-init.config

scripts=["
    create service user my-data-reader-service
 
    set ACL on /var/my-data
        allow jcr:read for my-data-reader-service
    end
 
    create path (sling:Folder) /conf/my-app/settings
"]

Repoinit vs. Ensure Authorizable

Ensure Service User config:
ui.apps > src > main > content > jcr_root > apps > site-com > config > com.adobe.acs.commons.users.impl.EnsureServiceUser-user-1.xml (this is the config for user-1 only; for user-2 we will need to create a separate xml config)

<!--?xml version="1.0" encoding="UTF-8"?-->
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primarytype="sling:OsgiConfig" principalname="user-1" operation="add" ensure-immediately="{Boolean}true" aces="[type=allow;privileges=jcr:read;path=/content/dam]">
</jcr:root>

Equivalent Repoinit script:

create service user user-1, user-2

set ACL on /content/dam
    allow jcr:read for user-1,user-2
end

Content Management: Repoinit vs. OnDeploy Scripts

Imagine that we need to create a “/path/A” resource and update the existing one under “/path/B”.

The OnDeplyScript might look something like this:

public class PrecreateContentScript extends OnDeployScriptBase {
 
    @Override
    protected void execute() throws Exception {
        Resource parent = this.getResourceResolver().resolve("/path");
        this.getResourceResolver().create(parent, "A",
                ImmutableMap.of(
                        "sling:ResourceType", "x/y/z",
                        "someInteger", 42,
                        "someBoolean", true,
                        "someDate", new Date()));
 
        Resource b = this.getResourceResolver().resolve("/path/B");
        ValueMap map = b.adaptTo(ModifiableValueMap.class);
 
        map.putIfAbsent("sling:ResourceType", "x/y/z");
        map.putIfAbsent("someInteger", 42);
        map.putIfAbsent("someBoolean", true);
        map.putIfAbsent("someDate", new Date());
    }
}

And this is a script which does the same thing:

create path (nt:unstructured) /path/A
set properties on /path/A, /path/B
  set sling:ResourceType{String} to /x/y/z
  default someInteger{Long} to 42
  set someFlag{Boolean} to true
  default someDate{Date} to "2020-03-19T11:39:33.437+05:30"
end

These are just a few examples of how we used Repoinit for our project; find more ideas about what you can do using Repoinit here.

Resource Hub

Our Latest Stories & Industry Insights

View Resource Hub

Dark geometric prism with glowing teal outlines and abstract digital particles on a dark background.

Content Fragments in Depth. CF Management Workflow. Part 3.3

7 min read

May 24, 2023

#AEM #Content Fragments #Digital Experience #Digital Marketing Technology

Blue wireless computer mouse next to a laptop keyboard with pink backlit keys on dark surface.

Content Fragments in Depth. AEM Workflow Management. Part 3.1

7 min read

May 10, 2023

#AEM #Content Fragments #Digital Experience #Digital Marketing Technology

3D blue cubes connected by glowing pink lines on a dark surface representing a digital network.

AEM Content Fragments in Depth. Custom Schemas. Part 2

10 min read

May 3, 2023

#AEM #Content Fragments #Digital Experience #Digital Marketing Technology

Empty picture frame with red and green edges on teal textured wall.

How to Add Image Management to an AEM TouchUI Dialog: Tutorial

16 min read

October 12, 2021

#AEM #Open Source #Touch UI #Digital Marketing Technology #EXADEL Authoring Kit For AEM

Two people sitting at a table with a laptop.

Let’s make your next project faster, safer, smarter.

Get In Touch