mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-20 04:27:48 -05:00
#86 - updates playlist configuration file save operations to 1)create a backup copy, 2)create a lock file, 3)store the playlist, and 4)remove the lock file. On startup, if it detects a residual lock file, the playlist manager will delete the existing playlist (assumes it is incomplete or corrupted), restore the previous playlist from the backup file, and remove the lock file.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* SDR Trunk
|
* SDR Trunk
|
||||||
* Copyright (C) 2014-2016 Dennis Sheirer
|
* Copyright (C) 2014-2017 Dennis Sheirer
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -17,32 +17,10 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package playlist;
|
package playlist;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.bind.Marshaller;
|
|
||||||
import javax.xml.bind.Unmarshaller;
|
|
||||||
|
|
||||||
import audio.broadcast.BroadcastEvent;
|
|
||||||
import audio.broadcast.BroadcastModel;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import playlist.version1.PlaylistConverterV1ToV2;
|
|
||||||
import properties.SystemProperties;
|
|
||||||
import sample.Listener;
|
|
||||||
import alias.AliasEvent;
|
import alias.AliasEvent;
|
||||||
import alias.AliasModel;
|
import alias.AliasModel;
|
||||||
|
import audio.broadcast.BroadcastEvent;
|
||||||
|
import audio.broadcast.BroadcastModel;
|
||||||
import controller.ThreadPoolManager;
|
import controller.ThreadPoolManager;
|
||||||
import controller.channel.Channel.ChannelType;
|
import controller.channel.Channel.ChannelType;
|
||||||
import controller.channel.ChannelEvent;
|
import controller.channel.ChannelEvent;
|
||||||
@@ -50,18 +28,40 @@ import controller.channel.ChannelEventListener;
|
|||||||
import controller.channel.ChannelModel;
|
import controller.channel.ChannelModel;
|
||||||
import controller.channel.map.ChannelMapEvent;
|
import controller.channel.map.ChannelMapEvent;
|
||||||
import controller.channel.map.ChannelMapModel;
|
import controller.channel.map.ChannelMapModel;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import playlist.version1.PlaylistConverterV1ToV2;
|
||||||
|
import properties.SystemProperties;
|
||||||
|
import sample.Listener;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Marshaller;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public class PlaylistManager implements ChannelEventListener
|
public class PlaylistManager implements ChannelEventListener
|
||||||
{
|
{
|
||||||
private final static Logger mLog =
|
private final static Logger mLog = LoggerFactory.getLogger(PlaylistManager.class);
|
||||||
LoggerFactory.getLogger( PlaylistManager.class );
|
|
||||||
|
|
||||||
private ThreadPoolManager mThreadPoolManager;
|
private ThreadPoolManager mThreadPoolManager;
|
||||||
private AliasModel mAliasModel;
|
private AliasModel mAliasModel;
|
||||||
private BroadcastModel mBroadcastModel;
|
private BroadcastModel mBroadcastModel;
|
||||||
private ChannelModel mChannelModel;
|
private ChannelModel mChannelModel;
|
||||||
private ChannelMapModel mChannelMapModel;
|
private ChannelMapModel mChannelMapModel;
|
||||||
private Path mCurrentPlaylistPath;
|
|
||||||
|
private Path mPlaylistFolderPath;
|
||||||
|
private Path mPlaylistCurrentPath;
|
||||||
|
private Path mPlaylistBackupPath;
|
||||||
|
private Path mPlaylistLockPath;
|
||||||
|
|
||||||
private AtomicBoolean mPlaylistSavePending = new AtomicBoolean();
|
private AtomicBoolean mPlaylistSavePending = new AtomicBoolean();
|
||||||
private boolean mPlaylistLoading = false;
|
private boolean mPlaylistLoading = false;
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ public class PlaylistManager implements ChannelEventListener
|
|||||||
* @param threadPoolManager
|
* @param threadPoolManager
|
||||||
* @param channelModel
|
* @param channelModel
|
||||||
*/
|
*/
|
||||||
public PlaylistManager( ThreadPoolManager threadPoolManager,
|
public PlaylistManager(ThreadPoolManager threadPoolManager,
|
||||||
AliasModel aliasModel,
|
AliasModel aliasModel,
|
||||||
BroadcastModel broadcastModel,
|
BroadcastModel broadcastModel,
|
||||||
ChannelModel channelModel,
|
ChannelModel channelModel,
|
||||||
@@ -89,25 +89,25 @@ public class PlaylistManager implements ChannelEventListener
|
|||||||
|
|
||||||
//Register for alias, channel and channel map events so that we can
|
//Register for alias, channel and channel map events so that we can
|
||||||
//save the playlist when there are any changes
|
//save the playlist when there are any changes
|
||||||
mChannelModel.addListener( this );
|
mChannelModel.addListener(this);
|
||||||
|
|
||||||
mAliasModel.addListener( new Listener<AliasEvent>()
|
mAliasModel.addListener(new Listener<AliasEvent>()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void receive( AliasEvent t )
|
public void receive(AliasEvent t)
|
||||||
{
|
{
|
||||||
schedulePlaylistSave();
|
schedulePlaylistSave();
|
||||||
}
|
}
|
||||||
} );
|
});
|
||||||
|
|
||||||
mChannelMapModel.addListener( new Listener<ChannelMapEvent>()
|
mChannelMapModel.addListener(new Listener<ChannelMapEvent>()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void receive( ChannelMapEvent t )
|
public void receive(ChannelMapEvent t)
|
||||||
{
|
{
|
||||||
schedulePlaylistSave();
|
schedulePlaylistSave();
|
||||||
}
|
}
|
||||||
} );
|
});
|
||||||
|
|
||||||
mBroadcastModel.addListener(new Listener<BroadcastEvent>()
|
mBroadcastModel.addListener(new Listener<BroadcastEvent>()
|
||||||
{
|
{
|
||||||
@@ -125,36 +125,21 @@ public class PlaylistManager implements ChannelEventListener
|
|||||||
*/
|
*/
|
||||||
public void init()
|
public void init()
|
||||||
{
|
{
|
||||||
SystemProperties props = SystemProperties.getInstance();
|
PlaylistV2 playlist = load();
|
||||||
|
|
||||||
Path playlistFolder = props.getApplicationFolder( "playlist" );
|
|
||||||
|
|
||||||
String defaultPlaylistFile =
|
|
||||||
props.get( "playlist.v2.defaultfilename", "playlist_v2.xml" );
|
|
||||||
|
|
||||||
String playlistFile =
|
|
||||||
props.get( "playlist.v2.currentfilename", defaultPlaylistFile );
|
|
||||||
|
|
||||||
mCurrentPlaylistPath = playlistFolder.resolve( playlistFile );
|
|
||||||
|
|
||||||
PlaylistV2 playlist = load( mCurrentPlaylistPath );
|
|
||||||
|
|
||||||
boolean saveRequired = false;
|
boolean saveRequired = false;
|
||||||
|
|
||||||
if( playlist == null )
|
if(playlist == null)
|
||||||
{
|
{
|
||||||
mLog.info( "Couldn't find version 2 playlist - looking for "
|
mLog.info("Couldn't find version 2 playlist - looking for version 1 playlist to convert");
|
||||||
+ "version 1 playlist to convert" );
|
|
||||||
|
|
||||||
Path playlistV1Path = playlistFolder.resolve( "playlist.xml" );
|
Path playlistV1Path = getPlaylistFolderPath().resolve("playlist.xml");
|
||||||
|
|
||||||
PlaylistConverterV1ToV2 converter =
|
PlaylistConverterV1ToV2 converter = new PlaylistConverterV1ToV2(playlistV1Path);
|
||||||
new PlaylistConverterV1ToV2( playlistV1Path );
|
|
||||||
|
|
||||||
if( converter.hasErrorMessages() )
|
if(converter.hasErrorMessages())
|
||||||
{
|
{
|
||||||
mLog.error( "Playlist version 1 conversion errors: " +
|
mLog.error("Playlist version 1 conversion errors: " + converter.getErrorMessages());
|
||||||
converter.getErrorMessages() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
playlist = converter.getConvertedPlaylist();
|
playlist = converter.getConvertedPlaylist();
|
||||||
@@ -162,9 +147,9 @@ public class PlaylistManager implements ChannelEventListener
|
|||||||
saveRequired = true;
|
saveRequired = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
transferPlaylistToModels( playlist );
|
transferPlaylistToModels(playlist);
|
||||||
|
|
||||||
if( saveRequired )
|
if(saveRequired)
|
||||||
{
|
{
|
||||||
schedulePlaylistSave();
|
schedulePlaylistSave();
|
||||||
}
|
}
|
||||||
@@ -173,16 +158,16 @@ public class PlaylistManager implements ChannelEventListener
|
|||||||
/**
|
/**
|
||||||
* Transfers data from persisted playlist into system models
|
* Transfers data from persisted playlist into system models
|
||||||
*/
|
*/
|
||||||
private void transferPlaylistToModels( PlaylistV2 playlist )
|
private void transferPlaylistToModels(PlaylistV2 playlist)
|
||||||
{
|
{
|
||||||
if( playlist != null )
|
if(playlist != null)
|
||||||
{
|
{
|
||||||
mPlaylistLoading = true;
|
mPlaylistLoading = true;
|
||||||
|
|
||||||
mAliasModel.addAliases( playlist.getAliases() );
|
mAliasModel.addAliases(playlist.getAliases());
|
||||||
mBroadcastModel.addBroadcastConfigurations(playlist.getBroadcastConfigurations());
|
mBroadcastModel.addBroadcastConfigurations(playlist.getBroadcastConfigurations());
|
||||||
mChannelModel.addChannels( playlist.getChannels() );
|
mChannelModel.addChannels(playlist.getChannels());
|
||||||
mChannelMapModel.addChannelMaps( playlist.getChannelMaps() );
|
mChannelMapModel.addChannelMaps(playlist.getChannelMaps());
|
||||||
|
|
||||||
mPlaylistLoading = false;
|
mPlaylistLoading = false;
|
||||||
}
|
}
|
||||||
@@ -190,216 +175,264 @@ public class PlaylistManager implements ChannelEventListener
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Channel event listener method. Monitors channel events for events that
|
* Channel event listener method. Monitors channel events for events that indicate that the playlist has changed
|
||||||
* indicate that the playlist has changed and queues automatic playlist
|
* and queues automatic playlist saving.
|
||||||
* saving.
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelChanged( ChannelEvent event )
|
public void channelChanged(ChannelEvent event)
|
||||||
{
|
{
|
||||||
//Only save playlist for changes to standard channels (not traffic)
|
//Only save playlist for changes to standard channels (not traffic)
|
||||||
if( event.getChannel().getChannelType() == ChannelType.STANDARD )
|
if(event.getChannel().getChannelType() == ChannelType.STANDARD)
|
||||||
{
|
{
|
||||||
switch( event.getEvent() )
|
switch(event.getEvent())
|
||||||
{
|
{
|
||||||
case NOTIFICATION_ADD:
|
case NOTIFICATION_ADD:
|
||||||
case NOTIFICATION_CONFIGURATION_CHANGE:
|
case NOTIFICATION_CONFIGURATION_CHANGE:
|
||||||
case NOTIFICATION_DELETE:
|
case NOTIFICATION_DELETE:
|
||||||
case NOTIFICATION_PROCESSING_START:
|
|
||||||
case NOTIFICATION_PROCESSING_STOP:
|
|
||||||
schedulePlaylistSave();
|
schedulePlaylistSave();
|
||||||
break;
|
break;
|
||||||
case NOTIFICATION_ENABLE_REJECTED:
|
|
||||||
case NOTIFICATION_SELECTION_CHANGE:
|
|
||||||
case NOTIFICATION_STATE_RESET:
|
|
||||||
case REQUEST_DELETE:
|
|
||||||
case REQUEST_DESELECT:
|
|
||||||
case REQUEST_DISABLE:
|
|
||||||
case REQUEST_ENABLE:
|
|
||||||
case REQUEST_SELECT:
|
|
||||||
//Do nothing for these event types
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
//When a new event enum entry is added and received, throw an
|
|
||||||
//exception here to ensure developer adds support for the
|
|
||||||
//use case
|
|
||||||
throw new IllegalArgumentException( "Unrecognized Channel "
|
|
||||||
+ "Event [" + event.getEvent().name() + "]" );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save()
|
/**
|
||||||
|
* Folder where playlist, backup and lock file are stored
|
||||||
|
*/
|
||||||
|
private Path getPlaylistFolderPath()
|
||||||
{
|
{
|
||||||
PlaylistV2 playlist = new PlaylistV2();
|
if(mPlaylistFolderPath == null)
|
||||||
|
|
||||||
playlist.setAliases( mAliasModel.getAliases() );
|
|
||||||
playlist.setBroadcastConfigurations(mBroadcastModel.getBroadcastConfigurations());
|
|
||||||
playlist.setChannels( mChannelModel.getChannels() );
|
|
||||||
playlist.setChannelMaps( mChannelMapModel.getChannelMaps() );
|
|
||||||
|
|
||||||
JAXBContext context = null;
|
|
||||||
|
|
||||||
if( mCurrentPlaylistPath == null )
|
|
||||||
{
|
{
|
||||||
SystemProperties props = SystemProperties.getInstance();
|
SystemProperties props = SystemProperties.getInstance();
|
||||||
|
|
||||||
Path playlistPath = props.getApplicationFolder( "playlist" );
|
mPlaylistFolderPath = props.getApplicationFolder("playlist");
|
||||||
|
|
||||||
String playlistDefault = props.get( "playlist.defaultfilename",
|
|
||||||
"playlist_v2.xml" );
|
|
||||||
|
|
||||||
String playlistCurrent = props.get( "playlist.currentfilename",
|
|
||||||
playlistDefault );
|
|
||||||
|
|
||||||
mCurrentPlaylistPath = playlistPath.resolve( playlistCurrent );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
File outputFile = new File( mCurrentPlaylistPath.toString() );
|
return mPlaylistFolderPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path to current playlist
|
||||||
|
*/
|
||||||
|
private Path getPlaylistPath()
|
||||||
|
{
|
||||||
|
if(mPlaylistCurrentPath == null)
|
||||||
|
{
|
||||||
|
SystemProperties props = SystemProperties.getInstance();
|
||||||
|
|
||||||
|
String playlistDefault = props.get("playlist.defaultfilename", "playlist_v2.xml");
|
||||||
|
|
||||||
|
String playlistCurrent = props.get("playlist.currentfilename", playlistDefault);
|
||||||
|
|
||||||
|
mPlaylistCurrentPath = getPlaylistFolderPath().resolve(playlistCurrent);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mPlaylistCurrentPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path to most recent playlist backup
|
||||||
|
*/
|
||||||
|
private Path getPlaylistBackupPath()
|
||||||
|
{
|
||||||
|
if(mPlaylistBackupPath == null)
|
||||||
|
{
|
||||||
|
SystemProperties props = SystemProperties.getInstance();
|
||||||
|
|
||||||
|
String playlistDefault = props.get("playlist.defaultfilename", "playlist_v2.xml");
|
||||||
|
|
||||||
|
String playlistCurrent = props.get("playlist.currentfilename", playlistDefault);
|
||||||
|
|
||||||
|
String playlistBackup = playlistCurrent.replace(".xml", ".bak");
|
||||||
|
|
||||||
|
mPlaylistBackupPath = getPlaylistFolderPath().resolve(playlistBackup);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mPlaylistBackupPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path to playlist lock file that is created prior to saving a playlist and removed immediately thereafter.
|
||||||
|
* Presence of a lock file indicates an incomplete or corrupt playlist file on startup.
|
||||||
|
*/
|
||||||
|
private Path getPlaylistLockPath()
|
||||||
|
{
|
||||||
|
if(mPlaylistLockPath == null)
|
||||||
|
{
|
||||||
|
SystemProperties props = SystemProperties.getInstance();
|
||||||
|
|
||||||
|
String playlistDefault = props.get("playlist.defaultfilename", "playlist_v2.xml");
|
||||||
|
|
||||||
|
String playlistCurrent = props.get("playlist.currentfilename", playlistDefault);
|
||||||
|
|
||||||
|
String playlistLock = playlistCurrent.replace(".xml", ".lock");
|
||||||
|
|
||||||
|
mPlaylistLockPath = getPlaylistFolderPath().resolve(playlistLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mPlaylistLockPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the current playlist
|
||||||
|
*/
|
||||||
|
private void save()
|
||||||
|
{
|
||||||
|
PlaylistV2 playlist = new PlaylistV2();
|
||||||
|
|
||||||
|
playlist.setAliases(mAliasModel.getAliases());
|
||||||
|
playlist.setBroadcastConfigurations(mBroadcastModel.getBroadcastConfigurations());
|
||||||
|
playlist.setChannels(mChannelModel.getChannels());
|
||||||
|
playlist.setChannelMaps(mChannelMapModel.getChannelMaps());
|
||||||
|
|
||||||
|
JAXBContext context = null;
|
||||||
|
|
||||||
|
//Create a backup copy of the current playlist
|
||||||
|
if(Files.exists(getPlaylistPath()))
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if( !outputFile.exists() )
|
Files.copy(getPlaylistPath(), getPlaylistBackupPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||||
{
|
|
||||||
outputFile.createNewFile();
|
|
||||||
}
|
}
|
||||||
}
|
catch(Exception e)
|
||||||
catch( Exception e )
|
|
||||||
{
|
{
|
||||||
mLog.error( "couldn't create file to save "
|
mLog.error("Error creating backup copy of current playlist prior to saving updates [" +
|
||||||
+ "playlist [" + mCurrentPlaylistPath.toString() + "]" );
|
getPlaylistPath().toString() + "]", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputStream out = null;
|
//Create a temporary lock file to signify that we're in the process of updating the playlist
|
||||||
|
if(!Files.exists(getPlaylistLockPath()))
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
out = new FileOutputStream( outputFile );
|
Files.createFile(getPlaylistLockPath());
|
||||||
|
}
|
||||||
try
|
catch(IOException e)
|
||||||
{
|
{
|
||||||
context = JAXBContext.newInstance( PlaylistV2.class );
|
mLog.error("Error creating temporary lock file prior to saving playlist [" +
|
||||||
|
getPlaylistLockPath().toString() + "]", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try(OutputStream out = Files.newOutputStream(getPlaylistPath()))
|
||||||
|
{
|
||||||
|
context = JAXBContext.newInstance(PlaylistV2.class);
|
||||||
|
|
||||||
Marshaller m = context.createMarshaller();
|
Marshaller m = context.createMarshaller();
|
||||||
|
|
||||||
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||||
|
|
||||||
m.marshal( playlist, out );
|
m.marshal(playlist, out);
|
||||||
}
|
|
||||||
catch ( JAXBException e )
|
out.flush();
|
||||||
|
|
||||||
|
//Remove the playlist lock file to indicate that we successfully saved the file
|
||||||
|
if(Files.exists(getPlaylistLockPath()))
|
||||||
{
|
{
|
||||||
mLog.error( "jaxb exception while saving playlist: ", e );
|
Files.delete(getPlaylistLockPath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch ( Exception e )
|
catch(JAXBException je)
|
||||||
{
|
{
|
||||||
mLog.error( "couldn't open outputstream to save playlist [" +
|
mLog.error("JAXB exception while serializing the playlist to a file [" + getPlaylistPath().toString() +
|
||||||
mCurrentPlaylistPath.toString() + "]", e );
|
"]", je);
|
||||||
}
|
}
|
||||||
finally
|
catch(IOException ioe)
|
||||||
{
|
{
|
||||||
if( out != null )
|
mLog.error("IO error while writing the playlist to a file [" + getPlaylistPath().toString() + "]", ioe);
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
catch ( IOException e )
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
mLog.error("Error while saving playlist [" + getPlaylistPath().toString() + "]", e);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a version 2 playlist
|
* Loads a version 2 playlist
|
||||||
*/
|
*/
|
||||||
public PlaylistV2 load( Path playlistPath )
|
public PlaylistV2 load()
|
||||||
{
|
{
|
||||||
mLog.info( "loading version 2 playlist file [" + playlistPath.toString() + "]" );
|
mLog.info("loading version 2 playlist file [" + getPlaylistPath().toString() + "]");
|
||||||
|
|
||||||
PlaylistV2 playlist = null;
|
PlaylistV2 playlist = null;
|
||||||
|
|
||||||
if( Files.exists( playlistPath ) )
|
//Check for a lock file that indicates the previous save attempt was incomplete or had an error
|
||||||
|
if(Files.exists(getPlaylistLockPath()))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//Remove the previous playlist
|
||||||
|
Files.delete(getPlaylistPath());
|
||||||
|
|
||||||
|
//Copy the backup file to restore the previous playlist
|
||||||
|
if(Files.exists(getPlaylistBackupPath()))
|
||||||
|
{
|
||||||
|
Files.copy(getPlaylistBackupPath(), getPlaylistPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Remove the lock file
|
||||||
|
Files.delete(getPlaylistLockPath());
|
||||||
|
}
|
||||||
|
catch(IOException ioe)
|
||||||
|
{
|
||||||
|
mLog.error("Previous playlist save attempt was incomplete and there was an error restoring the " +
|
||||||
|
"playlist backup file", ioe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Files.exists(getPlaylistPath()))
|
||||||
{
|
{
|
||||||
JAXBContext context = null;
|
JAXBContext context = null;
|
||||||
|
|
||||||
InputStream in = null;
|
try(InputStream in = Files.newInputStream(getPlaylistPath()))
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
in = new FileInputStream( playlistPath.toString() );
|
context = JAXBContext.newInstance(PlaylistV2.class);
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
context = JAXBContext.newInstance( PlaylistV2.class );
|
|
||||||
|
|
||||||
Unmarshaller m = context.createUnmarshaller();
|
Unmarshaller m = context.createUnmarshaller();
|
||||||
|
|
||||||
playlist = (PlaylistV2)m.unmarshal( in );
|
playlist = (PlaylistV2) m.unmarshal(in);
|
||||||
}
|
}
|
||||||
catch ( JAXBException e )
|
catch(JAXBException je)
|
||||||
{
|
{
|
||||||
mLog.error( "jaxb exception while loading playlist: ", e );
|
mLog.error("JAXB exception while loading/unmarshalling playlist", je);
|
||||||
}
|
}
|
||||||
}
|
catch(IOException ioe)
|
||||||
catch ( Exception e )
|
|
||||||
{
|
{
|
||||||
mLog.error( "couldn't open input stream to load playlist [" + playlistPath.toString() + "]", e );
|
mLog.error("IO error while reading playlist file", ioe);
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if( in != null )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
catch ( IOException e )
|
|
||||||
{
|
|
||||||
mLog.error( "exception while closing " +
|
|
||||||
"the playlist file inputstream reader", e );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mLog.info( "PlaylistManager - playlist not found at [" +
|
mLog.info("PlaylistManager - playlist not found at [" + getPlaylistPath().toString() + "]");
|
||||||
playlistPath.toString() + "]" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return playlist;
|
return playlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schedules a playlist save task. Subsequent calls to this method will be
|
* Schedules a playlist save task. Subsequent calls to this method will be ignored until the save event occurs,
|
||||||
* ignored until the save event occurs, thus limiting repetitive playlist
|
* thus limiting repetitive playlist saving to a minimum.
|
||||||
* saving to a minimum.
|
|
||||||
*/
|
*/
|
||||||
private void schedulePlaylistSave()
|
private void schedulePlaylistSave()
|
||||||
{
|
{
|
||||||
if( !mPlaylistLoading )
|
if(!mPlaylistLoading)
|
||||||
{
|
{
|
||||||
if( mPlaylistSavePending.compareAndSet( false, true ) )
|
if(mPlaylistSavePending.compareAndSet(false, true))
|
||||||
{
|
{
|
||||||
mThreadPoolManager.scheduleOnce( new PlaylistSaveTask(),
|
mThreadPoolManager.scheduleOnce(new PlaylistSaveTask(), 2, TimeUnit.SECONDS);
|
||||||
2, TimeUnit.SECONDS );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resets the playlist save pending flag to false and proceeds to save the
|
* Resets the playlist save pending flag to false and proceeds to save the playlist.
|
||||||
* playlist.
|
|
||||||
*/
|
*/
|
||||||
public class PlaylistSaveTask implements Runnable
|
public class PlaylistSaveTask implements Runnable
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
mPlaylistSavePending.set( false );
|
mPlaylistSavePending.set(false);
|
||||||
|
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user