1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics;
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;
30 * @author Tuukka Lehtonen <tuukka.lehtonen@semantum.fi>
32 public class AutosaveVirtualGraphs extends Job {
34 private static final boolean TRACE = false;
36 private boolean enabled = true;
38 private Preferences defaultPrefs = DefaultScope.INSTANCE.getNode(AutosavePreferences.P_NODE);
39 private Preferences prefs = InstanceScope.INSTANCE.getNode(AutosavePreferences.P_NODE);
41 private static AutosaveVirtualGraphs INSTANCE;
43 private boolean getBooleanPref(String preference, boolean def) {
44 return prefs.getBoolean(preference, defaultPrefs.getBoolean(preference, def));
47 private int getIntPref(String preference, int def) {
48 return prefs.getInt(preference, defaultPrefs.getInt(preference, def));
51 private AutosaveVirtualGraphs() {
52 super("Autosave Virtual Graphs");
54 setPriority(Job.LONG);
57 public static synchronized AutosaveVirtualGraphs getInstance() {
58 if(INSTANCE == null) {
59 INSTANCE = new AutosaveVirtualGraphs();
68 public boolean isEnabled() {
73 * @param enabled new enabled state
74 * @return previous enabled state
76 public boolean setEnabled(boolean enabled) {
77 if (enabled == this.enabled)
79 this.enabled = enabled;
81 scheduleAfterInterval();
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())) {
94 // Then check preference.
95 if (!getBooleanPref(AutosavePreferences.P_VG_AUTOSAVE_ENABLED, AutosavePreferences.DEFAULT_VG_AUTOSAVE_ENABLED))
99 int interval = getIntPref(AutosavePreferences.P_VG_AUTOSAVE_INTERVAL, AutosavePreferences.DEFAULT_VG_AUTOSAVE_INTERVAL);
100 schedule(interval*1000L);
105 public boolean shouldSchedule() {
110 public boolean shouldRun() {
114 protected IStatus runHeadless(IProgressMonitor monitor) {
116 Session session = Simantics.peekSession();
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;
124 monitor.beginTask("Autosaving virtual graphs...", IProgressMonitor.UNKNOWN);
126 System.out.println("Autosaving virtual graphs...");
127 long startTime = System.nanoTime();
129 session.syncRequest(new ReadRequest() {
131 public void run(ReadGraph graph) throws DatabaseException {
137 long endTime = System.nanoTime();
138 System.out.println("Autosave of virtual graphs completed in " + (endTime-startTime)*1e-6 + " ms");
141 return Status.OK_STATUS;
142 } catch (DatabaseException e) {
143 return new Status(Status.ERROR, Activator.PLUGIN_ID, "Autosaving virtual graphs failed", e);
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);
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;
162 // if(PlatformUI.isWorkbenchRunning()) {
163 // return runInWorkbench(monitor);
165 return runHeadless(monitor);
168 schedule(interval*1000L);
172 public static void doSave(ReadGraph graph) throws DatabaseException {
173 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
177 public static void saveVirtualGraphsPeriodically() {
178 getInstance().scheduleAfterInterval();