Author: [email protected]
Date: Wed Jun 13 09:40:06 2012
New Revision: 2484

Log:
[AMDATUCASSANDRA-204] Added zip support

Modified:
   
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CassandraInstallerImpl.java
   
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java

Modified: 
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CassandraInstallerImpl.java
==============================================================================
--- 
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CassandraInstallerImpl.java
  (original)
+++ 
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CassandraInstallerImpl.java
  Wed Jun 13 09:40:06 2012
@@ -91,7 +91,7 @@
         else {
             m_logService.log(LogService.LOG_INFO, "Existing Cassandra 
installation found. Installed version: "
                 + m_configurator.getInstalledVersion());
-            
+
             // If no update is required, still some configuration property 
could be changed and
             // so we will need to rewrite the cassandra.yaml and 
log4server.properties
             if (!updateCassandraConfig(m_configurator.getInstalledVersion())) {
@@ -124,6 +124,7 @@
         try {
             File downloadedFile = download(downloadUrl, 
m_configurator.getWorkDirectory());
             m_logService.log(LogService.LOG_INFO, "Deflating file...");
+            long timer = System.currentTimeMillis();
 
             // The tmp directory is '[prefix][installed_version]-tmp'
             File tmpDir = 
m_configurator.getTargetCassandraHome(downloadVersion + "-tmp");
@@ -137,7 +138,8 @@
             File targetDir = 
m_configurator.getTargetCassandraHome(downloadVersion);
             srcDir.renameTo(targetDir);
             FileUtils.deleteDirectory(tmpDir);
-            m_logService.log(LogService.LOG_INFO, "Deflating finished 
successfully.");
+            String diff = (System.currentTimeMillis() - timer) / 1000 + " 
seconds";
+            m_logService.log(LogService.LOG_INFO, "Deflating finished 
successfully in " + diff + ".");
         }
         catch (IOException e) {
             m_logService.log(LogService.LOG_ERROR, "Unable to download and 
decompress file from '" + downloadUrl
@@ -148,7 +150,7 @@
         // Step 1d. Update the cassandra.yaml and log4-jserver.properties and 
return the result
         return updateCassandraConfig(downloadVersion);
     }
-    
+
     public boolean updateCassandraConfig(String version) {
         // Step 1c. Find the cassandra.yaml and log4j-server.properties files 
and update them
         File downloadedDir = m_configurator.getTargetCassandraHome(version);

Modified: 
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java
==============================================================================
--- 
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java
 (original)
+++ 
branches/amdatu-cassandra-0.2.5/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java
 Wed Jun 13 09:40:06 2012
@@ -23,8 +23,11 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
 
 /**
@@ -48,6 +51,10 @@
             deleteFiles.add(currentFile);
             currentFile = CompressionUtil.decompressTar(currentFile, 
targetDir);
         }
+        if (currentFile.getName().endsWith(".zip")) {
+            deleteFiles.add(currentFile);
+            currentFile = CompressionUtil.decompressZip(currentFile, 
targetDir);
+        }
         for (File deleteFile : deleteFiles) {
             deleteFile.delete();
         }
@@ -111,7 +118,7 @@
             while ((tarEntry = tis.getNextTarEntry()) != null) {
                 File destFile = new File(targetDir, tarEntry.getName());
                 if (!tarEntry.isDirectory()) {
-                    deflateTarEntry(tis, destFile);
+                    deflateArchiveEntry(tis, destFile);
                 }
                 else {
                     destFile.mkdirs();
@@ -125,9 +132,43 @@
         }
         return targetDir;
     }
+    
+    /**
+     * Decompress a zip file.
+     * 
+     * @param file zip file to decompress
+     * @return Directory containing the decompressed files
+     * @throws IOException
+     */
+    public static File decompressZip(File file, File dir) throws IOException {
+        String targetFileName = file.getName().substring(0, 
file.getName().lastIndexOf(".zip"));
+        File targetDir = new File(dir.getAbsolutePath(), targetFileName);
+        targetDir.mkdir();
+        ZipArchiveInputStream zis = null;
+        try {
+            FileInputStream fis = new FileInputStream(file);
+            zis = new ZipArchiveInputStream(fis);
+            ArchiveEntry tarEntry = null;
+            while ((tarEntry = zis.getNextEntry()) != null) {
+                File destFile = new File(targetDir, tarEntry.getName());
+                if (!tarEntry.isDirectory()) {
+                    deflateArchiveEntry(zis, destFile);
+                }
+                else {
+                    destFile.mkdirs();
+                }
+            }
+        }
+        finally {
+            if (zis != null) {
+                zis.close();
+            }
+        }
+        return targetDir;
+    }
 
     // Deflate a single tar entry
-    private static void deflateTarEntry(TarArchiveInputStream tin, File file) 
throws IOException {
+    private static void deflateArchiveEntry(ArchiveInputStream tin, File file) 
throws IOException {
         FileOutputStream fout = null;
         try {
             fout = new FileOutputStream(file);
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to