]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/VariableSetListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / subscription / VariableSetListener.java
1 package org.simantics.modeling.subscription;
2
3 import java.util.List;
4 import java.util.concurrent.RejectedExecutionException;
5 import java.util.logging.Level;
6
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;
17
18 /**
19  * Listener for {@link SubscriptionCollectionResult} request. Reloads
20  * {@link ItemCollector} on every execution.
21  */
22 class VariableSetListener extends ListenerAdapter<SubscriptionCollectionResult> {
23     /**
24          * 
25          */
26         private final ModelHistoryCollector modelHistoryCollector;
27
28         /**
29          * @param modelHistoryCollector
30          */
31         VariableSetListener(ModelHistoryCollector modelHistoryCollector) {
32                 this.modelHistoryCollector = modelHistoryCollector;
33         }
34
35         boolean disposed;
36
37     @Override
38     public void execute(final SubscriptionCollectionResult result) {
39         Job job = new Job("Reloading history subscriptions") {
40             @Override
41             protected IStatus run(IProgressMonitor monitor) {
42                 try {
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());
47
48                     // Load in specified thread.
49                     final IStatus[] status = { null };
50                     Runnable loader = new Runnable() {
51                         @Override
52                         public void run() {
53                             try {
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); 
63                             }
64                         }
65                     };
66                     if (VariableSetListener.this.modelHistoryCollector.loadThread != null)
67                         VariableSetListener.this.modelHistoryCollector.loadThread.syncExec(loader);
68                     else
69                         loader.run();
70
71                     if (status[0] == null && VariableSetListener.this.modelHistoryCollector.loadCallback != null)
72                         VariableSetListener.this.modelHistoryCollector.loadCallback.run();
73
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
78                     // disposed.
79                     return new Status(IStatus.INFO, "org.simantics.modeling", e.getLocalizedMessage(), e);
80                 } finally {
81                     VariableSetListener.this.modelHistoryCollector.initMutex.release();
82                 }
83             }
84         };
85         job.setRule(new ObjectIdentitySchedulingRule(this.modelHistoryCollector));
86         job.setUser(false);
87         job.schedule();
88     }
89     
90     public void dispose() {
91         disposed = true;
92     }
93     
94     @Override
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);
98     }
99     
100     @Override
101     public boolean isDisposed() {
102         return disposed;
103     }
104 }