Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.1.1 #3

Merged
merged 9 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
@@ -0,0 +1 @@
* joshua@hypera.dev
10 changes: 10 additions & 0 deletions .github/dependabot.yml
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "maven"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
2 changes: 1 addition & 1 deletion .github/workflows/publish-release.yml
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Java
uses: actions/setup-java@v2
with:
java-version: '8'
java-version: '11'
distribution: 'adopt'
server-id: hypera-releases
server-username: MAVEN_USERNAME
Expand Down
22 changes: 0 additions & 22 deletions .github/workflows/publish-snapshot.yml

This file was deleted.

1 change: 1 addition & 0 deletions SECURITY.md
Expand Up @@ -4,6 +4,7 @@

| Version | Supported |
| :------ | -----------------: |
| 3.x.x | :white_check_mark: |
| 2.0.x | :white_check_mark: |
| < 2.0 | :x: |

Expand Down
11 changes: 2 additions & 9 deletions pom.xml
Expand Up @@ -22,7 +22,7 @@

<groupId>dev.hypera</groupId>
<artifactId>UpdateLib</artifactId>
<version>2.1.2</version>
<version>3.1.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>UpdateLib</name>
Expand Down Expand Up @@ -113,7 +113,7 @@
</build>

<dependencies>
<!-- To read and use the Spigot API -->
<!-- JSON-Simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
Expand All @@ -126,13 +126,6 @@
</exclusion>
</exclusions>
</dependency>

<!-- To compare semver -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>

</project>
135 changes: 135 additions & 0 deletions src/main/java/dev/hypera/updatelib/UpdateLib.java
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2021 Joshua Sing <joshua@hypera.dev>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package dev.hypera.updatelib;

import dev.hypera.updatelib.annotations.RunAsync;
import dev.hypera.updatelib.checkers.UpdateChecker;
import dev.hypera.updatelib.data.CheckData;
import dev.hypera.updatelib.objects.UpdateStatus;
import dev.hypera.updatelib.objects.UpdateStatusBuilder;

import java.util.Timer;
import java.util.TimerTask;
import java.util.function.Consumer;

public class UpdateLib {

private final static String VERSION = "3.1.2-SNAPSHOT"; // Current UpdateLib version.

private final long resourceId;
private final String currentVersion;
private final int connectionTimeout;

private final UpdateChecker updateChecker;

private final Consumer<UpdateStatus> completeAction;
private final Consumer<Throwable> errorHandler;

private UpdateStatus lastStatus = null;
private long lastCheck = 0L;

/**
* UpdateLib Constructor - Used internally by UpdateLib.
*
* @param resourceId Resource ID.
* @param currentVersion Current version of the Resource.
* @param repeatingChecksEnabled If UpdateLib should check for updates periodically.
* @param interval How often UpdateLib should check for updates.
* @param connectionTimeout The amount of milliseconds UpdateLib should wait before timing out on requests.
* @param updateChecker {@link UpdateChecker} to be used by UpdateLib to check for updates.
* @param completeAction Action to run after UpdateLib has checked for an update.
* @param errorHandler Consumer to run if UpdateLib encounters an exception.
*/
protected UpdateLib(long resourceId, String currentVersion, boolean repeatingChecksEnabled, long interval, int connectionTimeout, UpdateChecker updateChecker, Consumer<UpdateStatus> completeAction, Consumer<Throwable> errorHandler) {
this.resourceId = resourceId;
this.currentVersion = currentVersion;
this.connectionTimeout = connectionTimeout;
this.updateChecker = updateChecker;
this.completeAction = completeAction;
this.errorHandler = errorHandler;

Thread thread = new Thread(this::checkNow);
thread.setName("UpdateLib-" + thread.getId());
thread.start();

if(repeatingChecksEnabled) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
checkNow();
}
}, interval);
}
}


/**
* Start an update check. It's recommended to only run this asynchronously as it may take time to fetch data from
* the API.
*
* @return Response - Instance of {@link UpdateStatus}
* @since 2.0.0-SNAPSHOT
*/
@RunAsync
public UpdateStatus checkNow() {
try {
lastStatus = updateChecker.check(new CheckData(resourceId, currentVersion, connectionTimeout));
lastCheck = System.currentTimeMillis();
if(null != completeAction)
completeAction.accept(lastStatus);
} catch (Exception ex) {
if(null == errorHandler)
ex.printStackTrace();
else
errorHandler.accept(ex);
}

return lastStatus;
}

/**
* Get last stored {@link UpdateStatus}. If UpdateLib hasn't checked for an update or the last check failed, this
* will return null.
*
* @return Last stored {@link UpdateStatus}
* @since 3.0.0-SNAPSHOT
*/
public UpdateStatus getLastStatus() {
return lastStatus;
}

/**
* Get the last time UpdateLib checked for updates.
*
* @return Last check time in milliseconds.
* @since 2.0.0-SNAPSHOT
*/
public long getLastCheck() {
return lastCheck;
}

/**
* Get the current version of UpdateLib.
*
* @return Current version of UpdateLib.
* @since 2.1.0-SNAPSHOT
*/
public static String getVersion() {
return VERSION;
}

}
92 changes: 70 additions & 22 deletions src/main/java/dev/hypera/updatelib/UpdateLibBuilder.java
Expand Up @@ -16,33 +16,51 @@

package dev.hypera.updatelib;

import dev.hypera.updatelib.internal.UpdateLib;
import dev.hypera.updatelib.internal.UpdateResponse;
import dev.hypera.updatelib.annotations.Builder;
import dev.hypera.updatelib.checkers.UpdateChecker;
import dev.hypera.updatelib.checkers.impl.SpigotUpdateChecker;
import dev.hypera.updatelib.objects.UpdateStatus;
import dev.hypera.updatelib.utils.Check;

import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

@SuppressWarnings("unused")
@Builder
public class UpdateLibBuilder {

private final long resourceId;
private final String currentVersion;

private boolean repeatingChecksEnabled = true;
private long checkInterval = 2 * (60 * (60 * 1000)); // 2 Hours
private int connectionTimeout = 5000; // 5 Seconds
private long checkInterval = 2 * (60 * (60 * 1000)); // Defaults to 2 hours.
private int connectionTimeout = 10000; // Defaults to 10 seconds.

private Consumer<UpdateResponse> consumer = null;
/**
* Defaults to using {@link SpigotUpdateChecker}.
*/
private UpdateChecker updateChecker = new SpigotUpdateChecker();

private Consumer<UpdateStatus> completeAction = null;
private Consumer<Throwable> errorHandler = null; // If error handler is not provided, stack traces will be printed by UpdateLib.


/**
* UpdateLibBuilder Constructor - Used internally by UpdateLib.
*
* @param currentVersion Current version of the resource.
* @param resourceId Resource ID.
*/
private UpdateLibBuilder(String currentVersion, long resourceId) {
Check.notNull("currentVersion", currentVersion);
this.currentVersion = currentVersion;
this.resourceId = resourceId;
}

/**
* Creates a new instance of {@link UpdateLibBuilder}.
* Create a new instance of {@link UpdateLibBuilder}
*
* @param currentVersion Current version of the resource.
* @param resourceId SpigotMC Resource Id.
* @param resourceId Resource ID.
*
* @return Instance of {@link UpdateLibBuilder}
* @since 2.0.0-SNAPSHOT
Expand All @@ -52,11 +70,11 @@ public static UpdateLibBuilder create(String currentVersion, long resourceId) {
}

/**
* Should UpdateLib keep checking for updates? (Time defined by checkInterval)
* Sets if UpdateLib should check for updates periodically.
*
* @param enabled Repeating checks enabled.
* @param enabled If UpdateLib should check for updates periodically.
*
* @see #setCheckInterval(long)
* @return {@link UpdateLibBuilder}
* @since 2.0.0-SNAPSHOT
*/
public UpdateLibBuilder setRepeatingChecksEnabled(boolean enabled) {
Expand All @@ -65,23 +83,26 @@ public UpdateLibBuilder setRepeatingChecksEnabled(boolean enabled) {
}

/**
* How often should UpdateLib check for updates? (Only works if repeatingChecksEnabled is true)
* Sets how often UpdateLib should check for updates? This is only needed if 'repeatingChecksEnabled' is true.
*
* @param interval Interval in milliseconds.
* @param time Time.
* @param unit TimeUnit.
*
* @return {@link UpdateLibBuilder}
* @see #setRepeatingChecksEnabled(boolean)
* @since 2.0.0-SNAPSHOT
*/
public UpdateLibBuilder setCheckInterval(long interval) {
this.checkInterval = interval;
public UpdateLibBuilder setCheckInterval(long time, TimeUnit unit) {
this.checkInterval = unit.toMillis(time);
return this;
}

/**
* After how many milliseconds should we timeout the request to SpigotMC's API?
* Sets the amount of milliseconds UpdateLib should wait before timing out on requests.
*
* @param timeout Timeout in milliseconds.
*
* @return {@link UpdateLibBuilder}
* @since 2.0.0-SNAPSHOT
*/
public UpdateLibBuilder setConnectionTimeout(int timeout) {
Expand All @@ -90,25 +111,52 @@ public UpdateLibBuilder setConnectionTimeout(int timeout) {
}

/**
* Sets a consumer to run after UpdateLib has checked for an update.
* Sets the {@link UpdateChecker} to be used by UpdateLib to check for updates.
*
* @param updateChecker Instance of an {@link UpdateChecker}
*
* @return {@link UpdateLibBuilder}
* @since 3.0.0-SNAPSHOT
*/
public UpdateLibBuilder setUpdateChecker(UpdateChecker updateChecker) {
this.updateChecker = updateChecker;
return this;
}

/**
* Sets an action to run after UpdateLib has checked for an update.
*
* @param consumer Consumer to run after checking for an update.
* @param action Consumer to run after checking for an update.
*
* @return {@link UpdateLibBuilder}
* @since 2.1.0-SNAPSHOT
*/
public UpdateLibBuilder setConsumer(Consumer<UpdateResponse> consumer) {
this.consumer = consumer;
public UpdateLibBuilder setCompleteAction(Consumer<UpdateStatus> action) {
this.completeAction = action;
return this;
}

/**
* Sets an consumer to run if UpdateLib encounters an exception.
*
* @param handler Error handler.
*
* @return {@link UpdateLibBuilder}
* @since 3.0.0-SNAPSHOT
*/
public UpdateLibBuilder setErrorHandler(Consumer<Throwable> handler) {
this.errorHandler = handler;
return this;
}

/**
* Builds a new instance of {@link UpdateLib}.
* Builds a new instance of {@link UpdateLib}
*
* @return Instance of {@link UpdateLib}
* @since 2.0.0-SNAPSHOT
*/
public UpdateLib build() {
return new UpdateLib(resourceId, currentVersion, repeatingChecksEnabled, checkInterval, connectionTimeout, consumer);
return new UpdateLib(resourceId, currentVersion, repeatingChecksEnabled, checkInterval, connectionTimeout, updateChecker, completeAction, errorHandler);
}

}