]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/AutosaveVirtualGraphs.java
SimanticsPlatform.startUp ensures updateness of installed feature groups
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / AutosaveVirtualGraphs.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics;
13
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.core.runtime.preferences.DefaultScope;
19 import org.eclipse.core.runtime.preferences.InstanceScope;
20 import org.osgi.service.prefs.Preferences;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Session;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.service.LifecycleSupport;
26 import org.simantics.db.service.VirtualGraphSupport;
27 import org.simantics.internal.Activator;
28
29 /**
30  * @author Tuukka Lehtonen <tuukka.lehtonen@semantum.fi>
31  */
32 public class AutosaveVirtualGraphs extends Job {
33
34         private static final boolean         TRACE        = false;
35
36         private boolean                      enabled      = true;
37
38         private Preferences                  defaultPrefs = DefaultScope.INSTANCE.getNode(AutosavePreferences.P_NODE);
39         private Preferences                  prefs        = InstanceScope.INSTANCE.getNode(AutosavePreferences.P_NODE);
40
41         private static AutosaveVirtualGraphs INSTANCE;
42
43         private boolean getBooleanPref(String preference, boolean def) {
44                 return prefs.getBoolean(preference, defaultPrefs.getBoolean(preference, def));
45         }
46
47         private int getIntPref(String preference, int def) {
48                 return prefs.getInt(preference, defaultPrefs.getInt(preference, def));
49         }
50
51         private AutosaveVirtualGraphs() {
52                 super("Autosave Virtual Graphs");
53                 setSystem(true);
54                 setPriority(Job.LONG);
55         }
56
57         public static synchronized AutosaveVirtualGraphs getInstance() {
58                 if(INSTANCE == null) {
59                         INSTANCE = new AutosaveVirtualGraphs();
60                 }
61                 return INSTANCE;
62         }
63
64         /**
65          * @param enabled
66          * @return
67          */
68         public boolean isEnabled() {
69                 return enabled;
70         }
71
72         /**
73          * @param enabled new enabled state
74          * @return previous enabled state
75          */
76         public boolean setEnabled(boolean enabled) {
77                 if (enabled == this.enabled)
78                         return enabled;
79                 this.enabled = enabled;
80                 if (enabled)
81                         scheduleAfterInterval();
82                 else
83                         cancel();
84                 return !enabled;
85         }
86
87         public AutosaveVirtualGraphs scheduleAfterInterval() {
88                 // Check global disable first.
89                 String enabled = System.getProperty(AutosavePreferences.SYSTEM_PROPERTY_AUTOSAVE);
90                 if (enabled != null && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) {
91                         return this;
92                 }
93
94                 // Then check preference.
95                 if (!getBooleanPref(AutosavePreferences.P_VG_AUTOSAVE_ENABLED, AutosavePreferences.DEFAULT_VG_AUTOSAVE_ENABLED))
96                         return this;
97
98                 wakeUp();
99                 int interval = getIntPref(AutosavePreferences.P_VG_AUTOSAVE_INTERVAL, AutosavePreferences.DEFAULT_VG_AUTOSAVE_INTERVAL);
100                 schedule(interval*1000L);
101                 return this;
102         }
103
104         @Override
105         public boolean shouldSchedule() {
106                 return enabled;
107         }
108
109         @Override
110         public boolean shouldRun() {
111                 return enabled;
112         }
113
114         protected IStatus runHeadless(IProgressMonitor monitor) {
115                 try {
116                         Session session = Simantics.peekSession();
117                         if (session == null)
118                                 return Status.CANCEL_STATUS;
119                         LifecycleSupport lfs = session.peekService(LifecycleSupport.class);
120                         if (lfs == null || lfs.isClosed() || lfs.isClosing())
121                                 return Status.CANCEL_STATUS;
122
123                         // Save
124                         monitor.beginTask("Autosaving virtual graphs...", IProgressMonitor.UNKNOWN);
125                         if (TRACE)
126                                 System.out.println("Autosaving virtual graphs...");
127                         long startTime = System.nanoTime();
128
129                         session.syncRequest(new ReadRequest() {
130                                 @Override
131                                 public void run(ReadGraph graph) throws DatabaseException {
132                                         doSave(graph);
133                                 }
134                         });
135
136                         if (TRACE) {
137                                 long endTime = System.nanoTime();
138                                 System.out.println("Autosave of virtual graphs completed in " + (endTime-startTime)*1e-6 + " ms");
139                         }
140
141                         return Status.OK_STATUS;
142                 } catch (DatabaseException e) {
143                         return new Status(Status.ERROR, Activator.PLUGIN_ID, "Autosaving virtual graphs failed", e);
144                 } finally {
145                 }
146         }
147
148         @Override
149         protected IStatus run(IProgressMonitor monitor) {
150                 if (!getBooleanPref(AutosavePreferences.P_VG_AUTOSAVE_ENABLED, AutosavePreferences.DEFAULT_VG_AUTOSAVE_ENABLED))
151                         return Status.OK_STATUS;
152                 int interval = getIntPref(AutosavePreferences.P_VG_AUTOSAVE_INTERVAL, AutosavePreferences.DEFAULT_VG_AUTOSAVE_INTERVAL);
153                 try {
154                         // Never run while a heavy database job is in progress.
155                         if (DatabaseJob.inProgress()) {
156                                 // Schedule again in at most 10 seconds instead of
157                                 // waiting for the whole autosave period again.
158                                 interval = Math.min(10, interval);
159                                 return Status.OK_STATUS;
160                         }
161
162 //                      if(PlatformUI.isWorkbenchRunning()) {
163 //                              return runInWorkbench(monitor);
164 //                      } else {
165                                 return runHeadless(monitor);
166 //                      }
167                 } finally {
168                         schedule(interval*1000L);
169                 }
170         }
171
172         public static void doSave(ReadGraph graph) throws DatabaseException {
173                 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
174                 vgs.saveAll();
175         }
176
177         public static void saveVirtualGraphsPeriodically() {
178                 getInstance().scheduleAfterInterval();
179         }
180
181 }