blob: cd85841f5bd38077fec354e3b6c7dadd23b1fa8e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*******************************************************************************
* Copyright (c) 2013 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.internal.framework;
import java.io.IOException;
import java.util.concurrent.*;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
public final class StorageSaver {
private static class StorageSaverTask implements Runnable {
private final EquinoxContainer container;
public StorageSaverTask(EquinoxContainer container) {
this.container = container;
}
@Override
public void run() {
try {
container.getStorage().save();
} catch (IOException e) {
container.getLogServices().log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Error saving on update", e); //$NON-NLS-1$
}
}
}
private final EquinoxContainer container;
private final long delay;
private final ScheduledFuture<?> future;
private final Thread hook;
private final StorageSaverTask task;
public StorageSaver(EquinoxContainer container) {
this.container = container;
task = new StorageSaverTask(container);
delay = computeDelay();
future = scheduleTask();
hook = registerShutdownHook();
}
public void close() {
unregisterShutdownHook();
unscheduleTask();
}
public void save() {
if (delay != 0)
// Periodic saves are enabled or saves are disabled altogether.
return;
// Immediately save on request.
task.run();
}
private Thread registerShutdownHook() {
Thread thread = new Thread(task, "Equinox Shutdown Hook"); //$NON-NLS-1$
Runtime.getRuntime().addShutdownHook(thread);
return thread;
}
private long computeDelay() {
EquinoxConfiguration configuration = container.getConfiguration();
// Default provided by the configuration, if necessary.
String delayProp = configuration.getConfiguration(EquinoxConfiguration.PROP_STATE_SAVE_DELAY_INTERVAL);
// Type compatibility verified by the configuration.
return Long.parseLong(delayProp);
}
private ScheduledFuture<?> scheduleTask() {
// Negative delay disables saves. Zero delay results in immediate saves.
if (delay <= 0)
return null;
ScheduledExecutorService executor = container.getScheduledExecutor();
return executor.scheduleWithFixedDelay(task, delay, delay, TimeUnit.MILLISECONDS);
}
private void unregisterShutdownHook() {
try {
Runtime.getRuntime().removeShutdownHook(hook);
} catch (IllegalStateException e) {
// Ignore.
}
}
private void unscheduleTask() {
if (future != null)
future.cancel(false);
}
}
|