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