General event listening interface for DB and purge events. 95/2595/2
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 17 Jan 2019 14:27:55 +0000 (16:27 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 17 Jan 2019 14:46:21 +0000 (16:46 +0200)
gitlab #245

Change-Id: Icdceaba3f8848751a24ee5aa9e1888b2540f0c73

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 a0c2ef860ba0353b6a1eb7ea0feabe605efb2a63..172fdd103150f28b88fac4ce604a74723e49b504 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,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 (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 a18b66e6978a03d66e46b965848b0f9c956fa961..d32c066bcdfc9bee732cd6ecf3829b5b0eb7a9a1 100644 (file)
@@ -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 (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);
+    }
+}