Feature Flags

Recently, I try to implement a feature toggle in my project. I find Sling already have such feature: Feature Flags

https://sling.apache.org/documentation/the-sling-engine/featureflags.html

The API document in AEM6 is

http://docs.adobe.com/content/docs/en/aem/6-0/develop/ref/javadoc/org/apache/sling/featureflags/package-summary.html

My implementation details list below

1. bundle configuration

The config file should be /config/org.apache.sling.featureflags.Feature.xml.
This file name can be gotten from  Factory Persistent Identifier (Factory PID) which is located in /system/console/configMgr

The configure details are

<?xml version=”1.0″ encoding=”UTF-8″?>
<jcr:root xmlns:sling=”http://sling.apache.org/jcr/sling/1.0&#8243; xmlns:jcr=”http://www.jcp.org/jcr/1.0&#8243;
jcr:primaryType=”sling:OsgiConfig”
name=”feature1″
description=”feature1-des”
enabled=”{Boolean}true”/>

Note: You can get all parameters by manually create a config then go to /system/console/status-Configurations and search “featureflags”

2. Multiple feature flags which are done by adding extra string to the name of the xml file.

If you like to use mutiple feature flags, you can create xml file by appending “-string” after the name, and all
e.g. org.apache.sling.featureflags.Feature-feature1.xml org.apache.sling.featureflags.Feature-feature1.xml

The results like below (Click image to get lager image)

featureflags

feature_flag_details

3. The Featureflag service can be access via Felix @Reference annotation or code below

import org.apache.sling.featureflags.Features;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
public class FeatureSwitcher {
   public static boolean isFeatureOneOn() {
       BundleContext bundleContext = FrameworkUtil.getBundle(Features.class).getBundleContext();
       ServiceReference factoryRef = bundleContext.getServiceReference(Features.class.getName());
       Features features = (Features) bundleContext.getService(factoryRef);
       return features.isEnabled("feature1");
   }
}

}

4. Use it in Sightly

data-sly-use.featureFlag="com.aemtreasury.FeatureFlag"

${featureFlag.featureOneOn} 
package com.aemtreasury;

import com.adobe.cq.sightly.WCMUse;
import org.apache.sling.featureflags.Features;

public class FeatureFlag extends WCMUse {

    private Features features;

    @Override
    public void activate() throws Exception {
       features = getSlingScriptHelper().getService(Features.class);
    }

    public Boolean isFeatureOneOn() {
       return features.isEnabled("feature1");
    }
}