From: Marko Luukkainen Date: Thu, 17 Jan 2019 14:27:55 +0000 (+0200) Subject: General event listening interface for DB and purge events. X-Git-Tag: v1.43.0~136^2~208^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=b2bb3ff110b5bf929b6d676f0122a15a43358440;p=simantics%2Fplatform.git General event listening interface for DB and purge events. gitlab #245 Change-Id: Icdceaba3f8848751a24ee5aa9e1888b2540f0c73 --- diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/GraphClientImpl2.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/GraphClientImpl2.java index a0c2ef860..172fdd103 100644 --- a/bundles/org.simantics.acorn/src/org/simantics/acorn/GraphClientImpl2.java +++ b/bundles/org.simantics.acorn/src/org/simantics/acorn/GraphClientImpl2.java @@ -17,6 +17,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -45,18 +47,23 @@ import org.simantics.db.exception.SDBException; import org.simantics.db.server.ProCoreException; import org.simantics.db.service.ClusterSetsSupport; import org.simantics.db.service.ClusterUID; +import org.simantics.db.service.EventSupport; import org.simantics.db.service.LifecycleSupport; import org.simantics.utils.datastructures.Pair; import org.simantics.utils.logging.TimeLogger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fi.vtt.simantics.procore.internal.EventSupportImpl; import gnu.trove.map.hash.TLongObjectHashMap; public class GraphClientImpl2 implements Database.Session { private static final Logger LOGGER = LoggerFactory.getLogger(GraphClientImpl2.class); public static final boolean DEBUG = false; + + public static final String CLOSE = "close"; + public static final String PURGE = "purge"; final ClusterManager clusters; @@ -69,6 +76,7 @@ public class GraphClientImpl2 implements Database.Session { private ServiceLocator locator; private FileCache fileCache; private MainProgram mainProgram; + private EventSupportImpl eventSupport; private static class ClientThreadFactory implements ThreadFactory { @@ -102,6 +110,8 @@ public class GraphClientImpl2 implements Database.Session { cssi.updateWriteDirectory(clusters.workingDirectory); mainProgram = new MainProgram(this, clusters); executor.execute(mainProgram); + eventSupport = (EventSupportImpl)locator.getService(EventSupport.class); + } public Path getDbFolder() { @@ -216,6 +226,7 @@ public class GraphClientImpl2 implements Database.Session { throw new ProCoreException(e1); } closed = true; + eventSupport.fireEvent(CLOSE, null); } //impl.close(); } @@ -797,8 +808,10 @@ public class GraphClientImpl2 implements Database.Session { unexpectedClose = true; } finally { try { - if(tr != null) + if(tr != null) { endTransaction(tr.getTransactionId()); + eventSupport.fireEvent(PURGE, null); + } if (unexpectedClose) { LifecycleSupport support = getServiceLocator().getService(LifecycleSupport.class); try { diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/EventSupportImpl.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/EventSupportImpl.java new file mode 100644 index 000000000..89a1bfd3e --- /dev/null +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/EventSupportImpl.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2019 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package fi.vtt.simantics.procore.internal; + +import java.util.concurrent.CopyOnWriteArrayList; + +import org.simantics.db.service.EventSupport; + +/** + * Simple implementation for Event Support. + * + * Note: for more extended / complicated scenarios, we could use org.simantics.utils.threads.SyncListenerList. + * + * @author luukkainen + * + */ +public class EventSupportImpl implements EventSupport { + + private CopyOnWriteArrayList listeners = new CopyOnWriteArrayList<>(); + + @Override + public synchronized void addListener(EventListener l) { + listeners.add(l); + } + + @Override + public synchronized void removeListener(EventListener l) { + listeners.remove(l); + } + + public void fireEvent(String type, Object data) { + for (EventListener l : listeners) { + l.event(type, data); + } + } + +} diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java index a18b66e69..d32c066bc 100644 --- a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java @@ -143,6 +143,7 @@ import org.simantics.db.service.ClusteringSupport; import org.simantics.db.service.CollectionSupport; import org.simantics.db.service.DebugSupport; import org.simantics.db.service.DirectQuerySupport; +import org.simantics.db.service.EventSupport; import org.simantics.db.service.GraphChangeListenerSupport; import org.simantics.db.service.InitSupport; import org.simantics.db.service.LifecycleSupport; @@ -292,6 +293,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule serviceLocator.registerService(ExternalValueSupport.class, new ExternalValueSupportImpl(this)); serviceLocator.registerService(RandomAccessValueSupport.class, new RandomAccessValueSupportImpl()); serviceLocator.registerService(ServiceActivityMonitor.class, new ServiceActivityMonitorImpl()); + serviceLocator.registerService(EventSupport.class, new EventSupportImpl()); ServiceActivityUpdaterForWriteTransactions.register(this); this.virtualGraphServerSupport = new VirtualGraphServerSupportImpl(this, t); diff --git a/bundles/org.simantics.db/src/org/simantics/db/service/EventSupport.java b/bundles/org.simantics.db/src/org/simantics/db/service/EventSupport.java new file mode 100644 index 000000000..7d7995b44 --- /dev/null +++ b/bundles/org.simantics.db/src/org/simantics/db/service/EventSupport.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2019 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.db.service; + +/** + * Interface for listening DB events. + * + * Note: At the moment, only "purge" is supported. + * + * @author luukkainen + * + */ +public interface EventSupport { + + public void addListener(EventListener l); + + public void removeListener(EventListener l); + + + public interface EventListener extends java.util.EventListener { + /** + * + * @param type Type of the event. + * @param data Data related to the event, may be null. + */ + public void event(String type, Object data); + } +}