Skip to content

Commit

Permalink
Fixes AppliedEnergistics#1932: Better VersionChecker exception handling
Browse files Browse the repository at this point in the history
ModVersionFetcher will now return a MissingVersion in case of an exception
instead of letting it propagate upwards.

Also added a generic try/catch to the VersionChecker itself, just in case
any unchecked exception might be triggered inside the thread and at least
not logged correctly.
  • Loading branch information
yueh committed Oct 8, 2015
1 parent c14bc82 commit be10b86
Show file tree
Hide file tree
Showing 20 changed files with 560 additions and 57 deletions.
44 changes: 29 additions & 15 deletions src/main/java/appeng/services/VersionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

import java.util.Date;

import javax.annotation.Nonnull;

import com.google.common.base.Preconditions;

import net.minecraft.nbt.NBTTagCompound;

import cpw.mods.fml.common.Loader;
Expand Down Expand Up @@ -66,30 +70,40 @@ public final class VersionChecker implements Runnable
private static final int MS_TO_SEC = 1000;
private final VersionCheckerConfig config;

public VersionChecker( final VersionCheckerConfig config )
public VersionChecker( @Nonnull final VersionCheckerConfig config )
{
Preconditions.checkNotNull( config );

this.config = config;
}

@Override
public void run()
{
Thread.yield();
try
{
Thread.yield();

// persist the config
this.config.save();
// persist the config
this.config.save();

// retrieve data
final String rawLastCheck = this.config.lastCheck();
// retrieve data
final String rawLastCheck = this.config.lastCheck();

// process data
final long lastCheck = Long.parseLong( rawLastCheck );
final Date now = new Date();
final long nowInMs = now.getTime();
final long intervalInMs = this.config.interval() * SEC_TO_HOUR * MS_TO_SEC;
final long lastAfterInterval = lastCheck + intervalInMs;
// process data
final long lastCheck = Long.parseLong( rawLastCheck );
final Date now = new Date();
final long nowInMs = now.getTime();
final long intervalInMs = this.config.interval() * SEC_TO_HOUR * MS_TO_SEC;
final long lastAfterInterval = lastCheck + intervalInMs;

this.processInterval( nowInMs, lastAfterInterval );
this.processInterval( nowInMs, lastAfterInterval );
}
catch( Exception exception )
{
// Log any unhandled exception to prevent the JVM from reporting them as unhandled.
AELog.error( exception );
}

AELog.info( "Stopping AE2 VersionChecker" );
}
Expand Down Expand Up @@ -127,7 +141,7 @@ private void processInterval( final long nowInMs, final long lastAfterInterval )
* @param modVersion version of mod
* @param githubRelease release retrieved through github
*/
private void processVersions( final Version modVersion, final FormattedRelease githubRelease )
private void processVersions( @Nonnull final Version modVersion, @Nonnull final FormattedRelease githubRelease )
{
final Version githubVersion = githubRelease.version();
final String modFormatted = modVersion.formatted();
Expand Down Expand Up @@ -162,7 +176,7 @@ private void processVersions( final Version modVersion, final FormattedRelease g
* @param ghFormatted retrieved github version formatted as rv2-beta-8
* @param changelog retrieved github changelog
*/
private void interactWithVersionCheckerMod( final String modFormatted, final String ghFormatted, final String changelog )
private void interactWithVersionCheckerMod( @Nonnull final String modFormatted, @Nonnull final String ghFormatted, @Nonnull final String changelog )
{
if( Loader.isModLoaded( "VersionChecker" ) )
{
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/appeng/services/version/BaseVersion.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.services.version;


import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

import com.google.common.base.Preconditions;


/**
* Base version of {@link Version}.
*
* Provides a unified way to test for equality and print a formatted string
*/
public abstract class BaseVersion implements Version
{
@Nonnegative
private final int revision;
@Nonnull
private final Channel channel;
@Nonnegative
private final int build;

/**
Expand All @@ -20,10 +46,11 @@ public abstract class BaseVersion implements Version
*
* @throws AssertionError if assertion are enabled and revision or build are not natural numbers
*/
public BaseVersion( final int revision, final Channel channel, final int build )
public BaseVersion( @Nonnegative final int revision, @Nonnull final Channel channel, @Nonnegative final int build )
{
assert revision >= 0;
assert build >= 0;
Preconditions.checkArgument( revision >= 0 );
Preconditions.checkNotNull( channel );
Preconditions.checkArgument( build >= 0 );

this.revision = revision;
this.channel = channel;
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/appeng/services/version/DefaultVersion.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.services.version;


import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;


/**
* AE prints version like rv2-beta-8
* GitHub prints version like rv2.beta.8
Expand All @@ -13,7 +34,7 @@ public final class DefaultVersion extends BaseVersion
* @param channel either alpha, beta or release
* @param build natural number
*/
public DefaultVersion( final int revision, final Channel channel, final int build )
public DefaultVersion( @Nonnegative final int revision, @Nonnull final Channel channel, @Nonnegative final int build )
{
super( revision, channel, build );
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/appeng/services/version/MissingVersion.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.services.version;

Expand Down
41 changes: 38 additions & 3 deletions src/main/java/appeng/services/version/ModVersionFetcher.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.services.version;


import javax.annotation.Nonnull;

import appeng.core.AELog;
import appeng.services.version.exceptions.VersionCheckerException;


/**
* Wrapper for {@link VersionParser} to check if the check is happening in developer environment or in a pull request.
*
* In that case ignore the check.
*/
public final class ModVersionFetcher implements VersionFetcher
{
private static final Version EXCEPTIONAL_VERSION = new MissingVersion();

@Nonnull
private final String rawModVersion;
@Nonnull
private final VersionParser parser;

public ModVersionFetcher( final String rawModVersion, final VersionParser parser )
public ModVersionFetcher( @Nonnull final String rawModVersion, @Nonnull final VersionParser parser )
{
this.rawModVersion = rawModVersion;
this.parser = parser;
Expand All @@ -21,7 +48,8 @@ public ModVersionFetcher( final String rawModVersion, final VersionParser parser
/**
* Parses only, if not checked in developer environment or in a pull request
*
* @return {@link DoNotCheckVersion} if in developer environment or pull request, else the parsed {@link Version}
* @return {@link DoNotCheckVersion} if in developer environment or pull request, {@link MissingVersion} in case of
* a parser exception or else the parsed {@link Version}.
*/
@Override
public Version get()
Expand All @@ -30,11 +58,18 @@ public Version get()
{
return new DoNotCheckVersion();
}
else

try
{
final Version version = this.parser.parse( this.rawModVersion );

return version;
}
catch( final VersionCheckerException e )
{
AELog.error( e );

return EXCEPTIONAL_VERSION;
}
}
}
12 changes: 11 additions & 1 deletion src/main/java/appeng/services/version/VersionCheckerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import java.io.File;
import java.util.Date;

import javax.annotation.Nonnull;

import com.google.common.base.Preconditions;

import net.minecraftforge.common.config.Configuration;


Expand All @@ -34,13 +38,16 @@ public final class VersionCheckerConfig
private static final int MIN_INTERVAL_HOURS = 0;
private static final int MAX_INTERVAL_HOURS = 7 * 24;

@Nonnull
private final Configuration config;

private final boolean isEnabled;

@Nonnull
private final String lastCheck;
private final int interval;

@Nonnull
private final String level;

private final boolean shouldNotifyPlayer;
Expand All @@ -49,8 +56,11 @@ public final class VersionCheckerConfig
/**
* @param file requires fully qualified file in which the config is saved
*/
public VersionCheckerConfig( final File file )
public VersionCheckerConfig( @Nonnull final File file )
{
Preconditions.checkNotNull( file );
Preconditions.checkState( file.isFile() );

this.config = new Configuration( file );

// initializes default values by caching
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/appeng/services/version/VersionFetcher.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/

package appeng.services.version;

Expand Down
Loading

0 comments on commit be10b86

Please sign in to comment.