001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: // This file is part of KeY - Integrated Deductive Software Design
010: // Copyright (C) 2001-2004 Universitaet Karlsruhe, Germany
011: // Universitaet Koblenz-Landau, Germany
012: // Chalmers University of Technology, Sweden
013: //
014: // The KeY system is protected by the GNU General Public License.
015: // See LICENSE.TXT for details.
016: package de.uka.ilkd.key.util.install;
017:
018: import java.io.*;
019: import java.util.jar.JarFile;
020:
021: /**
022: * This class is a command line UI for installing KeY
023: */
024:
025: public class KeYInstallerCmdLine extends KeYInstallerUI {
026:
027: public KeYInstallerCmdLine(String keyHome, String keyLib,
028: String javaHome, String keyJarPath, String os) {
029:
030: super (keyHome, keyLib, javaHome, keyJarPath, os);
031: }
032:
033: // ASCII rendering
034: public String makeTitle(String title) {
035:
036: StringBuffer separator = new StringBuffer();
037:
038: for (int i = 0; i < title.length(); i++) {
039: separator.append("=");
040: }
041:
042: return title + "\n" + separator + "\n\n";
043: }
044:
045: // input methods
046:
047: /**
048: * reads input line until a character is read in, that also occurs in the
049: * given list of characters.
050: * @return the read character
051: */
052: public char expect(char[] expectedChars) {
053:
054: char result = (char) -1;
055: try {
056: char c;
057:
058: while (!containsChar((c = (char) System.in.read()),
059: expectedChars)) {
060: }
061: result = c;
062: } catch (IOException io) {
063: criticalError("Could not read from standard input stream.",
064: io);
065: System.exit(-1);
066: }
067:
068: return result;
069: }
070:
071: public void pause() {
072: System.out.println("Press <ENTER> to proceed:");
073: expect(new char[] { '\n' });
074: }
075:
076: protected String readLine() {
077:
078: String result = "";
079:
080: try {
081:
082: if (System.in.available() > 0) { // flush keyboard buffer
083: System.in.read(new byte[System.in.available()]);
084: }
085:
086: result = new BufferedReader(
087: new InputStreamReader(System.in)).readLine();
088:
089: } catch (IOException io) {
090: criticalError("Could not read from standard input stream.",
091: io);
092: System.exit(-1);
093: }
094:
095: return result;
096:
097: }
098:
099: public String readDir(boolean hasToExist) {
100:
101: String dir = "";
102:
103: boolean correct;
104: do {
105: correct = true;
106: dir = readLine();
107:
108: File f = new File(dir);
109:
110: if (!f.isDirectory() && f.exists()) {
111: print("Directory required.");
112: correct = false;
113: }
114: if (hasToExist && !f.exists()) {
115: print("Directory does not exist.");
116: correct = false;
117: }
118: } while (!correct);
119:
120: return dir;
121: }
122:
123: // helpers
124:
125: public final void print(String s) {
126: System.out.println(s);
127: }
128:
129: // text
130: protected String title() {
131: return "Welcome";
132: }
133:
134: protected String welcome() {
135: String welcome = "Dear User,\n"
136: + " thank you for choosing KeY. In case of any questions, problems "
137: + "or suggestions please contact us: mailto:key@ira.uka.de\n"
138: + "We would also be interested to know for which purpose you will use "
139: + " KeY, e.g. research, industry or teaching.\n"
140: + "\t Best regards,\n" + "\t Your KeY-Team";
141: return welcome;
142: }
143:
144: protected String titleGlobals() {
145: return " General Settings ";
146: }
147:
148: protected String globals() {
149: String globals = "";
150:
151: globals += "Directory where to install KeY.";
152: globals += "\n(1) KEY_HOME : "
153: + ("".equals(keyHome()) ? "(none)" : keyHome());
154:
155: globals += "\nDirectory where Together has been installed (ask your admin if unsure)";
156: globals += "\n(2) TOGETHER_HOME : "
157: + ("".equals(togetherHome()) ? "(none)"
158: : togetherHome());
159:
160: globals += "\nInstalled TogetherCC version (6.0, 6.0.1)";
161: globals += "\n(3) TOGETHER_VERSION : " + togetherVersion();
162:
163: globals += "\nDirectory, where your Java is installed "
164: + "(if TogetherCC uses another Java version than your system does "
165: + "(recommended: JDK 1.3.1)";
166: globals += "\n(4) JAVA_HOME : "
167: + ("".equals(javaHome()) ? "(none)" : javaHome());
168:
169: globals += "\n(c)ontinue";
170:
171: globals += "\nPress 1 to 4 to enter a directory, c for continue:";
172:
173: return globals;
174: }
175:
176: protected String titleLibrary() {
177: return " Library Settings ";
178: }
179:
180: protected String library() {
181: String library = " KeY makes use of different external libraries.";
182:
183: library += "For download and additional information see ";
184: library += "http://download.key-project.org";
185: library += "You have to install them in ONE directory (usually: ";
186: library += keyextjarsPath() + "). Enter below the directory ";
187: library += "where KeY looks for them.\n";
188: library += "Library Path:";
189: library += "\n(1) KEY_LIB : "
190: + ("".equals(keyLib()) ? "(none)" : keyLib());
191:
192: library += "\n(c)ontinue";
193:
194: library += "\nPress 1 to enter the library directory, c for continue : ";
195:
196: return library;
197: }
198:
199: public String readTgVersion() {
200:
201: char[] versionId = new char[supportedTgVersion().length];
202:
203: for (int i = 0; i < supportedTgVersion().length; i++) {
204: print("(" + i + ")" + " " + supportedTgVersion()[i]);
205: versionId[i] = (char) (i + (int) '0');
206: }
207: print("Enter number in brackets for your installed TogetherCC version:");
208:
209: return supportedTgVersion()[(int) (expect(versionId) - '0')];
210: }
211:
212: public String[] checkLibraries() {
213:
214: String[] missingLibs = super .checkLibraries();
215:
216: char c = '1';
217:
218: while ((missingLibs.length != 0) && c != '2') {
219:
220: StringBuffer missing = new StringBuffer(
221: "The following libraries are missing:\n");
222: for (int i = 0; i < missingLibs.length; i++) {
223: missing.append(missingLibs[i]);
224: missing.append("\n");
225: }
226:
227: missing.append("Please copy them to ");
228: missing.append(keyLib());
229: missing.append("\n");
230: missing.append("(1) I have copied them ");
231: missing
232: .append("(2) I will copy them later and promise to spend "
233: + "you a beer at the next conference, if I forget it.");
234:
235: print(trim(missing.toString(), 72));
236: switch (c) {
237: case '1':
238: missingLibs = super .checkLibraries();
239: break;
240: }
241:
242: c = expect(new char[] { '1', '2' });
243: }
244:
245: return missingLibs;
246: }
247:
248: // main methods
249:
250: public void start() {
251:
252: StringBuffer todo = new StringBuffer("");
253: String[] missingLibs = new String[0];
254:
255: print(makeTitle(title()));
256:
257: print(trim(welcome(), 72));
258:
259: pause();
260:
261: File keyJar = new File(keyJarFile());
262: while (!keyJar.exists()) {
263: print(trim("Have not found file 'key.jar' in "
264: + keyJarPath()
265: + ". Please enter the directory where you have "
266: + "put key.jar (usually the directory where you "
267: + "have unpacked key): ", 72));
268: keyJarPath(readDir(true));
269: keyJar = new File(keyJarFile());
270: }
271:
272: JarFile keyJarFile = null;
273: try {
274: keyJarFile = new JarFile(keyJar);
275: } catch (FileNotFoundException fne) {
276: error("Did not found a valid jarfile at " + keyJarPath()
277: + " Please check if it is there and "
278: + "if you have read and write access.\n Detail: "
279: + fne);
280: System.exit(-1);
281: } catch (IOException ioe) {
282: error("Error when trying to access 'key.jar' at "
283: + keyJarPath() + ".\n Detail: " + ioe);
284: System.exit(-1);
285: }
286:
287: print(makeTitle(titleGlobals()));
288:
289: char[] options = new char[] { '1', '2', '3', '4', 'c' };
290: char c;
291: print(trim(globals(), 72));
292: while ((c = expect(options)) != 'c') {
293: switch (c) {
294: case '1':
295: print("KEY_HOME : ");
296: keyHome(readDir(false));
297: keyLib(keyHome() + File.separatorChar + "key-ext-jars");
298: break;
299: case '2':
300: print("TOGETHER_HOME : ");
301: togetherHome(readDir(true));
302: break;
303: case '3':
304: print("TOGETHER_VERSION : ");
305: togetherVersion(readTgVersion());
306: print(trim(globals(), 72));
307: break;
308: case '4':
309: print("JAVA_HOME : ");
310: javaHome(readDir(true));
311: break;
312: default:
313: print("\n Wrong character (expected : 1,..,4 or c). Re-enter :");
314: break;
315: }
316: print(trim(globals(), 72));
317: }
318:
319: print(makeTitle(titleLibrary()));
320:
321: print(trim(library(), 72));
322:
323: while ((c = expect(new char[] { '1', 'c' })) != 'c') {
324: switch (c) {
325: case '1':
326: print("KEY_LIB : ");
327: keyLib(readDir(false));
328: missingLibs = checkLibraries();
329: break;
330: default:
331: print(" Wrong character (expected : 1 or c).");
332: break;
333: }
334: print(trim(library(), 72));
335: }
336:
337: mkdirs();
338:
339: try {
340: generateScripts(keyJarFile);
341: } catch (KeYInstallerException kie) {
342: error("Could not generate the shell scripts. Please "
343: + "resolve the problem first "
344: + "and redo the installation afterwards.\n Detail: "
345: + kie);
346: System.exit(-1);
347: }
348:
349: try {
350: extractTgScripts(keyJarFile);
351: } catch (KeYInstallerException kie) {
352: error("Could not generate the keyscripts. Please "
353: + "resolve the problem first "
354: + "and redo the installation afterwards.\n Detail:"
355: + kie);
356: System.exit(-1);
357: }
358:
359: if ("linux".equals(os())) {
360: try {
361: Runtime.getRuntime().exec(
362: "chmod a+x " + startScriptFilePath());
363: Runtime.getRuntime().exec(
364: "chmod a+x " + startProverScriptFilePath());
365: } catch (IOException e) {
366: todo.append("Please set " + startScriptFilePath()
367: + " executable : chmod a+x "
368: + startScriptFilePath() + "\n\t and chmod a+x "
369: + startProverScriptFilePath());
370: todo.append("\n");
371: }
372: }
373:
374: try {
375: copy(keyJar, systemPath());
376: } catch (KeYInstallerException kie) {
377: todo.append(" Copy file 'key.jar' from " + keyJar + " to "
378: + systemPath()
379: + "\n\t usually this should be done automatical, "
380: + "but failed due to: " + kie);
381: todo.append("\n");
382: }
383:
384: if (missingLibs.length > 0) {
385: todo.append("Please copy the following libraries to");
386: todo.append(keyLib());
387: for (int i = 0; i < missingLibs.length; i++) {
388: todo.append("\n");
389: todo.append(i + ". " + missingLibs[i]);
390: }
391: todo.append("\n");
392: }
393:
394: if (todo.length() > 0) {
395: print(trim(
396: "Some things are left to do. Please read carefully.",
397: 72));
398: print(trim(todo.toString(), 72));
399: print(trim(
400: "After you have done the things above, you can start KeY."
401: + "Therefore you have to change to directory "
402: + binaryPath() + "and execute "
403: + startScriptFileName(), 72));
404: } else
405: print(trim(
406: "Installation finished. To start change to directory"
407: + " " + binaryPath() + "\n and execute "
408: + startScriptFileName(), 72));
409:
410: }
411:
412: // error handling
413:
414: private void error(String msg) {
415: System.err.println(makeTitle("An Error Occured."));
416: System.err.println(trim("Error Description:" + msg, 72));
417: }
418:
419: private void criticalError(String msg, Exception e) {
420: System.err.println(makeTitle("A Critical Error Occured."));
421: System.err.println(trim("Error Description:" + msg, 72));
422: System.err
423: .println("A critical error has occured. Please send a bug "
424: + "report to: key@ira.uka.de\n"
425: + "The bug report should include:\n"
426: + "\t Error Classification: Developer Heart Attack\n"
427: + "\t Operating system \n"
428: + "\t Java Version \n"
429: + "\t Steps to reproduce ( if possible ) \n"
430: + "\t and the following error message: \n"
431: + e.getLocalizedMessage());
432: e.printStackTrace();
433: }
434: }
|