1 package org.simantics.modeling.subscription;
4 import java.util.concurrent.RejectedExecutionException;
5 import java.util.logging.Level;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.core.runtime.jobs.Job;
11 import org.simantics.ObjectIdentitySchedulingRule;
12 import org.simantics.db.common.procedure.adapter.ListenerAdapter;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.history.HistoryException;
15 import org.simantics.history.util.subscription.SubscriptionItem;
16 import org.simantics.modeling.subscription.ModelHistoryCollector.ItemCollector;
19 * Listener for {@link SubscriptionCollectionResult} request. Reloads
20 * {@link ItemCollector} on every execution.
22 class VariableSetListener extends ListenerAdapter<SubscriptionCollectionResult> {
26 private final ModelHistoryCollector modelHistoryCollector;
29 * @param modelHistoryCollector
31 VariableSetListener(ModelHistoryCollector modelHistoryCollector) {
32 this.modelHistoryCollector = modelHistoryCollector;
38 public void execute(final SubscriptionCollectionResult result) {
39 Job job = new Job("Reloading history subscriptions") {
41 protected IStatus run(IProgressMonitor monitor) {
43 if (VariableSetListener.this.modelHistoryCollector.itemCollector.isDisposed())
44 return Status.OK_STATUS;
45 if (!result.getStatus().isOK() && VariableSetListener.this.modelHistoryCollector.logger != null)
46 VariableSetListener.this.modelHistoryCollector.logger.log(result.getStatus());
48 // Load in specified thread.
49 final IStatus[] status = { null };
50 Runnable loader = new Runnable() {
54 List<SubscriptionItem> sampledItems = ModelHistoryCollector.sampledSubscriptionItems(result.getSubscriptions());
55 VariableSetListener.this.modelHistoryCollector.itemCollector.load( sampledItems );
56 } catch (HistoryException e) {
57 status[0] = new Status(IStatus.ERROR, "org.simantics.modeling", e.getLocalizedMessage(), e);
58 if (VariableSetListener.this.modelHistoryCollector.logger != null)
59 VariableSetListener.this.modelHistoryCollector.logger.log(status[0]);
60 } catch (DatabaseException e) {
61 VariableSetListener.this.modelHistoryCollector.log.log(Level.WARNING, "Unexpected failure in history variable collection request.", e);
62 status[0] = new Status(IStatus.ERROR, "org.simantics.modeling", e.getLocalizedMessage(), e);
66 if (VariableSetListener.this.modelHistoryCollector.loadThread != null)
67 VariableSetListener.this.modelHistoryCollector.loadThread.syncExec(loader);
71 if (status[0] == null && VariableSetListener.this.modelHistoryCollector.loadCallback != null)
72 VariableSetListener.this.modelHistoryCollector.loadCallback.run();
74 return status[0] != null ? status[0] : Status.OK_STATUS;
75 } catch (RejectedExecutionException e) {
76 // IThreadWorkQueue.syncExec may produce this.
77 // Usually this means the executor has been
79 return new Status(IStatus.INFO, "org.simantics.modeling", e.getLocalizedMessage(), e);
81 VariableSetListener.this.modelHistoryCollector.initMutex.release();
85 job.setRule(new ObjectIdentitySchedulingRule(this.modelHistoryCollector));
90 public void dispose() {
95 public void exception(Throwable t) {
96 this.modelHistoryCollector.initMutex.release();
97 this.modelHistoryCollector.log.log(Level.WARNING, "Unexpected failure in history variable collection request.", t);
101 public boolean isDisposed() {