rusefi/java_tools/ts_plugin_launcher/src/main/java/com/rusefi/ts_plugin/Updater.java

210 lines
7.7 KiB
Java
Raw Normal View History

2020-06-16 21:35:16 -07:00
package com.rusefi.ts_plugin;
import com.rusefi.core.ui.AutoupdateUtil;
import com.rusefi.core.net.ConnectionAndMeta;
import com.rusefi.core.FileUtil;
2020-07-14 20:19:07 -07:00
import org.jetbrains.annotations.Nullable;
2020-06-16 22:14:45 -07:00
import org.putgemin.VerticalFlowLayout;
2020-06-16 21:35:16 -07:00
import javax.swing.*;
2020-06-22 16:52:40 -07:00
import java.awt.*;
2020-06-16 21:35:16 -07:00
import java.awt.event.ActionEvent;
2020-06-21 22:35:37 -07:00
import java.awt.event.ActionListener;
2020-06-16 22:14:45 -07:00
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
2020-06-16 21:35:16 -07:00
import java.net.URLClassLoader;
2020-06-17 19:14:22 -07:00
import java.util.Date;
2020-06-21 22:35:37 -07:00
import java.util.concurrent.atomic.AtomicInteger;
2020-06-16 21:35:16 -07:00
2020-06-16 22:14:45 -07:00
import static com.rusefi.ts_plugin.TsPluginLauncher.VERSION;
2020-07-26 21:16:38 -07:00
/**
* Download fresh copy of {@link #PLUGIN_BODY_JAR} and launch {@link #PLUGIN_ENTRY_CLASS} via reflection.
* @see ConnectionAndMeta#BASE_URL_LATEST
2020-07-26 21:16:38 -07:00
*/
2020-06-16 21:35:16 -07:00
public class Updater {
2020-07-26 21:16:38 -07:00
private static final String PLUGIN_ENTRY_CLASS = "com.rusefi.ts_plugin.PluginEntry";
2020-06-16 21:35:16 -07:00
private static final String PLUGIN_BODY_JAR = "rusefi_plugin_body.jar";
2020-07-26 21:16:38 -07:00
private static final String LOCAL_JAR_FILE_NAME = FileUtil.RUSEFI_SETTINGS_FOLDER + File.separator + PLUGIN_BODY_JAR;
2020-06-16 22:14:45 -07:00
private static final String TITLE = "rusEFI plugin installer " + VERSION;
2020-06-16 21:35:16 -07:00
2020-06-16 22:14:45 -07:00
private final JPanel content = new JPanel(new VerticalFlowLayout());
2020-06-21 22:35:37 -07:00
private static final ImageIcon LOGO = AutoupdateUtil.loadIcon("/rusefi_online_color_300.png");
private final JLabel countDownLabel = new JLabel();
private final AtomicInteger autoStartCounter = new AtomicInteger(4);
2020-07-14 20:19:07 -07:00
private TsPluginBody instance;
2020-06-21 22:35:37 -07:00
private final Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (autoStartCounter.decrementAndGet() == 0) {
timer.stop();
try {
if (shouldAutoStart) {
2020-07-14 20:19:07 -07:00
shouldAutoStart = false;
System.out.println("Auto-starting startPlugin");
2020-06-21 22:35:37 -07:00
startPlugin();
}
} catch (IllegalAccessException | MalformedURLException | ClassNotFoundException | InstantiationException ex) {
2020-07-14 20:19:07 -07:00
ex.printStackTrace();
2020-06-21 22:35:37 -07:00
JOptionPane.showMessageDialog(content, "Error " + ex);
}
} else {
countDownLabel.setText("Will auto-start in " + autoStartCounter + " seconds");
}
}
});
2020-07-14 20:19:07 -07:00
private volatile boolean shouldAutoStart = true;
2020-06-16 21:35:16 -07:00
public Updater() {
2020-06-16 22:14:45 -07:00
content.add(new JLabel("" + VERSION));
2020-06-17 20:50:57 -07:00
content.add(new JLabel(LOGO));
2020-06-17 20:19:07 -07:00
2020-06-16 22:14:45 -07:00
String version = null;
2020-06-17 19:14:22 -07:00
File localFile = new File(LOCAL_JAR_FILE_NAME);
if (localFile.exists()) {
2020-06-16 22:14:45 -07:00
version = getVersion();
}
2020-06-17 20:19:07 -07:00
JButton download = new JButton("Update plugin");
2020-07-14 20:19:07 -07:00
JButton run = createRunThisVersionButton(version);
2020-06-16 21:35:16 -07:00
2020-06-17 19:14:22 -07:00
new Thread(new Runnable() {
@Override
public void run() {
2020-07-01 21:00:14 -07:00
ConnectionAndMeta connectionAndMeta;
2020-06-17 19:14:22 -07:00
try {
2020-10-07 16:44:06 -07:00
connectionAndMeta = new ConnectionAndMeta(PLUGIN_BODY_JAR).invoke(ConnectionAndMeta.BASE_URL_LATEST);
} catch (Exception e) {
2020-06-17 19:14:22 -07:00
e.printStackTrace();
return;
}
System.out.println("Server has " + connectionAndMeta.getCompleteFileSize() + " from " + new Date(connectionAndMeta.getLastModified()));
if (AutoupdateUtil.hasExistingFile(LOCAL_JAR_FILE_NAME, connectionAndMeta.getCompleteFileSize(), connectionAndMeta.getLastModified())) {
System.out.println("We already have latest update " + new Date(connectionAndMeta.getLastModified()));
SwingUtilities.invokeLater(() -> {
download.setText("We have latest plugin version");
download.setEnabled(false);
});
return;
}
}
}).start();
2020-06-16 21:35:16 -07:00
download.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
2020-07-14 20:19:07 -07:00
if (run != null)
run.setEnabled(false);
2020-06-21 22:35:37 -07:00
cancelAutoStart();
2020-06-16 21:35:16 -07:00
new Thread(() -> startDownload(download)).start();
}
});
content.add(download);
}
2020-07-14 20:19:07 -07:00
@Nullable
private JButton createRunThisVersionButton(String version) {
if (version == null)
return null;
JButton run = new JButton("Run Version " + version);
run.setBackground(new Color(0x90EE90));
run.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
cancelAutoStart();
System.out.println("run startPlugin");
startPlugin();
} catch (IllegalAccessException | MalformedURLException | ClassNotFoundException | InstantiationException ex) {
run.setText(e.toString());
}
}
});
content.add(run);
content.add(countDownLabel);
timer.start();
return run;
}
2020-06-21 22:35:37 -07:00
private void cancelAutoStart() {
timer.stop();
shouldAutoStart = false;
}
2020-06-16 22:14:45 -07:00
private String getVersion() {
try {
Class clazz = getPluginClass();
Method method = clazz.getMethod(TsPluginBody.GET_VERSION);
return (String) method.invoke(null);
} catch (NoSuchMethodException | MalformedURLException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
return null;
}
}
2020-06-16 21:35:16 -07:00
private void startDownload(JButton download) {
2020-07-14 20:19:07 -07:00
System.out.println("startDownload");
2020-06-16 21:35:16 -07:00
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
download.setEnabled(false);
}
});
try {
2020-10-07 16:22:39 -07:00
ConnectionAndMeta connectionAndMeta = new ConnectionAndMeta(PLUGIN_BODY_JAR).invoke(ConnectionAndMeta.BASE_URL_LATEST);
2020-06-16 21:35:16 -07:00
2020-06-17 19:14:22 -07:00
AutoupdateUtil.downloadAutoupdateFile(LOCAL_JAR_FILE_NAME, connectionAndMeta,
2020-06-16 21:35:16 -07:00
TITLE);
2020-07-14 20:19:07 -07:00
System.out.println("Downloaded, now startPlugin");
2020-06-16 22:14:45 -07:00
startPlugin();
2020-06-16 21:35:16 -07:00
} catch (Exception e) {
2020-06-17 13:50:14 -07:00
e.printStackTrace();
2020-06-16 21:35:16 -07:00
download.setEnabled(true);
}
2020-06-16 22:14:45 -07:00
}
private void startPlugin() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
2020-07-14 20:19:07 -07:00
System.out.println("Starting plugin " + this);
2020-06-16 22:14:45 -07:00
Class clazz = getPluginClass();
2020-07-14 20:19:07 -07:00
synchronized (this) {
if (instance != null) {
System.out.println("Not starting second instance");
return; // avoid having two instances running
}
instance = (TsPluginBody) clazz.newInstance();
}
2020-06-16 22:14:45 -07:00
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
replaceWith(instance);
}
});
}
2020-06-16 21:35:16 -07:00
2020-06-16 22:14:45 -07:00
private static Class getPluginClass() throws MalformedURLException, ClassNotFoundException {
2020-06-17 13:50:14 -07:00
URLClassLoader jarClassLoader = AutoupdateUtil.getClassLoaderByJar(LOCAL_JAR_FILE_NAME);
2020-07-26 21:16:38 -07:00
return Class.forName(PLUGIN_ENTRY_CLASS, true, jarClassLoader);
2020-06-16 21:35:16 -07:00
}
private void replaceWith(TsPluginBody instance) {
content.removeAll();
content.add(instance.getContent());
2020-06-16 22:14:45 -07:00
AutoupdateUtil.trueLayout(content.getParent());
2020-06-22 17:58:52 -07:00
Window windowAncestor = SwingUtilities.getWindowAncestor(content);
2020-06-22 21:24:32 -07:00
AutoupdateUtil.pack(windowAncestor);
2020-06-16 21:35:16 -07:00
}
public JPanel getContent() {
return content;
}
}