refactoring
This commit is contained in:
parent
a95a0a473d
commit
6a63cf022b
|
@ -1,5 +1,6 @@
|
||||||
package com.rusefi.autoupdate;
|
package com.rusefi.autoupdate;
|
||||||
|
|
||||||
|
import com.rusefi.shared.ConnectionAndMeta;
|
||||||
import com.rusefi.ui.util.FrameHelper;
|
import com.rusefi.ui.util.FrameHelper;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
@ -8,7 +9,6 @@ import java.awt.event.ActionEvent;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -67,7 +67,7 @@ public class Autoupdate {
|
||||||
private static void handleBundle(String bundleFullName, UpdateMode mode) {
|
private static void handleBundle(String bundleFullName, UpdateMode mode) {
|
||||||
try {
|
try {
|
||||||
String zipFileName = bundleFullName + "_autoupdate" + ".zip";
|
String zipFileName = bundleFullName + "_autoupdate" + ".zip";
|
||||||
AutoupdateUtil.ConnectionAndMeta connectionAndMeta = new AutoupdateUtil.ConnectionAndMeta(zipFileName).invoke();
|
ConnectionAndMeta connectionAndMeta = new ConnectionAndMeta(zipFileName).invoke();
|
||||||
System.out.println("Server has " + connectionAndMeta.getCompleteFileSize() + " from " + new Date(connectionAndMeta.getLastModified()));
|
System.out.println("Server has " + connectionAndMeta.getCompleteFileSize() + " from " + new Date(connectionAndMeta.getLastModified()));
|
||||||
|
|
||||||
if (AutoupdateUtil.hasExistingFile(zipFileName, connectionAndMeta.getCompleteFileSize(), connectionAndMeta.getLastModified())) {
|
if (AutoupdateUtil.hasExistingFile(zipFileName, connectionAndMeta.getCompleteFileSize(), connectionAndMeta.getLastModified())) {
|
||||||
|
|
|
@ -1,46 +1,25 @@
|
||||||
package com.rusefi.autoupdate;
|
package com.rusefi.autoupdate;
|
||||||
|
|
||||||
|
import com.rusefi.shared.ConnectionAndMeta;
|
||||||
import com.rusefi.ui.util.FrameHelper;
|
import com.rusefi.ui.util.FrameHelper;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.net.ssl.*;
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.security.KeyManagementException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.security.cert.CertificateException;
|
|
||||||
import java.security.cert.X509Certificate;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
public class AutoupdateUtil {
|
public class AutoupdateUtil {
|
||||||
public static final boolean runHeadless = Boolean.getBoolean("run_headless") || GraphicsEnvironment.isHeadless();
|
public static final boolean runHeadless = Boolean.getBoolean("run_headless") || GraphicsEnvironment.isHeadless();
|
||||||
|
|
||||||
private static final int BUFFER_SIZE = 32 * 1024;
|
|
||||||
private static final int STEPS = 1000;
|
|
||||||
// todo: figure out a better way to work with absolute path
|
// todo: figure out a better way to work with absolute path
|
||||||
private static final String APPICON = "/appicon.png";
|
private static final String APPICON = "/appicon.png";
|
||||||
|
|
||||||
public static void downloadAutoupdateFile(String localZipFileName, ConnectionAndMeta connectionAndMeta, String title) throws IOException {
|
public static void downloadAutoupdateFile(String localZipFileName, ConnectionAndMeta connectionAndMeta, String title) throws IOException {
|
||||||
HttpURLConnection httpConnection = connectionAndMeta.httpConnection;
|
|
||||||
long completeFileSize = connectionAndMeta.completeFileSize;
|
|
||||||
Objects.requireNonNull(httpConnection, "httpConnection");
|
|
||||||
BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream());
|
|
||||||
FileOutputStream fos = new FileOutputStream(localZipFileName);
|
|
||||||
BufferedOutputStream bout = new BufferedOutputStream(fos, BUFFER_SIZE);
|
|
||||||
byte[] data = new byte[BUFFER_SIZE];
|
|
||||||
long downloadedFileSize = 0;
|
|
||||||
int newDataSize;
|
|
||||||
|
|
||||||
int printedPercentage = 0;
|
|
||||||
|
|
||||||
FrameHelper frameHelper = null;
|
FrameHelper frameHelper = null;
|
||||||
final AtomicReference<JProgressBar> jProgressBarAtomicReference = new AtomicReference<>();
|
final AtomicReference<JProgressBar> jProgressBarAtomicReference = new AtomicReference<>();
|
||||||
if (!runHeadless) {
|
if (!runHeadless) {
|
||||||
|
@ -48,32 +27,18 @@ public class AutoupdateUtil {
|
||||||
JProgressBar jProgressBar = new JProgressBar();
|
JProgressBar jProgressBar = new JProgressBar();
|
||||||
|
|
||||||
frameHelper.getFrame().setTitle(title);
|
frameHelper.getFrame().setTitle(title);
|
||||||
jProgressBar.setMaximum(STEPS);
|
jProgressBar.setMaximum(ConnectionAndMeta.STEPS);
|
||||||
jProgressBarAtomicReference.set(jProgressBar);
|
jProgressBarAtomicReference.set(jProgressBar);
|
||||||
frameHelper.showFrame(jProgressBar, true);
|
frameHelper.showFrame(jProgressBar, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((newDataSize = in.read(data, 0, BUFFER_SIZE)) >= 0) {
|
ConnectionAndMeta.DownloadProgressListener listener = currentProgress -> {
|
||||||
downloadedFileSize += newDataSize;
|
|
||||||
|
|
||||||
// calculate progress
|
|
||||||
final int currentProgress = (int) ((((double) downloadedFileSize) / ((double) completeFileSize)) * STEPS);
|
|
||||||
|
|
||||||
int currentPercentage = (int) (100L * downloadedFileSize / completeFileSize);
|
|
||||||
if (currentPercentage > printedPercentage + 5) {
|
|
||||||
System.out.println("Downloaded " + currentPercentage + "%");
|
|
||||||
printedPercentage = currentPercentage;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!runHeadless) {
|
if (!runHeadless) {
|
||||||
SwingUtilities.invokeLater(() -> jProgressBarAtomicReference.get().setValue(currentProgress));
|
SwingUtilities.invokeLater(() -> jProgressBarAtomicReference.get().setValue(currentProgress));
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
bout.write(data, 0, newDataSize);
|
ConnectionAndMeta.downloadFile(localZipFileName, connectionAndMeta, listener);
|
||||||
}
|
|
||||||
bout.close();
|
|
||||||
in.close();
|
|
||||||
new File(localZipFileName).setLastModified(connectionAndMeta.lastModified);
|
|
||||||
|
|
||||||
if (!runHeadless) {
|
if (!runHeadless) {
|
||||||
frameHelper.getFrame().dispose();
|
frameHelper.getFrame().dispose();
|
||||||
|
@ -123,53 +88,4 @@ public class AutoupdateUtil {
|
||||||
trueLayout(window);
|
trueLayout(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ConnectionAndMeta {
|
|
||||||
private String zipFileName;
|
|
||||||
private HttpsURLConnection httpConnection;
|
|
||||||
private long completeFileSize;
|
|
||||||
private long lastModified;
|
|
||||||
|
|
||||||
public ConnectionAndMeta(String zipFileName) {
|
|
||||||
this.zipFileName = zipFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpURLConnection getHttpConnection() {
|
|
||||||
return httpConnection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getCompleteFileSize() {
|
|
||||||
return completeFileSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getLastModified() {
|
|
||||||
return lastModified;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConnectionAndMeta invoke() throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
|
||||||
// user can have java with expired certificates or funny proxy, we shall accept any certificate :(
|
|
||||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
|
||||||
ctx.init(new KeyManager[0], new TrustManager[] {new AcceptAnyCertificateTrustManager()}, new SecureRandom());
|
|
||||||
|
|
||||||
URL url = new URL("https://rusefi.com/build_server/autoupdate/" + zipFileName);
|
|
||||||
httpConnection = (HttpsURLConnection) url.openConnection();
|
|
||||||
httpConnection.setSSLSocketFactory(ctx.getSocketFactory());
|
|
||||||
completeFileSize = httpConnection.getContentLength();
|
|
||||||
lastModified = httpConnection.getLastModified();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class AcceptAnyCertificateTrustManager implements X509TrustManager {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public X509Certificate[] getAcceptedIssuers() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
package com.rusefi.shared;
|
||||||
|
|
||||||
|
import javax.net.ssl.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class ConnectionAndMeta {
|
||||||
|
private static final int BUFFER_SIZE = 32 * 1024;
|
||||||
|
public static final int STEPS = 1000;
|
||||||
|
private String zipFileName;
|
||||||
|
private HttpsURLConnection httpConnection;
|
||||||
|
private long completeFileSize;
|
||||||
|
private long lastModified;
|
||||||
|
|
||||||
|
public ConnectionAndMeta(String zipFileName) {
|
||||||
|
this.zipFileName = zipFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void downloadFile(String localTargetFileName, ConnectionAndMeta connectionAndMeta, DownloadProgressListener listener) throws IOException {
|
||||||
|
HttpURLConnection httpConnection = connectionAndMeta.getHttpConnection();
|
||||||
|
long completeFileSize = connectionAndMeta.getCompleteFileSize();
|
||||||
|
Objects.requireNonNull(httpConnection, "httpConnection");
|
||||||
|
BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream());
|
||||||
|
FileOutputStream fos = new FileOutputStream(localTargetFileName);
|
||||||
|
BufferedOutputStream bout = new BufferedOutputStream(fos, BUFFER_SIZE);
|
||||||
|
byte[] data = new byte[BUFFER_SIZE];
|
||||||
|
long downloadedFileSize = 0;
|
||||||
|
int newDataSize;
|
||||||
|
|
||||||
|
int printedPercentage = 0;
|
||||||
|
|
||||||
|
while ((newDataSize = in.read(data, 0, BUFFER_SIZE)) >= 0) {
|
||||||
|
downloadedFileSize += newDataSize;
|
||||||
|
|
||||||
|
// calculate progress
|
||||||
|
final int currentProgress = (int) ((((double) downloadedFileSize) / ((double) completeFileSize)) * STEPS);
|
||||||
|
|
||||||
|
int currentPercentage = (int) (100L * downloadedFileSize / completeFileSize);
|
||||||
|
if (currentPercentage > printedPercentage + 5) {
|
||||||
|
System.out.println("Downloaded " + currentPercentage + "%");
|
||||||
|
printedPercentage = currentPercentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
listener.onPercentage(currentProgress);
|
||||||
|
|
||||||
|
bout.write(data, 0, newDataSize);
|
||||||
|
}
|
||||||
|
bout.close();
|
||||||
|
in.close();
|
||||||
|
new File(localTargetFileName).setLastModified(connectionAndMeta.getLastModified());
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpURLConnection getHttpConnection() {
|
||||||
|
return httpConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCompleteFileSize() {
|
||||||
|
return completeFileSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLastModified() {
|
||||||
|
return lastModified;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConnectionAndMeta invoke() throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
||||||
|
// user can have java with expired certificates or funny proxy, we shall accept any certificate :(
|
||||||
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||||
|
ctx.init(new KeyManager[0], new TrustManager[]{new AcceptAnyCertificateTrustManager()}, new SecureRandom());
|
||||||
|
|
||||||
|
URL url = new URL("https://rusefi.com/build_server/autoupdate/" + zipFileName);
|
||||||
|
httpConnection = (HttpsURLConnection) url.openConnection();
|
||||||
|
httpConnection.setSSLSocketFactory(ctx.getSocketFactory());
|
||||||
|
completeFileSize = httpConnection.getContentLength();
|
||||||
|
lastModified = httpConnection.getLastModified();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface DownloadProgressListener {
|
||||||
|
void onPercentage(int currentProgress);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class AcceptAnyCertificateTrustManager implements X509TrustManager {
|
||||||
|
@Override
|
||||||
|
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue