Added checking for new releases

This commit is contained in:
Vhati 2013-09-05 02:14:44 -04:00
parent 40c522ec8f
commit ff6f5b70bc
6 changed files with 290 additions and 28 deletions

View file

@ -0,0 +1,77 @@
package net.vhati.modmanager.json;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.vhati.modmanager.core.AutoUpdateInfo;
import net.vhati.modmanager.core.ComparableVersion;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class JacksonAutoUpdateReader {
private static final Logger log = LogManager.getLogger(JacksonAutoUpdateReader.class);
public static AutoUpdateInfo parse( File jsonFile ) {
AutoUpdateInfo aui = new AutoUpdateInfo();
Exception exception = null;
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure( JsonParser.Feature.ALLOW_SINGLE_QUOTES, true );
mapper.setVisibility( PropertyAccessor.FIELD, Visibility.ANY );
JsonNode rootNode = mapper.readTree( jsonFile );
JsonNode historiesNode = rootNode.get( "history_versions" );
JsonNode historyNode = historiesNode.get( "1" );
JsonNode latestNode = historyNode.get( "latest" );
aui.setLatestVersion( new ComparableVersion( latestNode.get( "version" ).textValue() ) );
Iterator<Map.Entry<String,JsonNode>> fieldIt = latestNode.get( "urls" ).fields();
while ( fieldIt.hasNext() ) {
Map.Entry<String,JsonNode> entry = fieldIt.next();
aui.putLatestURL( entry.getKey(), entry.getValue().textValue() );
}
JsonNode changelogNode = historyNode.get( "changelog" );
for ( JsonNode releaseNode : changelogNode ) {
String releaseVersion = releaseNode.get( "version" ).textValue();
List<String> changeList = new ArrayList<String>( releaseNode.get( "changes" ).size() );
for ( JsonNode changeNode : releaseNode.get( "changes" ) ) {
changeList.add( changeNode.textValue() );
}
aui.putChanges( new ComparableVersion( releaseVersion ), changeList );
}
}
catch ( JsonProcessingException e ) {
exception = e;
}
catch ( IOException e ) {
exception = e;
}
if ( exception != null ) {
log.error( exception );
return null;
}
return aui;
}
}

View file

@ -76,7 +76,7 @@ public class URLFetcher {
int responseCode = httpConn.getResponseCode();
if ( responseCode == HttpURLConnection.HTTP_NOT_MODIFIED ) {
log.debug( String.format( "The server's \"%s\" has not been modified since the previous check.", httpConn.getURL().getFile() ) );
log.debug( String.format( "No need to update \"%s\", the server's copy has not been modified since the previous check.", localFile.getName() ) );
// Update the local file's timestamp as if it had downloaded.
localFile.setLastModified( new Date().getTime() );
@ -98,7 +98,7 @@ public class URLFetcher {
}
}
else {
log.error( String.format( "Download request failed: HTTP Code %d (%s).", responseCode, httpConn.getResponseMessage() ) );
log.error( String.format( "Download request failed for \"%s\": HTTP Code %d (%s).", httpConn.getURL(), responseCode, httpConn.getResponseMessage() ) );
return false;
}
}