Search in sources :

Example 1 with StoreListener

use of org.apache.sis.storage.event.StoreListener in project geotoolkit by Geomatys.

the class WeakListenerTest method testWeakStorageListener.

/**
 * Test no memory leak in weak style listener
 */
@Test
public void testWeakStorageListener() throws DataStoreException {
    FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test1");
    ftb.addAttribute(Integer.class).setName("att");
    final FeatureType type1 = ftb.build();
    ftb = new FeatureTypeBuilder();
    ftb.setName("test2");
    ftb.addAttribute(Integer.class).setName("att2");
    final FeatureType type2 = ftb.build();
    final AtomicInteger count = new AtomicInteger(0);
    final InMemoryAggregate store = new InMemoryAggregate();
    StoreListener listener = new StoreListener() {

        @Override
        public void eventOccured(StoreEvent event) {
            count.incrementAndGet();
        }
    };
    final StorageListener.Weak ref = new StorageListener.Weak(listener);
    ref.registerSource(store);
    store.add(new DefiningFeatureSet(type1, null));
    assertEquals(1, count.get());
    listener = null;
    System.gc();
    store.add(new DefiningFeatureSet(type2, null));
    // listener should have desapear now, so the event should not have been send
    assertEquals(1, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StorageListener(org.geotoolkit.storage.event.StorageListener) InMemoryAggregate(org.geotoolkit.storage.memory.InMemoryAggregate) StoreListener(org.apache.sis.storage.event.StoreListener) StoreEvent(org.apache.sis.storage.event.StoreEvent) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) Test(org.junit.Test)

Example 2 with StoreListener

use of org.apache.sis.storage.event.StoreListener in project geotoolkit by Geomatys.

the class AbstractFeatureCollection method eventOccured.

// //////////////////////////////////////////////////////////////////////////
// listeners methods ///////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
@Override
public void eventOccured(StoreEvent event) {
    if (event instanceof FeatureStoreManagementEvent) {
        FeatureStoreManagementEvent fevent = (FeatureStoreManagementEvent) event;
        final FeatureType currentType = getType();
        // forward events only if the collection is typed and match the type name
        if (currentType != null && currentType.getName().equals(fevent.getFeatureTypeName())) {
            fevent = fevent.copy(this);
            final StoreListener[] lst;
            synchronized (listeners) {
                lst = listeners.toArray(new StoreListener[listeners.size()]);
            }
            for (final StoreListener listener : lst) {
                listener.eventOccured(fevent);
            }
        }
    } else if (event instanceof FeatureStoreContentEvent) {
        final FeatureStoreContentEvent fevent = (FeatureStoreContentEvent) event;
        final FeatureType currentType = getType();
        // forward events only if the collection is typed and match the type name
        if (currentType != null && currentType.getName().equals(fevent.getFeatureTypeName())) {
            sendEvent(fevent.copy(this));
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureStoreManagementEvent(org.geotoolkit.storage.event.FeatureStoreManagementEvent) FeatureStoreContentEvent(org.geotoolkit.storage.event.FeatureStoreContentEvent) StoreListener(org.apache.sis.storage.event.StoreListener)

Aggregations

StoreListener (org.apache.sis.storage.event.StoreListener)2 FeatureType (org.opengis.feature.FeatureType)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)1 StoreEvent (org.apache.sis.storage.event.StoreEvent)1 FeatureStoreContentEvent (org.geotoolkit.storage.event.FeatureStoreContentEvent)1 FeatureStoreManagementEvent (org.geotoolkit.storage.event.FeatureStoreManagementEvent)1 StorageListener (org.geotoolkit.storage.event.StorageListener)1 DefiningFeatureSet (org.geotoolkit.storage.feature.DefiningFeatureSet)1 InMemoryAggregate (org.geotoolkit.storage.memory.InMemoryAggregate)1 Test (org.junit.Test)1