22
33import java .io .IOException ;
44import java .net .HttpURLConnection ;
5+ import java .net .MalformedURLException ;
56import java .net .URI ;
67import java .net .URL ;
78import java .util .Properties ;
1314 */
1415public class Updater {
1516
17+ private static final String VERSION_PROPERTY_FILE = "commands.properties" ;
18+ private static URL URL_LATEST_RELEASE ;
19+ private static Logger LOGGER = Logger .getLogger ("CommandsAPI" );
20+
21+ static {
22+ try {
23+ URL_LATEST_RELEASE = URI .create ("https://api.github.com/repos/Traqueur-dev/CommandsAPI/releases/latest" ).toURL ();
24+ } catch (MalformedURLException e ) {
25+ throw new RuntimeException (e );
26+ }
27+ }
28+
29+ public static void setUrlLatestRelease (URL URL_LATEST_RELEASE ) {
30+ Updater .URL_LATEST_RELEASE = URL_LATEST_RELEASE ;
31+ }
32+
33+ public static void setLogger (Logger LOGGER ) {
34+ Updater .LOGGER = LOGGER ;
35+ }
36+
1637 /**
1738 * Private constructor to prevent instantiation
1839 */
@@ -23,7 +44,7 @@ private Updater() {}
2344 */
2445 public static void checkUpdates () {
2546 if (!Updater .isUpToDate ()) {
26- Logger . getLogger ( "CommandsAPI" ) .warning ("The framework is not up to date, the latest version is " + Updater .fetchLatestVersion ());
47+ LOGGER .warning ("The framework is not up to date, the latest version is " + Updater .fetchLatestVersion ());
2748 }
2849 }
2950
@@ -34,7 +55,7 @@ public static void checkUpdates() {
3455 public static String getVersion () {
3556 Properties prop = new Properties ();
3657 try {
37- prop .load (Updater .class .getClassLoader ().getResourceAsStream ("commands.properties" ));
58+ prop .load (Updater .class .getClassLoader ().getResourceAsStream (VERSION_PROPERTY_FILE ));
3859 return prop .getProperty ("version" );
3960 } catch (IOException e ) {
4061 throw new RuntimeException (e );
@@ -60,8 +81,7 @@ public static boolean isUpToDate() {
6081 */
6182 public static String fetchLatestVersion () {
6283 try {
63- URL url = URI .create ("https://api.github.com/repos/Traqueur-dev/CommandsAPI/releases/latest" ).toURL ();
64- String responseString = getString (url );
84+ String responseString = getString ();
6585 int tagNameIndex = responseString .indexOf ("\" tag_name\" " );
6686 int start = responseString .indexOf ('\"' , tagNameIndex + 10 ) + 1 ;
6787 int end = responseString .indexOf ('\"' , start );
@@ -75,8 +95,8 @@ public static String fetchLatestVersion() {
7595 * Get the latest version of the plugin
7696 * @return The latest version of the plugin
7797 */
78- private static String getString (URL url ) throws IOException {
79- HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
98+ private static String getString () throws IOException {
99+ HttpURLConnection connection = (HttpURLConnection ) Updater . URL_LATEST_RELEASE .openConnection ();
80100 connection .setRequestMethod ("GET" );
81101
82102 StringBuilder response = new StringBuilder ();
0 commit comments