mirror of
https://github.com/correl/mage.git
synced 2024-11-24 19:19:56 +00:00
Renamed mage-updater-client to Mage.Updater
This commit is contained in:
parent
9a24b1d950
commit
d4eee79ec1
4 changed files with 216 additions and 1 deletions
31
Mage.Updater/pom.xml
Normal file
31
Mage.Updater/pom.xml
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?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>0.8.5</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.mage</groupId>
|
||||
<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.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,37 @@
|
|||
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;
|
||||
}
|
||||
}
|
147
Mage.Updater/src/main/java/com/magefree/update/Main.java
Normal file
147
Mage.Updater/src/main/java/com/magefree/update/Main.java
Normal file
|
@ -0,0 +1,147 @@
|
|||
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<String, String> local = m.readLocalData();
|
||||
HashMap<String, String> remote = m.downloadAndParseUpdateData();
|
||||
List<String> downloadList = m.findUpdated(local, remote);
|
||||
downloadList.addAll(m.findNew(local, remote));
|
||||
m.downloadAndUpdate(downloadList);
|
||||
m.removeFiles(m.findRemoved(local, remote));
|
||||
}
|
||||
|
||||
public HashMap<String, String> readLocalData() throws Exception {
|
||||
HashMap<String, String> result = new HashMap<String, String>();
|
||||
for (File f : findJars()) {
|
||||
result.put(f.getPath().replaceAll("\\\\", "/"), ChechsumHelper.getSHA1Checksum(f.getPath()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<File> findJars() throws Exception {
|
||||
ArrayList<File> result = new ArrayList<File>();
|
||||
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<File> findJarsInDir(String dir) {
|
||||
ArrayList<File> result = new ArrayList<File>();
|
||||
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<String, String> downloadAndParseUpdateData() throws Exception {
|
||||
HashMap<String, String> result = new HashMap<String, String>();
|
||||
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<String> findUpdated(HashMap<String, String> local, HashMap<String, String> remote) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
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<String> findNew(HashMap<String, String> local, HashMap<String, String> remote) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
for (String remoteFile : remote.keySet()) {
|
||||
if (!local.containsKey(remoteFile)) {
|
||||
System.out.println("new jar found - " + remoteFile);
|
||||
result.add(remoteFile);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<String> findRemoved(HashMap<String, String> local, HashMap<String, String> remote) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
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<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) {
|
||||
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<String> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
2
pom.xml
2
pom.xml
|
@ -32,7 +32,7 @@
|
|||
<module>Mage.Server.Plugins</module>
|
||||
<module>Mage.Server.Console</module>
|
||||
<module>Mage.Tests</module>
|
||||
<module>mage.updater.client</module>
|
||||
<module>Mage.Updater</module>
|
||||
</modules>
|
||||
|
||||
<repositories>
|
||||
|
|
Loading…
Reference in a new issue