Search in sources :

Example 11 with XmlConfig

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

the class ConsoleOperations method importFile.

/**
 * User action to import Features from a properties files.
 *
 * @param in
 *            inpustream from configuration file
 * @throws IOException
 *             Error raised if the configuration cannot be read
 */
public static void importFile(FF4j ff4j, InputStream in) throws IOException {
    FeatureStore store = ff4j.getFeatureStore();
    XmlConfig xmlConfig = new XmlParser().parseConfigurationFile(in);
    Map<String, Feature> mapsOfFeat = xmlConfig.getFeatures();
    for (Entry<String, Feature> feature : mapsOfFeat.entrySet()) {
        if (store.exist(feature.getKey())) {
            store.update(feature.getValue());
        } else {
            store.create(feature.getValue());
        }
    }
    LOGGER.info(mapsOfFeat.size() + " features have been imported.");
    PropertyStore pstore = ff4j.getPropertiesStore();
    Map<String, Property<?>> mapsOfProperties = xmlConfig.getProperties();
    for (Entry<String, Property<?>> p : mapsOfProperties.entrySet()) {
        if (pstore.existProperty(p.getKey())) {
            pstore.updateProperty(p.getValue());
        } else {
            pstore.createProperty(p.getValue());
        }
    }
    LOGGER.info(mapsOfProperties.size() + " features have been imported.");
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) FeatureStore(org.ff4j.core.FeatureStore) PropertyStore(org.ff4j.property.store.PropertyStore)

Example 12 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)

Example 13 with XmlConfig

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

the class FeatureXmlParserTest method testParsingProperties.

@Test
public void testParsingProperties() throws IOException {
    // Given
    XmlParser parser = new XmlParser();
    InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j-parser-properties.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 14 with XmlConfig

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

the class FeatureXmlParserTest method importThenExportALL.

@Test
public void importThenExportALL() throws IOException {
    // Given
    XmlParser parser = new XmlParser();
    InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j-parser-all.xml");
    XmlConfig conf = parser.parseConfigurationFile(in);
    Assert.assertNotNull(conf.getFeatures());
    Assert.assertNotNull(conf.getProperties());
    // When
    InputStream in3 = parser.exportAll(conf);
    // Then
    XmlConfig conf2 = parser.parseConfigurationFile(in3);
    Assert.assertNotNull(conf2.getFeatures());
    Assert.assertNotNull(conf2.getProperties());
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

XmlConfig (org.ff4j.conf.XmlConfig)14 XmlParser (org.ff4j.conf.XmlParser)14 InputStream (java.io.InputStream)13 Test (org.junit.Test)10 Feature (org.ff4j.core.Feature)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Property (org.ff4j.property.Property)6 FF4jConfiguration (org.ff4j.conf.FF4jConfiguration)3 Date (java.util.Date)1 Map (java.util.Map)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 FeatureStore (org.ff4j.core.FeatureStore)1 YamlParser (org.ff4j.parser.yaml.YamlParser)1 PropertyLogLevel (org.ff4j.property.PropertyLogLevel)1 PropertyStore (org.ff4j.property.store.PropertyStore)1