mirror of
https://github.com/correl/mage.git
synced 2024-11-25 03:00:11 +00:00
Removed outdated files updater
This commit is contained in:
parent
e6680d062a
commit
deedd586ff
5 changed files with 0 additions and 381 deletions
|
@ -1,30 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mage-root</artifactId>
|
||||
<groupId>org.mage</groupId>
|
||||
<version>1.4.37</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mage-updater</artifactId>
|
||||
<name>Mage Updater</name>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.magefree.update.Updater</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -1,193 +0,0 @@
|
|||
package com.magefree.update;
|
||||
|
||||
import com.magefree.update.helpers.ChecksumHelper;
|
||||
import com.magefree.update.helpers.FileHelper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
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 static final 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<String, String> local = m.readLocalData();
|
||||
|
||||
// request information for files on update server
|
||||
HashMap<String, String> remote = m.downloadAndParseUpdateData();
|
||||
|
||||
// compare to find updated files
|
||||
List<String> downloadList = m.findUpdated(local, remote);
|
||||
downloadList.addAll(m.findNew(local, remote));
|
||||
|
||||
// download and replace
|
||||
m.downloadAndUpdate(downloadList);
|
||||
|
||||
// remove odd files
|
||||
List<String> removeList = m.findRemoved(local, remote);
|
||||
m.removeFiles(removeList);
|
||||
|
||||
if (downloadList.isEmpty() && removeList.isEmpty()) {
|
||||
System.out.println("Already up-to-date.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<String, String> readLocalData() throws Exception {
|
||||
HashMap<String, String> result = new HashMap<>();
|
||||
for (File f : findFiles()) {
|
||||
result.put(f.getPath().replaceAll("\\\\", "/"), ChecksumHelper.getSHA1Checksum(f.getPath()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get required files.
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public List<File> findFiles() throws Exception {
|
||||
ArrayList<File> 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<String, String> downloadAndParseUpdateData() throws Exception {
|
||||
HashMap<String, String> 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<String> findUpdated(HashMap<String, String> local, HashMap<String, String> remote) {
|
||||
ArrayList<String> 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 + " local: " + local.get(remoteFile) + ", remoteL " + remote.get(remoteFile));
|
||||
result.add(remoteFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<String> findNew(HashMap<String, String> local, HashMap<String, String> remote) {
|
||||
ArrayList<String> 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<String> findRemoved(HashMap<String, String> local, HashMap<String, String> remote) {
|
||||
ArrayList<String> 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<String> 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) {
|
||||
FileHelper.downloadFile(filename, urlConnection);
|
||||
} else {
|
||||
System.out.println(filename + " error status : " + urlConnection.getResponseMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes files from the list.
|
||||
*
|
||||
* @param files
|
||||
*/
|
||||
public void removeFiles(List<String> files) {
|
||||
FileHelper.removeFiles(files);
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package com.magefree.update.helpers;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* @author Loki
|
||||
*/
|
||||
public final class ChecksumHelper {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
package com.magefree.update.helpers;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.Closeable;
|
||||
/**
|
||||
* Helper for file operations.
|
||||
*
|
||||
* @author noxx
|
||||
*/
|
||||
public final class FileHelper {
|
||||
|
||||
private FileHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters out dirs.
|
||||
*/
|
||||
private static final FileFilter anyFileFilter = f -> f.isFile();
|
||||
|
||||
/**
|
||||
* Filters out jars.
|
||||
*/
|
||||
private static final FilenameFilter jarFileFilter = (dir, name) -> name.endsWith(".jar");
|
||||
|
||||
/**
|
||||
* Gets .jar files from specified folder.
|
||||
*
|
||||
* @param dir Folder to scan for rile
|
||||
* @return
|
||||
*/
|
||||
public static List<File> findJarsInDir(String dir) {
|
||||
ArrayList<File> 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<File> findAllFilesInDir(String dir) {
|
||||
ArrayList<File> 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<String> files) {
|
||||
for (String filename : files) {
|
||||
File f = new File(filename);
|
||||
if (f.exists()) {
|
||||
if(f.delete()) {
|
||||
System.out.println("File has been deleted: " + filename);
|
||||
}
|
||||
} else {
|
||||
System.out.println("ERROR. Couldn't find file to delete: " + filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads specified file.
|
||||
*
|
||||
* @param filename
|
||||
* @param urlConnection
|
||||
*/
|
||||
public static void downloadFile(String filename, HttpURLConnection urlConnection) {
|
||||
System.out.println("Downloading " + filename);
|
||||
try (InputStream in = urlConnection.getInputStream() ; FileOutputStream out = new FileOutputStream(filename)){
|
||||
File f = new File(filename);
|
||||
if (!f.exists() && f.getParentFile() != null) {
|
||||
if(f.getParentFile().mkdirs()) {
|
||||
System.out.println("Directories have been created: " + f.getParentFile().getPath());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
1
pom.xml
1
pom.xml
|
@ -63,7 +63,6 @@
|
|||
<module>Mage.Server.Plugins</module>
|
||||
<module>Mage.Server.Console</module>
|
||||
<module>Mage.Tests</module>
|
||||
<module>Mage.Updater</module>
|
||||
<module>Mage.Verify</module>
|
||||
</modules>
|
||||
|
||||
|
|
Loading…
Reference in a new issue