Search in sources :

Example 1 with XmlConfig

use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.

the class AbstractPropertyStore method importPropertiesFromXmlFile.

/**
 * Initialize store from XML Configuration File.
 *
 * @param xmlConfFile
 *      xml configuration file
 */
public Map<String, Property<?>> importPropertiesFromXmlFile(String xmlConfFile) {
    // Argument validation
    if (xmlConfFile == null || xmlConfFile.isEmpty()) {
        throw new IllegalArgumentException("Configuration filename cannot be null nor empty");
    }
    // Load as Inputstream
    InputStream xmlIS = getClass().getClassLoader().getResourceAsStream(xmlConfFile);
    if (xmlIS == null) {
        throw new IllegalArgumentException("File " + xmlConfFile + " could not be read, please check path and rights");
    }
    // Use the Feature Parser
    XmlConfig conf = new XmlParser().parseConfigurationFile(xmlIS);
    Map<String, Property<?>> properties = conf.getProperties();
    // Override existing configuration within database
    for (Map.Entry<String, Property<?>> featureName : properties.entrySet()) {
        if (existProperty(featureName.getKey())) {
            deleteProperty(featureName.getKey());
        }
        createProperty(featureName.getValue());
    }
    return properties;
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) InputStream(java.io.InputStream) Property(org.ff4j.property.Property) Map(java.util.Map)

Example 2 with XmlConfig

use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.

the class FeatureXmlParserTest method testParsingALL.

@Test
public void testParsingALL() throws IOException {
    // Given
    XmlParser parser = new XmlParser();
    InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j-parser-all.xml");
    // When
    XmlConfig conf = parser.parseConfigurationFile(in);
    // Then
    Map<String, Feature> features = conf.getFeatures();
    Assert.assertNotNull(features);
    // Then
    Map<String, Property<?>> properties = conf.getProperties();
    Assert.assertNotNull(properties);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 3 with XmlConfig

use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.

the class FeatureXmlParserTest method testParsingFeatures.

@Test
public void testParsingFeatures() throws IOException {
    // Given
    XmlParser parser = new XmlParser();
    InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j-parser-features.xml");
    // When
    XmlConfig conf = parser.parseConfigurationFile(in);
    // Then
    Map<String, Feature> features = conf.getFeatures();
    Assert.assertNotNull(features);
    // Then
    Map<String, Property<?>> properties = conf.getProperties();
    Assert.assertNotNull(properties);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 4 with XmlConfig

use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.

the class FeatureXmlParserTest method testPropertiesParsing.

@Test
public void testPropertiesParsing() throws IOException {
    // Given
    XmlParser parser = new XmlParser();
    InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j.xml");
    // When
    XmlConfig conf = parser.parseConfigurationFile(in);
    // Then
    Map<String, Feature> features = conf.getFeatures();
    Assert.assertNotNull(features);
    Feature f = features.get("first");
    Assert.assertNotNull(f);
    Assert.assertNotNull(f.getUid());
    Assert.assertNotNull(f.getCustomProperties());
    Assert.assertNotNull(f.getCustomProperties().get("ppint"));
    Assert.assertEquals(f.getCustomProperties().get("ppint").asInt(), 12);
    Assert.assertEquals(f.getCustomProperties().get("ppdouble").asDouble(), 12.5, 0);
    Assert.assertEquals(f.getCustomProperties().get("ppboolean").asBoolean(), true);
    Assert.assertEquals(f.getCustomProperties().get("ppstring").asString(), "hello");
    Assert.assertEquals(f.getCustomProperties().get("regionIdentifier").asString(), "AMER");
    Assert.assertNotNull(f.getCustomProperties().get("regionIdentifier").getFixedValues());
    Assert.assertFalse(f.getCustomProperties().get("regionIdentifier").getFixedValues().isEmpty());
    PropertyLogLevel pll = (PropertyLogLevel) f.getCustomProperties().get("myLogLevel");
    Assert.assertEquals(pll.getValue(), LogLevel.DEBUG);
    // Then
    Map<String, Property<?>> properties = conf.getProperties();
    Assert.assertNotNull(properties);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) PropertyLogLevel(org.ff4j.property.PropertyLogLevel) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 5 with XmlConfig

use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.

the class AbstractFeatureStore method importFeaturesFromXmlFile.

/**
 * Initialize store from XML Configuration File.
 *
 * @param xmlConfFile
 *      xml configuration file
 */
public Map<String, Feature> importFeaturesFromXmlFile(String xmlConfFile) {
    // Argument validation
    if (xmlConfFile == null || xmlConfFile.isEmpty()) {
        throw new IllegalArgumentException("Configuration filename cannot be null nor empty");
    }
    // Load as Inputstream
    InputStream xmlIS = getClass().getClassLoader().getResourceAsStream(xmlConfFile);
    if (xmlIS == null) {
        throw new IllegalArgumentException("File " + xmlConfFile + " could not be read, please check path and rights");
    }
    // Use the Feature Parser
    XmlConfig conf = new XmlParser().parseConfigurationFile(xmlIS);
    Map<String, Feature> features = conf.getFeatures();
    importFeatures(features.values());
    return features;
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature)

Aggregations

XmlConfig (org.ff4j.conf.XmlConfig)8 XmlParser (org.ff4j.conf.XmlParser)8 InputStream (java.io.InputStream)7 Feature (org.ff4j.core.Feature)6 Property (org.ff4j.property.Property)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Test (org.junit.Test)5 Map (java.util.Map)1 FeatureStore (org.ff4j.core.FeatureStore)1 PropertyLogLevel (org.ff4j.property.PropertyLogLevel)1 PropertyStore (org.ff4j.property.store.PropertyStore)1