Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 0f6ea8c0ce
Fetching contributors…

Cannot retrieve contributors at this time

73 lines (60 sloc) 2.244 kB
package com.goldin.plugins.teamcity.messenger.impl
import com.goldin.plugins.teamcity.messenger.api.MessagesContext
import com.goldin.plugins.teamcity.messenger.api.MessagesPersistency
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import jetbrains.buildServer.serverSide.ServerPaths
import org.gcontracts.annotations.Requires
import org.gcontracts.annotations.Invariant
/**
* {@link MessagesPersistency} implementation
*/
@Invariant({
this.dataDirectory.directory &&
this.context &&
this.jsonFile.file &&
this.jsonFile.canWrite()
})
class MessagesPersistencyImpl implements MessagesPersistency
{
private final File dataDirectory
private final MessagesContext context
private final File jsonFile
@Requires({ context && paths })
MessagesPersistencyImpl ( MessagesContext context, ServerPaths paths )
{
this.dataDirectory = new File( paths.pluginDataDirectory, context.pluginName )
this.context = context
this.jsonFile = new File( dataDirectory, 'messages.json' )
if ( ! dataDirectory.isDirectory()) { assert dataDirectory.mkdirs() }
if ( ! jsonFile.isFile()) { assert jsonFile.createNewFile()}
}
@Override
void save ( Map data )
{
try
{
jsonFile.write( new JsonBuilder( data ).toString())
}
catch ( e )
{
context.log.error( "Failed to persist [$data] to [$jsonFile.canonicalPath]: $e", e )
}
}
@Override
Map restore ()
{
def jsonData = jsonFile.text
try
{
return ( jsonData ? ( Map ) new JsonSlurper().parseText( jsonData ) : [:] )
}
catch ( Throwable t )
{
def copyFile = new File( dataDirectory, "messages-failed-to-load-${ System.currentTimeMillis() }.json" )
assert jsonFile.with{ renameTo( copyFile ) && createNewFile() }
context.log.error( "Failed to restore JSON data from [$jsonFile.canonicalPath], copied to [$copyFile.canonicalPath]: $t", t )
return [:]
}
}
}
Jump to Line
Something went wrong with that request. Please try again.