X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fsubscription%2FVariableSetListener.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fsubscription%2FVariableSetListener.java;h=4af644c084233ea64e5f1d1f4fcde5b80eb635fc;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..4af644c08 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/VariableSetListener.java @@ -0,0 +1,104 @@ +package org.simantics.modeling.subscription; + +import java.util.List; +import java.util.concurrent.RejectedExecutionException; +import java.util.logging.Level; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.simantics.ObjectIdentitySchedulingRule; +import org.simantics.db.common.procedure.adapter.ListenerAdapter; +import org.simantics.db.exception.DatabaseException; +import org.simantics.history.HistoryException; +import org.simantics.history.util.subscription.SubscriptionItem; +import org.simantics.modeling.subscription.ModelHistoryCollector.ItemCollector; + +/** + * Listener for {@link SubscriptionCollectionResult} request. Reloads + * {@link ItemCollector} on every execution. + */ +class VariableSetListener extends ListenerAdapter { + /** + * + */ + private final ModelHistoryCollector modelHistoryCollector; + + /** + * @param modelHistoryCollector + */ + VariableSetListener(ModelHistoryCollector modelHistoryCollector) { + this.modelHistoryCollector = modelHistoryCollector; + } + + boolean disposed; + + @Override + public void execute(final SubscriptionCollectionResult result) { + Job job = new Job("Reloading history subscriptions") { + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + if (VariableSetListener.this.modelHistoryCollector.itemCollector.isDisposed()) + return Status.OK_STATUS; + if (!result.getStatus().isOK() && VariableSetListener.this.modelHistoryCollector.logger != null) + VariableSetListener.this.modelHistoryCollector.logger.log(result.getStatus()); + + // Load in specified thread. + final IStatus[] status = { null }; + Runnable loader = new Runnable() { + @Override + public void run() { + try { + List sampledItems = ModelHistoryCollector.sampledSubscriptionItems(result.getSubscriptions()); + VariableSetListener.this.modelHistoryCollector.itemCollector.load( sampledItems ); + } catch (HistoryException e) { + status[0] = new Status(IStatus.ERROR, "org.simantics.modeling", e.getLocalizedMessage(), e); + if (VariableSetListener.this.modelHistoryCollector.logger != null) + VariableSetListener.this.modelHistoryCollector.logger.log(status[0]); + } catch (DatabaseException e) { + VariableSetListener.this.modelHistoryCollector.log.log(Level.WARNING, "Unexpected failure in history variable collection request.", e); + status[0] = new Status(IStatus.ERROR, "org.simantics.modeling", e.getLocalizedMessage(), e); + } + } + }; + if (VariableSetListener.this.modelHistoryCollector.loadThread != null) + VariableSetListener.this.modelHistoryCollector.loadThread.syncExec(loader); + else + loader.run(); + + if (status[0] == null && VariableSetListener.this.modelHistoryCollector.loadCallback != null) + VariableSetListener.this.modelHistoryCollector.loadCallback.run(); + + return status[0] != null ? status[0] : Status.OK_STATUS; + } catch (RejectedExecutionException e) { + // IThreadWorkQueue.syncExec may produce this. + // Usually this means the executor has been + // disposed. + return new Status(IStatus.INFO, "org.simantics.modeling", e.getLocalizedMessage(), e); + } finally { + VariableSetListener.this.modelHistoryCollector.initMutex.release(); + } + } + }; + job.setRule(new ObjectIdentitySchedulingRule(this.modelHistoryCollector)); + job.setUser(false); + job.schedule(); + } + + public void dispose() { + disposed = true; + } + + @Override + public void exception(Throwable t) { + this.modelHistoryCollector.initMutex.release(); + this.modelHistoryCollector.log.log(Level.WARNING, "Unexpected failure in history variable collection request.", t); + } + + @Override + public boolean isDisposed() { + return disposed; + } +} \ No newline at end of file