KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > upgrade > CommandLineUpgrader


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.upgrade;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
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 JavaDoc upgrades;
37     
38     public CommandLineUpgrader(String JavaDoc[] args) throws Exception JavaDoc {
39
40         File JavaDoc oldDir = new File JavaDoc(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 JavaDoc newDir = new File JavaDoc(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 JavaDoc(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 JavaDoc(oldDir, "upgrade").exists()) {
59             System.err.println("Old installation does not appear to be 0.1.15+");
60             System.exit(1);
61         }
62         File JavaDoc oldDbDir = new File JavaDoc(oldDir, "db");
63         File JavaDoc newDbDir = new File JavaDoc(newDir, "db");
64
65         upgrades = new ArrayList JavaDoc();
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 JavaDoc message) {
77         log.error(message);
78
79     }
80
81     public void error(String JavaDoc message, Throwable JavaDoc exception) {
82         log.error(message, exception);
83     }
84
85     public void info(String JavaDoc message) {
86         log.info(message);
87     }
88
89     public void upgrade() throws Exception JavaDoc {
90         for (Iterator JavaDoc 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 JavaDoc e) {
102                 log.error("Failed upgrader.", e);
103             }
104         }
105
106     }
107
108     public void warn(String JavaDoc message) {
109         log.warn(message);
110
111     }
112
113     public void warn(String JavaDoc message, Throwable JavaDoc exception) {
114         log.warn(message, exception);
115     }
116
117     public static boolean yesno(String JavaDoc message, boolean yesByDefault) {
118         while (true) {
119             log.info(message + " (" + (yesByDefault ? "[Yes]" : "Yes") + " / " + (yesByDefault ? "No" : "[Yes]") + "): ");
120             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
121             try {
122                 String JavaDoc 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 JavaDoc ioe) {
139                 return yesByDefault;
140             }
141         }
142     }
143
144 }
145
Popular Tags