diff --git a/Mage.Updater/pom.xml b/Mage.Updater/pom.xml
index d19343091f..37d7d89340 100644
--- a/Mage.Updater/pom.xml
+++ b/Mage.Updater/pom.xml
@@ -21,7 +21,7 @@
- com.magefree.update.Main
+ com.magefree.update.Updater
diff --git a/Mage.Updater/src/main/java/com/magefree/update/ChechsumHelper.java b/Mage.Updater/src/main/java/com/magefree/update/ChechsumHelper.java
index f208807ee0..1f294780a4 100644
--- a/Mage.Updater/src/main/java/com/magefree/update/ChechsumHelper.java
+++ b/Mage.Updater/src/main/java/com/magefree/update/ChechsumHelper.java
@@ -4,23 +4,35 @@ import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
+/**
+ * @author Loki
+ */
public class ChechsumHelper {
- public static byte[] createChecksum(String filename) throws
- Exception
- {
- InputStream fis = new FileInputStream(filename);
- byte[] buffer = new byte[1024];
- MessageDigest complete = MessageDigest.getInstance("SHA1");
- int numRead;
- do {
- numRead = fis.read(buffer);
- if (numRead > 0) {
- complete.update(buffer, 0, numRead);
+ public static byte[] createChecksum(String filename) throws Exception {
+ InputStream fis = null;
+ MessageDigest complete;
+
+ try {
+ fis = new FileInputStream(filename);
+
+ byte[] buffer = new byte[1024];
+ complete = MessageDigest.getInstance("SHA1");
+
+ int numRead;
+ do {
+ numRead = fis.read(buffer);
+ if (numRead > 0) {
+ complete.update(buffer, 0, numRead);
+ }
+ } while (numRead != -1);
+
+ return complete.digest();
+ } finally {
+ if (fis != null) {
+ fis.close();
}
- } while (numRead != -1);
- fis.close();
- return complete.digest();
+ }
}
// see this How-to for a faster way to convert
@@ -28,9 +40,9 @@ public class ChechsumHelper {
public static String getSHA1Checksum(String filename) throws Exception {
byte[] b = createChecksum(filename);
String result = "";
- for (int i=0; i < b.length; i++) {
+ for (int i = 0; i < b.length; i++) {
result +=
- Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
+ Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
diff --git a/Mage.Updater/src/main/java/com/magefree/update/FileHelper.java b/Mage.Updater/src/main/java/com/magefree/update/FileHelper.java
new file mode 100644
index 0000000000..d6007c756f
--- /dev/null
+++ b/Mage.Updater/src/main/java/com/magefree/update/FileHelper.java
@@ -0,0 +1,88 @@
+package com.magefree.update;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Helper for file operations.
+ *
+ * @author noxx
+ */
+public class FileHelper {
+
+ private FileHelper() {
+ }
+
+ /**
+ * Gets .jar files from specified folder.
+ *
+ * @param dir Folder to scan for rile
+ * @return
+ */
+ public static List findJarsInDir(String dir) {
+ ArrayList result = new ArrayList();
+ File directory = new File(dir);
+ if (directory.exists() && directory.isDirectory()) {
+ for (File jar : directory.listFiles(jarFileFilter)) {
+ result.add(jar);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Gets non-dir files from specified folder.
+ *
+ * @param dir Folder to scan for rile
+ * @return
+ */
+ public static List findAllFilesInDir(String dir) {
+ ArrayList result = new ArrayList();
+ File directory = new File(dir);
+ if (directory.exists() && directory.isDirectory()) {
+ for (File jar : directory.listFiles(anyFileFilter)) {
+ result.add(jar);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Removes all files from the list.
+ *
+ * @param files
+ */
+ public static void removeFiles(List files) {
+ for (String filename : files) {
+ File f = new File(filename);
+ if (f.exists()) {
+ f.delete();
+ System.out.println("File has been deleted: " + filename);
+ } else {
+ System.out.println("ERROR. Couldn't find file to delete: " + filename);
+ }
+ }
+ }
+
+ /**
+ * Filters out dirs.
+ */
+ private static final FilenameFilter anyFileFilter = new FilenameFilter() {
+ @Override
+ public boolean accept(File dir, String name) {
+ return dir.isFile();
+ }
+ };
+
+ /**
+ * Filters out jars.
+ */
+ private static final FilenameFilter jarFileFilter = new FilenameFilter() {
+ @Override
+ public boolean accept(File dir, String name) {
+ return name.endsWith(".jar");
+ }
+ };
+}
diff --git a/Mage.Updater/src/main/java/com/magefree/update/Main.java b/Mage.Updater/src/main/java/com/magefree/update/Main.java
deleted file mode 100644
index 24d4523859..0000000000
--- a/Mage.Updater/src/main/java/com/magefree/update/Main.java
+++ /dev/null
@@ -1,147 +0,0 @@
-package com.magefree.update;
-
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Scanner;
-
-public class Main {
- private final static String URL_PREFIX = "http://download.magefree.com/update/";
-
- public static void main(String[] args) throws Exception {
- Main m = new Main();
- HashMap local = m.readLocalData();
- HashMap remote = m.downloadAndParseUpdateData();
- List downloadList = m.findUpdated(local, remote);
- downloadList.addAll(m.findNew(local, remote));
- m.downloadAndUpdate(downloadList);
- m.removeFiles(m.findRemoved(local, remote));
- }
-
- public HashMap readLocalData() throws Exception {
- HashMap result = new HashMap();
- for (File f : findJars()) {
- result.put(f.getPath().replaceAll("\\\\", "/"), ChechsumHelper.getSHA1Checksum(f.getPath()));
- }
- return result;
- }
-
- public List findJars() throws Exception {
- ArrayList result = new ArrayList();
- result.addAll(findJarsInDir("mage-client/lib"));
- result.addAll(findJarsInDir("mage-client/plugins"));
- result.addAll(findJarsInDir("mage-server/lib"));
- result.addAll(findJarsInDir("mage-server/plugins"));
- return result;
- }
-
- public List findJarsInDir(String dir) {
- ArrayList result = new ArrayList();
- File directory = new File(dir);
- if (directory.exists() && directory.isDirectory()) {
- for (File jar : directory.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.endsWith(".jar");
- }
- })) {
- result.add(jar);
- }
- }
- return result;
- }
-
- public HashMap downloadAndParseUpdateData() throws Exception {
- HashMap result = new HashMap();
- URL url = new URL(URL_PREFIX + "update-data.txt");
- URLConnection urlConnection = url.openConnection();
- urlConnection.connect();
- Scanner scanner = new Scanner(urlConnection.getInputStream());
- while (scanner.hasNextLine()) {
- String[] lines = scanner.nextLine().split(" ");
- if (lines.length == 2) {
- result.put(lines[1], lines[0]);
- System.out.println("jar " + lines[1] + ", checksum " + lines[0]);
- }
- }
- return result;
- }
-
- public List findUpdated(HashMap local, HashMap remote) {
- ArrayList result = new ArrayList();
- for (String remoteFile : remote.keySet()) {
- if (local.containsKey(remoteFile)) {
- if (!local.get(remoteFile).equals(remote.get(remoteFile))) {
- System.out.println("jar need to be updated - " + remoteFile);
- result.add(remoteFile);
- }
- }
- }
- return result;
- }
-
- public List findNew(HashMap local, HashMap remote) {
- ArrayList result = new ArrayList();
- for (String remoteFile : remote.keySet()) {
- if (!local.containsKey(remoteFile)) {
- System.out.println("new jar found - " + remoteFile);
- result.add(remoteFile);
- }
- }
- return result;
- }
-
- public List findRemoved(HashMap local, HashMap remote) {
- ArrayList result = new ArrayList();
- for (String localFile : local.keySet()) {
- if (!remote.containsKey(localFile)) {
- System.out.println("deleted jar found - " + localFile);
- result.add(localFile);
- }
- }
- return result;
- }
-
- public void downloadAndUpdate(List downloadList) throws IOException {
- for (String filename : downloadList) {
- URL url = new URL(URL_PREFIX + filename);
- HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.connect();
- if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
- System.out.println("downloading " + filename);
- try {
- InputStream in = urlConnection.getInputStream();
- File f = new File(filename);
- if (!f.exists())
- f.getParentFile().mkdirs();
- FileOutputStream out = new FileOutputStream(filename);
- byte[] buf = new byte[4 * 1024];
- int bytesRead;
- while ((bytesRead = in.read(buf)) != -1) {
- out.write(buf, 0, bytesRead);
- }
- } catch (IOException e) {
- System.out.println("i/o exception - " + e.getMessage());
- }
-
- } else {
- System.out.println(filename + " error status : " + urlConnection.getResponseMessage());
- }
- }
- }
-
- public void removeFiles(List files) {
- for (String filename : files) {
- File f = new File(filename);
- if (f.exists()) {
- f.delete();
- } else {
- System.out.println("ERROR. File was found but currently not found");
- }
- }
- }
-}
diff --git a/Mage.Updater/src/main/java/com/magefree/update/Updater.java b/Mage.Updater/src/main/java/com/magefree/update/Updater.java
new file mode 100644
index 0000000000..19d64ceeaa
--- /dev/null
+++ b/Mage.Updater/src/main/java/com/magefree/update/Updater.java
@@ -0,0 +1,214 @@
+package com.magefree.update;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Scanner;
+
+/**
+ * Mage Updater for updating Mage based on metadata from remote server.
+ *
+ * @author Loki, noxx
+ */
+public class Updater {
+
+ /**
+ * URL to get metadata and files from.
+ */
+ private final static String URL_PREFIX = "http://download.magefree.com/update/";
+
+ /**
+ * Main. Application Entry Point.
+ *
+ * @param args No args are used.
+ *
+ * @throws Exception
+ */
+ public static void main(String[] args) throws Exception {
+ Updater m = new Updater();
+
+ // check files on local machine
+ HashMap local = m.readLocalData();
+
+ // request information for files on update server
+ HashMap remote = m.downloadAndParseUpdateData();
+
+ // compare to find updated files
+ List downloadList = m.findUpdated(local, remote);
+ downloadList.addAll(m.findNew(local, remote));
+
+ // download and replace
+ m.downloadAndUpdate(downloadList);
+
+ // remove odd files
+ m.removeFiles(m.findRemoved(local, remote));
+ }
+
+ /**
+ * Gets lists of files on local machine.
+ * For each such file an map's entry is created with path and checksum.
+ *
+ * @return
+ * @throws Exception
+ */
+ public HashMap readLocalData() throws Exception {
+ HashMap result = new HashMap();
+ for (File f : findFiles()) {
+ result.put(f.getPath().replaceAll("\\\\", "/"), ChechsumHelper.getSHA1Checksum(f.getPath()));
+ }
+ return result;
+ }
+
+ /**
+ * Get required files.
+ *
+ * @return
+ * @throws Exception
+ */
+ public List findFiles() throws Exception {
+ ArrayList result = new ArrayList();
+ result.addAll(FileHelper.findAllFilesInDir("mage-client/lib"));
+ result.addAll(FileHelper.findAllFilesInDir("mage-client/plugins"));
+ result.addAll(FileHelper.findAllFilesInDir("mage-server/lib"));
+ result.addAll(FileHelper.findAllFilesInDir("mage-server/plugins"));
+ return result;
+ }
+
+ /**
+ * Downloads metadata from remote server getting checksums for files.
+ * This information will be used to find out what files should be downloaded and replaced or removed locally.
+ *
+ * @return
+ * @throws Exception
+ */
+ public HashMap downloadAndParseUpdateData() throws Exception {
+ HashMap result = new HashMap();
+ URL url = new URL(URL_PREFIX + "update-data.txt");
+ URLConnection urlConnection = url.openConnection();
+ urlConnection.connect();
+ Scanner scanner = new Scanner(urlConnection.getInputStream());
+ while (scanner.hasNextLine()) {
+ String[] lines = scanner.nextLine().split(" ");
+ if (lines.length == 2) {
+ result.put(lines[1], lines[0]);
+ //System.out.println("jar " + lines[1] + ", checksum " + lines[0]);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Finds the list of files that have been updated and should be replaced.
+ * The fact of being changed is determined based on checksum received from remote server.
+ *
+ * @param local List of local files with check sums to be compared with remote.
+ * @param remote List of remove files with check sum to be compared with local.
+ *
+ * @return List of files to be replaced with newer versions.
+ */
+ public List findUpdated(HashMap local, HashMap remote) {
+ ArrayList result = new ArrayList();
+ for (String remoteFile : remote.keySet()) {
+ if (local.containsKey(remoteFile)) {
+ if (!local.get(remoteFile).equals(remote.get(remoteFile))) {
+ //System.out.println("jar need to be updated - " + remoteFile);
+ result.add(remoteFile);
+ }
+ }
+ }
+ return result;
+ }
+
+ public List findNew(HashMap local, HashMap remote) {
+ ArrayList result = new ArrayList();
+ for (String remoteFile : remote.keySet()) {
+ if (!local.containsKey(remoteFile)) {
+ //System.out.println("new jar found - " + remoteFile);
+ result.add(remoteFile);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Finds files that should be removed.
+ *
+ * @param local List of local files with check sums to be compared with remote.
+ * @param remote List of remove files with check sum to be compared with local.
+ *
+ * @return List of files to be removed.
+ */
+ public List findRemoved(HashMap local, HashMap remote) {
+ ArrayList result = new ArrayList();
+ for (String localFile : local.keySet()) {
+ if (!remote.containsKey(localFile)) {
+ //System.out.println("deleted jar found - " + localFile);
+ result.add(localFile);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Downloads files and updated them.
+ *
+ * @param downloadList
+ * @throws IOException
+ */
+ public void downloadAndUpdate(List downloadList) throws IOException {
+ for (String filename : downloadList) {
+ URL url = new URL(URL_PREFIX + filename);
+ HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
+ urlConnection.connect();
+
+ if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
+ downloadFile(filename, urlConnection);
+ } else {
+ System.out.println(filename + " error status : " + urlConnection.getResponseMessage());
+ }
+ }
+ }
+
+ /**
+ * Downloads specified file.
+ *
+ * @param filename
+ * @param urlConnection
+ */
+ private void downloadFile(String filename, HttpURLConnection urlConnection) {
+ System.out.println("Downloading " + filename);
+ try {
+ InputStream in = urlConnection.getInputStream();
+ File f = new File(filename);
+ if (!f.exists()) {
+ f.getParentFile().mkdirs();
+ System.out.println("Directories have been created: " + f.getParentFile().getPath());
+ }
+
+ FileOutputStream out = new FileOutputStream(filename);
+ byte[] buf = new byte[4 * 1024];
+ int bytesRead;
+
+ while ((bytesRead = in.read(buf)) != -1) {
+ out.write(buf, 0, bytesRead);
+ }
+
+ System.out.println("File has been updated: " + filename);
+ } catch (IOException e) {
+ System.out.println("i/o exception - " + e.getMessage());
+ }
+ }
+
+ /**
+ * Removes files from the list.
+ *
+ * @param files
+ */
+ public void removeFiles(List files) {
+ FileHelper.removeFiles(files);
+ }
+}
diff --git a/mage.updater.client/pom.xml b/mage.updater.client/pom.xml
deleted file mode 100644
index b7c605358b..0000000000
--- a/mage.updater.client/pom.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-
-
-
- mage-root
- org.mage
- 0.8.5
-
- 4.0.0
-
- Mage Client Updater
- mage-updater-client
-
-
-
-
- org.apache.maven.plugins
- maven-jar-plugin
-
-
-
- com.magefree.update.Main
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/mage.updater.client/src/main/java/com/magefree/update/ChechsumHelper.java b/mage.updater.client/src/main/java/com/magefree/update/ChechsumHelper.java
deleted file mode 100644
index f208807ee0..0000000000
--- a/mage.updater.client/src/main/java/com/magefree/update/ChechsumHelper.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package com.magefree.update;
-
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.security.MessageDigest;
-
-public class ChechsumHelper {
- public static byte[] createChecksum(String filename) throws
- Exception
- {
- InputStream fis = new FileInputStream(filename);
-
- byte[] buffer = new byte[1024];
- MessageDigest complete = MessageDigest.getInstance("SHA1");
- int numRead;
- do {
- numRead = fis.read(buffer);
- if (numRead > 0) {
- complete.update(buffer, 0, numRead);
- }
- } while (numRead != -1);
- fis.close();
- return complete.digest();
- }
-
- // see this How-to for a faster way to convert
- // a byte array to a HEX string
- public static String getSHA1Checksum(String filename) throws Exception {
- byte[] b = createChecksum(filename);
- String result = "";
- for (int i=0; i < b.length; i++) {
- result +=
- Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
- }
- return result;
- }
-}
diff --git a/mage.updater.client/src/main/java/com/magefree/update/Main.java b/mage.updater.client/src/main/java/com/magefree/update/Main.java
deleted file mode 100644
index 24d4523859..0000000000
--- a/mage.updater.client/src/main/java/com/magefree/update/Main.java
+++ /dev/null
@@ -1,147 +0,0 @@
-package com.magefree.update;
-
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Scanner;
-
-public class Main {
- private final static String URL_PREFIX = "http://download.magefree.com/update/";
-
- public static void main(String[] args) throws Exception {
- Main m = new Main();
- HashMap local = m.readLocalData();
- HashMap remote = m.downloadAndParseUpdateData();
- List downloadList = m.findUpdated(local, remote);
- downloadList.addAll(m.findNew(local, remote));
- m.downloadAndUpdate(downloadList);
- m.removeFiles(m.findRemoved(local, remote));
- }
-
- public HashMap readLocalData() throws Exception {
- HashMap result = new HashMap();
- for (File f : findJars()) {
- result.put(f.getPath().replaceAll("\\\\", "/"), ChechsumHelper.getSHA1Checksum(f.getPath()));
- }
- return result;
- }
-
- public List findJars() throws Exception {
- ArrayList result = new ArrayList();
- result.addAll(findJarsInDir("mage-client/lib"));
- result.addAll(findJarsInDir("mage-client/plugins"));
- result.addAll(findJarsInDir("mage-server/lib"));
- result.addAll(findJarsInDir("mage-server/plugins"));
- return result;
- }
-
- public List findJarsInDir(String dir) {
- ArrayList result = new ArrayList();
- File directory = new File(dir);
- if (directory.exists() && directory.isDirectory()) {
- for (File jar : directory.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.endsWith(".jar");
- }
- })) {
- result.add(jar);
- }
- }
- return result;
- }
-
- public HashMap downloadAndParseUpdateData() throws Exception {
- HashMap result = new HashMap();
- URL url = new URL(URL_PREFIX + "update-data.txt");
- URLConnection urlConnection = url.openConnection();
- urlConnection.connect();
- Scanner scanner = new Scanner(urlConnection.getInputStream());
- while (scanner.hasNextLine()) {
- String[] lines = scanner.nextLine().split(" ");
- if (lines.length == 2) {
- result.put(lines[1], lines[0]);
- System.out.println("jar " + lines[1] + ", checksum " + lines[0]);
- }
- }
- return result;
- }
-
- public List findUpdated(HashMap local, HashMap remote) {
- ArrayList result = new ArrayList();
- for (String remoteFile : remote.keySet()) {
- if (local.containsKey(remoteFile)) {
- if (!local.get(remoteFile).equals(remote.get(remoteFile))) {
- System.out.println("jar need to be updated - " + remoteFile);
- result.add(remoteFile);
- }
- }
- }
- return result;
- }
-
- public List findNew(HashMap local, HashMap remote) {
- ArrayList result = new ArrayList();
- for (String remoteFile : remote.keySet()) {
- if (!local.containsKey(remoteFile)) {
- System.out.println("new jar found - " + remoteFile);
- result.add(remoteFile);
- }
- }
- return result;
- }
-
- public List findRemoved(HashMap local, HashMap remote) {
- ArrayList result = new ArrayList();
- for (String localFile : local.keySet()) {
- if (!remote.containsKey(localFile)) {
- System.out.println("deleted jar found - " + localFile);
- result.add(localFile);
- }
- }
- return result;
- }
-
- public void downloadAndUpdate(List downloadList) throws IOException {
- for (String filename : downloadList) {
- URL url = new URL(URL_PREFIX + filename);
- HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.connect();
- if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
- System.out.println("downloading " + filename);
- try {
- InputStream in = urlConnection.getInputStream();
- File f = new File(filename);
- if (!f.exists())
- f.getParentFile().mkdirs();
- FileOutputStream out = new FileOutputStream(filename);
- byte[] buf = new byte[4 * 1024];
- int bytesRead;
- while ((bytesRead = in.read(buf)) != -1) {
- out.write(buf, 0, bytesRead);
- }
- } catch (IOException e) {
- System.out.println("i/o exception - " + e.getMessage());
- }
-
- } else {
- System.out.println(filename + " error status : " + urlConnection.getResponseMessage());
- }
- }
- }
-
- public void removeFiles(List files) {
- for (String filename : files) {
- File f = new File(filename);
- if (f.exists()) {
- f.delete();
- } else {
- System.out.println("ERROR. File was found but currently not found");
- }
- }
- }
-}