Search in sources :

Example 26 with XmlParser

use of org.ff4j.conf.XmlParser 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 27 with XmlParser

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

the class InMemoryPropertiesStoreTest method testProperty.

public void testProperty() {
    FF4j ff4j = new FF4j(new XmlParser(), "ff4j.xml");
    ff4j.getPropertiesStore().createProperty(new PropertyDate("property_3", new Date()));
    Property<?> ap = ff4j.getPropertiesStore().readProperty("property_3");
    PropertyDate pDate = (PropertyDate) ap;
    pDate.setValue(new Date());
    ff4j.getPropertiesStore().updateProperty(pDate);
    ff4j.getPropertiesStore().deleteProperty("property_3");
}
Also used : XmlParser(org.ff4j.conf.XmlParser) FF4j(org.ff4j.FF4j) Date(java.util.Date) PropertyDate(org.ff4j.property.PropertyDate) PropertyDate(org.ff4j.property.PropertyDate)

Example 28 with XmlParser

use of org.ff4j.conf.XmlParser 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 29 with XmlParser

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

the class XmlParserTest method testNullValues.

@Test
public void testNullValues() throws IOException {
    new XmlParser().escapeXML(null);
    new XmlParser().exportProperties(new HashMap<String, Property<?>>());
}
Also used : XmlParser(org.ff4j.conf.XmlParser) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 30 with XmlParser

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

the class XmlParserTest method parseFile.

private void parseFile(String fileName) {
    // Given
    InputStream in = getClass().getClassLoader().getResourceAsStream(fileName);
    if (in == null)
        Assert.fail("Xml file must exist");
    // When
    new XmlParser().parseConfigurationFile(in);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) InputStream(java.io.InputStream)

Aggregations

XmlParser (org.ff4j.conf.XmlParser)44 Test (org.junit.Test)29 InputStream (java.io.InputStream)22 FF4j (org.ff4j.FF4j)17 XmlConfig (org.ff4j.conf.XmlConfig)14 Feature (org.ff4j.core.Feature)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Property (org.ff4j.property.Property)7 FF4jConfiguration (org.ff4j.conf.FF4jConfiguration)4 Date (java.util.Date)3 Before (org.junit.Before)3 ArrayList (java.util.ArrayList)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 FileItem (org.apache.commons.fileupload.FileItem)2 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 YamlParser (org.ff4j.parser.yaml.YamlParser)2 PropertyDate (org.ff4j.property.PropertyDate)2 SpringSecurityAuthorisationManager (org.ff4j.security.SpringSecurityAuthorisationManager)2 TestConstantsFF4j (org.ff4j.test.TestConstantsFF4j)2