1 19 20 package com.sslexplorer.upgrade; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 public class CommandLineUpgrader implements Upgrader { 34 final static Log log = LogFactory.getLog(CommandLineUpgrader.class); 35 36 private List upgrades; 37 38 public CommandLineUpgrader(String [] args) throws Exception { 39 40 File oldDir = new File (args[0]); 41 if (!oldDir.exists() || !oldDir.isDirectory()) { 42 System.err.println(oldDir.getAbsolutePath() + " does not exists or is not a directory"); 43 System.exit(1); 44 } 45 File newDir = new File (args[1]); 46 if (!newDir.exists() || !newDir.isDirectory()) { 47 System.err.println(newDir.getAbsolutePath() + " does not exists or is not a directory"); 48 System.exit(1); 49 } 50 if (oldDir.getCanonicalFile().equals(newDir.getCanonicalFile())) { 51 System.err.println("Old and new installation directories are identical"); 52 System.exit(1); 53 } 54 if (!new File (newDir, "install").exists()) { 55 System.err.println("New installation does not appear to be 0.2.5+"); 56 System.exit(1); 57 } 58 if (!new File (oldDir, "upgrade").exists()) { 59 System.err.println("Old installation does not appear to be 0.1.15+"); 60 System.exit(1); 61 } 62 File oldDbDir = new File (oldDir, "db"); 63 File newDbDir = new File (newDir, "db"); 64 65 upgrades = new ArrayList (); 66 upgrades.add(new UserUpgrade(oldDbDir, newDbDir)); 67 upgrades.add(new AuthSchemeUpgrade(oldDbDir, newDbDir)); 68 upgrades.add(new TunnelsUpgrade(oldDbDir, newDbDir)); 69 upgrades.add(new NetworkPlacesUpgrade(oldDbDir, newDbDir)); 70 upgrades.add(new WebForwardsUpgrade(oldDbDir, newDbDir)); 71 upgrades.add(new IPRestrictionsUpgrade(oldDbDir, newDbDir)); 72 upgrades.add(new ApplicationShortcutsUpgrade(oldDbDir, newDbDir)); 73 upgrades.add(new ReplacementsUpgrade(oldDbDir, newDbDir)); 74 } 75 76 public void error(String message) { 77 log.error(message); 78 79 } 80 81 public void error(String message, Throwable exception) { 82 log.error(message, exception); 83 } 84 85 public void info(String message) { 86 log.info(message); 87 } 88 89 public void upgrade() throws Exception { 90 for (Iterator i = upgrades.iterator(); i.hasNext();) { 91 try { 92 AbstractDatabaseUpgrade upgrade = (AbstractDatabaseUpgrade)i.next(); 93 log.info("*** " + upgrade.getName() + " ***"); 94 log.info(upgrade.getDescription()); 95 if(yesno("Do you wish to run this upgrade?", upgrade.isSelectedByDefault())) { 96 upgrade.upgrade(this); 97 } 98 else { 99 log.info("Skipped " + upgrade.getName()); 100 } 101 } catch (Exception e) { 102 log.error("Failed upgrader.", e); 103 } 104 } 105 106 } 107 108 public void warn(String message) { 109 log.warn(message); 110 111 } 112 113 public void warn(String message, Throwable exception) { 114 log.warn(message, exception); 115 } 116 117 public static boolean yesno(String message, boolean yesByDefault) { 118 while (true) { 119 log.info(message + " (" + (yesByDefault ? "[Yes]" : "Yes") + " / " + (yesByDefault ? "No" : "[Yes]") + "): "); 120 BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 121 try { 122 String l = br.readLine(); 123 if (l == null) { 124 return yesByDefault; 125 } 126 l = l.toLowerCase(); 127 if (l.trim().equals("")) { 128 return yesByDefault; 129 } 130 if (l.equals("y") || l.equals("yes")) { 131 return true; 132 } 133 if (l.equals("n") || l.equals("no")) { 134 return true; 135 } 136 log.error("Invalid reply."); 137 } 138 catch(IOException ioe) { 139 return yesByDefault; 140 } 141 } 142 } 143 144 } 145 | Popular Tags |