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