Search in sources :

Example 41 with Property

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

the class Store2CachePollingWorker method run.

/**
 * {@inheritDoc}
 */
@Override
public void run() {
    try {
        FF4JCacheManager cacheManager;
        if (ff4JCacheProxy != null) {
            cacheManager = new InMemoryCacheManager();
        } else {
            cacheManager = this.cacheManager;
        }
        if (sourceFeatureStore != null) {
            // Access the store, if failed an error is raised and cache is not cleared.
            Map<String, Feature> mapOfFeatures = sourceFeatureStore.readAll();
            // Clear cache
            cacheManager.clearFeatures();
            // Fill Cache
            for (Feature f : mapOfFeatures.values()) {
                cacheManager.putFeature(f);
            }
        }
        if (sourcePropertyStore != null) {
            // Access the store, if failed an error is raised and cache is not cleared.
            Map<String, Property<?>> mapOfProperties = sourcePropertyStore.readAllProperties();
            // Clear cache
            cacheManager.clearProperties();
            // Fill Cache
            for (Property<?> p : mapOfProperties.values()) {
                cacheManager.putProperty(p);
            }
        }
        if (ff4JCacheProxy != null) {
            ff4JCacheProxy.setCacheManager(cacheManager);
        }
    } catch (Exception ex) {
        // Work in background (worker) failed 'silently'
        ex.printStackTrace();
    }
}
Also used : Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property)

Example 42 with Property

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

the class FeatureJsonParserTest method assertMarshalling.

/**
 * Check serialized string against json serializer.
 *
 * @param json
 *            json value
 * @param feat
 *            feature
 */
private void assertMarshalling(Feature feat) throws Exception {
    Map<String, Property<?>> props = feat.getCustomProperties();
    if (props != null && !props.isEmpty()) {
        // Custom properties are unforce to PropertyJsonBean
        for (String pName : props.keySet()) {
            PropertyJsonBean pjb = new PropertyJsonBean(props.get(pName));
            Assert.assertEquals(marshallWithJackson(pjb), pjb.asJson());
        }
        feat.setCustomProperties(new HashMap<String, Property<?>>());
    }
    Assert.assertEquals(marshallWithJackson(feat), feat.toJson());
    feat.setCustomProperties(props);
}
Also used : Property(org.ff4j.property.Property) PropertyJsonBean(org.ff4j.property.util.PropertyJsonBean)

Example 43 with Property

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

the class CacheProxyWithPollingTest method testCacheProxyManagerPropertyDuringRefresh.

@Test
public void testCacheProxyManagerPropertyDuringRefresh() throws InterruptedException {
    FeatureStore fs = new InMemoryFeatureStore("ff4j.xml");
    PropertyStore ps = new InMemoryPropertyStore("ff4j.xml");
    FF4JCacheManager cm = new InMemoryCacheManager();
    final FF4jCacheProxy proxy = new FF4jCacheProxy(fs, ps, cm);
    // scheduler refreshing cache through worker thread
    Store2CachePollingScheduler store2CachePollingScheduler = new Store2CachePollingScheduler(proxy);
    // setting polling delay 10ms
    store2CachePollingScheduler.start(10);
    // 20 threads trying to fetch property from cacheManager
    ExecutorService fetchPropertyService = Executors.newFixedThreadPool(20);
    Callable<Property<?>> callable = new Callable<Property<?>>() {

        @Override
        public Property<?> call() throws Exception {
            try {
                return proxy.getCacheManager().getProperty("a");
            } catch (Exception e) {
                throw e;
            }
        }
    };
    List<Callable<Property<?>>> multiplePropertyFetchCalls = new ArrayList<Callable<Property<?>>>(1000);
    // generating 100000 requests
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 10; j++) {
            // clear to avoid accumulation of callables over multiple iterations
            multiplePropertyFetchCalls.clear();
        }
        for (int k = 0; k < 100; k++) {
            multiplePropertyFetchCalls.add(callable);
        }
        // execute 100 property fetch calls on 20 threads
        List<Future<Property<?>>> fetchPropertyCalls = fetchPropertyService.invokeAll(multiplePropertyFetchCalls);
        // property should never be null
        for (Future<Property<?>> property : fetchPropertyCalls) {
            Assert.assertNotNull(property);
        }
    }
}
Also used : FF4JCacheManager(org.ff4j.cache.FF4JCacheManager) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) InMemoryCacheManager(org.ff4j.cache.InMemoryCacheManager) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) FF4jCacheProxy(org.ff4j.cache.FF4jCacheProxy) Property(org.ff4j.property.Property) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) FeatureStore(org.ff4j.core.FeatureStore) Store2CachePollingScheduler(org.ff4j.cache.Store2CachePollingScheduler) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) PropertyStore(org.ff4j.property.store.PropertyStore) Test(org.junit.Test)

Example 44 with Property

use of org.ff4j.property.Property 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 45 with Property

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

the class InMemoryPropertiesStoreTest method testInvalidXML.

@Test(expected = IllegalArgumentException.class)
public void testInvalidXML() {
    new InMemoryPropertyStore(new HashMap<String, Property<?>>());
    InputStream in = getClass().getClassLoader().getResourceAsStream("invalid.xml");
    new InMemoryPropertyStore(in);
}
Also used : InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) InputStream(java.io.InputStream) PropertyString(org.ff4j.property.PropertyString) Property(org.ff4j.property.Property) 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