Package org.openstreetmap.josm.gui.layer
Class AutosaveTask
- java.lang.Object
-
- java.util.TimerTask
-
- org.openstreetmap.josm.gui.layer.AutosaveTask
-
- All Implemented Interfaces:
java.lang.Runnable
,DataSetListenerAdapter.Listener
,NoteData.NoteDataUpdateListener
,LayerManager.LayerChangeListener
public class AutosaveTask extends java.util.TimerTask implements LayerManager.LayerChangeListener, DataSetListenerAdapter.Listener, NoteData.NoteDataUpdateListener
Saves data and note layers periodically so they can be recovered in case of a crash. There are 2 directories - autosave dir: copies of the currently open data layers are saved here every PROP_INTERVAL seconds. When a data layer is closed normally, the corresponding files are removed. If this dir is non-empty on start, JOSM assumes that it crashed last time. - deleted layers dir: "secondary archive" - when autosaved layers are restored they are copied to this directory. We cannot keep them in the autosave folder, but just deleting it would be dangerous: Maybe a feature inside the file caused JOSM to crash. If the data is valuable, the user can still try to open with another versions of JOSM or fix the problem manually. The deleted layers dir keeps at most PROP_DELETED_LAYERS files.- Since:
- 3378 (creation), 10386 (new LayerChangeListener interface)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AutosaveTask.AutosaveLayerInfo<T extends AbstractModifiableLayer>
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
AUTOSAVE_DIR
private java.io.File
autosaveDir
private java.util.Set<Data>
changedData
private DataSetListenerAdapter
datasetAdapter
private static java.lang.String
DELETED_LAYERS_DIR
private java.util.Deque<java.io.File>
deletedLayers
private java.io.File
deletedLayersDir
private static char[]
ILLEGAL_CHARACTERS
private java.util.List<AutosaveTask.AutosaveLayerInfo<?>>
layersInfo
private java.lang.Object
layersLock
static BooleanProperty
PROP_AUTOSAVE_ENABLED
If autosave is enabledstatic IntegerProperty
PROP_DELETED_LAYERS
How many deleted layers should be storedstatic IntegerProperty
PROP_FILES_PER_LAYER
The number of files to store per layerstatic IntegerProperty
PROP_INDEX_LIMIT
The maximum number of autosave files to storestatic IntegerProperty
PROP_INTERVAL
The autosave interval, in secondsstatic BooleanProperty
PROP_NOTIFICATION
Defines if a notification should be displayed after each autosave
-
Constructor Summary
Constructors Constructor Description AutosaveTask()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
cleanupLayer(AbstractModifiableLayer removedLayer)
private static void
createNewPidFile(java.io.File autosaveDir, java.lang.String filename)
boolean
dataUpdated(Data data)
Indicate that data has changed, and it might be a good idea to autosave.void
discardUnsavedLayers()
Mark all unsaved layers as deleted.protected void
displayNotification()
java.nio.file.Path
getAutosaveDir()
Replies the autosave directory.private static java.lang.String
getFileName(java.lang.String layerName, int index)
protected java.io.File
getNewLayerFile(AutosaveTask.AutosaveLayerInfo<?> layer, java.time.Instant now, int startIndex)
protected java.io.File
getPidFile(java.io.File osmFile)
java.util.List<java.io.File>
getUnsavedLayersFiles()
Replies the list of .osm files still present in autosave dir, that are not currently managed by another instance of JOSM.private static boolean
jvmPerfDataFileExists(java.lang.String jvmId)
void
layerAdded(LayerManager.LayerAddEvent e)
Notifies this listener that a layer has been added.void
layerOrderChanged(LayerManager.LayerOrderChangeEvent e)
Notifies this listener that the order of layers was changed.void
layerRemoving(LayerManager.LayerRemoveEvent e)
Notifies this listener that a layer was just removed.private void
moveToDeletedLayersFolder(java.io.File f)
Move file to the deleted layers directory.void
noteDataUpdated(NoteData data)
Called when the note data is updatedvoid
processDatasetEvent(AbstractDatasetChangedEvent event)
Called when notified about any type of dataset change event.java.util.concurrent.Future<?>
recoverUnsavedLayers()
Recover the unsaved layers and open them asynchronously.private void
registerNewlayer(NoteLayer layer)
private void
registerNewlayer(OsmDataLayer layer)
void
run()
private void
savelayer(AutosaveTask.AutosaveLayerInfo<?> info)
void
schedule()
Starts the autosave background task.void
selectedNoteChanged(NoteData noteData)
The selected node was changedprivate void
setLayerFileName(AutosaveTask.AutosaveLayerInfo<?> layer)
-
-
-
Field Detail
-
ILLEGAL_CHARACTERS
private static final char[] ILLEGAL_CHARACTERS
-
AUTOSAVE_DIR
private static final java.lang.String AUTOSAVE_DIR
- See Also:
- Constant Field Values
-
DELETED_LAYERS_DIR
private static final java.lang.String DELETED_LAYERS_DIR
- See Also:
- Constant Field Values
-
PROP_AUTOSAVE_ENABLED
public static final BooleanProperty PROP_AUTOSAVE_ENABLED
If autosave is enabled
-
PROP_FILES_PER_LAYER
public static final IntegerProperty PROP_FILES_PER_LAYER
The number of files to store per layer
-
PROP_DELETED_LAYERS
public static final IntegerProperty PROP_DELETED_LAYERS
How many deleted layers should be stored
-
PROP_INTERVAL
public static final IntegerProperty PROP_INTERVAL
The autosave interval, in seconds
-
PROP_INDEX_LIMIT
public static final IntegerProperty PROP_INDEX_LIMIT
The maximum number of autosave files to store
-
PROP_NOTIFICATION
public static final BooleanProperty PROP_NOTIFICATION
Defines if a notification should be displayed after each autosave
-
datasetAdapter
private final DataSetListenerAdapter datasetAdapter
-
changedData
private final java.util.Set<Data> changedData
-
layersInfo
private final java.util.List<AutosaveTask.AutosaveLayerInfo<?>> layersInfo
-
layersLock
private final java.lang.Object layersLock
-
deletedLayers
private final java.util.Deque<java.io.File> deletedLayers
-
autosaveDir
private final java.io.File autosaveDir
-
deletedLayersDir
private final java.io.File deletedLayersDir
-
-
Constructor Detail
-
AutosaveTask
public AutosaveTask()
-
-
Method Detail
-
getAutosaveDir
public final java.nio.file.Path getAutosaveDir()
Replies the autosave directory.- Returns:
- the autosave directory
- Since:
- 10299
-
schedule
public void schedule()
Starts the autosave background task.
-
getFileName
private static java.lang.String getFileName(java.lang.String layerName, int index)
-
setLayerFileName
private void setLayerFileName(AutosaveTask.AutosaveLayerInfo<?> layer)
-
getNewLayerFile
protected java.io.File getNewLayerFile(AutosaveTask.AutosaveLayerInfo<?> layer, java.time.Instant now, int startIndex)
-
createNewPidFile
private static void createNewPidFile(java.io.File autosaveDir, java.lang.String filename)
-
savelayer
private void savelayer(AutosaveTask.AutosaveLayerInfo<?> info)
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
- Specified by:
run
in classjava.util.TimerTask
-
displayNotification
protected void displayNotification()
-
layerOrderChanged
public void layerOrderChanged(LayerManager.LayerOrderChangeEvent e)
Description copied from interface:LayerManager.LayerChangeListener
Notifies this listener that the order of layers was changed.Listeners are called in the EDT thread. You should not do blocking or long-running tasks in this method.
- Specified by:
layerOrderChanged
in interfaceLayerManager.LayerChangeListener
- Parameters:
e
- The order change event.
-
registerNewlayer
private void registerNewlayer(OsmDataLayer layer)
-
registerNewlayer
private void registerNewlayer(NoteLayer layer)
-
layerAdded
public void layerAdded(LayerManager.LayerAddEvent e)
Description copied from interface:LayerManager.LayerChangeListener
Notifies this listener that a layer has been added.Listeners are called in the EDT thread. You should not do blocking or long-running tasks in this method.
- Specified by:
layerAdded
in interfaceLayerManager.LayerChangeListener
- Parameters:
e
- The new added layer event
-
layerRemoving
public void layerRemoving(LayerManager.LayerRemoveEvent e)
Description copied from interface:LayerManager.LayerChangeListener
Notifies this listener that a layer was just removed.Listeners are called in the EDT thread after the layer was removed. Use
LayerManager.LayerRemoveEvent.scheduleRemoval(Collection)
to remove more layers. You should not do blocking or long-running tasks in this method.- Specified by:
layerRemoving
in interfaceLayerManager.LayerChangeListener
- Parameters:
e
- The layer to be removed (as event)
-
cleanupLayer
private void cleanupLayer(AbstractModifiableLayer removedLayer)
-
processDatasetEvent
public void processDatasetEvent(AbstractDatasetChangedEvent event)
Description copied from interface:DataSetListenerAdapter.Listener
Called when notified about any type of dataset change event.- Specified by:
processDatasetEvent
in interfaceDataSetListenerAdapter.Listener
- Parameters:
event
- dataset change event
-
noteDataUpdated
public void noteDataUpdated(NoteData data)
Description copied from interface:NoteData.NoteDataUpdateListener
Called when the note data is updated- Specified by:
noteDataUpdated
in interfaceNoteData.NoteDataUpdateListener
- Parameters:
data
- The data that was changed
-
dataUpdated
public boolean dataUpdated(Data data)
Indicate that data has changed, and it might be a good idea to autosave.- Parameters:
data
- The data that has changed- Returns:
- See
Set.add(E)
- Since:
- 16548
-
selectedNoteChanged
public void selectedNoteChanged(NoteData noteData)
Description copied from interface:NoteData.NoteDataUpdateListener
The selected node was changed- Specified by:
selectedNoteChanged
in interfaceNoteData.NoteDataUpdateListener
- Parameters:
noteData
- The data of which the selected node was changed
-
getPidFile
protected java.io.File getPidFile(java.io.File osmFile)
-
getUnsavedLayersFiles
public java.util.List<java.io.File> getUnsavedLayersFiles()
Replies the list of .osm files still present in autosave dir, that are not currently managed by another instance of JOSM. These files are hence unsaved layers from an old instance of JOSM that crashed and may be recovered by this instance.- Returns:
- The list of .osm files still present in autosave dir, that are not currently managed by another instance of JOSM
-
jvmPerfDataFileExists
private static boolean jvmPerfDataFileExists(java.lang.String jvmId)
-
recoverUnsavedLayers
public java.util.concurrent.Future<?> recoverUnsavedLayers()
Recover the unsaved layers and open them asynchronously.- Returns:
- A future that can be used to wait for the completion of this task.
-
moveToDeletedLayersFolder
private void moveToDeletedLayersFolder(java.io.File f)
Move file to the deleted layers directory. If moving does not work, it will try to delete the file directly. Afterwards, if the number of deleted layers gets larger than PROP_DELETED_LAYERS, some files in the deleted layers directory will be removed.- Parameters:
f
- the file, usually from the autosave dir
-
discardUnsavedLayers
public void discardUnsavedLayers()
Mark all unsaved layers as deleted. They are still preserved in the deleted layers folder.
-
-