Search in sources :

Example 1 with FlippingStrategy

use of org.ff4j.core.FlippingStrategy in project ff4j by ff4j.

the class ConsoleOperations method updateFlippingStrategy.

private static void updateFlippingStrategy(Feature fp, String strategy, String strategyParams) {
    if (null != strategy && !strategy.isEmpty()) {
        try {
            Class<?> strategyClass = Class.forName(strategy);
            FlippingStrategy fstrategy = (FlippingStrategy) strategyClass.newInstance();
            if (null != strategyParams && !strategyParams.isEmpty()) {
                Map<String, String> initParams = new HashMap<String, String>();
                String[] params = strategyParams.split(";");
                for (String currentP : params) {
                    String[] cur = currentP.split("=");
                    if (cur.length < 2) {
                        throw new IllegalArgumentException("Invalid Syntax : param1=val1,val2;param2=val3,val4");
                    }
                    initParams.put(cur[0], cur[1]);
                }
                fstrategy.init(fp.getUid(), initParams);
            }
            fp.setFlippingStrategy(fstrategy);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Cannot find strategy class", e);
        } catch (InstantiationException e) {
            throw new IllegalArgumentException("Cannot instantiate strategy", e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("Cannot instantiate : no public constructor", e);
        }
    }
}
Also used : HashMap(java.util.HashMap) FlippingStrategy(org.ff4j.core.FlippingStrategy)

Example 2 with FlippingStrategy

use of org.ff4j.core.FlippingStrategy in project ff4j by ff4j.

the class ConsoleRenderer method renderFeatureRows.

/**
 * Produce the rows of the Feature Table.
 *
 * @param ff4j
 *            target ff4j.
 * @param req
 *            current http request
 * @return string representing the list of features
 */
private static final String renderFeatureRows(FF4j ff4j, HttpServletRequest req) {
    StringBuilder sb = new StringBuilder();
    final Map<String, Feature> mapOfFeatures = ff4j.getFeatures();
    for (Map.Entry<String, Feature> uid : mapOfFeatures.entrySet()) {
        Feature currentFeature = uid.getValue();
        sb.append("<tr>" + END_OF_LINE);
        // Column with uid and description as tooltip
        sb.append("<td><a class=\"ff4j-tooltip\" ");
        if (null != currentFeature.getDescription()) {
            sb.append(" tooltip=\"");
            sb.append(currentFeature.getDescription());
            sb.append("\"");
        }
        sb.append(">");
        sb.append(currentFeature.getUid());
        sb.append("</a>");
        // Colonne Group
        sb.append("</td><td>");
        if (null != currentFeature.getGroup()) {
            sb.append(currentFeature.getGroup());
        } else {
            sb.append("--");
        }
        // Colonne Permissions
        sb.append("</td><td>");
        Set<String> permissions = currentFeature.getPermissions();
        if (null != permissions && !permissions.isEmpty()) {
            boolean first = true;
            for (String perm : permissions) {
                if (!first) {
                    sb.append(",");
                }
                sb.append(perm);
                first = false;
            }
        } else {
            sb.append("--");
        }
        // Colonne Strategy
        sb.append("</td><td style=\"word-break: break-all;\">");
        FlippingStrategy fs = currentFeature.getFlippingStrategy();
        if (null != fs) {
            sb.append(renderValue(fs.getClass().getName(), 50));
            if (fs.getInitParams() != null) {
                for (Map.Entry<String, String> entry : fs.getInitParams().entrySet()) {
                    sb.append("<li>" + renderValue(entry.getKey() + " =  " + entry.getValue(), 40));
                }
            }
        } else {
            sb.append("--");
        }
        // Colonne 'Holy' Toggle
        sb.append("</td><td style=\"width:8%;text-align:center\">");
        sb.append("<label class=\"switch switch-green\">");
        sb.append("<input id=\"" + currentFeature.getUid() + "\" type=\"checkbox\" class=\"switch-input\"");
        sb.append(" onclick=\"javascript:toggle(this)\" ");
        if (currentFeature.isEnable()) {
            sb.append(" checked");
        }
        sb.append(">");
        sb.append("<span class=\"switch-label\" data-on=\"On\" data-off=\"Off\"></span>");
        sb.append("<span class=\"switch-handle\"></span>");
        sb.append("</label>");
        // Colonne Button Edit
        sb.append("</td><td style=\"width:5%;text-align:center\">");
        sb.append("<a data-toggle=\"modal\" href=\"#modalEdit\" data-id=\"" + currentFeature.getUid() + "\" ");
        sb.append(" data-desc=\"" + currentFeature.getDescription() + "\"");
        sb.append(" data-group=\"" + currentFeature.getGroup() + "\"");
        sb.append(" data-strategy=\"");
        if (null != currentFeature.getFlippingStrategy()) {
            sb.append(currentFeature.getFlippingStrategy().getClass().getName());
        }
        sb.append("\" data-stratparams=\"");
        if (null != currentFeature.getFlippingStrategy()) {
            sb.append(currentFeature.getFlippingStrategy().getInitParams());
        }
        sb.append("\" data-permissions=\"");
        if (null != currentFeature.getPermissions() && !currentFeature.getPermissions().isEmpty()) {
            sb.append(currentFeature.getPermissions());
        }
        sb.append("\" style=\"width:6px;\" class=\"open-EditFlipDialog btn\">");
        sb.append("<i class=\"icon-pencil\" style=\"margin-left:-5px;\"></i></a>");
        // Colonne Button Delete
        sb.append("</td><td style=\"width:5%;text-align:center\">");
        sb.append("<a href=\"");
        sb.append(req.getContextPath());
        sb.append(req.getServletPath());
        sb.append("?op=" + OP_RMV_FEATURE + "&" + FEATID + "=" + uid.getKey());
        sb.append("\" style=\"width:6px;\" class=\"btn\">");
        sb.append("<i class=\"icon-trash\" style=\"margin-left:-5px;\"></i>");
        sb.append("</a>");
        sb.append("</td></tr>");
    }
    return sb.toString();
}
Also used : FlippingStrategy(org.ff4j.core.FlippingStrategy) PropertyString(org.ff4j.property.PropertyString) Feature(org.ff4j.core.Feature) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with FlippingStrategy

use of org.ff4j.core.FlippingStrategy in project ff4j by ff4j.

the class FeaturesController method buildFlippingStrategy.

/**
 * Create Flipping Strategy from parameters.
 *
 * @param req
 *            current http query
 * @param uid
 *      unique feature identifier
 * @return instance of strategy
 */
private FlippingStrategy buildFlippingStrategy(HttpServletRequest req, String uid) {
    String strategy = req.getParameter(STRATEGY);
    String strategyParams = req.getParameter(STRATEGY_INIT);
    FlippingStrategy fstrategy = null;
    Map<String, String> initParams = new HashMap<String, String>();
    if (Util.hasLength(strategy)) {
        if (Util.hasLength(strategyParams)) {
            String[] params = strategyParams.split(";");
            for (String currentP : params) {
                String[] cur = currentP.split("=");
                String value = (cur.length < 2) ? "" : cur[1];
                initParams.put(cur[0], value);
            }
        }
        fstrategy = MappingUtil.instanceFlippingStrategy(uid, strategy, initParams);
    }
    return fstrategy;
}
Also used : HashMap(java.util.HashMap) FlippingStrategy(org.ff4j.core.FlippingStrategy)

Example 4 with FlippingStrategy

use of org.ff4j.core.FlippingStrategy in project ff4j by ff4j.

the class FeatureAdvisor method check.

/**
 * Call if Flipped based on different parameters of the annotation
 *
 * @param ff
 *            annotation over current method
 * @param context
 * @return if flippinf should be considere
 */
protected boolean check(Flip ff, MethodInvocation mi) {
    // Retrieve optional context with ThreadLocal
    FlippingExecutionContext context = getFlippingContext(ff, mi);
    // Check ff4j
    String featureId = ff.name();
    if (ff.flippingStrategy() != NullType.class) {
        String fsClassName = ff.flippingStrategy().getName();
        FlippingStrategy fs = instanceFlippingStrategy(featureId, fsClassName, toMap(ff.flippingInitParams()));
        return getFf4j().checkOveridingStrategy(featureId, fs, context);
    }
    return getFf4j().check(featureId, context);
}
Also used : FlippingStrategy(org.ff4j.core.FlippingStrategy) MappingUtil.instanceFlippingStrategy(org.ff4j.utils.MappingUtil.instanceFlippingStrategy) FlippingExecutionContext(org.ff4j.core.FlippingExecutionContext)

Example 5 with FlippingStrategy

use of org.ff4j.core.FlippingStrategy in project ff4j by ff4j.

the class CoreFeatureStoreTestSupport method testUpdateFeatureCoreData.

/**
 * TDD.
 */
@Test
public void testUpdateFeatureCoreData() {
    // Parameters
    String newDescription = "new-description";
    FlippingStrategy newStrategy = new PonderationStrategy(0.12);
    // Given
    assertFf4j.assertThatFeatureExist(F1);
    Assert.assertFalse(newDescription.equals(testedStore.read(F1).getDescription()));
    // When
    Feature fpBis = testedStore.read(F1);
    fpBis.setDescription(newDescription);
    fpBis.setFlippingStrategy(newStrategy);
    testedStore.update(fpBis);
    // Then
    Feature updatedFeature = testedStore.read(F1);
    Assert.assertTrue(newDescription.equals(updatedFeature.getDescription()));
    Assert.assertNotNull(updatedFeature.getFlippingStrategy());
    Assert.assertEquals(newStrategy.toString(), updatedFeature.getFlippingStrategy().toString());
}
Also used : PonderationStrategy(org.ff4j.strategy.PonderationStrategy) FlippingStrategy(org.ff4j.core.FlippingStrategy) PropertyString(org.ff4j.property.PropertyString) Feature(org.ff4j.core.Feature) Test(org.junit.Test)

Aggregations

FlippingStrategy (org.ff4j.core.FlippingStrategy)18 Feature (org.ff4j.core.Feature)9 PropertyString (org.ff4j.property.PropertyString)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 FlippingExecutionContext (org.ff4j.core.FlippingExecutionContext)4 PonderationStrategy (org.ff4j.strategy.PonderationStrategy)4 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 InMemoryFeatureStore (org.ff4j.store.InMemoryFeatureStore)2 AbstractFf4jTest (org.ff4j.test.AbstractFf4jTest)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 NodeList (org.w3c.dom.NodeList)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 FeatureStore (org.ff4j.core.FeatureStore)1 FeatureAccessException (org.ff4j.exception.FeatureAccessException)1