[Yanel-commits] rev 26671 - in public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder: . utils

simon at wyona.com simon at wyona.com
Fri Aug 10 17:18:38 CEST 2007


Author: simon
Date: 2007-08-10 17:18:38 +0200 (Fri, 10 Aug 2007)
New Revision: 26671

Modified:
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/UpdateFinder.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/utils/WarFetcher.java
Log:
made warfetcher starts in a new thread. add progressbar which is indacating how long it takes

Modified: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/UpdateFinder.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/UpdateFinder.java	2007-08-10 13:33:54 UTC (rev 26670)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/UpdateFinder.java	2007-08-10 15:18:38 UTC (rev 26671)
@@ -29,6 +29,7 @@
 import java.io.FileInputStream;
 import java.io.InputStream;
 
+import java.net.HttpURLConnection;
 import java.net.URL;
 
 import java.util.ArrayList;
@@ -57,6 +58,7 @@
 import javax.xml.transform.stream.StreamSource;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -68,6 +70,8 @@
 
 import com.hp.hpl.jena.rdf.model.*;
 
+
+
 /**
  * 
  */
@@ -209,14 +213,34 @@
      * Generate screen
      */
     private String getScreen() {
+        
+        StringBuffer body = new StringBuffer();
+        StringBuffer head = new StringBuffer();
+        Enumeration parameters = request.getParameterNames();
+        if (!parameters.hasMoreElements()) {
+            plainRequest(body);
+        } else {
+            if (request.getParameter("save-as") != null) {
+                plainRequest(body);
+            } else if (request.getParameter("usecase") != null && request.getParameter("usecase").equals("update")) {
+                getUpdateConfirmScreen(body);
+            } else if (request.getParameter("usecase") != null && request.getParameter("usecase").equals("updateconfirmed")) {    
+                getUpdateScreen(body,head);
+            } else {
+                log.info("Fallback ...");
+                plainRequest(body);
+            }
+        }
+        
         StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
         sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
         sb.append("<head>");
         sb.append("<title>Yanel Updater</title>");
 
-/*
+
         sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\""
-                + PathUtil.getGlobalHtdocsPath(this) + "yanel-css/progressBar.css\"/>");
+                + PathUtil.getGlobalHtdocsPath(this) + "yanel-css/progressBarTerminated.css\"/>");
+/*        
         sb.append("<script src=\"" + PathUtil.getGlobalHtdocsPath(this)
                 + "yanel-js/prototype.js\" type=\"text/javascript\"></script>");
         sb.append("<script src=\"" + PathUtil.getGlobalHtdocsPath(this)
@@ -236,27 +260,13 @@
             }
         }
 */
+        sb.append(head);
 
         sb.append("</head>");
         sb.append("<body>");
         sb.append("<h1>Yanel Updater</h1>");
 
-        StringBuffer body = new StringBuffer();
-        Enumeration parameters = request.getParameterNames();
-        if (!parameters.hasMoreElements()) {
-            body = plainRequest();
-        } else {
-            if (request.getParameter("save-as") != null) {
-                body = plainRequest();
-            } else if (request.getParameter("usecase") != null && request.getParameter("usecase").equals("update")) {
-                body = getUpdateConfirmScreen();
-            } else if (request.getParameter("usecase") != null && request.getParameter("usecase").equals("updateconfirmed")) {    
-                body = getUpdateScreen();
-            } else {
-                log.info("Fallback ...");
-                body = plainRequest();
-            }
-        }
+
         sb.append(body);
 
         sb.append("</body>");
@@ -267,14 +277,15 @@
     /**
      *
      */
-    private StringBuffer plainRequest() {
+    private void plainRequest(StringBuffer htmlBodyContent) {
 
         InstallInfo installInfo = null;
         try {
             installInfo = getInstallInfo();
         } catch (Exception e) {
             log.error(e.getMessage(), e);
-            return new StringBuffer("<p>Could not get install information. " + e.getMessage() + "</p>");
+            htmlBodyContent.append("<p>Could not get install information. " + e.getMessage() + "</p>");
+            return;
         }
 
         UpdateInfo updateInfo = null;
@@ -282,16 +293,17 @@
             updateInfo = getUpdateInfo();
         } catch (Exception e) {
             log.error(e.getMessage(), e);
-            return new StringBuffer("<p>Could not get update information. " + e.getMessage() + "</p>");
+            htmlBodyContent.append("<p>Could not get update information. " + e.getMessage() + "</p>");
+            return;
         }
 
         if (!installInfo.getInstalltype().equals("bin-snapshot")) {
-            return new StringBuffer("<p>This Yanel was not installed from binary. You can only use the updater if you installed yanel from binary. Please use Subversion or get another source snapshot.</p><p>NOTE: In order to enhance the Yanel Updater resource developers might want to modify <a href=\"file://" + installInfo.getInstallRdfFilename() + "\">" + installInfo.getInstallRdfFilename() + "</a> by replacing the installtype \"source\" with \"bin-snapshot\" and also customize the version and revision!</p>");
+            htmlBodyContent.append("<p>This Yanel was not installed from binary. You can only use the updater if you installed yanel from binary. Please use Subversion or get another source snapshot.</p><p>NOTE: In order to enhance the Yanel Updater resource developers might want to modify <a href=\"file://" + installInfo.getInstallRdfFilename() + "\">" + installInfo.getInstallRdfFilename() + "</a> by replacing the installtype \"source\" with \"bin-snapshot\" and also customize the version and revision!</p>");
+            return;
         }
 
         String idVersionRevisionCurrent = installInfo.getId() + "-v-" + installInfo.getVersion() + "-r-" + installInfo.getRevision();
 
-        StringBuffer htmlBodyContent = new StringBuffer();
         // show installed version
         htmlBodyContent.append("<p>");
         htmlBodyContent.append("Your installed yanel is: " + installInfo.getId() + "-v-" + installInfo.getVersion() + "-r-" + installInfo.getRevision());
@@ -388,14 +400,12 @@
             htmlBodyContent.append("<p>Could not get installed versions. " + e.getMessage() + "</p>");
             //return;
         }
-        return htmlBodyContent;
     }
 
     /**
      *
      */
-    private StringBuffer getUpdateConfirmScreen() {
-        StringBuffer htmlBodyContent = new StringBuffer();
+    private void getUpdateConfirmScreen(StringBuffer htmlBodyContent) {
         try {
             UpdateInfo updateInfo = getUpdateInfo();
             InstallInfo installInfo = getInstallInfo();
@@ -444,34 +454,64 @@
             log.error(e.getMessage(), e);
             htmlBodyContent.append("<p>An error occoured. Exception: " + e.getMessage() + "</p>");
         }
-        return htmlBodyContent;
     }
     
     /**
      *
      */
-    private StringBuffer getUpdateScreen() {
-        StringBuffer htmlBodyContent = new StringBuffer();
+    private void getUpdateScreen(StringBuffer htmlBodyContent, StringBuffer head) {
+        HttpSession session = request.getSession();
         try {
             String destDir = request.getSession().getServletContext().getRealPath(".") + File.separator + "..";
             Map bestUpdater = getBestUpdater();
             
-            WarFetcher warFetcher = new WarFetcher(request, (String) bestUpdater.get("updateLink"), destDir);
-            warFetcher.fetch();
+            URL updaterURL = new URL("http://" + request.getServerName() + ":" + request.getServerPort() + "/" + bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision"));
+            HttpURLConnection updaterURLConn = (HttpURLConnection)updaterURL.openConnection();
+            if (updaterURLConn.getResponseCode() == 200) {
+                session.removeAttribute(WarFetcher.SESSION_ATTR_TASK);
+                session.removeAttribute(WarFetcher.SESSION_ATTR_PROGRESS);
+                session.removeAttribute(WarFetcher.SESSION_ATTR_ITEMS_DONE);
+                session.removeAttribute(WarFetcher.SESSION_ATTR_ITEMS_TO_BE_DONE);
+                head.append("<meta http-equiv=\"refresh\" content=\"0; URL=http://" + request.getServerName() + ":" + request.getServerPort() + "/" + bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision") + "/?updatelink=" + request.getParameter("updatelink") + "&amp;requestingwebapp=" + request.getParameter("requestingwebapp") + "\"/>");
+                htmlBodyContent.append("<p>Update-Manager has been downloaded and installed.</p>");
+                htmlBodyContent.append("<p>You will be <a href=\""+"http://" + request.getServerName() + ":" + request.getServerPort() + "/" + bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision") + "/?updatelink=" + request.getParameter("updatelink") + "&amp;requestingwebapp=" + request.getParameter("requestingwebapp") + ""+"\">redirected</a> to the update-manager which will automatically download and install the requested yanel.</p>");
+                return;
+            }
             
+            if (session.getAttribute(WarFetcher.SESSION_ATTR_TASK) == null ){
+                Runnable runFetcher = new WarFetcher(request, (String) bestUpdater.get("updateLink"), destDir);
+                new Thread(runFetcher).start();
+                session.setAttribute(WarFetcher.SESSION_ATTR_TASK, "started");
+                session.setAttribute(WarFetcher.SESSION_ATTR_PROGRESS, "0");
+            }
+            
             //TODO here it should set a password for the updater
             
-            TomcatContextHandler tomcatContextHandler = new TomcatContextHandler(request);
-            tomcatContextHandler.setContext(bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision"), bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision"));
+            if (session.getAttribute(WarFetcher.SESSION_ATTR_TASK) != null && session.getAttribute("org.wyona.yanel.updater.warfetcher.task").equals("loading")) {
+                TomcatContextHandler tomcatContextHandler = new TomcatContextHandler(request);
+                tomcatContextHandler.setContext(bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision"), bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision"));
+                htmlBodyContent.append("<p>Tomcat is loading and startup the update-manager</p>");
+                head.append("<meta http-equiv=\"refresh\" content=\"2; URL=?usecase=updateconfirmed&amp;updatelink=" + request.getParameter("updatelink") + "\"/>");
+            }
             
-            htmlBodyContent.append("<p>Update-Manager has been downloaded and installed.</p>");
-            htmlBodyContent.append("<p>You will be <a href=\""+"http://" + request.getServerName() + ":" + request.getServerPort() + "/" + bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision") + "/?updatelink=" + request.getParameter("updatelink") + "&amp;requestingwebapp=" + request.getParameter("requestingwebapp") + ""+"\">redirected</a> to the update-manager which will automatically download and install the requested yanel.</p>");
-            //String htmlHeadContent = "<meta http-equiv=\"refresh\" content=\"10; URL=" + "http://" + request.getServerName() + ":" + request.getServerPort() + "/" + bestUpdater.get("id") + "-v-" + bestUpdater.get("version") + "-r-" + bestUpdater.get("revision") + "/" + "?updatelink=" + request.getParameter("updatelink") + "&amp;requestingwebapp=" + request.getParameter("requestingwebapp") + "\"/>";
+            if (session.getAttribute(WarFetcher.SESSION_ATTR_TASK) != null && !session.getAttribute("org.wyona.yanel.updater.warfetcher.task").equals("loading")) {
+                head.append("<meta http-equiv=\"refresh\" content=\"2; URL=?usecase=updateconfirmed&amp;updatelink=" + request.getParameter("updatelink") + "\"/>");
+                htmlBodyContent.append("<p>Working: " + session.getAttribute(WarFetcher.SESSION_ATTR_TASK));
+                if (session.getAttribute(WarFetcher.SESSION_ATTR_ITEMS_DONE) != null) {
+                    if (session.getAttribute(WarFetcher.SESSION_ATTR_TASK).equals("download")) {
+                        htmlBodyContent.append(" " + session.getAttribute(WarFetcher.SESSION_ATTR_ITEMS_DONE) + " bytes of " + session.getAttribute(WarFetcher.SESSION_ATTR_ITEMS_TO_BE_DONE) + " bytes");
+                    }
+                    if (session.getAttribute(WarFetcher.SESSION_ATTR_TASK).equals("extract")) {
+                        htmlBodyContent.append(" " + session.getAttribute(WarFetcher.SESSION_ATTR_ITEMS_DONE) + " items of " + session.getAttribute(WarFetcher.SESSION_ATTR_ITEMS_TO_BE_DONE) + " items");
+                    }
+                }
+                htmlBodyContent.append("</p>");
+                htmlBodyContent.append("<p>Progress: <div id=\"yanelprogressbarterminated\"><div id=\"yanelprogressbarindicatorterminated\" style=\"width:" + session.getAttribute(WarFetcher.SESSION_ATTR_PROGRESS) + "%\">" + session.getAttribute(WarFetcher.SESSION_ATTR_PROGRESS) +"%</div></div></p>");
+            }
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             htmlBodyContent.append("<p>Update failed. Exception: " + e.getMessage() + "</p>");
         }
-        return htmlBodyContent;
     }
 
     private InstallInfo getInstallInfo() throws Exception {

Modified: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/utils/WarFetcher.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/utils/WarFetcher.java	2007-08-10 13:33:54 UTC (rev 26670)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updatefinder/utils/WarFetcher.java	2007-08-10 15:18:38 UTC (rev 26671)
@@ -5,6 +5,8 @@
 package org.wyona.yanel.impl.resources.updatefinder.utils;
 
 import org.apache.log4j.Category;
+import org.apache.tools.ant.taskdefs.Delete;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -15,100 +17,119 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
 import org.apache.commons.io.FileUtils;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.net.JarURLConnection;
 
 
-public class WarFetcher {
+public class WarFetcher implements Runnable {
     
     private static Category log = Category.getInstance(WarFetcher.class);
     //private String contextDirectoryPath;
     private String DestinationDirectoryPath;
     private String updateLink;
     //private HttpServletRequest request;
+    private HttpSession session;
     private InstallInfo installInfo;
+    public final static String SESSION_ATTR_TASK = "org.wyona.yanel.updater.warfetcher.task";
+    public final static String SESSION_ATTR_PROGRESS = "org.wyona.yanel.updater.warfetcher.progress";
+    public final static String SESSION_ATTR_ITEMS_DONE = "org.wyona.yanel.updater.warfetcher.itemsdone";
+    public final static String SESSION_ATTR_ITEMS_TO_BE_DONE = "org.wyona.yanel.updater.warfetcher.itemstobedone";
     
-    
     public WarFetcher(HttpServletRequest request, String updateLink, String destDir) throws Exception {
         //this.request = request;
+        this.session = request.getSession();
         //this.contextDirectoryPath = request.getSession().getServletContext().getRealPath(".");
         this.DestinationDirectoryPath = destDir;
         this.updateLink = updateLink;
         this.installInfo = new InstallInfo(request);
     }
     
+    public void run(){
+        try {
+            fetch();
+        } catch (Exception e) {
+            log.error("Error occoured while try to install war: " + e.getMessage() + e);
+        }
+    }
+    
     public void fetch() throws Exception {
         downloadUpdateWar(updateLink);
     }
     
-    //private File getInstalledWar() {
-    //    return new File(contextDirectoryPath + File.separator + ".." + File.separator + installInfo.getContextPrefix() + ".war");
-    //}
-    
-//    private File[] getUpdateProtectedFiles() {
-//        ArrayList FileNames = installInfo.getProtectedFiles();
-//        File[] protectedFiles = new File [FileNames.size()];
-//        for (int i = 0; i < FileNames.size(); i++) {
-//            File protectedFile = new File(contextDirectoryPath + File.separator + ((String) FileNames.get(i)));
-//            protectedFiles[i] = protectedFile; 
-//        }
-//        return protectedFiles;
-//    }
-    
-//    private void backUpProtected() {
-//        CreateZip protectedFile = new CreateZip();
-//        String destFileName = contextDirectoryPath + File.separator + ".." + File.separator + "yanel-conf-" + installInfo.getVersion() + ".zip";
-//        protectedFile.create(destFileName, getUpdateProtectedFiles(), installInfo.getProtectedFiles());
-//    }
-    
-    //private void backUpWar() throws java.io.IOException{
-    //    FileUtils.copyFile(getInstalledWar(), new File(contextDirectoryPath + File.separator + ".." + File.separator + "yanel-" + installInfo.getVersion() + ".jar"));
-    //}
-    
     private void downloadUpdateWar(String updateLink) throws Exception {
-        URL updateWarUrl = new URL("jar:" + updateLink + "!/");
-        //InputStream updateWarIn = updateWarUrl.openStream();
+        URL updateWarUrl = new URL(updateLink);
+        InputStream updateWarIn = updateWarUrl.openStream();
+        int targetContentLength = updateWarUrl.openConnection().getContentLength();
+        int currentContentLength = 0;
+        int currentPercentage = 0;
 
-        JarURLConnection URLcon=(JarURLConnection)(updateWarUrl.openConnection());
-        JarFile jar=URLcon.getJarFile();
-        
+        session.setAttribute(SESSION_ATTR_TASK, "download");
+        session.setAttribute(SESSION_ATTR_PROGRESS, "0");
+        session.setAttribute(SESSION_ATTR_ITEMS_DONE, "0");
+        session.setAttribute(SESSION_ATTR_ITEMS_TO_BE_DONE, ""+targetContentLength);
+
         UpdateInfo updateInfo = new UpdateInfo(installInfo.getUpdateURL(), installInfo);
         HashMap versionDetails = updateInfo.getUpdateVersionDetail("updateLink", updateLink);
         String version = (String) versionDetails.get("version");
         String revision = (String) versionDetails.get("revision");
         String id = (String) versionDetails.get("id");
+
+        OutputStream out = new FileOutputStream(DestinationDirectoryPath + File.separator + id + "-v-" + version + "-r-" + revision + ".war");
+    
+        byte[] buf = new byte[1024];
+        int len;
+        while ((len = updateWarIn.read(buf)) > 0) {
+            out.write(buf, 0, len);
+            currentContentLength = currentContentLength + 1024;
+            currentPercentage = currentContentLength / (targetContentLength / 100);
+            session.setAttribute(SESSION_ATTR_PROGRESS, "" + currentPercentage);
+            session.setAttribute(SESSION_ATTR_ITEMS_DONE, ""+currentContentLength);
+        }
+        updateWarIn.close();
+        out.close();
+        extractJar(new JarFile(DestinationDirectoryPath + File.separator + id + "-v-" + version + "-r-" + revision + ".war"), DestinationDirectoryPath + File.separator + id + "-v-" + version + "-r-" + revision);
         
-        extractJar(jar, DestinationDirectoryPath + File.separator + id + "-v-" + version + "-r-" + revision);
-//        OutputStream out = new FileOutputStream(contextDirectoryPath + File.separator + ".." + File.separator + id + "-r" + version + ".war");
-//    
-//        byte[] buf = new byte[1024];
-//        int len;
-//        while ((len = updateWarIn.read(buf)) > 0) {
-//            out.write(buf, 0, len);
-//        }
-//        updateWarIn.close();
-//        out.close();
+        new File(DestinationDirectoryPath + File.separator + id + "-v-" + version + "-r-" + revision + ".war").delete();
+        
+        session.setAttribute(SESSION_ATTR_TASK, "loading");
+        session.setAttribute(SESSION_ATTR_PROGRESS, "0");
     }
     
     private void extractJar(JarFile jar, String destDir) throws Exception {
+        int targetContentLength = jar.size();
+        int currentContentLength = 0;
+        int currentPercentage = 0;
+
+        session.setAttribute(SESSION_ATTR_TASK, "extract");
+        session.setAttribute(SESSION_ATTR_PROGRESS, "0");
+        session.setAttribute(SESSION_ATTR_ITEMS_DONE, "0");
+        session.setAttribute(SESSION_ATTR_ITEMS_TO_BE_DONE, ""+targetContentLength);
+        
         new File(destDir).mkdir();
         java.util.Enumeration entries = jar.entries();
-            while (entries.hasMoreElements()) {
-                java.util.jar.JarEntry file = (java.util.jar.JarEntry) entries.nextElement();
-                java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
-                if (file.isDirectory()) { // if its a directory, create it
-                    f.mkdir();
-                    continue;
-                }
-                java.io.InputStream is = jar.getInputStream(file); // get the input stream
-                java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
-                while (is.available() > 0) { // write contents of 'is' to 'fos'
-                    fos.write(is.read());
-                }
-                fos.close();
-                is.close();
+        while (entries.hasMoreElements()) {
+            currentContentLength++;
+            currentPercentage = currentContentLength / (targetContentLength / 100);
+            session.setAttribute(SESSION_ATTR_PROGRESS, "" + currentPercentage);
+            session.setAttribute(SESSION_ATTR_ITEMS_DONE, "" +currentContentLength);
+                
+            java.util.jar.JarEntry file = (java.util.jar.JarEntry) entries.nextElement();
+            java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
+            if (file.isDirectory()) { // if its a directory, create it
+                f.mkdir();
+                continue;
             }
+            java.io.InputStream is = jar.getInputStream(file); // get the input stream
+            java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
+            while (is.available() > 0) { // write contents of 'is' to 'fos'
+                fos.write(is.read());
+            }
+            fos.close();
+            is.close();
+        }
     }
 }
\ No newline at end of file



More information about the Yanel-commits mailing list