Search in sources :

Example 36 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class FeatureStoreCassandra method mapFeatureRow.

protected Feature mapFeatureRow(Row row) {
    Feature f = new Feature(row.getString(FEATURES_ATT_UID));
    f.setDescription(row.getString(FEATURES_ATT_DESCRIPTION));
    f.setEnable(row.getBoolean(FEATURES_ATT_ENABLED));
    f.setGroup(row.getString(FEATURES_ATT_GROUPNAME));
    f.setPermissions(row.getSet(FEATURES_ATT_ROLES, String.class));
    // Flipping Strategy
    UdtValue udtStrat = row.getUdtValue(FEATURES_ATT_STRATEGY);
    if (null != udtStrat) {
        String className = udtStrat.getString(UDT_STRATEGY_CLASS);
        Map<String, String> initparams = udtStrat.getMap(UDT_STRATEGY_PARAMS, String.class, String.class);
        f.setFlippingStrategy(MappingUtil.instanceFlippingStrategy(f.getUid(), className, initparams));
    }
    // Custom Properties
    Map<String, UdtValue> mapOfProperties = row.getMap(FEATURES_ATT_PROPERTIES, String.class, UdtValue.class);
    if (mapOfProperties != null) {
        Map<String, Property<?>> customProperties = new HashMap<String, Property<?>>();
        for (UdtValue udt : mapOfProperties.values()) {
            String propName = udt.getString(UDT_PROPERTY_UID);
            String propClass = udt.getString(UDT_PROPERTY_CLASS);
            String propDesc = udt.getString(UDT_PROPERTY_DESCRIPTION);
            String propVal = udt.getString(UDT_PROPERTY_VALUE);
            Set<String> fixV = udt.getSet(UDT_PROPERTY_FIXEDVALUES, String.class);
            Property<?> p = PropertyFactory.createProperty(propName, propClass, propVal, propDesc, fixV);
            customProperties.put(p.getName(), p);
        }
        f.setCustomProperties(customProperties);
    }
    return f;
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) HashMap(java.util.HashMap) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property)

Example 37 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class PropertyStoreEhCache method readAllProperties.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Property<?>> readAllProperties() {
    Map<String, Property<?>> myMap = new HashMap<String, Property<?>>();
    if (wrapper.getCacheProperties().getKeys() != null) {
        for (Object key : wrapper.getCacheProperties().getKeys()) {
            Element element = wrapper.getCacheProperties().get(key);
            if (element != null) {
                Property<?> p = (Property<?>) wrapper.getCacheProperties().get(key).getObjectValue();
                myMap.put((String) key, p);
            }
        }
    }
    return myMap;
}
Also used : HashMap(java.util.HashMap) Element(net.sf.ehcache.Element) Property(org.ff4j.property.Property)

Example 38 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class PropertyFactory method createProperty.

/**
 * Factory method to create property.
 *
 * @param pName
 *            property name.
 * @param pType
 *            property type
 * @param pValue
 *            property value
 * @return
 */
public static Property<?> createProperty(String pName, Object value) {
    Util.assertHasLength(pName);
    Util.assertNotNull(value);
    if (validPropertyPrimitives.containsKey(value.getClass())) {
        return PropertyFactory.createProperty(pName, validPropertyPrimitives.get(value.getClass()).getName(), String.valueOf(value), null, null);
    }
    if (value instanceof Date) {
        return PropertyFactory.createProperty(pName, PropertyDate.class.getName(), PropertyDate.SDF.format(value), null, null);
    }
    if (value instanceof Calendar) {
        Date valueDate = ((Calendar) value).getTime();
        return PropertyFactory.createProperty(pName, PropertyCalendar.class.getName(), PropertyCalendar.SDF.format(valueDate), null, null);
    }
    if (value instanceof Property<?>) {
        return (Property<?>) value;
    }
    // String Value
    if (value.getClass().isArray() || Util.isCollection(value)) {
        return PropertyFactory.createProperty(pName, PropertyString.class.getName(), Util.join(Util.asCollection(value), ","), null, null);
    }
    throw new IllegalArgumentException("Cannot create property with input type " + value.getClass() + value.toString());
}
Also used : PropertyString(org.ff4j.property.PropertyString) Calendar(java.util.Calendar) PropertyCalendar(org.ff4j.property.PropertyCalendar) PropertyCalendar(org.ff4j.property.PropertyCalendar) Property(org.ff4j.property.Property) Date(java.util.Date) PropertyDate(org.ff4j.property.PropertyDate) PropertyDate(org.ff4j.property.PropertyDate)

Example 39 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class XmlParser method exportFeaturesPart.

/**
 * Export Features part of the XML.
 *
 * @param mapOfFeatures
 *      current map of feaures.
 *
 * @return
 *      all XML
 */
private String exportFeaturesPart(Map<String, Feature> mapOfFeatures) {
    // Create <features>
    StringBuilder sb = new StringBuilder(BEGIN_FEATURES);
    // Recreate Groups
    Map<String, List<Feature>> featuresPerGroup = new HashMap<String, List<Feature>>();
    if (mapOfFeatures != null && !mapOfFeatures.isEmpty()) {
        for (Feature feat : mapOfFeatures.values()) {
            String groupName = feat.getGroup();
            if (!featuresPerGroup.containsKey(groupName)) {
                featuresPerGroup.put(groupName, new ArrayList<Feature>());
            }
            featuresPerGroup.get(groupName).add(feat);
        }
    }
    for (Map.Entry<String, List<Feature>> groupName : featuresPerGroup.entrySet()) {
        // / Building featureGroup
        if (null != groupName.getKey() && !groupName.getKey().isEmpty()) {
            sb.append(" <" + FEATUREGROUP_TAG + " " + FEATUREGROUP_ATTNAME + "=\"" + groupName.getKey() + "\" >\n\n");
        }
        // Loop on feature
        for (Feature feat : groupName.getValue()) {
            sb.append(MessageFormat.format(XML_FEATURE, feat.getUid(), escapeXML(feat.getDescription()), feat.isEnable()));
            // <security>
            if (null != feat.getPermissions() && !feat.getPermissions().isEmpty()) {
                sb.append("   <" + SECURITY_TAG + ">\n");
                for (String auth : feat.getPermissions()) {
                    sb.append(MessageFormat.format(XML_AUTH, escapeXML(auth)));
                }
                sb.append("   </" + SECURITY_TAG + ">\n");
            }
            // <flipstrategy>
            FlippingStrategy fs = feat.getFlippingStrategy();
            if (null != fs) {
                sb.append("   <" + FLIPSTRATEGY_TAG + " class=\"" + fs.getClass().getName() + "\" >\n");
                for (String p : fs.getInitParams().keySet()) {
                    sb.append("     <" + FLIPSTRATEGY_PARAMTAG + " " + FLIPSTRATEGY_PARAMNAME + "=\"");
                    sb.append(p);
                    sb.append("\" " + FLIPSTRATEGY_PARAMVALUE + "=\"");
                    // Escape special characters to build XML
                    // https://github.com/clun/ff4j/issues/63
                    String paramValue = fs.getInitParams().get(p);
                    sb.append(escapeXML(paramValue));
                    sb.append("\" />\n");
                }
                sb.append("   </" + FLIPSTRATEGY_TAG + ">\n");
            }
            // <custom-properties>
            Map<String, Property<?>> props = feat.getCustomProperties();
            if (props != null && !props.isEmpty()) {
                sb.append(BEGIN_CUSTOMPROPERTIES);
                sb.append(buildPropertiesPart(feat.getCustomProperties()));
                sb.append(END_CUSTOMPROPERTIES);
            }
            sb.append(END_FEATURE);
        }
        if (null != groupName.getKey() && !groupName.getKey().isEmpty()) {
            sb.append(" </" + FEATUREGROUP_TAG + ">\n\n");
        }
    }
    sb.append(END_FEATURES);
    return sb.toString();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FlippingStrategy(org.ff4j.core.FlippingStrategy) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) PropertyString(org.ff4j.property.PropertyString) Feature(org.ff4j.core.Feature) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) Property(org.ff4j.property.Property)

Example 40 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class GeneratorUtils method generateInterfaceConstantsSource.

/**
 * Generate target Java Interface.
 *
 * @param ff4j
 *      current ff4J bean
 * @return
 *      data as inputstream
 * @throws IOException
 *             error occu
 */
public static String generateInterfaceConstantsSource(FF4j ff4j) {
    Util.assertNotNull(ff4j);
    StringBuilder sb = new StringBuilder();
    sb.append("/**\r\n * Constants for ff4j features and properties.");
    sb.append("\r\n * Generated on : " + new SimpleDateFormat("yyyy-MM-DD HH:mm").format(new Date()));
    sb.append("\r\n *");
    sb.append("\r\n * @author FF4J Generator Engine");
    sb.append("\r\n */");
    sb.append("\r\npublic interface FF4jConstants {");
    sb.append("\r\n");
    sb.append("\r\n   // -------------------------");
    sb.append("\r\n   //  Features ");
    sb.append("\r\n   // -------------------------");
    sb.append("\r\n");
    for (Map.Entry<String, Feature> feat : ff4j.getFeatureStore().readAll().entrySet()) {
        sb.append("\r\n   /* Feature '" + feat.getKey() + "' : '" + feat.getValue().getDescription() + "' */");
        sb.append("\r\n   String FEATURE_" + feat.getKey().replaceAll(" ", "_").toUpperCase() + " = \"" + feat.getKey() + "\";");
        sb.append("\r\n");
    }
    sb.append("\r\n   // -------------------------");
    sb.append("\r\n   //  Groups ");
    sb.append("\r\n   // -------------------------");
    sb.append("\r\n");
    for (String groupName : ff4j.getFeatureStore().readAllGroups()) {
        sb.append("\r\n   /* Group '" + groupName + "' */");
        sb.append("\r\n   String FEATURE_GROUP_" + groupName.replaceAll(" ", "_").toUpperCase() + " = \"" + groupName + "\";");
        sb.append("\r\n");
    }
    sb.append("\r\n   // -------------------------");
    sb.append("\r\n   //  Properties ");
    sb.append("\r\n   // -------------------------");
    sb.append("\r\n");
    for (Map.Entry<String, Property<?>> prop : ff4j.getPropertiesStore().readAllProperties().entrySet()) {
        sb.append("\r\n   /* Property '" + prop.getKey() + "' : '" + prop.getValue().getDescription() + "' */");
        sb.append("\r\n   String PROPERTY_" + prop.getKey().replaceAll(" ", "_").toUpperCase() + " = \"" + prop.getKey() + "\";");
        sb.append("\r\n");
    }
    sb.append("\r\n}");
    return sb.toString();
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Date(java.util.Date)

Aggregations

Property (org.ff4j.property.Property)56 HashMap (java.util.HashMap)20 PropertyString (org.ff4j.property.PropertyString)20 Test (org.junit.Test)20 Feature (org.ff4j.core.Feature)16 Map (java.util.Map)11 InputStream (java.io.InputStream)9 XmlParser (org.ff4j.conf.XmlParser)7 InMemoryPropertyStore (org.ff4j.property.store.InMemoryPropertyStore)7 LinkedHashMap (java.util.LinkedHashMap)6 XmlConfig (org.ff4j.conf.XmlConfig)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 FeatureStore (org.ff4j.core.FeatureStore)4 PropertyAccessException (org.ff4j.exception.PropertyAccessException)4 PropertyStore (org.ff4j.property.store.PropertyStore)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 FF4jCacheProxy (org.ff4j.cache.FF4jCacheProxy)3