]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
General event listening interface for DB and purge events. 68/2668/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 17 Jan 2019 14:27:55 +0000 (16:27 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 21 Feb 2019 16:54:14 +0000 (16:54 +0000)
gitlab #245

Change-Id: Icdceaba3f8848751a24ee5aa9e1888b2540f0c73
(cherry picked from commit b2bb3ff110b5bf929b6d676f0122a15a43358440)

bundles/org.simantics.acorn/src/org/simantics/acorn/GraphClientImpl2.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/EventSupportImpl.java [new file with mode: 0644]
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java
bundles/org.simantics.db/src/org/simantics/db/service/EventSupport.java [new file with mode: 0644]

index 2ffdc715ff2546db5e03e67e102858a2317b87ef..251a80314c32f9dc37f1565597cf061070280f11 100644 (file)
@@ -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,6 +47,7 @@ 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.DataContainer;
 import org.simantics.utils.datastructures.Pair;
@@ -52,12 +55,16 @@ 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;
 
@@ -70,6 +77,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 {
 
@@ -103,6 +111,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() {
@@ -217,6 +227,7 @@ public class GraphClientImpl2 implements Database.Session {
                                throw new ProCoreException(e1);
                        }
                        closed = true;
+                       eventSupport.fireEvent(CLOSE, null);
                }
                //impl.close();
        }
@@ -798,8 +809,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 (file)
index 0000000..89a1bfd
--- /dev/null
@@ -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<EventListener> 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);
+        }
+    }
+
+}
index d71a8bfc81c491d52dc7afe30cae65a5e8e95d60..3f4d320ba87c590136995c4ba7b7b9124353ff43 100644 (file)
@@ -136,6 +136,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;
@@ -282,6 +283,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 (file)
index 0000000..7d7995b
--- /dev/null
@@ -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);
+    }
+}