@Override public boolean isActive(FeatureState state, FeatureUser user) { if (user != null && Strings.isNotBlank(user.getName())) { String percentageAsString = state.getParameter(PARAM_PERCENTAGE); try { int percentage = Integer.valueOf(percentageAsString); if (percentage > 0) { int hashCode = Math.abs(calculateHashCode(user, state.getFeature())); return (hashCode % 100) < percentage; } } catch (NumberFormatException e) { log.error( "Invalid gradual rollout percentage for feature " + state.getFeature().name() + ": " + percentageAsString); } } return false; }
@Override public boolean isActive(FeatureState featureState, FeatureUser user) { String lang = featureState.getParameter(PARAM_LANG); String script = featureState.getParameter(PARAM_SCRIPT); ScriptEngine engine = engineManager.getEngineByName(lang); if (engine == null) { log.error("Could not find script engine for: " + lang); return false; } engine.put("user", user); engine.put("date", new Date()); try { Object result = engine.eval(script); if (result instanceof Boolean) { return ((Boolean) result).booleanValue(); } } catch (ScriptException e) { log.error( "Could not evaluate script for feature " + featureState.getFeature().name() + ": " + e.getMessage()); } return false; }
@Override public void setFeatureState(FeatureState state) { if (state.isEnabled()) { activeFeatures.add(state.getFeature().name()); } else { activeFeatures.remove(state.getFeature().name()); } }
@Override public FeatureState getFeatureState(Feature feature) { // first try to find it from the cache CacheEntry entry = cache.get(feature); if (entry != null && !isExpired(entry)) { return entry.getState() != null ? entry.getState().copy() : null; } // no cache hit FeatureState featureState = delegate.getFeatureState(feature); // cache the result (may be null) cache.put(feature, new CacheEntry(featureState != null ? featureState.copy() : null)); // return the result return featureState; }
@Override public void setFeatureState(FeatureState featureState) { delegate.setFeatureState(featureState); cache.remove(featureState.getFeature()); }