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 classAutosaveTask.AutosaveLayerInfo<T extends AbstractModifiableLayer>
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringAUTOSAVE_DIRprivate java.io.FileautosaveDirprivate java.util.Set<Data>changedDataprivate DataSetListenerAdapterdatasetAdapterprivate static java.lang.StringDELETED_LAYERS_DIRprivate java.util.Deque<java.io.File>deletedLayersprivate java.io.FiledeletedLayersDirprivate static char[]ILLEGAL_CHARACTERSprivate java.util.List<AutosaveTask.AutosaveLayerInfo<?>>layersInfoprivate java.lang.ObjectlayersLockstatic BooleanPropertyPROP_AUTOSAVE_ENABLEDIf autosave is enabledstatic IntegerPropertyPROP_DELETED_LAYERSHow many deleted layers should be storedstatic IntegerPropertyPROP_FILES_PER_LAYERThe number of files to store per layerstatic IntegerPropertyPROP_INDEX_LIMITThe maximum number of autosave files to storestatic IntegerPropertyPROP_INTERVALThe autosave interval, in secondsstatic BooleanPropertyPROP_NOTIFICATIONDefines 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 voidcleanupLayer(AbstractModifiableLayer removedLayer)private static voidcreateNewPidFile(java.io.File autosaveDir, java.lang.String filename)booleandataUpdated(Data data)Indicate that data has changed, and it might be a good idea to autosave.voiddiscardUnsavedLayers()Mark all unsaved layers as deleted.protected voiddisplayNotification()java.nio.file.PathgetAutosaveDir()Replies the autosave directory.private static java.lang.StringgetFileName(java.lang.String layerName, int index)protected java.io.FilegetNewLayerFile(AutosaveTask.AutosaveLayerInfo<?> layer, java.time.Instant now, int startIndex)protected java.io.FilegetPidFile(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 booleanjvmPerfDataFileExists(java.lang.String jvmId)voidlayerAdded(LayerManager.LayerAddEvent e)Notifies this listener that a layer has been added.voidlayerOrderChanged(LayerManager.LayerOrderChangeEvent e)Notifies this listener that the order of layers was changed.voidlayerRemoving(LayerManager.LayerRemoveEvent e)Notifies this listener that a layer was just removed.private voidmoveToDeletedLayersFolder(java.io.File f)Move file to the deleted layers directory.voidnoteDataUpdated(NoteData data)Called when the note data is updatedvoidprocessDatasetEvent(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 voidregisterNewlayer(NoteLayer layer)private voidregisterNewlayer(OsmDataLayer layer)voidrun()private voidsavelayer(AutosaveTask.AutosaveLayerInfo<?> info)voidschedule()Starts the autosave background task.voidselectedNoteChanged(NoteData noteData)The selected node was changedprivate voidsetLayerFileName(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:
runin interfacejava.lang.Runnable- Specified by:
runin classjava.util.TimerTask
-
displayNotification
protected void displayNotification()
-
layerOrderChanged
public void layerOrderChanged(LayerManager.LayerOrderChangeEvent e)
Description copied from interface:LayerManager.LayerChangeListenerNotifies 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:
layerOrderChangedin 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.LayerChangeListenerNotifies 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:
layerAddedin interfaceLayerManager.LayerChangeListener- Parameters:
e- The new added layer event
-
layerRemoving
public void layerRemoving(LayerManager.LayerRemoveEvent e)
Description copied from interface:LayerManager.LayerChangeListenerNotifies 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:
layerRemovingin 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.ListenerCalled when notified about any type of dataset change event.- Specified by:
processDatasetEventin interfaceDataSetListenerAdapter.Listener- Parameters:
event- dataset change event
-
noteDataUpdated
public void noteDataUpdated(NoteData data)
Description copied from interface:NoteData.NoteDataUpdateListenerCalled when the note data is updated- Specified by:
noteDataUpdatedin 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.NoteDataUpdateListenerThe selected node was changed- Specified by:
selectedNoteChangedin 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.
-
-