KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > installer > ConsoleLauncher


1 /*
2  * $Id: ConsoleLauncher.java,v 1.9 2005/01/13 22:54:28 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.installer;
27
28 import java.io.BufferedReader JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.io.OutputStream JavaDoc;
36
37 import com.j2biz.blogunity.IConstants;
38
39 public class ConsoleLauncher {
40
41     private static String JavaDoc currentDir = System.getProperty("user.dir");
42
43     private static String JavaDoc blogunityDir;
44
45     private static String JavaDoc blogunityDataDir;
46
47     private static String JavaDoc blogunityTempDir;
48
49     private static String JavaDoc logsDirectory;
50
51     private static String JavaDoc driverClass;
52
53     private static String JavaDoc dbUrl;
54
55     private static String JavaDoc dbUser;
56
57     private static String JavaDoc dbPass;
58
59     private static String JavaDoc sqlDialect;
60
61     private static String JavaDoc adminUser;
62
63     private static String JavaDoc adminPass;
64
65     private static String JavaDoc adminEmail;
66
67     private static String JavaDoc passwordEncryptionType;
68
69     public static void main(String JavaDoc[] args) {
70         try {
71             BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
72
73             welcomeScreen();
74
75             String JavaDoc webappDirectory = getWebappDirectory(in);
76             String JavaDoc webappName = getWebappName(in);
77
78             File JavaDoc f = new File JavaDoc(webappDirectory, webappName);
79             if (f.exists()) {
80                 System.out.println(f.getAbsolutePath() + " already exists! Installation canceled!");
81                 System.exit(-1);
82             }
83
84             blogunityDir = f.getAbsolutePath();
85
86             String JavaDoc dataDirectory = getDataDirectory(in);
87             String JavaDoc tempDirectory = getTempDirectory(in);
88             String JavaDoc logsDir = getLogsDirectory(in);
89
90             blogunityDataDir = new File JavaDoc(dataDirectory).getAbsolutePath();
91             blogunityTempDir = new File JavaDoc(tempDirectory).getAbsolutePath();
92             logsDirectory = new File JavaDoc(logsDir).getAbsolutePath();
93
94             driverClass = getDriverClass(in);
95             dbUrl = getDatabaseUrl(in);
96             dbUser = getDatabaseUser(in);
97             dbPass = getDatabasePass(in);
98             sqlDialect = getSqlDialect(in);
99             adminUser = getAdminUsername(in);
100             adminPass = getAdminPassword(in);
101             adminEmail = getAdminEmail(in);
102             passwordEncryptionType = getPasswordEncryption(in);
103
104             executeInstaller();
105
106         } catch (Exception JavaDoc e) {
107             e.printStackTrace();
108         }
109
110     }
111
112     /**
113      *
114      */

115     private static void welcomeScreen() {
116
117         System.out.println("################################################################");
118         System.out.println("# Blogunity Installer " + IConstants.VERSION
119                 + " #");
120         System.out.println("################################################################");
121         System.out.println("# This application helps you to install Blogunity on your #");
122         System.out.println("# computer. #");
123         System.out.println("# Please follow the installation steps below. #");
124         System.out.println("# #");
125         System.out.println("# ATTENTION! #");
126         System.out.println("# -------------- #");
127         System.out.println("# Check you have copied jdbc-driver #");
128         System.out.println("# of the database your choice into: #");
129         System.out.println("# '" + currentDir + "/WEB-INF/lib'-directory #");
130         System.out.println("# and if you are not using pre-installed hsql-database #");
131         System.out.println("# already created an empty one with the name your choice. #");
132         System.out.println("################################################################");
133         System.out.println();
134     }
135
136     /**
137      * @throws IOException
138      *
139      */

140     private static void executeInstaller() {
141         // 1. create webapp-directory
142
File JavaDoc f = new File JavaDoc(blogunityDir);
143
144         if (f.exists()) {
145             if (!f.isDirectory() || !f.canWrite()) {
146                 System.out.println("'" + f.getAbsolutePath()
147                         + "' is not a directory or cannot be written!");
148                 System.out.println("Installation canceled!");
149                 System.exit(-1);
150             }
151         } else {
152             boolean result = f.mkdir();
153             if (!result) {
154                 System.out.println("Unable to create directory '" + f.getAbsolutePath() + "'!");
155                 System.out.println("Installation canceled!");
156                 System.exit(-1);
157             }
158         }
159
160         // 2. create data-directory
161
File JavaDoc d = new File JavaDoc(blogunityDataDir);
162         if (d.exists()) {
163             if (!d.canRead() || !d.canWrite()) {
164                 System.out.println("'" + d.getAbsolutePath()
165                         + "' is not a directory or cannot be written!");
166                 System.out.println("Installation canceled!");
167                 System.exit(-1);
168             }
169         } else {
170             boolean result = d.mkdirs();
171             if (!result) {
172                 System.out.println("Unable to create directory '" + d.getAbsolutePath() + "'!");
173                 System.out.println("Installation canceled!");
174                 System.exit(-1);
175             }
176         }
177
178         // 2a. create sub-datadirs for blogs and users
179
File JavaDoc d1 = new File JavaDoc(blogunityDataDir, "blogs");
180         if (d1.exists()) {
181             if (!d1.canRead() || !d1.canWrite()) {
182                 System.out.println("'" + d1.getAbsolutePath()
183                         + "' is not a directory or cannot be written!");
184                 System.out.println("Installation canceled!");
185                 System.exit(-1);
186             }
187         } else {
188             boolean result = d1.mkdirs();
189             if (!result) {
190                 System.out.println("Unable to create directory '" + d1.getAbsolutePath() + "'!");
191                 System.out.println("Installation canceled!");
192                 System.exit(-1);
193             }
194         }
195
196         File JavaDoc d2 = new File JavaDoc(blogunityDataDir, "users");
197         if (d2.exists()) {
198             if (!d2.canRead() || !d2.canWrite()) {
199                 System.out.println("'" + d2.getAbsolutePath()
200                         + "' is not a directory or cannot be written!");
201                 System.out.println("Installation canceled!");
202                 System.exit(-1);
203             }
204         } else {
205             boolean result = d2.mkdirs();
206             if (!result) {
207                 System.out.println("Unable to create directory '" + d2.getAbsolutePath() + "'!");
208                 System.out.println("Installation canceled!");
209                 System.exit(-1);
210             }
211         }
212
213         // 3. create logs-directory
214
File JavaDoc l = new File JavaDoc(logsDirectory);
215         if (l.exists()) {
216             System.out.println("'" + l.getAbsolutePath()
217                     + "' is not a directory or cannot be written!");
218             System.out.println("Installation canceled!");
219             System.exit(-1);
220         } else {
221             boolean result = l.mkdirs();
222             if (!result) {
223                 System.out.println("Unable to create directory '" + l.getAbsolutePath() + "'!");
224                 System.out.println("Installation canceled!");
225                 System.exit(-1);
226             }
227         }
228
229         // 4. copy application
230
System.out.println("Copying files from '" + currentDir + "' to '" + f.getAbsolutePath()
231                 + "'...");
232         try {
233             copyDirectory(new File JavaDoc(currentDir), f);
234         } catch (IOException JavaDoc e) {
235             System.out.println("Unable to copy from '" + currentDir + "' to '"
236                     + f.getAbsolutePath() + "'.");
237             System.out.println("Installation canceled!");
238             System.exit(-1);
239         }
240
241         // 5. execute installer
242
Installer.install(blogunityDir, blogunityDataDir, blogunityTempDir, logsDirectory, dbUrl,
243                 dbUser, dbPass, driverClass, sqlDialect, adminUser, adminPass, adminEmail, passwordEncryptionType);
244     }
245
246     /**
247      * @return
248      * @throws IOException
249      */

250     private static String JavaDoc getWebappDirectory(BufferedReader JavaDoc in) throws IOException JavaDoc {
251         // webapp directory
252
String JavaDoc dir = "";
253         String JavaDoc line = null;
254         boolean ok = false;
255         while (!ok) {
256             System.out.print("Please, specify directory, where you want to install bloguntiy"
257                     + " (e.g. <Path to Webserver>/webapps): ");
258             line = in.readLine();
259             if (line != null && line.trim().length() > 0) {
260                 File JavaDoc f = new File JavaDoc(line);
261                 if (f.exists() && f.canWrite() && f.isDirectory()) {
262                     dir = line;
263                     ok = true;
264                 } else {
265                     System.out.println("Unable to find directory: " + line);
266                 }
267             }
268         }
269
270         return dir;
271     }
272
273     private static String JavaDoc getDataDirectory(BufferedReader JavaDoc in) throws IOException JavaDoc {
274         // webapp directory
275
String JavaDoc dir = "";
276         String JavaDoc line = null;
277         boolean ok = false;
278         while (!ok) {
279             System.out
280                     .print("Please, specify directory, where you want your blogs-data will be saved"
281                             + " (default: " + blogunityDir + "/WEB-INF/data): ");
282             line = in.readLine();
283             if (line != null && line.trim().length() > 0) {
284                 File JavaDoc f = new File JavaDoc(line);
285                 if (f.exists() && f.canWrite() && f.isDirectory()) {
286                     dir = line;
287                     ok = true;
288                 } else {
289                     System.out.println("Unable to find directory: " + line);
290                 }
291             } else {
292                 dir = blogunityDir + "/WEB-INF/data";
293                 ok = true;
294             }
295         }
296
297         return dir;
298     }
299
300     private static String JavaDoc getTempDirectory(BufferedReader JavaDoc in) throws IOException JavaDoc {
301         // webapp directory
302
String JavaDoc dir = "";
303         String JavaDoc line = null;
304         boolean ok = false;
305         while (!ok) {
306             System.out.print("Please, specify temporary directory" + " (default: " + blogunityDir
307                     + "/WEB-INF/temp): ");
308             line = in.readLine();
309             if (line != null && line.trim().length() > 0) {
310                 File JavaDoc f = new File JavaDoc(line);
311                 if (f.exists() && f.canWrite() && f.isDirectory()) {
312                     dir = line;
313                     ok = true;
314                 } else {
315                     System.out.println("Unable to find directory: " + line);
316                 }
317             } else {
318                 dir = blogunityDir + "/WEB-INF/temp";
319                 ok = true;
320             }
321         }
322
323         return dir;
324     }
325
326     private static String JavaDoc getLogsDirectory(BufferedReader JavaDoc in) throws IOException JavaDoc {
327         // webapp directory
328
String JavaDoc dir = "";
329         String JavaDoc line = null;
330         boolean ok = false;
331         while (!ok) {
332             System.out.print("Please, specify directory, where you want your logs will be saved"
333                     + " (default: " + blogunityDir + "/WEB-INF/logs): ");
334             line = in.readLine();
335             if (line != null && line.trim().length() > 0) {
336                 File JavaDoc f = new File JavaDoc(line);
337                 if (f.exists() && f.canWrite() && f.isDirectory()) {
338                     dir = line;
339                     ok = true;
340                 } else {
341                     System.out.println("Unable to find directory: " + line);
342                 }
343             } else {
344                 dir = blogunityDir + "/WEB-INF/logs";
345                 ok = true;
346             }
347         }
348
349         return dir;
350     }
351
352     /**
353      * @param in
354      * @return
355      * @throws IOException
356      */

357     private static String JavaDoc getWebappName(BufferedReader JavaDoc in) throws IOException JavaDoc {
358         String JavaDoc name = "blogunity";
359         String JavaDoc line = null;
360         boolean ok = false;
361         while (!ok) {
362             System.out.print("Please, specify name for the blogunity webapplication "
363                     + " (default: blogunity): ");
364             line = in.readLine();
365             if (line != null && line.trim().length() > 0) name = line;
366             ok = true;
367         }
368         return name;
369     }
370
371     private static String JavaDoc getAdminUsername(BufferedReader JavaDoc in) throws IOException JavaDoc {
372         String JavaDoc name = "admin";
373         String JavaDoc line = null;
374         boolean ok = false;
375         while (!ok) {
376             System.out.print("Please, specify administrator's nickname" + " (default: admin): ");
377             line = in.readLine();
378             if (line == null || line.trim().length() == 0) line = "admin";
379             name = line;
380             ok = true;
381         }
382         return name;
383     }
384
385     private static String JavaDoc getAdminPassword(BufferedReader JavaDoc in) throws IOException JavaDoc {
386         String JavaDoc pass = "";
387         String JavaDoc line = null;
388         boolean ok = false;
389         while (!ok) {
390             System.out.print("Please, specify administrator's password (default: <no password>): ");
391             line = in.readLine();
392             if (line != null && line.trim().length() > 0) {
393                 pass = line;
394             }
395             ok = true;
396         }
397         return pass;
398     }
399
400     private static String JavaDoc getAdminEmail(BufferedReader JavaDoc in) throws IOException JavaDoc {
401         String JavaDoc email = null;
402         String JavaDoc line = null;
403         boolean ok = false;
404         while (!ok) {
405             System.out.print("Please, specify administrator's email: ");
406             line = in.readLine();
407             if (line != null && line.trim().length() > 0) {
408                 email = line;
409                 ok = true;
410             }
411
412         }
413         return email;
414     }
415
416     private static String JavaDoc getPasswordEncryption(BufferedReader JavaDoc in) throws IOException JavaDoc {
417         String JavaDoc type = "none";
418         String JavaDoc line = null;
419         boolean ok = false;
420         while (!ok) {
421             System.out
422                     .print("Please, specify one of the following password encryption type to save user passworts (none, md5, sha): ");
423             line = in.readLine();
424             if (line != null && line.trim().length() > 0) {
425                 // try to find
426
type = line;
427                 if (!(type.equalsIgnoreCase("none") || type.equalsIgnoreCase("md5") || type
428                         .equalsIgnoreCase("sha"))) {
429                     System.out.println("Wrong encryption type specified!");
430                     ok = false;
431                 } else
432                     ok = true;
433             } else{
434                 type = "none";
435                 ok = true;
436             }
437         }
438         return type;
439     }
440
441     private static String JavaDoc getDriverClass(BufferedReader JavaDoc in) throws IOException JavaDoc {
442         // String driver = "com.mysql.jdbc.Driver";
443
String JavaDoc driver = "org.hsqldb.jdbcDriver";
444         String JavaDoc line = null;
445         boolean ok = false;
446         while (!ok) {
447             System.out.print("Please, specify name for the database driver to be used "
448                     + " (default: org.hsqldb.jdbcDriver): ");
449             line = in.readLine();
450             if (line != null && line.trim().length() > 0) {
451                 // try to find
452
driver = line;
453                 // try to load instance
454

455                 try {
456                     Class.forName(driver);
457                     ok = true;
458                 } catch (ClassNotFoundException JavaDoc e) {
459                     System.out
460                             .println("Driver '"
461                                     + driver
462                                     + "' no found in your classpath! Copy driver to your 'lib' directory and start again installation routine.");
463                 }
464
465             } else {
466                 ok = true;
467             }
468         }
469         return driver;
470     }
471
472     private static String JavaDoc getDatabaseUrl(BufferedReader JavaDoc in) throws IOException JavaDoc {
473         // String url = "jdbc:mysql://localhost/blogunity";
474
String JavaDoc url = "jdbc:hsqldb:file:" + blogunityDir + "/WEB-INF/db/blogunity";
475         String JavaDoc line = null;
476         boolean ok = false;
477         while (!ok) {
478             System.out.print("Please, specify database url to be used " + " (default: " + url
479                     + "): ");
480             line = in.readLine();
481             if (line != null && line.trim().length() > 0) {
482                 // try to find
483
url = line;
484                 ok = true;
485             } else {
486                 ok = true;
487             }
488         }
489         return url;
490     }
491
492     private static String JavaDoc getDatabaseUser(BufferedReader JavaDoc in) throws IOException JavaDoc {
493         String JavaDoc user = "sa";
494         String JavaDoc line = null;
495         boolean ok = false;
496         while (!ok) {
497             System.out.print("Please, specify database user to be used (default: sa): ");
498             line = in.readLine();
499             if (line != null && line.trim().length() > 0) {
500                 // try to find
501
user = line;
502             }
503             ok = true;
504         }
505         return user;
506     }
507
508     private static String JavaDoc getDatabasePass(BufferedReader JavaDoc in) throws IOException JavaDoc {
509         String JavaDoc pass = "";
510         String JavaDoc line = null;
511         boolean ok = false;
512         while (!ok) {
513             System.out.print("Please, specify user's password (default: <no password>): ");
514             line = in.readLine();
515             if (line == null || line.trim().length() == 0) line = "";
516
517             // try to find
518
pass = line;
519             ok = true;
520
521         }
522         return pass;
523     }
524
525     private static String JavaDoc getSqlDialect(BufferedReader JavaDoc in) throws IOException JavaDoc {
526         String JavaDoc dialect = "net.sf.hibernate.dialect.HSQLDialect";
527         String JavaDoc line = null;
528         boolean ok = false;
529         while (!ok) {
530             System.out
531                     .println("Please, specify one of the following sql dialects to be used or empty string for generic sql dialect");
532
533             String JavaDoc[] dialects = Installer.SQL_DIALECT_NAMES;
534             for (int i = 0; i < dialects.length; i++) {
535                 System.out.println("\t[" + (i + 1) + "] " + dialects[i]);
536             }
537             System.out.println("(default: " + dialect + ")");
538
539             line = in.readLine();
540             if (line != null && line.trim().length() > 0) {
541
542                 try {
543                     int number = Integer.parseInt(line);
544                     if (number < 1 || number > dialects.length)
545                             throw new IllegalArgumentException JavaDoc("Wrong array-index specified!");
546
547                     dialect = Installer.SQL_DIALECT_CLASSES[number - 1];
548                     ok = true;
549
550                 } catch (Exception JavaDoc e) {
551                     System.out.println("Error! You have to specify dialect-number between 1 and "
552                             + dialects.length);
553                 }
554
555             } else {
556                 ok = true;
557             }
558
559         }
560         return dialect;
561     }
562
563     private static void copyFile(File JavaDoc src, File JavaDoc dst) throws IOException JavaDoc {
564         // don't copy distribution itself
565
if (src.getAbsolutePath().endsWith("tar.gz") || src.getAbsolutePath().endsWith("sh")
566                 || src.getAbsolutePath().endsWith("bat")) return;
567
568         InputStream JavaDoc in = new FileInputStream JavaDoc(src);
569         OutputStream JavaDoc out = new FileOutputStream JavaDoc(dst);
570
571         // Transfer bytes from in to out
572
byte[] buf = new byte[1024];
573         int len;
574         while ((len = in.read(buf)) > 0) {
575             out.write(buf, 0, len);
576         }
577         in.close();
578         out.close();
579     } // Copies all files under srcDir to dstDir.
580

581     // If dstDir does not exist, it will be created.
582
public static void copyDirectory(File JavaDoc srcDir, File JavaDoc dstDir) throws IOException JavaDoc {
583         if (srcDir.isDirectory()) {
584             if (!dstDir.exists()) {
585                 dstDir.mkdir();
586             }
587
588             String JavaDoc[] children = srcDir.list();
589             for (int i = 0; i < children.length; i++) {
590                 copyDirectory(new File JavaDoc(srcDir, children[i]), new File JavaDoc(dstDir, children[i]));
591             }
592         } else {
593             // This method is implemented in e1071 Copying a File
594
copyFile(srcDir, dstDir);
595         }
596     }
597
598 }
Popular Tags