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());
}
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));
}
}
}
Aggregations