1 17 package upgrade; 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedOutputStream ; 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.FileReader ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.net.URLClassLoader ; 33 import java.sql.Connection ; 34 import java.sql.Driver ; 35 import java.sql.SQLException ; 36 import java.sql.Statement ; 37 import java.util.Enumeration ; 38 import java.util.Properties ; 39 import java.util.Vector ; 40 import java.util.jar.JarFile ; 41 import java.util.zip.ZipEntry ; 42 import java.util.zip.ZipInputStream ; 43 44 45 46 public class JahiaUpgrade 47 { 48 private static final String jahiaUpgradeClassFileName = "upgrade.jar"; 49 private static String jahiaUpgradeClassFilePath = ""; 50 private static String launchDirectoryPath = ""; 51 private static String jahiaFilesPath = ""; 52 private static String jahiaPropertiesFilePath = ""; 53 54 private static String jahiaDBScript = ""; 55 private static String jahiaDBDriver = ""; 56 private static String jahiaDBUrl = ""; 57 private static String jahiaDBUsername = ""; 58 private static String jahiaDBPassword = ""; 59 60 private static final String SCRIPT_SEPARATOR = ":"; 61 private static final String SCRIPT_PREFIX = "release."; 62 63 private static String newReleaseNumber = ""; 64 private static int jahiaInstalledRelease; 65 66 private static File jahiaUpgradeClass; 67 private static URLClassLoader upgradeClassLoader; 68 69 70 71 77 public static void main( String args[] ) 78 { 79 startupConsole(); 81 82 checkLaunchDirectory(); 84 85 createDifferentClassLoader(); 87 88 checkInstalledRelease(); 90 91 extractJarFile(); 93 94 executeDatabaseRuntime(); 96 97 changeJahiaPropertiesFile(); 99 100 writeReleaseNumber(); 102 103 removeDummiesFiles(); 105 106 shutdownConsole(); 108 109 } 111 112 113 119 private static void checkLaunchDirectory() 120 { 121 launchDirectoryPath = System.getProperties().getProperty("user.dir"); 122 jahiaFilesPath = launchDirectoryPath + File.separator + "WEB-INF" + File.separator + "jahiafiles"; 123 jahiaUpgradeClassFilePath = launchDirectoryPath + File.separator + jahiaUpgradeClassFileName; 124 125 jahiaUpgradeClass = new File (jahiaUpgradeClassFilePath); 126 File jahiaFilesDirectory = new File (jahiaFilesPath); 127 128 if(!jahiaUpgradeClass.exists()) { 130 toConsole(jahiaUpgradeClassFileName + " not found."); 131 toConsole("You must launch JahiaUpgrade using \"java -jar upgrade.jar\"."); 132 System.exit(0); 133 } 134 135 if(!jahiaFilesDirectory.exists()) { 137 toConsole("You must be in the Jahia root to launch JahiaUpgrade."); 138 System.exit(0); 139 } 140 } 142 143 144 149 private static void createDifferentClassLoader() 150 { 151 String jahiaLibDirectoryPath = launchDirectoryPath + File.separator + "WEB-INF" + File.separator + "lib"; 152 153 File jahiaLibDirectory = new File (jahiaLibDirectoryPath); 154 File [] filesInThisDirectory = jahiaLibDirectory.listFiles(); 155 156 URL [] urlsFromJarFiles = new URL [filesInThisDirectory.length]; 157 158 for(int i=0; i<filesInThisDirectory.length; i++) { 160 String fileName = filesInThisDirectory[i].getPath(); 161 try { 162 if(fileName.substring(fileName.length()-4, fileName.length()).equals(".jar")) { 163 File thisJarFile = new File (fileName); 164 URL thisJarFileURL = thisJarFile.toURL(); 165 urlsFromJarFiles[i] = thisJarFileURL; 166 } 167 } catch (IndexOutOfBoundsException ioobe) { 168 } catch (MalformedURLException mue) { 169 } 170 } 171 172 upgradeClassLoader = new URLClassLoader ( urlsFromJarFiles ); 173 } 175 176 177 182 private static void checkInstalledRelease() 183 { 184 toConsole("Checking current installed Jahia release."); 185 186 try 188 { 189 jahiaPropertiesFilePath = jahiaFilesPath + File.separator + "config" + File.separator + "jahia.properties"; 190 191 Properties jahiaPropertiesObject = new Properties (); 192 FileInputStream inputStream = new FileInputStream (jahiaPropertiesFilePath); 193 194 jahiaPropertiesObject.load(inputStream); 195 inputStream.close(); 196 197 String jahiaReleaseNumber = jahiaPropertiesObject.getProperty("release"); 199 jahiaDBScript = jahiaPropertiesObject.getProperty("db_script"); 200 jahiaDBDriver = jahiaPropertiesObject.getProperty("db_driver"); 201 jahiaDBUrl = jahiaPropertiesObject.getProperty("db_url"); 202 jahiaDBUsername = jahiaPropertiesObject.getProperty("db_username"); 203 jahiaDBPassword = jahiaPropertiesObject.getProperty("db_password"); 204 205 if((jahiaReleaseNumber == null) || (jahiaDBScript == null) || (jahiaDBDriver == null) || (jahiaDBUrl == null) || (jahiaDBUsername == null) || (jahiaDBPassword == null)) 206 { 207 toConsole("Jahia properties file not found or corrupt."); 209 toConsole("Have you completed the Jahia installation before?"); 210 System.exit(0); 211 } 212 213 jahiaInstalledRelease = Integer.parseInt(jahiaReleaseNumber); 215 216 } catch (IOException ioe) { 217 toConsole("Jahia properties file not found or corrupt."); 219 toConsole("Have you completed the Jahia installation before?"); 220 System.exit(0); 221 } 222 223 toConsole("Upgrading Jahia from release " + jahiaInstalledRelease + "...\n"); 224 } 226 227 228 234 private static void extractJarFile() 235 { 236 try { 237 toConsole("Decompressing upgrade files... please wait."); 238 239 JarFile jahiaUpgradeJarFile = new JarFile (jahiaUpgradeClass); 240 FileInputStream fileStream = new FileInputStream (jahiaUpgradeClassFilePath); 241 BufferedInputStream bufferedStream = new BufferedInputStream (fileStream); 242 ZipInputStream zipStream = new ZipInputStream (bufferedStream); 243 ZipEntry zipEntry; 244 245 try 246 { 247 while((zipEntry = zipStream.getNextEntry()) != null) 248 { 249 File fileOutput = new File ( launchDirectoryPath + File.separator + generatePathFile(zipEntry.getName()) ); 250 if(zipEntry.isDirectory()) { 251 fileOutput.mkdirs(); 252 } else { 253 File parent = new File (fileOutput.getParent()); 254 parent.mkdirs(); 255 256 FileOutputStream outputStream = new FileOutputStream (fileOutput); 257 copyStream(zipStream, outputStream); 258 } 259 zipStream.closeEntry(); 260 } 261 } finally { 262 fileStream.close(); 263 zipStream.close(); 264 bufferedStream.close(); 265 } 266 267 toConsole("Decompression is done.\n"); 268 } catch (IOException ioe) { 269 } 270 } 272 273 274 280 private static void removeDummiesFiles() 281 { 282 Enumeration eachLines = readLines(launchDirectoryPath + File.separator + "upgrade" + File.separator + "remove_files.upgrade"); 283 284 while(eachLines.hasMoreElements()) { 285 try 286 { 287 String fileName = (String ) eachLines.nextElement(); 288 if(fileName.trim().length() > 0) 289 { 290 boolean doOperation = false; 291 int separatorPosition = fileName.indexOf(SCRIPT_SEPARATOR); 292 String releaseNumber = fileName.substring(SCRIPT_PREFIX.length(), separatorPosition).trim(); 293 294 if(releaseNumber.equals("all")) { 295 doOperation = true; 296 } else { 297 try { 298 int releaseNumberInt = Integer.parseInt(releaseNumber); 299 if(releaseNumberInt > jahiaInstalledRelease) { 300 doOperation = true; 301 } 302 } catch (NumberFormatException nfe) { 303 } 304 } 305 306 if(doOperation) { 307 String completeFileName = launchDirectoryPath + File.separator + fileName.substring(separatorPosition+1, fileName.length()).trim(); 308 File theFile = new File ( completeFileName ); 309 deleteFile( theFile ); 310 } 311 } 312 } catch (IndexOutOfBoundsException ioobe) { 313 } 314 } 315 } 317 318 319 324 private static void executeDatabaseRuntime() 325 { 326 Enumeration eachLines = readLines(launchDirectoryPath + File.separator + "upgrade" + File.separator + jahiaDBScript); 327 328 try 329 { 330 toConsole("Upgrading your database... please wait."); 331 332 Class dbDriverClass = Class.forName( jahiaDBDriver, false, upgradeClassLoader ); 333 Driver dbDriver = (Driver ) dbDriverClass.newInstance(); 334 335 Properties dbProperties = new Properties (); 336 dbProperties.setProperty("user", jahiaDBUsername); 337 dbProperties.setProperty("password", jahiaDBPassword); 338 339 Connection dbConnection = dbDriver.connect( jahiaDBUrl, dbProperties ); 340 Statement dbStatement = dbConnection.createStatement(); 341 342 while(eachLines.hasMoreElements()) { 343 try 344 { 345 String sqlCode = (String ) eachLines.nextElement(); 346 if(sqlCode.trim().length() > 0) 347 { 348 int separatorPosition = sqlCode.indexOf(SCRIPT_SEPARATOR); 349 String releaseNumber = sqlCode.substring(SCRIPT_PREFIX.length(), separatorPosition).trim(); 350 351 try { 352 int releaseNumberInt = Integer.parseInt(releaseNumber); 353 if(releaseNumberInt > jahiaInstalledRelease) { 354 try { 355 String completeSqlCode = sqlCode.substring(separatorPosition+1, sqlCode.length()).trim(); 356 if((completeSqlCode.substring(0,6).equals("CREATE")) || (completeSqlCode.substring(0,4).equals("DROP"))) { 357 dbStatement.executeQuery( completeSqlCode ); 358 } else { 359 dbStatement.executeUpdate( completeSqlCode ); 360 } 361 } catch (SQLException sqle) { 362 } catch (IndexOutOfBoundsException ioobe) { 363 } 364 } 365 } catch (NumberFormatException nfe) { 366 } 367 } 368 } catch (IndexOutOfBoundsException ioobe) { 369 } 370 } 371 372 dbStatement.close(); 373 toConsole("Upgrade database is done.\n"); 374 375 } catch (ClassNotFoundException cnfe) { 376 toConsole(jahiaDBDriver + " driver class not found."); 378 } catch (SQLException sqle) { 379 toConsole("Can't connect to database or connection refused."); 381 toConsole("Jahia must be configured if you want to make an upgrade."); 382 } catch (Throwable t) { 383 t.printStackTrace(); 384 } 385 } 387 388 389 394 private static void changeJahiaPropertiesFile() 395 { 396 Enumeration eachLines = readLines(launchDirectoryPath + File.separator + "upgrade" + File.separator + "jahia_properties.upgrade"); 397 398 while(eachLines.hasMoreElements()) { 399 try 400 { 401 String property = (String ) eachLines.nextElement(); 402 if(property.trim().length() > 0) 403 { 404 int separatorPosition = property.indexOf(SCRIPT_SEPARATOR); 405 String releaseNumber = property.substring(SCRIPT_PREFIX.length(), separatorPosition).trim(); 406 407 try { 408 int releaseNumberInt = Integer.parseInt(releaseNumber); 409 if(releaseNumberInt > jahiaInstalledRelease) { 410 String propertyName = property.substring(separatorPosition+1, property.indexOf("=")).trim(); 411 String propvalue = property.substring(property.indexOf("=")+1, property.length()).trim(); 412 413 } 416 } catch (NumberFormatException nfe) { 417 } 418 } 419 } catch (IndexOutOfBoundsException ioobe) { 420 } 421 } 422 } 424 425 426 431 private static void writeReleaseNumber() 432 { 433 try 434 { 435 String releaseNumberFilePath = launchDirectoryPath + File.separator + "upgrade" + File.separator + "release.upgrade"; 436 Properties releaseNumberProperties = new Properties (); 437 FileInputStream inputStream = new FileInputStream (releaseNumberFilePath); 438 439 releaseNumberProperties.load(inputStream); 440 inputStream.close(); 441 442 newReleaseNumber = releaseNumberProperties.getProperty("release"); 444 445 updatepropvalue( "release", newReleaseNumber, jahiaPropertiesFilePath ); 447 448 } catch (IOException ioe) { 449 } 450 } 452 453 454 462 private static Enumeration readLines( String filePath ) 463 { 464 Vector allLines = new Vector (); 465 466 try 467 { 468 BufferedReader buffered = new BufferedReader ( new FileReader (filePath) ); 469 String buffer = ""; 470 471 while((buffer = buffered.readLine()) != null) { 472 if(buffer.trim().length() > 0) { 473 allLines.add(buffer); 474 } 475 } 476 buffered.close(); 477 } catch (IOException ioe) { 478 } 479 480 return allLines.elements(); 481 } 483 484 485 492 private static String generatePathFile( String entryName ) 493 { 494 StringBuffer thePath = new StringBuffer (entryName.length()); 495 for(int i=0; i<entryName.length(); i++) { 496 if(entryName.charAt(i) == '/') { 497 thePath.append(File.separator); 498 } else { 499 thePath.append(entryName.charAt(i)); 500 } 501 } 502 return(thePath.toString()); 503 } 504 505 506 507 512 private static void copyStream( InputStream inputStream, 513 OutputStream outputStream ) 514 throws IOException 515 { 516 int bufferRead; 517 int bufferSize = 1024; 518 byte[] writeBuffer = new byte[bufferSize]; 519 520 BufferedOutputStream bufferedStream = new BufferedOutputStream (outputStream, bufferSize); 521 while((bufferRead = inputStream.read(writeBuffer)) != -1) 522 523 bufferedStream.write(writeBuffer, 0, bufferRead); 524 bufferedStream.flush(); 525 bufferedStream.close(); 526 527 outputStream.flush(); 528 outputStream.close(); 529 } 531 532 533 539 private static void deleteFile( File theFile ) 540 { 541 if(theFile.isDirectory()) 542 { 543 File [] filesInThisDirectory = theFile.listFiles(); 544 for(int i=0; i<filesInThisDirectory.length; i++) { 545 if(filesInThisDirectory[i].isFile()) { 546 filesInThisDirectory[i].delete(); 547 } else { 548 deleteFile(filesInThisDirectory[i]); 549 } 550 } 551 } 552 theFile.delete(); 553 } 555 556 557 566 private static void updatepropvalue( String propertyName, 567 String propvalue, 568 String path ) 569 { 570 Vector bufferVector = new Vector (); 571 String lineReaded = null; 572 int position = 0; 573 boolean lineFound = false; 574 575 try 576 { 577 BufferedReader buffered = new BufferedReader ( new FileReader (path) ); 579 while((lineReaded = buffered.readLine()) != null) { 580 if(lineReaded.indexOf(propertyName) >= 0) { 581 position = lineReaded.lastIndexOf("="); 582 if(position >= 0) { 583 bufferVector.add( lineReaded.substring(0,position+1) + " " + propvalue ); 584 lineFound = true; 585 } 586 } else { 587 bufferVector.add(lineReaded); 588 } 589 } 590 buffered.close(); 591 592 if(!lineFound) 594 { 595 bufferVector.add( propertyName + " = " + propvalue ); 596 } 597 598 File thisFile = new File (path); 600 FileWriter fileWriter = new FileWriter ( thisFile ); 601 StringBuffer outputBuffer = new StringBuffer (); 602 603 for(int i=0; i < bufferVector.size(); i++) { 604 outputBuffer.append((String ) bufferVector.get(i)); 605 } 606 607 fileWriter.write( outputBuffer.toString() ); 608 fileWriter.close(); 609 } catch (java.io.IOException ioe) { 610 } 611 } 613 614 615 621 private static void toConsole( String msg ) 622 { 623 } 626 627 628 633 private static void startupConsole() 634 { 635 StringBuffer msg = new StringBuffer ("\n\n\n"); 636 msg.append(" ____.\n"); 637 msg.append(" __/\\ ______| |__/\\. _______\n"); 638 msg.append(" __ .____| | \\ | +----+ \\\n"); 639 msg.append(" _______| /--| | | - \\ _ | : - \\_________\n"); 640 msg.append(" \\\\______: :---| : : | : | \\________>\n"); 641 msg.append(" |__\\---\\_____________:______: :____|____:_____\\\n"); 642 msg.append(" /_____|\n"); 643 msg.append("\n"); 644 msg.append(" . . . j a h i a u p g r a d e . . .\n"); 645 msg.append("\n\n"); 646 msg.append(" Copyright 2002 - Jahia Ltd http://www.jahia.org - all rights reserved\n"); 647 msg.append("\n\n"); 648 System.out.println(msg.toString()); 649 } 651 652 653 658 private static void shutdownConsole() 659 { 660 } 662 663 664 } | Popular Tags |