org.openstreetmap.josm.data
Class AutosaveTask

java.lang.Object
  extended by java.util.TimerTask
      extended by org.openstreetmap.josm.data.AutosaveTask
All Implemented Interfaces:
java.lang.Runnable, DataSetListenerAdapter.Listener, MapView.LayerChangeListener

public class AutosaveTask
extends java.util.TimerTask
implements MapView.LayerChangeListener, DataSetListenerAdapter.Listener

Saves data 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.


Nested Class Summary
private static class AutosaveTask.AutosaveLayerInfo
           
 
Field Summary
private static java.lang.String AUTOSAVE_DIR
           
private  java.io.File autosaveDir
           
private  java.util.Set<DataSet> changedDatasets
           
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
           
static IntegerProperty PROP_DELETED_LAYERS
           
static IntegerProperty PROP_FILES_PER_LAYER
           
static IntegerProperty PROP_INDEX_LIMIT
           
static IntegerProperty PROP_INTERVAL
           
private  java.util.Timer timer
           
 
Constructor Summary
AutosaveTask()
           
 
Method Summary
 void activeLayerChange(Layer oldLayer, Layer newLayer)
          Notifies this listener that the active layer has changed.
 void dicardUnsavedLayers()
           
private  java.lang.String getFileName(java.lang.String layerName, int index)
           
private  java.io.File getNewLayerFile(AutosaveTask.AutosaveLayerInfo layer)
           
 java.util.List<java.io.File> getUnsavedLayersFiles()
           
 void layerAdded(Layer newLayer)
          Notifies this listener that a layer has been added.
 void layerRemoved(Layer oldLayer)
          Notifies this listener that a layer has been removed.
private  void moveToDeletedLayersFolder(java.io.File f)
          Move file to the deleted layers directory.
 void processDatasetEvent(AbstractDatasetChangedEvent event)
           
 void recoverUnsavedLayers()
           
private  void registerNewlayer(OsmDataLayer layer)
           
 void run()
           
private  void savelayer(AutosaveTask.AutosaveLayerInfo info)
           
 void schedule()
           
private  void setLayerFileName(AutosaveTask.AutosaveLayerInfo layer)
           
 
Methods inherited from class java.util.TimerTask
cancel, scheduledExecutionTime
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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

PROP_FILES_PER_LAYER

public static final IntegerProperty PROP_FILES_PER_LAYER

PROP_DELETED_LAYERS

public static final IntegerProperty PROP_DELETED_LAYERS

PROP_INTERVAL

public static final IntegerProperty PROP_INTERVAL

PROP_INDEX_LIMIT

public static final IntegerProperty PROP_INDEX_LIMIT

datasetAdapter

private final DataSetListenerAdapter datasetAdapter

changedDatasets

private java.util.Set<DataSet> changedDatasets

layersInfo

private final java.util.List<AutosaveTask.AutosaveLayerInfo> layersInfo

timer

private java.util.Timer timer

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

schedule

public void schedule()

getFileName

private java.lang.String getFileName(java.lang.String layerName,
                                     int index)

setLayerFileName

private void setLayerFileName(AutosaveTask.AutosaveLayerInfo layer)

getNewLayerFile

private java.io.File getNewLayerFile(AutosaveTask.AutosaveLayerInfo layer)

savelayer

private void savelayer(AutosaveTask.AutosaveLayerInfo info)
                throws java.io.IOException
Throws:
java.io.IOException

run

public void run()
Specified by:
run in interface java.lang.Runnable
Specified by:
run in class java.util.TimerTask

activeLayerChange

public void activeLayerChange(Layer oldLayer,
                              Layer newLayer)
Description copied from interface: MapView.LayerChangeListener
Notifies this listener that the active layer has changed.

Specified by:
activeLayerChange in interface MapView.LayerChangeListener
Parameters:
oldLayer - The previous active layer
newLayer - The new activer layer

registerNewlayer

private void registerNewlayer(OsmDataLayer layer)

layerAdded

public void layerAdded(Layer newLayer)
Description copied from interface: MapView.LayerChangeListener
Notifies this listener that a layer has been added.

Specified by:
layerAdded in interface MapView.LayerChangeListener
Parameters:
newLayer - The new added layer

layerRemoved

public void layerRemoved(Layer oldLayer)
Description copied from interface: MapView.LayerChangeListener
Notifies this listener that a layer has been removed.

Specified by:
layerRemoved in interface MapView.LayerChangeListener
Parameters:
oldLayer - The old removed layer

processDatasetEvent

public void processDatasetEvent(AbstractDatasetChangedEvent event)
Specified by:
processDatasetEvent in interface DataSetListenerAdapter.Listener

getUnsavedLayersFiles

public java.util.List<java.io.File> getUnsavedLayersFiles()

recoverUnsavedLayers

public void recoverUnsavedLayers()

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

dicardUnsavedLayers

public void dicardUnsavedLayers()


JOSM