incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1547074 - in /sling/whiteboard/bdelacretaz/feature-flags: ./ src/main/java/org/apache/sling/extensions/featureflags/api/ src/main/java/org/apache/sling/extensions/featureflags/impl/ src/main/java/org/apache/sling/extensions/featureflags/im...
Date Mon, 02 Dec 2013 16:02:32 GMT
Author: bdelacretaz
Date: Mon Dec  2 16:02:32 2013
New Revision: 1547074

URL: http://svn.apache.org/r1547074
Log:
SLING-3148 - feature flags, prototype that allows for hiding resources based on synthetic
resource types

Added:
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/HideResourceDecorator.java
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/NullRenderingServlet.java
Modified:
    sling/whiteboard/bdelacretaz/feature-flags/pom.xml
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlags.java
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlagsProvider.java
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/FeatureFlagsImpl.java
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RandomProvider.java
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RequestHeaderProvider.java
    sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/ResourcePathProvider.java

Modified: sling/whiteboard/bdelacretaz/feature-flags/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/pom.xml?rev=1547074&r1=1547073&r2=1547074&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/pom.xml (original)
+++ sling/whiteboard/bdelacretaz/feature-flags/pom.xml Mon Dec  2 16:02:32 2013
@@ -38,10 +38,12 @@
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
@@ -52,11 +54,19 @@
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.json</artifactId>
             <version>2.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.osgi</artifactId>
+            <version>2.2.0</version>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>

Modified: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlags.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlags.java?rev=1547074&r1=1547073&r2=1547074&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlags.java
(original)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlags.java
Mon Dec  2 16:02:32 2013
@@ -20,15 +20,19 @@ package org.apache.sling.extensions.feat
 
 import java.util.Collection;
 
-import org.apache.sling.api.SlingHttpServletRequest;
-
 /** Simple feature flags service for Sling.
  *  Uses {@link FeatureFlagsProvider} services
  *  for the actual enabled/disabled decisions.
  */
 public interface FeatureFlags {
-    /** True if the given feature flag is enabled for supplied request */
-    boolean isEnabled(String featureFlagName, SlingHttpServletRequest request);
+    /** True if the given feature flag is enabled in the given
+     *  context, which can be a Request, Resource etc. depending
+     *  on the active {@link FeatureFlagsProvider}.
+     *  
+     *  @param context will be ignored by providers if it's not
+     *  what they expect.
+     */
+    boolean isEnabled(String featureFlagName, Object context);
     
     /** Get the list of active feature flags */
     Collection<String> getFlagNames();

Modified: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlagsProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlagsProvider.java?rev=1547074&r1=1547073&r2=1547074&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlagsProvider.java
(original)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/FeatureFlagsProvider.java
Mon Dec  2 16:02:32 2013
@@ -18,19 +18,17 @@
  */
 package org.apache.sling.extensions.featureflags.api;
 
-import org.apache.sling.api.SlingHttpServletRequest;
-
 /** Service that implements the actual decision of
  *  enabling a feature flag or not.
  *  Not meant to be used directly, use 
  *  {@link FeatureFlags} instead.
  */
 public interface FeatureFlagsProvider {
-    /** True if the given feature flag is enabled for supplied request 
-     *  @return null means "don't care", otherwise Boolean indicates
-     *      whether the feature is enabled for supplied request
+    /** True if the given feature flag is enabled for supplied request
+     *  @param context can be anything or null - provider should just
+     *      ignore it if it's not what they expect. 
      * */
-    boolean isEnabled(String featureFlagName, SlingHttpServletRequest request);
+    boolean isEnabled(String featureFlagName, Object context);
     
     /** Get the list of flag names that this provider supports */
     String [] getFlagNames();

Added: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/HideResourceDecorator.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/HideResourceDecorator.java?rev=1547074&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/HideResourceDecorator.java
(added)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/HideResourceDecorator.java
Mon Dec  2 16:02:32 2013
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.extensions.featureflags.api;
+
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceDecorator;
+import org.apache.sling.api.resource.SyntheticResource;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Experimental {@link ResourceDecorator} that "hides"
+ *  resources by changing their resource type to
+ *  FRAGS_HIDDEN. The goal is to allow us to create prototypes
+ *  of frags usage without requiring changes to the Sling
+ *  core, this might not be a durable thing.
+ */
+@Component(
+        configurationFactory=true,
+        policy=ConfigurationPolicy.REQUIRE,
+        metatype=true
+        )
+@Service
+public class HideResourceDecorator implements ResourceDecorator {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private String flagName;
+    private String hiddenResourceType;
+    
+    @Reference
+    FeatureFlags featureFlags;
+    
+    @Property(
+            label="Flag name",
+            description="The name of the feature flag to check")
+    public static final String PROP_FLAG_NAME = "flag.name";
+    
+    @Property(
+            label="Hidden resource type",
+            description="The resource type to set on resources if the checked feature flag
is false")
+    public static final String PROP_HIDDEN_RESOURCE_TYPE = "hidden.resource.type";
+    
+    @Activate
+    protected void activate(Map<String, Object> config) {
+        flagName = PropertiesUtil.toString(config.get(PROP_FLAG_NAME), null);
+        hiddenResourceType = PropertiesUtil.toString(config.get(PROP_HIDDEN_RESOURCE_TYPE),
null);
+        log.info("{} activated", this);
+    }
+    
+    @Override
+    public String toString() {
+        return new StringBuilder()
+        .append(getClass().getSimpleName())
+        .append(", flag name=")
+        .append(flagName)
+        .append(", hidden resource type=")
+        .append(hiddenResourceType)
+        .toString();
+    }
+    
+    public Resource decorate(Resource resource) {
+        return maybeWrap(resource, resource);
+    }
+
+    public Resource decorate(Resource resource, HttpServletRequest request) {
+        return maybeWrap(resource, request);
+    }
+    
+    /** If our feature flag is false, hide r behind
+     *  a SyntheticResource with our configured resource
+     *  type.
+     */
+    private Resource maybeWrap(Resource r, Object context) {
+        if(!featureFlags.isEnabled(flagName, context)) {
+            final Resource result = new SyntheticResource(r.getResourceResolver(), r.getPath(),
hiddenResourceType); 
+            log.debug("Flag {} is not enabled, returning {} for {}",new Object[] { flagName,
result, r });
+            return result;
+        }
+        return r;
+    }
+}

Added: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/NullRenderingServlet.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/NullRenderingServlet.java?rev=1547074&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/NullRenderingServlet.java
(added)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/api/NullRenderingServlet.java
Mon Dec  2 16:02:32 2013
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.extensions.featureflags.api;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.scr.annotations.sling.SlingServlet;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Servlet that works with our {@link HideResourceDecorator}
+ *  to provide an empty rendering for resources that have 
+ *  a specific resource type. Experimental!
+ */
+@SuppressWarnings("serial")
+@SlingServlet(
+        methods={"GET","POST","PUT","DELETE"},
+        resourceTypes=NullRenderingServlet.HIDDEN_BY_FLAG_RESOURCE_TYPE)
+public class NullRenderingServlet extends SlingAllMethodsServlet {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
+    public static final String HIDDEN_BY_FLAG_RESOURCE_TYPE = "hidden-by-flag";
+
+    @Override
+    protected void service(SlingHttpServletRequest request,
+            SlingHttpServletResponse response) throws ServletException, IOException {
+        log.info("Null rendering for {} resource", HIDDEN_BY_FLAG_RESOURCE_TYPE);
+        // Just do nothing
+    }
+}

Modified: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/FeatureFlagsImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/FeatureFlagsImpl.java?rev=1547074&r1=1547073&r2=1547074&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/FeatureFlagsImpl.java
(original)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/FeatureFlagsImpl.java
Mon Dec  2 16:02:32 2013
@@ -29,7 +29,6 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.extensions.featureflags.api.FeatureFlags;
 import org.apache.sling.extensions.featureflags.api.FeatureFlagsProvider;
 import org.osgi.framework.BundleContext;
@@ -51,8 +50,8 @@ public class FeatureFlagsImpl implements
     private int lastTrackerCount = -1;
     private BundleContext bundleContext;
     
-    /** True if the given feature flag is enabled for supplied request */
-    public boolean isEnabled(String featureFlagName, SlingHttpServletRequest request) {
+    /** True if the given feature flag is enabled for supplied context */
+    public boolean isEnabled(String featureFlagName, Object context) {
         maybeLoadProviders();
         
         final FeatureFlagsProvider provider = providers.get(featureFlagName);
@@ -62,7 +61,7 @@ public class FeatureFlagsImpl implements
             return false;
         }
         
-        return provider.isEnabled(featureFlagName, request);
+        return provider.isEnabled(featureFlagName, context);
     }
     
     public Collection<String> getFlagNames() {

Modified: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RandomProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RandomProvider.java?rev=1547074&r1=1547073&r2=1547074&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RandomProvider.java
(original)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RandomProvider.java
Mon Dec  2 16:02:32 2013
@@ -18,22 +18,65 @@
  */
 package org.apache.sling.extensions.featureflags.impl.providers;
 
+import java.util.Map;
+
 import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.extensions.featureflags.api.FeatureFlagsProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import aQute.bnd.annotation.component.Activate;
 
-@Component
+@Component(
+    configurationFactory=true,
+    policy=ConfigurationPolicy.REQUIRE,
+    metatype=true
+    )
 @Service
 @Property(name="service.ranking", intValue=200)
 public class RandomProvider implements FeatureFlagsProvider {
-    public boolean isEnabled(String featureFlagName, SlingHttpServletRequest request) {
-        // TODO this is just a simplistic example meant to demonstrate the concept...
-        return Math.random() > 0.5;
+    
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private double activeRatio;
+    private String name;
+    
+    @Property(
+            label="Active percentage",
+            description="How often this flag returns true, randomly, in percent")
+    public static String PROP_ACTIVE_PERCENTAGE = "active.percentage"; 
+
+    @Property(
+            label="Name",
+            description="The name of this feature flag")
+    public static String PROP_NAME = "name"; 
+
+    @Activate
+    protected void activate(Map<String, Object> config) {
+        activeRatio = PropertiesUtil.toDouble(config.get(PROP_ACTIVE_PERCENTAGE), 50) / 100;
+        name = PropertiesUtil.toString(config.get(PROP_NAME), "NO_NAME");
+        log.info("{} activated", this);
+    }
+    
+    @Override
+    public String toString() {
+        return new StringBuilder()
+        .append(getClass().getSimpleName())
+        .append(", name=")
+        .append(name)
+        .append(", active percentage=")
+        .append(activeRatio * 100)
+        .toString();
+    }
+    
+    public boolean isEnabled(String featureFlagName, Object context) {
+        return Math.random() <= activeRatio;
     }
     
     public String [] getFlagNames() {
-        return new String[] { "random.feature" };
+        return new String[] { name };
     }
 }

Modified: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RequestHeaderProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RequestHeaderProvider.java?rev=1547074&r1=1547073&r2=1547074&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RequestHeaderProvider.java
(original)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/RequestHeaderProvider.java
Mon Dec  2 16:02:32 2013
@@ -18,10 +18,11 @@
  */
 package org.apache.sling.extensions.featureflags.impl.providers;
 
+import javax.servlet.http.HttpServletRequest;
+
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.extensions.featureflags.api.FeatureFlagsProvider;
 
 @Component
@@ -30,9 +31,13 @@ import org.apache.sling.extensions.featu
 public class RequestHeaderProvider implements FeatureFlagsProvider {
     public static final String HEADER_NAME = "X-sling-feature";
 
-    public boolean isEnabled(String featureFlagName, SlingHttpServletRequest request) {
-        // TODO this is just a simplistic example meant to demonstrate the concept...
-        return request.getHeader(HEADER_NAME) != null;
+    public boolean isEnabled(String featureFlagName, Object context) {
+        if(context instanceof HttpServletRequest) {
+            final HttpServletRequest request = (HttpServletRequest)context;
+            // TODO this is just a simplistic example meant to demonstrate the concept...
+            return request.getHeader(HEADER_NAME) != null;
+        }
+        return false;
     }
     
     public String [] getFlagNames() {

Modified: sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/ResourcePathProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/ResourcePathProvider.java?rev=1547074&r1=1547073&r2=1547074&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/ResourcePathProvider.java
(original)
+++ sling/whiteboard/bdelacretaz/feature-flags/src/main/java/org/apache/sling/extensions/featureflags/impl/providers/ResourcePathProvider.java
Mon Dec  2 16:02:32 2013
@@ -22,15 +22,27 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.extensions.featureflags.api.FeatureFlagsProvider;
 
 @Component
 @Service
 @Property(name="service.ranking", intValue=100)
 public class ResourcePathProvider implements FeatureFlagsProvider {
-    public boolean isEnabled(String featureFlagName, SlingHttpServletRequest request) {
+    public boolean isEnabled(String featureFlagName, Object context) {
+        Resource r = null;
+        
+        if(context instanceof Resource) {
+            r = (Resource)context;
+        } else if(context instanceof SlingHttpServletRequest) {
+            r = ((SlingHttpServletRequest)context).getResource();
+        }
+        
         // TODO this is just a simplistic example meant to demonstrate the concept...
-        return request.getResource().getPath().contains("/tmp/");
+        if(r != null) {
+            return r.getPath().contains("/tmp/");
+        }
+        return false;
     }
     
     public String [] getFlagNames() {



Mime
View raw message