Search in sources :

Example 1 with FF4jConfiguration

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

the class PropertiesParser method parseConfiguration.

/**
 * Load properties from environments.
 */
public FF4jConfiguration parseConfiguration(Map<String, String> mapProperties) {
    Util.assertNotNull(mapProperties, "Cannot parse null properties");
    FF4jConfiguration ff4jConfig = new FF4jConfiguration();
    // Audit
    if (mapProperties.containsKey(FF4J_TAG + "." + GLOBAL_AUDIT_TAG)) {
        ff4jConfig.setAudit(Boolean.valueOf(mapProperties.get(FF4J_TAG + "." + GLOBAL_AUDIT_TAG)));
    }
    // AutoCreate
    if (mapProperties.containsKey(FF4J_TAG + "." + GLOBAL_AUTOCREATE)) {
        ff4jConfig.setAutoCreate(Boolean.valueOf(mapProperties.get(FF4J_TAG + "." + GLOBAL_AUTOCREATE)));
    }
    // Properties
    ff4jConfig.getProperties().putAll(parseProperties(FF4J_TAG + "." + PROPERTIES_TAG, mapProperties));
    // Features
    parseFeatures(ff4jConfig, mapProperties);
    return ff4jConfig;
}
Also used : FF4jConfiguration(org.ff4j.conf.FF4jConfiguration)

Example 2 with FF4jConfiguration

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

the class PropertiesParserTest method importProperties_should_be_same_asXMLImport.

@Test
public void importProperties_should_be_same_asXMLImport() {
    // Give XML an YAML files
    InputStream xmlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.xml");
    InputStream ymlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.properties");
    // When parsing those files
    XmlConfig xmlConfig = new XmlParser().parseConfigurationFile(xmlFile);
    FF4jConfiguration propsConfig = new PropertiesParser().parseConfigurationFile(ymlFile);
    // Than both config are even
    assertEquals(xmlConfig.getFeatures().size(), propsConfig.getFeatures().size());
    assertEquals(xmlConfig.getProperties().size(), propsConfig.getProperties().size());
    // Custom-properties
    Feature f1Xml = xmlConfig.getFeatures().get("first");
    Feature f1props = propsConfig.getFeatures().get("first");
    assertEquals(f1Xml.getDescription(), f1props.getDescription());
    assertEquals(f1Xml.getCustomProperties().size(), f1props.getCustomProperties().size());
    // FlipStrategy & Permission
    Feature f3Xml = xmlConfig.getFeatures().get("third");
    Feature f3Props = propsConfig.getFeatures().get("third");
    assertEquals(f3Xml.getFlippingStrategy().getClass(), f3Props.getFlippingStrategy().getClass());
    assertEquals(f3Xml.getFlippingStrategy().getInitParams().get("expression"), f3Props.getFlippingStrategy().getInitParams().get("expression"));
    assertEquals(f3Xml.getPermissions(), f3Props.getPermissions());
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) FF4jConfiguration(org.ff4j.conf.FF4jConfiguration) Test(org.junit.Test)

Example 3 with FF4jConfiguration

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

the class YamlParser method parseConfigurationFile.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public FF4jConfiguration parseConfigurationFile(InputStream inputStream) {
    Util.assertNotNull(inputStream, "Cannot read file stream is empty, check readability and path.");
    // Strengthen serialization
    Map<?, ?> yamlConfigFile = safeYaml.load(inputStream);
    Map<?, ?> ff4jYamlMap = (Map<?, ?>) yamlConfigFile.get(FF4J_TAG);
    FF4jConfiguration ff4jConfig = new FF4jConfiguration();
    if (ff4jYamlMap != null) {
        // Audit
        if (ff4jYamlMap.containsKey(GLOBAL_AUDIT_TAG)) {
            ff4jConfig.setAudit(Boolean.valueOf(ff4jYamlMap.get(GLOBAL_AUDIT_TAG).toString()));
        }
        // AutoCreate
        if (ff4jYamlMap.containsKey(GLOBAL_AUTOCREATE)) {
            ff4jConfig.setAutoCreate(Boolean.valueOf(ff4jYamlMap.get(GLOBAL_AUTOCREATE).toString()));
        }
        // Properties
        ff4jConfig.getProperties().putAll(parseProperties((List<Map<String, Object>>) ff4jYamlMap.get(PROPERTIES_TAG)));
        // Features
        parseFeatures(ff4jConfig, (List<Map<String, Object>>) ff4jYamlMap.get(FEATURES_TAG));
    }
    return ff4jConfig;
}
Also used : List(java.util.List) PropertyString(org.ff4j.property.PropertyString) HashMap(java.util.HashMap) Map(java.util.Map) FF4jConfiguration(org.ff4j.conf.FF4jConfiguration)

Example 4 with FF4jConfiguration

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

the class YamlParserTest method importYaml_should_be_same_asXMLImport.

@Test
public void importYaml_should_be_same_asXMLImport() {
    // Give XML an YAML files
    InputStream xmlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.xml");
    InputStream ymlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.yml");
    // When parsing those files
    XmlConfig xmlConfig = new XmlParser().parseConfigurationFile(xmlFile);
    FF4jConfiguration ymlConfig = new YamlParser().parseConfigurationFile(ymlFile);
    // Than both config are even
    assertEquals(xmlConfig.getFeatures().size(), ymlConfig.getFeatures().size());
    assertEquals(xmlConfig.getProperties().size(), ymlConfig.getProperties().size());
    // Custom-properties
    Feature f1Xml = xmlConfig.getFeatures().get("first");
    Feature f1Yml = ymlConfig.getFeatures().get("first");
    assertEquals(f1Xml.getDescription(), f1Yml.getDescription());
    assertEquals(f1Xml.getCustomProperties().size(), f1Yml.getCustomProperties().size());
    // FlipStrategy & Permission
    Feature f3Xml = xmlConfig.getFeatures().get("third");
    Feature f3Yml = ymlConfig.getFeatures().get("third");
    assertEquals(f3Xml.getFlippingStrategy().getClass(), f3Yml.getFlippingStrategy().getClass());
    assertEquals(f3Xml.getFlippingStrategy().getInitParams().get("expression"), f3Yml.getFlippingStrategy().getInitParams().get("expression"));
    assertEquals(f3Xml.getPermissions(), f3Yml.getPermissions());
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) FF4jConfiguration(org.ff4j.conf.FF4jConfiguration) Test(org.junit.Test)

Example 5 with FF4jConfiguration

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

the class ConsoleOperations method exportConfiguration.

/**
 * Export configuration.
 *
 * @param ff4j
 *      feature flags for java
 * @param res
 *      http response
 * @param format
 *      format
 * @throws IOException
 *      error occured when exporting
 */
public static void exportConfiguration(FF4j ff4j, HttpServletResponse res, String format) throws IOException {
    if (format != null) {
        InputStream in = null;
        ServletOutputStream sos = null;
        String fileName = "ff4j-" + SDF.format(new Date()) + ".";
        try {
            sos = res.getOutputStream();
            if (FORMAT_XML.equals(format.toLowerCase())) {
                res.setContentType(CONTENT_TYPE_XML);
                res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_XML + "\"");
                in = new XmlParser().exportAll(new XmlConfig(ff4j));
            } else if (FORMAT_YML.equals(format.toLowerCase()) || FORMAT_YAML.equals(format.toLowerCase())) {
                res.setContentType(CONTENT_TYPE_YAML);
                res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_YML + "\"");
                in = new YamlParser().export(new FF4jConfiguration(ff4j));
            } else if (FORMAT_PROPERTIES.equals(format.toLowerCase())) {
                res.setContentType(CONTENT_TYPE_PROPERTIES);
                res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_PROPERTIES + "\"");
                in = new YamlParser().export(new FF4jConfiguration(ff4j));
            }
            if (in != null) {
                org.apache.commons.io.IOUtils.copy(in, sos);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (sos != null) {
                sos.flush();
                sos.close();
            }
        }
    }
}
Also used : XmlParser(org.ff4j.conf.XmlParser) YamlParser(org.ff4j.parser.yaml.YamlParser) XmlConfig(org.ff4j.conf.XmlConfig) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) Date(java.util.Date) FF4jConfiguration(org.ff4j.conf.FF4jConfiguration)

Aggregations

FF4jConfiguration (org.ff4j.conf.FF4jConfiguration)6 XmlParser (org.ff4j.conf.XmlParser)4 InputStream (java.io.InputStream)3 XmlConfig (org.ff4j.conf.XmlConfig)3 Feature (org.ff4j.core.Feature)2 YamlParser (org.ff4j.parser.yaml.YamlParser)2 Test (org.junit.Test)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 FileItem (org.apache.commons.fileupload.FileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 FF4jCacheProxy (org.ff4j.cache.FF4jCacheProxy)1 PropertiesParser (org.ff4j.parser.properties.PropertiesParser)1 PropertyString (org.ff4j.property.PropertyString)1 HomeBean (org.ff4j.web.bean.HomeBean)1