From: Tuukka Lehtonen Date: Fri, 27 Oct 2017 22:14:27 +0000 (+0300) Subject: Made DB ListenerAdapter abstract to force isDisposed implementation X-Git-Tag: v1.31.0~85 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F51%2F1151%2F4;p=simantics%2Fplatform.git Made DB ListenerAdapter abstract to force isDisposed implementation This forces the user of the adapter to still handle the life-cycle of the listener, which should never be neglected. refs #7581 Change-Id: I1a54b3f130dfe5f82fcdf01eb799ac96b31e7636 --- diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/AsyncListenerAdapter.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/AsyncListenerAdapter.java index 3f4d95a0e..0046e413d 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/AsyncListenerAdapter.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/AsyncListenerAdapter.java @@ -14,7 +14,7 @@ package org.simantics.db.common.procedure.adapter; import org.simantics.db.AsyncReadGraph; import org.simantics.db.procedure.AsyncListener; -public class AsyncListenerAdapter implements AsyncListener { +public abstract class AsyncListenerAdapter implements AsyncListener { @Override public void exception(AsyncReadGraph graph, Throwable t) { @@ -25,8 +25,6 @@ public class AsyncListenerAdapter implements AsyncListener { } @Override - public boolean isDisposed() { - return false; - } + public abstract boolean isDisposed(); } diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/ListenerAdapter.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/ListenerAdapter.java index 6959236d6..ab681f8ef 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/ListenerAdapter.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/ListenerAdapter.java @@ -13,7 +13,7 @@ package org.simantics.db.common.procedure.adapter; import org.simantics.db.procedure.Listener; -public class ListenerAdapter implements Listener { +public abstract class ListenerAdapter implements Listener { @Override public void exception(Throwable t) { @@ -24,8 +24,6 @@ public class ListenerAdapter implements Listener { } @Override - public boolean isDisposed() { - return false; - } + public abstract boolean isDisposed(); } diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/SyncListenerAdapter.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/SyncListenerAdapter.java index 0b8b99208..b5bdb5bca 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/SyncListenerAdapter.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/adapter/SyncListenerAdapter.java @@ -14,7 +14,7 @@ package org.simantics.db.common.procedure.adapter; import org.simantics.db.ReadGraph; import org.simantics.db.procedure.SyncListener; -public class SyncListenerAdapter implements SyncListener { +public abstract class SyncListenerAdapter implements SyncListener { @Override public void exception(ReadGraph graph, Throwable t) { @@ -25,8 +25,6 @@ public class SyncListenerAdapter implements SyncListener { } @Override - public boolean isDisposed() { - return false; - } + public abstract boolean isDisposed(); } diff --git a/bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/ScenegraphLoaderUtils.java b/bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/ScenegraphLoaderUtils.java index acf585dcc..e40773002 100644 --- a/bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/ScenegraphLoaderUtils.java +++ b/bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/ScenegraphLoaderUtils.java @@ -15,7 +15,7 @@ import org.simantics.db.ReadGraph; import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.common.NamedResource; -import org.simantics.db.common.procedure.adapter.ListenerAdapter; +import org.simantics.db.common.procedure.adapter.ProcedureAdapter; import org.simantics.db.common.request.BinaryRead; import org.simantics.db.common.request.ParametrizedPrimitiveRead; import org.simantics.db.common.request.ResourceRead; @@ -29,6 +29,7 @@ import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.VariableBuilder; import org.simantics.db.layer0.variable.Variables; import org.simantics.db.procedure.Listener; +import org.simantics.db.procedure.Procedure; import org.simantics.layer0.Layer0; import org.simantics.scenegraph.INode; import org.simantics.scenegraph.LoaderNode; @@ -44,7 +45,7 @@ import org.simantics.utils.threads.ThreadUtils; public class ScenegraphLoaderUtils { - static Map, Collection>> externalMap = new HashMap, Collection>>(); + static Map, Collection>> externalMap = new HashMap, Collection>>(); static Map, Object> externalValueMap = new HashMap, Object>(); final public static class ScenegraphPropertyReference { @@ -59,12 +60,12 @@ public class ScenegraphLoaderUtils { public void register(ReadGraph graph, final Listener procedure) { Object value = externalValueMap.get(parameter); procedure.execute((T)value); - Collection> listeners = externalMap.get(parameter); + Collection> listeners = externalMap.get(parameter); if(listeners == null) { - listeners = new ArrayList>(); + listeners = new ArrayList>(); externalMap.put(parameter, listeners); } - listeners.add(new ListenerAdapter() { + listeners.add(new ProcedureAdapter() { @Override public void execute(Object result) { @@ -353,9 +354,9 @@ public class ScenegraphLoaderUtils { public Boolean apply(String property, Object value) { Pair key = Pair.make(reference.first, reference.second + "#" + property); externalValueMap.put(key, value); - Collection> listeners = externalMap.get(key); + Collection> listeners = externalMap.get(key); if(listeners != null) { - for(Listener listener : listeners) listener.execute(value); + for(Procedure listener : listeners) listener.execute(value); } return true; } diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/RecoveryFromExceptedState.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/RecoveryFromExceptedState.java index 9a18036d8..31e995f1e 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/RecoveryFromExceptedState.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/RecoveryFromExceptedState.java @@ -63,7 +63,12 @@ public class RecoveryFromExceptedState extends ExistingDatabaseTest { return graph.getResource("http://A"); } - }, new ListenerAdapter()); + }, new ListenerAdapter() { + @Override + public boolean isDisposed() { + return false; + } + }); /* * Invalidates the request. Request is updated (is has a listener) and enters 'Excepted' state. diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/listening/ObjectsListeningTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/listening/ObjectsListeningTest.java index 6811ce5f4..998eb94e2 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/listening/ObjectsListeningTest.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/listening/ObjectsListeningTest.java @@ -65,6 +65,11 @@ public class ObjectsListeningTest extends ExistingDatabaseTest { executions.incrementAndGet(); } + @Override + public boolean isDisposed() { + return false; + } + }); session.syncRequest(new WriteRequest() { diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java index be32a81fb..2c89c1ecc 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java @@ -104,6 +104,11 @@ public class RequestProcessorTest1 extends WriteReadTest { listeners.add(result); } + @Override + public boolean isDisposed() { + return false; + } + }; assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R1(L0.ConsistsOf))); diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest1.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest1.java index cac163968..e96ea1cbb 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest1.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest1.java @@ -61,7 +61,12 @@ public class ThreadingTest1 extends ExistingDatabaseTest { return 0; } - }, new ListenerAdapter()); + }, new ListenerAdapter() { + @Override + public boolean isDisposed() { + return false; + } + }); assert(result == 1); diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest2.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest2.java index f3e1cd14a..6bd67790f 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest2.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/thread/ThreadingTest2.java @@ -61,7 +61,12 @@ public class ThreadingTest2 extends ExistingDatabaseTest { return 0; } - }, new ListenerAdapter()); + }, new ListenerAdapter() { + @Override + public boolean isDisposed() { + return false; + } + }); assert(result == 1); diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/CachedDirectPredicatesWithNoCluster.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/CachedDirectPredicatesWithNoCluster.java index 19091b518..a9698e744 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/CachedDirectPredicatesWithNoCluster.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/CachedDirectPredicatesWithNoCluster.java @@ -31,7 +31,12 @@ public class CachedDirectPredicatesWithNoCluster extends ExistingDatabaseTest { return graph.hasStatement(L0.Entity); } - }, new ListenerAdapter()); + }, new ListenerAdapter() { + @Override + public boolean isDisposed() { + return false; + } + }); ClusterControl support = getSession().getService(ClusterControl.class); support.collectClusters(Integer.MAX_VALUE); diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/ObjectsWithNoClusterWithCachedRelationInfo.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/ObjectsWithNoClusterWithCachedRelationInfo.java index 0596f7658..759850721 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/ObjectsWithNoClusterWithCachedRelationInfo.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/ObjectsWithNoClusterWithCachedRelationInfo.java @@ -36,7 +36,12 @@ public class ObjectsWithNoClusterWithCachedRelationInfo extends ExistingDatabase }); // Cache RelationInfo for HasName - session.syncRequest(new PossibleObject(L0.Entity, L0.HasName), new ListenerAdapter()); + session.syncRequest(new PossibleObject(L0.Entity, L0.HasName), new ListenerAdapter() { + @Override + public boolean isDisposed() { + return false; + } + }); // Flush clusters ClusterControl support = getSession().getService(ClusterControl.class); diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/ReadHierarchicalNames.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/ReadHierarchicalNames.java index 974cc7104..66e3842e0 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/ReadHierarchicalNames.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/ReadHierarchicalNames.java @@ -94,7 +94,12 @@ public class ReadHierarchicalNames extends ExistingDatabaseTest { private void listenTime(String label, Read request) throws DatabaseException { long start = System.nanoTime(); - getSession().syncRequest(request, new ListenerAdapter()); + getSession().syncRequest(request, new ListenerAdapter() { + @Override + public boolean isDisposed() { + return false; + } + }); long duration = System.nanoTime() - start; System.out.println(label + " = " + 1e-9*duration);