]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/VariableSetListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / subscription / VariableSetListener.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/VariableSetListener.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/VariableSetListener.java
new file mode 100644 (file)
index 0000000..4af644c
--- /dev/null
@@ -0,0 +1,104 @@
+package org.simantics.modeling.subscription;\r
+\r
+import java.util.List;\r
+import java.util.concurrent.RejectedExecutionException;\r
+import java.util.logging.Level;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.simantics.ObjectIdentitySchedulingRule;\r
+import org.simantics.db.common.procedure.adapter.ListenerAdapter;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.history.HistoryException;\r
+import org.simantics.history.util.subscription.SubscriptionItem;\r
+import org.simantics.modeling.subscription.ModelHistoryCollector.ItemCollector;\r
+\r
+/**\r
+ * Listener for {@link SubscriptionCollectionResult} request. Reloads\r
+ * {@link ItemCollector} on every execution.\r
+ */\r
+class VariableSetListener extends ListenerAdapter<SubscriptionCollectionResult> {\r
+    /**\r
+        * \r
+        */\r
+       private final ModelHistoryCollector modelHistoryCollector;\r
+\r
+       /**\r
+        * @param modelHistoryCollector\r
+        */\r
+       VariableSetListener(ModelHistoryCollector modelHistoryCollector) {\r
+               this.modelHistoryCollector = modelHistoryCollector;\r
+       }\r
+\r
+       boolean disposed;\r
+\r
+    @Override\r
+    public void execute(final SubscriptionCollectionResult result) {\r
+        Job job = new Job("Reloading history subscriptions") {\r
+            @Override\r
+            protected IStatus run(IProgressMonitor monitor) {\r
+                try {\r
+                    if (VariableSetListener.this.modelHistoryCollector.itemCollector.isDisposed())\r
+                        return Status.OK_STATUS;\r
+                    if (!result.getStatus().isOK() && VariableSetListener.this.modelHistoryCollector.logger != null)\r
+                        VariableSetListener.this.modelHistoryCollector.logger.log(result.getStatus());\r
+\r
+                    // Load in specified thread.\r
+                    final IStatus[] status = { null };\r
+                    Runnable loader = new Runnable() {\r
+                        @Override\r
+                        public void run() {\r
+                            try {\r
+                               List<SubscriptionItem> sampledItems = ModelHistoryCollector.sampledSubscriptionItems(result.getSubscriptions());\r
+                                VariableSetListener.this.modelHistoryCollector.itemCollector.load( sampledItems );\r
+                            } catch (HistoryException e) {\r
+                                status[0] = new Status(IStatus.ERROR, "org.simantics.modeling", e.getLocalizedMessage(), e);\r
+                                if (VariableSetListener.this.modelHistoryCollector.logger != null)\r
+                                    VariableSetListener.this.modelHistoryCollector.logger.log(status[0]);\r
+                            } catch (DatabaseException e) {\r
+                                VariableSetListener.this.modelHistoryCollector.log.log(Level.WARNING, "Unexpected failure in history variable collection request.", e);\r
+                                status[0] = new Status(IStatus.ERROR, "org.simantics.modeling", e.getLocalizedMessage(), e); \r
+                            }\r
+                        }\r
+                    };\r
+                    if (VariableSetListener.this.modelHistoryCollector.loadThread != null)\r
+                        VariableSetListener.this.modelHistoryCollector.loadThread.syncExec(loader);\r
+                    else\r
+                        loader.run();\r
+\r
+                    if (status[0] == null && VariableSetListener.this.modelHistoryCollector.loadCallback != null)\r
+                        VariableSetListener.this.modelHistoryCollector.loadCallback.run();\r
+\r
+                    return status[0] != null ? status[0] : Status.OK_STATUS;\r
+                } catch (RejectedExecutionException e) {\r
+                    // IThreadWorkQueue.syncExec may produce this.\r
+                    // Usually this means the executor has been\r
+                    // disposed.\r
+                    return new Status(IStatus.INFO, "org.simantics.modeling", e.getLocalizedMessage(), e);\r
+                } finally {\r
+                    VariableSetListener.this.modelHistoryCollector.initMutex.release();\r
+                }\r
+            }\r
+        };\r
+        job.setRule(new ObjectIdentitySchedulingRule(this.modelHistoryCollector));\r
+        job.setUser(false);\r
+        job.schedule();\r
+    }\r
+    \r
+    public void dispose() {\r
+        disposed = true;\r
+    }\r
+    \r
+    @Override\r
+    public void exception(Throwable t) {\r
+        this.modelHistoryCollector.initMutex.release();\r
+        this.modelHistoryCollector.log.log(Level.WARNING, "Unexpected failure in history variable collection request.", t);\r
+    }\r
+    \r
+    @Override\r
+    public boolean isDisposed() {\r
+        return disposed;\r
+    }\r
+}
\ No newline at end of file