package kubach.gui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import kubach.ConfigManager; import kubach.workers.DownloadFileWorker; import kubach.zip.ExtractingDialog; /** * A wrapper for the first launch of program. Downloads required libraries and * does self-update * * @author Cr0s */ public class FirstLaunch extends javax.swing.JFrame { /** * Creates new form FirstLaunch */ public FirstLaunch() { initComponents(); downloadPackage(); } private void downloadPackage() { // We definitely needs these condoms to loose virginity properly String packageUrl = ConfigManager.getInstance().getProperties().getProperty("virgincondoms"); // Libraries package DownloadFileWorker dfw = new DownloadFileWorker(this, packageUrl, ConfigManager.getInstance().pathToJar + File.separatorChar + "condoms.package"); dfw.execute(); } /** * Sun property pointing the main class and its arguments. Might not be * defined on non Hotspot VM implementations. */ public static final String SUN_JAVA_COMMAND = "sun.java.command"; /** * Restart the current Java application * * @param runBeforeRestart some custom code to be run before restarting * @throws IOException */ public static void restart() throws IOException { try { // java binary String java = System.getProperty("java.home") + System.getProperty("file.separator") + "bin" + System.getProperty("file.separator") + "java"; // vm arguments List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments(); StringBuffer vmArgsOneLine = new StringBuffer(); for (String arg : vmArguments) { // if it's the agent argument : we ignore it otherwise the // address of the old application and the new one will be in conflict if (!arg.contains("-agentlib")) { vmArgsOneLine.append(arg); vmArgsOneLine.append(" "); } } // init the command to execute, add the vm args final StringBuffer cmd = new StringBuffer(java + " " + vmArgsOneLine); // program main and program arguments String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" "); // program main is a jar if (mainCommand[0].endsWith(".jar")) { // if it's a jar, add -jar mainJar cmd.append("-jar ").append(new File(mainCommand[0]).getPath()); } else { // else it's a .class, add the classpath and mainClass cmd.append("-cp ").append(System.getProperty("java.class.path")).append(" ").append(mainCommand[0]); } // finally add program arguments for (int i = 1; i < mainCommand.length; i++) { cmd.append(" "); cmd.append(mainCommand[i]); } // execute the command in a shutdown hook, to be sure that all the // resources have been disposed before restarting the application Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { Runtime.getRuntime().exec(cmd.toString()); } catch (IOException e) { } } }); // exit System.exit(0); } catch (Exception e) { // something went wrong throw new IOException("Error while trying to restart the application", e); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); pbDownload = new javax.swing.JProgressBar(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Welcome"); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("Preparing to start Kubach..."); pbDownload.setString(":3"); pbDownload.setStringPainted(true); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pbDownload, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pbDownload, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold>//GEN-END:initComponents private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened evt.getWindow().setLocationRelativeTo(evt.getOppositeWindow()); this.setTitle(this.getTitle() + " (on " + ConfigManager.getInstance().getClientPrefix() + ")"); }//GEN-LAST:event_formWindowOpened // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JProgressBar pbDownload; // End of variables declaration//GEN-END:variables public void setDownloadProgress(String status, int progressValue, int totalProgressValue, final File file) { this.pbDownload.setString(status); this.pbDownload.setMaximum(totalProgressValue); this.pbDownload.setValue(progressValue); // Done downloading if (status.equals("Finished")) { if (file.getName().equals("condoms.package")) { ExtractingDialog ed = new ExtractingDialog(this, true, file, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Loose virginity ConfigManager.getInstance().getProperties().setProperty("virgin", "false"); ConfigManager.getInstance().saveProperties(); // Remove used condoms file.delete(); // Download launcher DownloadFileWorker dfw = new DownloadFileWorker(FirstLaunch.this, ConfigManager.getInstance().getLauncherDownloadUrl(), ConfigManager.getInstance().pathToJar + File.separatorChar + "Kubach.jar"); dfw.execute(); } }); ed.setVisible(true); } else if (file.getName().equals("Kubach.jar")) { try { // Finally restart restart(); } catch (IOException ex) { JOptionPane.showMessageDialog(FirstLaunch.this, "Please restart me\n" + ex.toString(), "I can't restart", JOptionPane.ERROR_MESSAGE); } } } } }