1 25 26 package com.j2biz.blogunity.installer; 27 28 import java.io.BufferedReader ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.InputStreamReader ; 35 import java.io.OutputStream ; 36 37 import com.j2biz.blogunity.IConstants; 38 39 public class ConsoleLauncher { 40 41 private static String currentDir = System.getProperty("user.dir"); 42 43 private static String blogunityDir; 44 45 private static String blogunityDataDir; 46 47 private static String blogunityTempDir; 48 49 private static String logsDirectory; 50 51 private static String driverClass; 52 53 private static String dbUrl; 54 55 private static String dbUser; 56 57 private static String dbPass; 58 59 private static String sqlDialect; 60 61 private static String adminUser; 62 63 private static String adminPass; 64 65 private static String adminEmail; 66 67 private static String passwordEncryptionType; 68 69 public static void main(String [] args) { 70 try { 71 BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); 72 73 welcomeScreen(); 74 75 String webappDirectory = getWebappDirectory(in); 76 String webappName = getWebappName(in); 77 78 File f = new File (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 dataDirectory = getDataDirectory(in); 87 String tempDirectory = getTempDirectory(in); 88 String logsDir = getLogsDirectory(in); 89 90 blogunityDataDir = new File (dataDirectory).getAbsolutePath(); 91 blogunityTempDir = new File (tempDirectory).getAbsolutePath(); 92 logsDirectory = new File (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 e) { 107 e.printStackTrace(); 108 } 109 110 } 111 112 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 140 private static void executeInstaller() { 141 File f = new File (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 File d = new File (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 File d1 = new File (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 d2 = new File (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 File l = new File (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 System.out.println("Copying files from '" + currentDir + "' to '" + f.getAbsolutePath() 231 + "'..."); 232 try { 233 copyDirectory(new File (currentDir), f); 234 } catch (IOException 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 Installer.install(blogunityDir, blogunityDataDir, blogunityTempDir, logsDirectory, dbUrl, 243 dbUser, dbPass, driverClass, sqlDialect, adminUser, adminPass, adminEmail, passwordEncryptionType); 244 } 245 246 250 private static String getWebappDirectory(BufferedReader in) throws IOException { 251 String dir = ""; 253 String 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 f = new File (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 getDataDirectory(BufferedReader in) throws IOException { 274 String dir = ""; 276 String 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 f = new File (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 getTempDirectory(BufferedReader in) throws IOException { 301 String dir = ""; 303 String 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 f = new File (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 getLogsDirectory(BufferedReader in) throws IOException { 327 String dir = ""; 329 String 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 f = new File (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 357 private static String getWebappName(BufferedReader in) throws IOException { 358 String name = "blogunity"; 359 String 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 getAdminUsername(BufferedReader in) throws IOException { 372 String name = "admin"; 373 String 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 getAdminPassword(BufferedReader in) throws IOException { 386 String pass = ""; 387 String 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 getAdminEmail(BufferedReader in) throws IOException { 401 String email = null; 402 String 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 getPasswordEncryption(BufferedReader in) throws IOException { 417 String type = "none"; 418 String 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 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 getDriverClass(BufferedReader in) throws IOException { 442 String driver = "org.hsqldb.jdbcDriver"; 444 String 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 driver = line; 453 455 try { 456 Class.forName(driver); 457 ok = true; 458 } catch (ClassNotFoundException 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 getDatabaseUrl(BufferedReader in) throws IOException { 473 String url = "jdbc:hsqldb:file:" + blogunityDir + "/WEB-INF/db/blogunity"; 475 String 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 url = line; 484 ok = true; 485 } else { 486 ok = true; 487 } 488 } 489 return url; 490 } 491 492 private static String getDatabaseUser(BufferedReader in) throws IOException { 493 String user = "sa"; 494 String 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 user = line; 502 } 503 ok = true; 504 } 505 return user; 506 } 507 508 private static String getDatabasePass(BufferedReader in) throws IOException { 509 String pass = ""; 510 String 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 pass = line; 519 ok = true; 520 521 } 522 return pass; 523 } 524 525 private static String getSqlDialect(BufferedReader in) throws IOException { 526 String dialect = "net.sf.hibernate.dialect.HSQLDialect"; 527 String 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 [] 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 ("Wrong array-index specified!"); 546 547 dialect = Installer.SQL_DIALECT_CLASSES[number - 1]; 548 ok = true; 549 550 } catch (Exception 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 src, File dst) throws IOException { 564 if (src.getAbsolutePath().endsWith("tar.gz") || src.getAbsolutePath().endsWith("sh") 566 || src.getAbsolutePath().endsWith("bat")) return; 567 568 InputStream in = new FileInputStream (src); 569 OutputStream out = new FileOutputStream (dst); 570 571 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 } 581 public static void copyDirectory(File srcDir, File dstDir) throws IOException { 583 if (srcDir.isDirectory()) { 584 if (!dstDir.exists()) { 585 dstDir.mkdir(); 586 } 587 588 String [] children = srcDir.list(); 589 for (int i = 0; i < children.length; i++) { 590 copyDirectory(new File (srcDir, children[i]), new File (dstDir, children[i])); 591 } 592 } else { 593 copyFile(srcDir, dstDir); 595 } 596 } 597 598 } | Popular Tags |