API Regions for the Feature Model

If you're assembling a platform (in contrast to a final application) out of several features and provide this platform for customers to build their application on top of, additional control of the API provided by the platform is needed. The bundles within the features provide all kinds of APIs but you might not want to expose all of these as extension points. You would rather want to use some of them internally within either a single feature or share within your platform features.

Visibility of Java API

A feature exports some API, however there are two different types of clients of the API:

  • Bundles shipped as part of the platform
  • Application bundles using the platform

We can generalize this by saying that API is either globally visible (to every client) or only visible to features within the same context. Usually this is referred to as a “region”: The platform spawns its own region and a customer application has its own region, too. In theory there could be several customer applications running in the same framework on top of the platform, and each application has its own region.

Without any further information, API is globally visible by default. However, for platform features we want the opposite as we want to ensure that newly added API is not visible to all bundles by default.

A feature can have an additional extension of type JSON named api-region. The following example exposes some packages to the global region and an additional package to the platform region. Exports declared earlier in the api-regions array also apply to later elements in the array, so the platform region also contains all exports declared for the global region.

Note that the global region is a predefined region that exports the listed packages to everyone. Other region names can be chosen freely. Packages listed in these other regions are only exposed to bundles in features that are in the same region.

    "api-regions:JSON|optional" : [
        {
            "name": "global",
            "exports": [
                "# Export Sling's resource API in the global region",
                "org.apache.sling.resource.api",
                "org.apache.sling.resource.api.adapter",
                "org.apache.sling.resource.api.auth",
                "org.apache.sling.resource.api.request",
                "org.apache.sling.resource.api.resource"
            ]
        },{
            "name": "platform",
            "exports": [
                "# Export the scheduler API in the platform region.",
                "# All exports in earlier regions defined here also apply.",
                "org.apache.sling.commons.scheduler"
            ]
        }
    ]

Of course the above mentioned packages need to be exported by some bundle within the feature. By exporting packages to a given region, a feature automatically also sees all packages available to that region (or regions).

A feature can also just consume packages from a region, without having to export any packages to it. This can be done by exporting an empty list of packages. For example:

    "api-regions:JSON|optional" : [
        {
            "name": "platform",
            "exports": []
        }
    ]

If the api-regions extension is missing or the api-regions information is missing, it is assumed that all packages are exported to the “global” region and all packages in the global region are visible to the feature.

If a feature exports no packages and only wants to have visibility of packages from the global region, this can be specified as follows:

    "api-regions:JSON|optional" : [
        {
            "name": "global",
            "exports": []
        }
    ]

Java API Behind a Toggle

API Regions support the usage of toggles, where API can be hidden behind a toggle and that API is only available in the region, if the toggle is enabled. For this, a package in a region can be configured with a toggle. In this case an object is used per package:

    "api-regions:JSON|optional" : [
        {
            "name": "global",
            "exports": [
                {
                    "name" : "org.apache.sling.api",
                    "toggle" : "SLING_API"
                }
            ]
        }
    ]

In the above example, the package org.apache.sling.api is only available if the toggle SLING_API is enabled.

While the above example enables a complete package based on a toggle, it is also possible to enable a new version of an existing API with a toggle:

    "api-regions:JSON|optional" : [
        {
            "name": "global",
            "exports": [
                {
                    "name" : "org.apache.sling.api",
                    "toggle" : "NEW_SLING_API",
                    "previous" : "org.apache.sling:org.apache.sling.api:1.1"
                }
            ]
        }
    ]

In the above example, if the toggle NEW_SLING_API is enabled, the package org.apache.sling.api is available based on the artifact in the feature model providing this package. If the toggle NEW_SLING_API is not enabled, the package org.apache.sling.api is available based on the artifact mentioned in the previous property. Usually that points to an older version of the API.

Deprecation of Java API

The usual process for deprecating Java API is to mark it with a corresponding annotation and javadoc tag. However, in cases where changing the source code of an API is not possible to add such a marker, the deprecation can be specified through an API region. This information can be used by tooling to mark and report usage of such API.

     "api-regions:JSON|false":[
        {
            "name":"global",
            "exports":[
                # Package exported without deprecation
                "org.apache.sling.api",
                # Package exported - full package deprecated
                {
                    "name" : "org.apache.sling.api.file",
                    "deprecated" : "Deprecation message"
                },
                # Package exported - single class and single member deprecated
                {
                    "name" : "org.apache.sling.api.io",
                    "deprecated" : {
                        "members" : {
                            "FileCache" : "Deprecation message",
                            "MemoryCache#getFile()" : "Deprecation message"
                        }
                    }
                },
                # Instead of just the message, additional information about when the
                # deprecation happened can be provided (this works in all of the
                # above places)
                {
                    "name":"org.apache.sling.incubator.api",
                    "deprecated":{
                        "msg":"This is deprecated",
                        "since":"Since Sling left the incubator"
                    }
                }
            ]
        }
     ]

OSGi Configurations

Apart from defining the Java API surface, for some applications it is benefitial to describe the OSGi configuration surface, too. For example, a framework might not allow an application to set some configurations or update existing configurations.

While the OSGi metatype specification allows to specify some type information for OSGi configurations, it does not allow to specify validation rules or differentiate between a private and a public configuration or configuration property.

For each configuration and factory configuration, the allowed list of properties can be specified together with a type and additional validation rules (like a range or a regualr expression). Properties not mentioned for a configuration are considered internal. In addition, configuration PIDs and factory configuration PIDs can be specified as internal as well.

A similar mechanism exists for framework properties.

Configurations

Each OSGi configuration that is part of the configuration API is listed below the configurations object with the PID of the configuration as the key. Each configuration can have the following properties:

  • title : A human readable title
  • description : A human readable description
  • properties : An object containing all properties that are allowed to be configured
  • deprecated : If this configuration should not be used anymore a human readable message.
"configuration-api:JSON" : {
    "configurations" : {
        "org.apache.sling.engine.impl.SlingMainServlet" : {
            "title" : "Apache Sling Main Servlet",
            "description" : "The configuration of the main servlet...",
            "properties" : {

            }
        }
    }
}

Properties

For each property a JSON object contains additional information about this property. The following keys can be set:

  • ‘type’ : The type of the property, defaults to STRING. The following types are supported:
    • STRING : A string value
    • LONG : A long value
    • INTEGER : An integer value
    • SHORT : A short value
    • CHARACTER : A single character
    • BYTE : A byte value
    • DOUBLE : A double value
    • FLOAT : A float value
    • BOOLEAN : A boolean value, either true or false
    • PASSWORD : A string containing a password/secret
    • URL : A string containing a URL
    • EMAIL : A string containing an email address
    • PATH : A string containing a unix-style path (starts with a slash)
  • cardinality : Single value property or multi value. Defaults to 1. If set to -1, the number of values is unlimited. Otherwise the value must be greater than zero and indicates the maximum number of values.
  • required : Boolean, whether the property is required and needs a configuration value.
  • title : A human readable title
  • description : A human readable description
  • deprecated : If this configuration should not be used anymore a human readable message.
  • includes : An array of values. If configured, these values must be present.
  • excludes : An array of values. If configured, these values must not be present.
  • pattern : A regular expression to validate the value.
  • range : An object which can have a min and/or a max property to further specify the value range.
  • options : An array of objects acting as an enumeration for the allowed values. Each option must have a value property. It might also have a title or description property.
"configuration-api:JSON" : {
  "configurations" : {
        "org.apache.sling.engine.impl.SlingMainServlet" : {
            "properties" : {
                "flag" : {
                    "type" : "BOOLEAN"
                },
                "output" : {
                    "options" : [
                        {
                            "title" : "Output to text file",
                            "value" : "TEXT
                        },
                        {
                            "title" : "Output to console",
                            "value" : "CONSOLE
                        }
                    ]
                },
                "number" : {
                    "type" : "INTEGER",
                    "range" : {
                        "min" : 5,
                        "max" : 50
                    }
                },
                "array_of_urls" : {
                    "type" : "URL",
                    "cardinality" : -1,
                    "includes": ["https://sling.apache.org"],
                    "excludes" : ["https://outdated.apache.org"]
                }
            }
        }
    }
  },

Factory Configurations

OSGi factory configurations are similarly described in the configuration API. If a factory configuration is part of the public API, it should be listed below the factory-configurations property using the factory PID as a key.

"configuration-api:JSON" : {
    "factory-configurations" : {
        "org.apache.sling.event.jobs.QueueConfiguration" : {
            "properties" : {

            },
            "internal-names" : "main",
            "operations" : ["CREATE"]
        }
    }
}

As with configurations, each public property needs to be described. Factory configurations support two additional properties:

  • internal-names : A list of factory configuration names that are not public and neither can't be updated or created.
  • operations : A list of operations out of CREATE and UPDATE - both are set by default. If CREATE is set, new factory configurations are allowed to be created. If UPDATE is set, existing configurations are allowed to be updated. But in both cases internal-names needs to be respected.

Framework Properties

Similar to properties of OSGi configurations, framework properties can be described, providing validation for such properties:

"configuration-api:JSON" : {
    "framework-properties" : {
        "org.apache.sling.http.port" : {
            "type" : "INTEGER"
        }
    }
}

A framework property has the same configuration options as a property inside a configuration.

Internal Configurations

Some OSGi configurations and factory configurations are not part of the public API and cannot be created/updated by application configuration. Same applies to framework properties. The PIDs, factory PIDs and names of these can be specified as well:

"configuration-api:JSON" : {
    "internal-configurations" : [
        // list of PIDs
    ],
    "internal-factory-configurations" : [
        // list of factory PIDs
    ],
    "internal-framework-properties" : {
        // list of property names
    }
}

OSGi Configuration Regions

Two regions are supported for OSGi configurations, internal and global. Without any further information, a feature is considered to be part of the global configuration region. If a feature wants to be part of the internal region it can specify this in the extension:

"configuration-api:JSON|optional" : {
  "region" : "INTERNAL"
}

When two features are aggregated, the resulting feature is only in the internal region if both source features are in the internal region. Otherwise, the resulting aggregate is always in the global region.