Merge pull request #15 from apache/feature/SLING-7768

Feature/sling 7768: String Interpolation for ETC Map
tree: 436c526a5e1cb97e207ee6c59a0f9a94a544b1ee
  1. src/
  2. .gitignore
  3. bnd.bnd
  4. CODE_OF_CONDUCT.md
  5. CONTRIBUTING.md
  6. Jenkinsfile
  7. LICENSE
  8. pom.xml
  9. README.md
README.md

Build Status Test Status Maven Central JavaDocs License

Apache Sling Resource Resolver

This module is part of the Apache Sling project.

This bundle provides the Resource Resolver and Resource Resolver Factory

ETC Map String Interpolation

Setting up ETC Mappings (/etc/map) for different instances like dev, stage, qa and production was time consuming and error prone due to copy-n-paste errors. As a new feature Sling now supports String Interpolation in the /etc/map. With this it is possible to add placeholders to sling:match entries to make it possible to have them work for different environments. The values are either provides by System, Bundle Context or String Interpolation Configuration values. The placeholders have this format: $[‘type’:‘name’;default=‘default value’]. The type is can be:

  • env: take from the System Properties
  • prop: taken from the Bundle Context Properties
  • otherwise: taken from the String Interpolation Configuration

With it it is possible to create a single set of etc-mapping and then adjust the actual values of an instance by an OSGi configuration. Note: the placeholder must be placed into a sling:match entry and cannot be the JCR Node Name as some of the characters are not allowed.

Setup

The Substitution Configuration can be found in the OSGi Configuration as Apache Sling String Interpolation Provider. The property Placeholder Values takes a list of key=value entries where each of them map a variable with its actual value. In our little introduction we add an entry of phv.default.host.name=localhost. Save the configuration for now. Before going on make sure that you know Mapping Location configuration in the OSGi configuration of Apache Sling Resource Resolver Factory. Now to to composum and go to that node. If it does not exist then create one. The mapping should look like this:

  • etc
    • map
      • http
        • my-mapping
          • sling:match=$[phv.fq.host.name].8080

Opening the page http://localhost:8080/starter/index.html should work just fine. This is a mapping from System Properties with a default:

  • etc
    • map
      • http
        • my-mapping
          • sling:match=$[env:phv.fq.host.name;default=localhost].8080

Testing

Now got back to the String Interpolation configuration and change the value to qa.author.acme.com and save it.

For local testing open your hosts file (/etc/hosts on Unix) and add a line like this:

127.0.0.1 qa.author.acme.com

save it and test with ping qa.author.acme.com to make sure the name resolves. Now you should be able to open the same page with: http://qa.author.acme.com/starter/index.html.

Now do the same with phv.fq.host.name=staging.author.acme.com.

The String Interpolation works with any part of the etc-map tree.