Search in sources :

Example 51 with Property

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

the class YamlParser method parseProperties.

@SuppressWarnings("unchecked")
private Map<String, Property<?>> parseProperties(List<Map<String, Object>> properties) {
    Map<String, Property<?>> result = new HashMap<>();
    if (null != properties) {
        properties.forEach(property -> {
            // Initiate with name and value
            String name = (String) property.get(PROPERTY_PARAMNAME);
            if (null == name) {
                throw new IllegalArgumentException("Invalid YAML File: 'name' is expected for properties");
            }
            Object objValue = property.get(PROPERTY_PARAMVALUE);
            if (null == objValue) {
                throw new IllegalArgumentException("Invalid YAML File: 'value' is expected for properties");
            }
            // Convert as a String
            String strValue = String.valueOf(objValue);
            if (objValue instanceof Date) {
                strValue = SIMPLE_DATE_FORMAT.format((Date) objValue);
            }
            Property<?> ap = new PropertyString(name, strValue);
            String optionalType = (String) property.get(PROPERTY_PARAMTYPE);
            // If specific type defined ?
            if (null != optionalType) {
                // Substitution if relevant (e.g. 'int' -> 'org.ff4j.property.PropertyInt')
                optionalType = MappingUtil.mapPropertyType(optionalType);
                try {
                    // Constructor (String, String) is mandatory in Property interface
                    Constructor<?> constr = Class.forName(optionalType).getConstructor(String.class, String.class);
                    ap = (Property<?>) constr.newInstance(name, strValue);
                } catch (Exception e) {
                    throw new IllegalArgumentException("Cannot instantiate '" + optionalType + "' check default constructor", e);
                }
            }
            // Description
            String description = (String) property.get(PROPERTY_PARAMDESCRIPTION);
            if (null != description) {
                ap.setDescription(description);
            }
            // Fixed Values
            List<Object> fixedValues = (List<Object>) property.get(PROPERTY_PARAMFIXED_VALUES);
            if (null != fixedValues && fixedValues.size() > 0) {
                fixedValues.stream().map(Object::toString).forEach(ap::add2FixedValueFromString);
            }
            // Check fixed value
            if (ap.getFixedValues() != null && !ap.getFixedValues().isEmpty() && !ap.getFixedValues().contains(ap.getValue())) {
                throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues());
            }
            result.put(ap.getName(), ap);
        });
    }
    return result;
}
Also used : PropertyString(org.ff4j.property.PropertyString) HashMap(java.util.HashMap) PropertyString(org.ff4j.property.PropertyString) Date(java.util.Date) HashMap(java.util.HashMap) Map(java.util.Map) List(java.util.List) Property(org.ff4j.property.Property)

Example 52 with Property

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

the class MongoFeatureMapper method mapCustomProperties.

/**
 * Custom Properties.
 *
 * @param dbObject
 *      db object
 * @return
 *      list of property
 */
private Map<String, Property<?>> mapCustomProperties(Document dbObject) {
    Map<String, Property<?>> mapOfCustomProperties = new HashMap<>();
    if (dbObject.containsKey(FEATURE_CUSTOMPROPERTIES)) {
        String properties = (String) dbObject.get(FEATURE_CUSTOMPROPERTIES);
        BasicDBObject values = BasicDBObject.parse(properties);
        for (Map.Entry<String, Object> entry : values.entrySet()) {
            mapOfCustomProperties.put(entry.getKey(), PMAPPER.fromStore((DBObject) entry.getValue()));
        }
    }
    return mapOfCustomProperties;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Property(org.ff4j.property.Property) HashMap(java.util.HashMap) Map(java.util.Map) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 53 with Property

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

the class HBaseFeatureMapper method toStore.

/**
 * {@inheritDoc}
 */
@Override
public Put toStore(Feature fp) {
    Put put = new Put(Bytes.toBytes(fp.getUid()));
    // uid
    put.addColumn(B_FEATURES_CF_CORE, B_FEAT_UID, Bytes.toBytes(fp.getUid()));
    // description
    byte[] desc = (fp.getDescription() == null) ? null : Bytes.toBytes(fp.getDescription());
    put.addColumn(B_FEATURES_CF_CORE, B_FEAT_DESCRIPTION, desc);
    // enable
    put.addColumn(B_FEATURES_CF_CORE, B_FEAT_ENABLE, Bytes.toBytes(fp.isEnable()));
    // group
    byte[] group = (fp.getGroup() == null) ? null : Bytes.toBytes(fp.getGroup());
    put.addColumn(B_FEATURES_CF_CORE, B_FEAT_GROUPNAME, group);
    // permission
    put.addColumn(B_FEATURES_CF_CORE, B_FEAT_ROLES, Bytes.toBytes(JsonUtils.permissionsAsJson(fp.getPermissions())));
    // Flipping strategy
    put.addColumn(B_FEATURES_CF_CORE, B_FEAT_STRATEGY, Bytes.toBytes(JsonUtils.flippingStrategyAsJson(fp.getFlippingStrategy())));
    // Custom Properties
    if (fp.getCustomProperties() != null && !fp.getCustomProperties().isEmpty()) {
        for (Map.Entry<String, Property<?>> customP : fp.getCustomProperties().entrySet()) {
            if (customP.getValue() != null) {
                put.addColumn(B_FEATURES_CF_PROPERTIES, Bytes.toBytes(customP.getKey()), Bytes.toBytes(customP.getValue().toJson()));
            }
        }
    }
    return put;
}
Also used : NavigableMap(java.util.NavigableMap) Map(java.util.Map) Property(org.ff4j.property.Property) Put(org.apache.hadoop.hbase.client.Put)

Example 54 with Property

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

the class FeatureDynamoDBMapper method fromStore.

@Override
public Feature fromStore(DynamoDbFeature item) {
    Feature feature = new Feature(item.getFeatureUid());
    feature.setEnable(item.isEnable());
    feature.setDescription(item.getDescription());
    feature.setGroup(item.getGroupName());
    String jsonFlippingStrategy = item.getFlippingStrategy();
    if (Util.hasLength(jsonFlippingStrategy)) {
        feature.setFlippingStrategy(FeatureJsonParser.parseFlipStrategyAsJson(feature.getUid(), jsonFlippingStrategy));
    }
    Set<String> perms = item.getPermissions();
    feature.setPermissions(perms == null ? new HashSet<>() : perms);
    Map<String, String> props = item.getProperties();
    if (props != null) {
        Map<String, Property<?>> customProperties = new HashMap<>();
        for (Map.Entry<String, String> propString : props.entrySet()) {
            customProperties.put(propString.getKey(), PropertyJsonParser.parseProperty(propString.getValue()));
        }
        feature.setCustomProperties(customProperties);
    }
    return feature;
}
Also used : HashMap(java.util.HashMap) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 55 with Property

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

the class JdbcFeatureStoreErrorTest method testcreateCustomKO.

@Test(expected = FeatureAccessException.class)
public void testcreateCustomKO() throws SQLException {
    DataSource mockDS = Mockito.mock(DataSource.class);
    doThrow(new SQLException()).when(mockDS).getConnection();
    JdbcFeatureStore jrepo = new JdbcFeatureStore(mockDS);
    jrepo.setDataSource(mockDS);
    List<Property<?>> lp = new ArrayList<Property<?>>();
    lp.add(new PropertyString("p1", "v1"));
    lp.add(new PropertyString("p2", "v2"));
    jrepo.createCustomProperties("F1", lp);
}
Also used : JdbcFeatureStore(org.ff4j.store.JdbcFeatureStore) PropertyString(org.ff4j.property.PropertyString) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Property(org.ff4j.property.Property) DataSource(javax.sql.DataSource) Test(org.junit.Test)

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