[Yanel-commits] rev 25695 - 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
Mon Jul 2 12:52:41 CEST 2007


Author: simon
Date: 2007-07-02 12:52:40 +0200 (Mon, 02 Jul 2007)
New Revision: 25695

Added:
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateJar.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateZip.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/InstallInfo.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/TomcatContextHandler.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfo.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfoVersionComparator.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/VersionComparator.java
   public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/WarFetcher.java
Log:
refactored package structure

Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateJar.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateJar.java	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateJar.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+public class CreateJar {
+
+    File file;
+    JarOutputStream jarOutput;
+    String source;
+    Manifest manifest = null;
+
+    public CreateJar(String destPath, String source) {
+        this.source = source;
+        try {
+            manifest = new Manifest();
+            jarOutput = new JarOutputStream(new FileOutputStream(destPath),
+                    manifest);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * This method is used to obtain the list of files present in a directory
+     * @param path of type String specifying the path of directory containing the files
+     * @return the list of files from a particular directory
+     */
+    public File[] getFiles(String path) {// This method is used to obtain the list of files in a
+                                            // directory
+        try {
+            file = new File(path);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return file.listFiles();
+    }
+
+    /**
+     * This method is used to create a jar file from a directory
+     * @param path of type String specifying the directory to make jar
+     */
+    public void createJar(String path) {// This method is used to create a jar file from
+    // a directory. If the directory contains several nested directory
+    // it will work.
+        try {
+            byte[] buff = new byte[2048];
+            File[] fileList = getFiles(path);
+
+            for (int i = 0; i < fileList.length; i++) {
+                if (fileList[i].isDirectory()) {
+                    createJar(fileList[i].getAbsolutePath());// Recusive method to get the files
+                } else {
+                    FileInputStream fin = new FileInputStream(fileList[i]);
+                    String temp = fileList[i].getAbsolutePath();
+                    String subTemp = temp.substring(temp.indexOf("bin") + 4, temp.length());
+                    // System.out.println( subTemp+":"+fin.getChannel().size());
+                    jarOutput.putNextEntry(new JarEntry(subTemp));
+                    int len;
+                    while ((len = fin.read(buff)) > 0) {
+                        jarOutput.write(buff, 0, len);
+                    }
+                    fin.close();
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Method used to close the object for JarOutputStream
+     */
+    public void close() {// This method is used to close the
+    // JarOutputStream
+        try {
+            jarOutput.flush();
+            jarOutput.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}
\ No newline at end of file

Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateZip.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateZip.java	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/CreateZip.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.ArrayList;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+public class CreateZip {
+
+    File files[];
+    ZipOutputStream zipOutput;
+
+    public void create(String destFile, File[] files, ArrayList originalPath) {
+        this.files = files;
+        try {
+            zipOutput = new ZipOutputStream(new FileOutputStream(destFile));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        createZip(originalPath);
+        close();
+    }
+    
+    public void createZip(ArrayList originalPath) {
+        try {
+            byte[] buff = new byte[10240];
+            File[] fileList = files;
+
+            for (int i = 0; i < fileList.length; i++) {
+                    FileInputStream fin = new FileInputStream(fileList[i]);
+                    zipOutput.putNextEntry(new ZipEntry(((String) originalPath.get(i))));
+                    int len;
+                    while ((len = fin.read(buff)) > 0) {
+                        zipOutput.write(buff, 0, len);
+                    }
+                    fin.close();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Method used to close the object for ZipOutputStream
+     */
+    public void close() {
+        try {
+            zipOutput.flush();
+            zipOutput.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}
\ No newline at end of file

Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/InstallInfo.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/InstallInfo.java	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/InstallInfo.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import org.apache.log4j.Category;
+import java.io.File;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+
+import com.hp.hpl.jena.rdf.model.*;
+import com.hp.hpl.jena.vocabulary.*;
+import com.hp.hpl.jena.rdf.model.impl.PropertyImpl;
+import javax.servlet.http.HttpServletRequest;
+/**
+ * 
+ */
+public class InstallInfo {
+    
+    private static Category log = Category.getInstance(InstallInfo.class);
+    private String id;
+    private String version;
+    private String revision;
+    private String installtype;
+    private String contextPrefix;
+    private String updateURL;
+    private String osName; //platform
+    private String javaVersion;
+    private String targetApplicationId;
+    private String targetApplicationVersion;
+    private ArrayList protectedFiles = new ArrayList();
+    
+    private String updateManagerNS = "http://www.wyona.org/update-manager/1.0#"; 
+    
+    public InstallInfo(HttpServletRequest request)  throws java.io.FileNotFoundException{
+        String WEBINFPath = request.getSession().getServletContext().getRealPath("WEB-INF");
+        contextPrefix = request.getSession().getServletContext().getServletContextName();
+        if (contextPrefix.equalsIgnoreCase("ROOT")) {
+            contextPrefix = "ROOT";
+        } else {
+            contextPrefix = contextPrefix.toLowerCase();
+        }
+        InputStream installRdfIn = new FileInputStream(new File(WEBINFPath + File.separator + "classes" + File.separator + "install.rdf"));
+        Model model = ModelFactory.createDefaultModel();
+        //read the RDF/XML file
+        model.read(installRdfIn, "");
+        parseModel(model);
+        
+        setServerInfoDetail(request);
+        
+        osName = System.getProperty("os.name");
+        javaVersion = System.getProperty("java.version");
+    }
+
+    private void parseModel(Model model) {
+        Resource install = model.getResource("urn:wyona:application:install");
+        
+        Property idProperty = new PropertyImpl(updateManagerNS, "id");
+        id = install.getRequiredProperty(idProperty).getString();
+        Property versionProperty = new PropertyImpl(updateManagerNS, "version");
+        version = install.getRequiredProperty(versionProperty).getString();
+        Property revisionProperty = new PropertyImpl(updateManagerNS, "revision");
+        revision = install.getRequiredProperty(revisionProperty).getString();
+        Property installtypeProperty = new PropertyImpl(updateManagerNS, "installtype");
+        installtype = install.getRequiredProperty(installtypeProperty).getString();
+        //Property contextPrefixProperty = new PropertyImpl(updateManagerNS, "contextprefix");
+        //contextPrefix = install.getRequiredProperty(contextPrefixProperty).getString();
+
+        Property updateURLProperty = new PropertyImpl(updateManagerNS, "updateURL");
+        updateURL = install.getRequiredProperty(updateURLProperty).getString();
+
+        Property protectedFilesProperty = new PropertyImpl(updateManagerNS, "protectedFiles");
+        Seq protectedFilesSeq = install.getRequiredProperty(protectedFilesProperty).getSeq();
+        
+        NodeIterator protectedFilesIter = protectedFilesSeq.iterator();
+        while (protectedFilesIter.hasNext()) {
+            protectedFiles.add(protectedFilesIter.next().toString());
+        }
+        
+        /*Property targetApplicationProperty = new PropertyImpl(updateManagerNS, "targetApplication");
+        Resource targetApplication = install.getProperty(targetApplicationProperty).getResource();
+        
+        Property targetApplicationIdProperty = new PropertyImpl(updateManagerNS, "id");
+        targetApplicationId = targetApplication.getRequiredProperty(targetApplicationIdProperty).getString();
+        Property targetApplicationVersionProperty = new PropertyImpl(updateManagerNS, "version");
+        targetApplicationVersion = targetApplication.getRequiredProperty(targetApplicationVersionProperty).getString();*/
+        
+    }
+
+    private void setServerInfoDetail(HttpServletRequest request) {
+        //this needs to be implemented for each servlet container since it doesn't seem the string of getServerInfo() is standardized
+        String serverInfo = request.getSession().getServletContext().getServerInfo();
+        
+        if (serverInfo.startsWith("Apache Tomcat")) {
+            targetApplicationId  = serverInfo.split("/")[0];
+            targetApplicationVersion  = serverInfo.split("/")[1];
+        } else {
+            targetApplicationId  = serverInfo.split("/")[0];
+            targetApplicationVersion  = serverInfo.split("/")[1];
+            log.info("the dedection of the servlet container name and version is just a guess. if there is something wrong please implement your servlets getServerInfo() string. thanks :)");
+        }
+        
+    }
+    
+
+    public String getId() {
+        return id;
+    }
+
+    public String getInstalltype() {
+        return installtype;
+    }
+    public String getContextPrefix() {
+        return contextPrefix;
+    }
+
+    public String getUpdateURL() {
+        return updateURL;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public String getRevision() {
+        return revision;
+    }
+
+    public String getJavaVersion() {
+        return javaVersion;
+    }
+
+    public String getOsName() {
+        return osName;
+    }
+
+    public String getTargetApplicationId() {
+        return targetApplicationId;
+    }
+
+    public String getTargetApplicationVersion() {
+        return targetApplicationVersion;
+    }
+
+    public ArrayList getProtectedFiles() {
+        return protectedFiles;
+    }
+    
+}
\ No newline at end of file

Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/TomcatContextHandler.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/TomcatContextHandler.java	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/TomcatContextHandler.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import org.apache.log4j.Category;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
+import javax.servlet.http.HttpServletRequest;
+
+
+
+public class TomcatContextHandler {
+    private static Category log = Category.getInstance(TomcatContextHandler.class);
+    private String webappsDirectoryPath;
+    private File webappsDirectory;
+    private String contextConfPath;
+    private File contextConfDirectory;
+    
+    public TomcatContextHandler(HttpServletRequest request) throws Exception {
+        this.webappsDirectoryPath = request.getSession().getServletContext().getRealPath(".") + File.separator + ".." + File.separator;
+        this.webappsDirectory = new File(webappsDirectoryPath);
+        this.contextConfPath = webappsDirectoryPath  + ".." + File.separator + "conf" + File.separator + "Catalina" + File.separator + "localhost" + File.separator;
+        this.contextConfDirectory = new File(contextConfPath);
+    }
+    
+    public String[] getWebappNames() {
+        String[] webapps = new String[this.webappsDirectory.listFiles().length];
+        for (int i = 0; i < this.webappsDirectory.listFiles().length; i++) {
+            webapps[i] = this.webappsDirectory.listFiles()[i].getName();
+        }
+        return webapps;
+    }
+    
+    public String[] getContextNames() {
+        String[] contexts = new String[this.contextConfDirectory.listFiles().length];
+        for (int i = 0; i < this.contextConfDirectory.listFiles().length; i++) {
+            contexts[i] = this.contextConfDirectory.listFiles()[i].getName().replaceAll(".xml", "");
+            if (contexts[i].equals("ROOT")) {
+                contexts[i] = "/";
+            }
+        }
+        return contexts;
+    }
+    
+    public Map getContextAndWebapp() throws Exception {
+        Map contextAndWebapps = new HashMap();
+        for (int i = 0; i < this.contextConfDirectory.listFiles().length; i++) {
+            String context = this.contextConfDirectory.listFiles()[i].getName().replaceAll(".xml", "");;
+            if (context.equals("ROOT")) {
+                context = "/";
+            }
+            String webapp = getWebappOfContext(context);
+            contextAndWebapps.put(context, webapp);
+        }
+        return contextAndWebapps;
+    }
+    
+    public String getWebappOfContext (String context) throws FileNotFoundException, IOException {
+        File file = new File( contextConfPath +  context);
+        String line = "";
+        String webapp = "";
+
+        FileInputStream fis = new FileInputStream(file);
+        BufferedInputStream  bis = new BufferedInputStream(fis);
+        DataInputStream  dis = new DataInputStream(bis);
+        while (dis.available() != 0) {
+          line = line + dis.readLine();
+        }
+        fis.close();
+        bis.close();
+        dis.close();
+        
+        line = line.replaceAll("[ ]+", " ");
+        line = line.replaceAll("\"/>", "");
+        webapp = line.split(File.separator)[line.split(File.separator).length];
+        
+        return webapp;
+    }
+
+    public void setContext (String context, String webapp) throws Exception, IOException {
+        if (context.equals("/")) {
+            context = "ROOT";
+        }
+        if (!context.equals(webapp) && new  File(contextConfPath + context + ".xml").exists()){
+            log.debug("Its prohibited to modify a context if context name and webapp name are the same.");
+            throw new Exception("Its prohibited to modify a context if context name and webapp name are the same."); 
+        }
+        String contextEntry = "<Context docBase=\"${catalina.home}/yanel-webapps/" + webapp + "\"/>";
+        BufferedWriter out = new BufferedWriter(new FileWriter(contextConfPath + context + ".xml"));
+        out.write(contextEntry);
+        out.close();
+    }
+
+    public void setWebappAsRoot(String webapp) throws Exception {
+        try {
+            setContext ("ROOT", webapp);
+        } catch (Exception e) {
+            log.error("Setting of webapp (" +  webapp + ") as root failed.");
+            throw new Exception("Setting of webapp (" +  webapp + ") as root failed.");
+        }
+    }
+    
+    public void removeWebapp (String webapp, String context) throws Exception {
+        if (context.equals("/") || context.equals("ROOT")) {
+            log.error("Deletion of root context prohibited");
+            throw new Exception("Deletion of root context prohibited. Use setWebappAsRoot(String webapp) instead");
+        }
+        if (!getWebappOfContext(context).equals(webapp)) {
+            log.error("This context (" + context + ") does not point to this webapp (" + webapp + ")");
+            throw new Exception("This context (" + context + ") does not point to this webapp (" + webapp + ")");
+        }
+        boolean success = (new File(contextConfPath + context)).delete();
+        if (!success) {
+            log.error("Deletion of contex file not successful!");
+            throw new Exception("Deletion of contex file (" + contextConfPath + context + ") not successful!");
+        }
+        try {
+            FileUtils.deleteDirectory(new File(webappsDirectoryPath + webapp));
+        } catch (Exception e) {
+            log.error("Deletion of webapp not successful!");
+            throw new Exception("Deletion of webapp (" + webappsDirectoryPath + webapp + ") not successful!" + e);
+        }
+    }
+}
\ No newline at end of file

Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfo.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfo.java	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfo.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,236 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import org.apache.log4j.Category;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+
+import com.hp.hpl.jena.rdf.model.*;
+import com.hp.hpl.jena.vocabulary.*;
+import com.hp.hpl.jena.rdf.model.impl.PropertyImpl;
+import java.util.HashMap;
+
+/**
+ * 
+ */
+public class UpdateInfo {
+    
+    private static Category log = Category.getInstance(UpdateInfo.class);
+    private ArrayList updateVersions = new ArrayList();
+    private Model updateRdfModel;
+    private InstallInfo installInfo;
+    
+    private String updateManagerNS = "http://www.wyona.org/update-manager/1.0#"; 
+    
+    public UpdateInfo(InputStream in, InstallInfo installInfo) throws Exception{
+        if (installInfo == null) {
+            throw new Exception("InstallInfo should not be null");
+        }
+        if (in == null) {
+            throw new Exception("InputStream should not be null");
+        }
+        Model model = ModelFactory.createDefaultModel();
+        //read the RDF/XML file
+        model.read(in, "");
+        this.updateRdfModel = model;
+        this.installInfo = installInfo;
+
+        setUpdateVersions();
+    }
+
+    private void setUpdateVersions() {
+        Resource type = updateRdfModel.getResource("urn:wyona:application");
+        Property typeProperty = new PropertyImpl(updateManagerNS, "type");
+        Property typesProperty = new PropertyImpl(updateManagerNS, "types");
+        Property versionsProperty = new PropertyImpl(updateManagerNS, "versions");
+        Seq types = type.getProperty(typesProperty).getSeq();
+        NodeIterator typeIter = types.iterator();
+        
+        while (typeIter.hasNext()) {
+            Resource typeResource = ((Resource) typeIter.next());
+        
+        //Resource update = updateRdfModel.getResource("urn:wyona:application:updates");
+        
+        Property versionProperty = new PropertyImpl(updateManagerNS, "version");
+        Property revisionProperty = new PropertyImpl(updateManagerNS, "revision");
+        //Seq versions = update.getProperty(versionsProperty).getSeq();
+        Seq versionsSeq = typeResource.getProperty(versionsProperty).getSeq();
+        
+        NodeIterator iter2 = versionsSeq.iterator();
+        Property idProperty = new PropertyImpl(updateManagerNS, "id");
+        Property targetApplicationIdProperty = new PropertyImpl(updateManagerNS, "targetApplicationId");
+        Property targetApplicationminVersionProperty = new PropertyImpl(updateManagerNS, "targetApplicationMinVersion");
+        Property targetApplicationmaxVersionProperty = new PropertyImpl(updateManagerNS, "targetApplicationMaxVersion");
+        Property targetApplicationminRevisionProperty = new PropertyImpl(updateManagerNS, "targetApplicationMinRevision");
+        Property targetApplicationmaxRevisionProperty = new PropertyImpl(updateManagerNS, "targetApplicationMaxRevision");
+        Property changeLogProperty = new PropertyImpl(updateManagerNS, "changelog");
+        Property titleProperty = new PropertyImpl(updateManagerNS, "title");
+        Property updateLinkProperty = new PropertyImpl(updateManagerNS, "updateLink");
+        
+        while (iter2.hasNext()) {
+            Resource versionResource = ((Resource) iter2.next());
+            
+            
+            
+            
+            
+            
+            HashMap updateVersionDetail = new HashMap();
+            updateVersionDetail.put("type", typeResource.getProperty(typeProperty).getString());
+            updateVersionDetail.put("title", versionResource.getProperty(titleProperty).getString());
+            updateVersionDetail.put("id", versionResource.getProperty(idProperty).getString());
+            updateVersionDetail.put("version", versionResource.getProperty(versionProperty).getString());
+            updateVersionDetail.put("revision", versionResource.getProperty(revisionProperty).getString());
+            updateVersionDetail.put("changeLog", versionResource.getProperty(changeLogProperty).getString());
+            updateVersionDetail.put("updateLink", versionResource.getProperty(updateLinkProperty).getString());
+            updateVersionDetail.put("targetApllicationId", versionResource.getProperty(idProperty).getString());
+            updateVersionDetail.put("targetApllicationMinVersion", versionResource.getProperty(targetApplicationminVersionProperty).getString());
+            updateVersionDetail.put("targetApllicationMaxVersion", versionResource.getProperty(targetApplicationmaxVersionProperty).getString());
+            updateVersionDetail.put("targetApllicationMinRevision", versionResource.getProperty(targetApplicationminRevisionProperty).getString());
+            updateVersionDetail.put("targetApllicationMaxRevision", versionResource.getProperty(targetApplicationmaxRevisionProperty).getString());
+            this.updateVersions.add(updateVersionDetail);
+            
+            
+            
+            //check id
+            //if (versionResource.getProperty(idProperty).getString().equals(installInfo.getId())) {
+                //check for targetApplicationId
+//                Seq targetApplicationSeq = versionResource.getProperty(targetApplicationProperty).getSeq();
+//                NodeIterator targetApplicationIter = targetApplicationSeq.iterator();
+//                while (targetApplicationIter.hasNext()) {
+//                    Resource targetApplicationResource = ((Resource) targetApplicationIter.next());
+//                    String test1 = targetApplicationResource.getProperty(idProperty).getString();
+//                    String test2 = installInfo.getTargetApplicationId();
+//                    System.out.println("taid: "+test1 +" install: "+ test2);
+//                    
+//                    //if (targetApplicationResource.getProperty(idProperty).getString().equals(installInfo.getTargetApplicationId())) {
+//
+//                        //check for minorVersion
+//                        //String minVersion = targetApplicationResource.getProperty(minVersionProperty).getString();
+//                        //String installVersion = installInfo.getTargetApplicationVersion();
+//                        //VersionComparator versionComparator = new VersionComparator(); 
+//                        //if (versionComparator.compare(installVersion, minVersion) >= 0) {
+//                            //check for maxVersion
+//                            //String maxVersion = targetApplicationResource.getProperty(maxVersionProperty).getString();
+//                            //if (versionComparator.compare(maxVersion, installVersion) >= 0) {
+//                                HashMap updateVersionDetail = new HashMap();
+//                                updateVersionDetail.put("type", typeResource.getProperty(typeProperty).getString());
+//                                updateVersionDetail.put("title", versionResource.getProperty(titleProperty).getString());
+//                                updateVersionDetail.put("id", versionResource.getProperty(idProperty).getString());
+//                                updateVersionDetail.put("version", versionResource.getProperty(versionProperty).getString());
+//                                updateVersionDetail.put("revision", versionResource.getProperty(revisionProperty).getString());
+//                                updateVersionDetail.put("changeLog", versionResource.getProperty(changeLogProperty).getString());
+//                                updateVersionDetail.put("updateLink", targetApplicationResource.getProperty(updateLinkProperty).getString());
+//                                updateVersionDetail.put("targetApllication", targetApplicationResource.getProperty(idProperty).getString());
+//                                updateVersionDetail.put("targetApllicationMinVersion", targetApplicationResource.getProperty(minVersionProperty).getString());
+//                                updateVersionDetail.put("targetApllicationMaxVersion", targetApplicationResource.getProperty(maxVersionProperty).getString());
+//                                this.updateVersions.add(updateVersionDetail);
+//                            //}
+//                        //}
+//                    //}
+//                //}
+//                }
+            }
+        }
+    }
+
+    public ArrayList getUpdateVersions() {
+        return updateVersions;
+    }
+    
+    public HashMap getUpdateVersionDetail(String key, String value) {
+        for (int i = 0; i < updateVersions.size(); i++) {
+            HashMap versionDetail = (HashMap) updateVersions.get(i);
+            if (versionDetail.get(key).equals(value)) {
+                return versionDetail;
+            }
+        }
+        return null;
+    }    
+
+
+    /**
+     * @return ArrayList with version which are matching the value of the key
+     * @param String key
+     * @param String value 
+     */
+    public ArrayList getUpdateVersionsOf(String key, String value) {
+        ArrayList selectedUpdateVersions = updateVersions;
+        for (int i = 0; i < selectedUpdateVersions.size(); i++) {
+            HashMap versionDetail = (HashMap) selectedUpdateVersions.get(i);
+            if (!versionDetail.get(key).equals(value)) {
+                selectedUpdateVersions.remove(i);
+            }
+        }
+        Collections.sort(selectedUpdateVersions, new UpdateInfoVersionComparator());
+        return selectedUpdateVersions;
+    }
+
+    /**
+     * @return ArrayList with version which are matching the value of the key, and fits in the version requirement
+     * @param String key
+     * @param String value 
+     */
+    public ArrayList getUpdateVersionsOf(String key, String value, String installInfoVersion) {
+        ArrayList selectedUpdateVersions = getUpdateVersionsOf(key, value);
+        VersionComparator versionComparator = new VersionComparator();  
+        for (int i = 0; i < selectedUpdateVersions.size(); i++) {
+            HashMap versionDetail = (HashMap) selectedUpdateVersions.get(i);
+            if (versionComparator.compare((String) versionDetail.get("targetApllicationMinVersion"), installInfoVersion) > 0 ) {
+                selectedUpdateVersions.remove(i);
+            }
+            if (versionComparator.compare((String) versionDetail.get("targetApllicationMaxVersion"), installInfoVersion) < 0 ) {
+                selectedUpdateVersions.remove(i);
+            }
+        }
+        Collections.sort(selectedUpdateVersions, new UpdateInfoVersionComparator());
+        return selectedUpdateVersions;
+    }
+    
+    /**
+     * @return HashMap with the newest version which are matching the value of the key
+     * @param String key
+     * @param String value 
+     */
+    public HashMap getNewestUpdateVersionsOf(String key, String value) {
+        ArrayList selectedUpdateVersions = getUpdateVersionsOf(key, value);
+        for (int i = 0; i < selectedUpdateVersions.size(); i++) {
+            HashMap versionDetail = (HashMap) selectedUpdateVersions.get(i);
+            if (!versionDetail.get(key).equals(value)) {
+                selectedUpdateVersions.remove(i);
+            }
+        }
+        Collections.sort(selectedUpdateVersions, new UpdateInfoVersionComparator());
+        return (HashMap) selectedUpdateVersions.get(0);
+    }    
+
+    /**
+     * @return HashMap with the newest version which are matching the value of the key
+     * @param String key
+     * @param String value 
+     */
+    public HashMap getNewestUpdateVersionsOf(String key, String value, String installInfoVersion) {
+        ArrayList selectedUpdateVersions = getUpdateVersionsOf(key, value);
+        VersionComparator versionComparator = new VersionComparator();  
+        for (int i = 0; i < selectedUpdateVersions.size(); i++) {
+            HashMap versionDetail = (HashMap) selectedUpdateVersions.get(i);
+            if (versionComparator.compare((String) versionDetail.get("targetApllicationMinVersion"), installInfoVersion) > 0 ) {
+                selectedUpdateVersions.remove(i);
+            }
+            if (versionComparator.compare((String) versionDetail.get("targetApllicationMaxVersion"), installInfoVersion) < 0 ) {
+                selectedUpdateVersions.remove(i);
+            }
+        }
+        Collections.sort(selectedUpdateVersions, new UpdateInfoVersionComparator());
+        return (HashMap) selectedUpdateVersions.get(0);
+    }    
+    
+    
+}

Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfoVersionComparator.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfoVersionComparator.java	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/UpdateInfoVersionComparator.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,18 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import java.util.Comparator;
+import java.util.Map;
+
+public class UpdateInfoVersionComparator implements Comparator {
+    
+    public int compare(Object info, Object anotherInfo) {
+        String version = (String) ((Map) info).get("version");
+        String anotherVersion = (String) ((Map) anotherInfo).get("version");
+        VersionComparator versionComparator = new VersionComparator();
+        return compare(version, anotherVersion);
+    }
+}
\ No newline at end of file

Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/VersionComparator.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/VersionComparator.java	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/VersionComparator.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,268 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Java XPCOM Bindings.
+ *
+ * The Initial Developer of the Original Code is
+ * IBM Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2005
+ * IBM Corporation. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Javier Pedemonte (jhpedemonte at gmail.com)
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import java.util.Enumeration;
+import java.util.StringTokenizer;
+
+
+
+/**
+ * Version strings are dot-separated sequences of version-parts.
+ * <p>
+ * A version-part consists of up to four parts, all of which are optional:
+ * <br><code>
+ * &lt;number-a&gt;&lt;string-b&gt;&lt;number-c&gt;
+ * &lt;string-d (everything else)&gt;
+ * </code> <p>
+ * A version-part may also consist of a single asterisk "*" which indicates
+ * "infinity".
+ * <p>
+ * Numbers are base-10, and are zero if left out.
+ * Strings are compared bytewise.
+ * <p>
+ * For additional backwards compatibility, if "string-b" is "+" then
+ * "number-a" is incremented by 1 and "string-b" becomes "pre".
+ * <p> <pre>
+ * 1.0pre1
+ * < 1.0pre2  
+ *   < 1.0 == 1.0.0 == 1.0.0.0
+ *     < 1.1pre == 1.1pre0 == 1.0+
+ *       < 1.1pre1a
+ *         < 1.1pre1
+ *           < 1.1pre10a
+ *             < 1.1pre10
+ * </pre>
+ * Although not required by this interface, it is recommended that
+ * numbers remain within the limits of a signed char, i.e. -127 to 128.
+ */
+public class VersionComparator {
+
+
+  /**
+   * Compare two version strings
+   * @param A   a version string
+   * @param B   a version string
+   * @return a value less than 0 if A < B; <br>
+   *         the value 0 if A == B; <br>
+   *         or a value greater than 0 if A > B <br>
+   */
+  public int compare(String A, String B) {
+    int result;
+    String a = A, b = B;
+
+    do {
+      VersionPart va = new VersionPart();
+      VersionPart vb = new VersionPart();
+      a = parseVersionPart(a, va);
+      b = parseVersionPart(b, vb);
+
+      result = compareVersionPart(va, vb);
+      if (result != 0) {
+        break;
+      }
+    } while (a != null || b != null);
+
+    return result;
+  }
+
+  private class VersionPart {
+    int     numA = 0;
+    String  strB;
+    int     numC = 0;
+    String  extraD;
+  }
+
+  private static String parseVersionPart(String aVersion, VersionPart result) {
+    if (aVersion == null || aVersion.length() == 0) {
+      return aVersion;
+    }
+
+    StringTokenizer tok = new StringTokenizer(aVersion.trim(), ".");
+    String part = tok.nextToken();
+
+    if (part.equals("*")) {
+      result.numA = Integer.MAX_VALUE;
+      result.strB = "";
+    } else {
+      VersionPartTokenizer vertok = new VersionPartTokenizer(part);
+      try {
+        result.numA = Integer.parseInt(vertok.nextToken());
+      } catch (NumberFormatException e) {
+        // parsing error; default to zero like 'strtol' C function
+        result.numA = 0;
+      }
+
+      if (vertok.hasMoreElements()) {
+        String str = vertok.nextToken();
+
+        // if part is of type "<num>+"
+        if (str.charAt(0) == '+') {
+          result.numA++;
+          result.strB = "pre";
+        } else {
+          // else if part is of type "<num><alpha>..."
+          result.strB = str;
+
+          if (vertok.hasMoreTokens()) {
+            try {
+              result.numC = Integer.parseInt(vertok.nextToken());
+            } catch (NumberFormatException e) {
+              // parsing error; default to zero like 'strtol' C function
+              result.numC = 0;
+            }
+            if (vertok.hasMoreTokens()) {
+              result.extraD = vertok.getRemainder();
+            }
+          }
+        }
+      }
+    }
+
+    if (tok.hasMoreTokens()) {
+      // return everything after "."
+      return aVersion.substring(part.length() + 1);
+    }
+    return null;
+  }
+
+  private int compareVersionPart(VersionPart va, VersionPart vb) {
+    int res = compareInt(va.numA, vb.numA);
+    if (res != 0) {
+      return res;
+    }
+
+    res = compareString(va.strB, vb.strB);
+    if (res != 0) {
+      return res;
+    }
+
+    res = compareInt(va.numC, vb.numC);
+    if (res != 0) {
+      return res;
+    }
+
+    return compareString(va.extraD, vb.extraD);
+  }
+
+  private int compareInt(int n1, int n2) {
+    return n1 - n2;
+  }
+
+  private int compareString(String str1, String str2) {
+    // any string is *before* no string
+    if (str1 == null) {
+      return (str2 != null) ? 1 : 0;
+    }
+
+    if (str2 == null) {
+      return -1;
+    }
+
+    return str1.compareTo(str2);
+  }
+
+}
+
+/**
+ * Specialized tokenizer for Mozilla version strings.  A token can
+ * consist of one of the four sections of a version string: <code>
+ * &lt;number-a&gt;&lt;string-b&gt;&lt;number-c&gt;
+ * &lt;string-d (everything else)&gt;</code>.
+ */
+class VersionPartTokenizer implements Enumeration {
+
+  String part;
+
+  public VersionPartTokenizer(String aPart) {
+    part = aPart;
+  }
+
+  public boolean hasMoreElements() {
+    return part.length() != 0;
+  }
+
+  public boolean hasMoreTokens() {
+    return part.length() != 0;
+  }
+
+  public Object nextElement() {
+    if (part.matches("[\\+\\-]?[0-9].*")) {
+      // if string starts with a number...
+      int index = 0;
+      if (part.charAt(0) == '+' || part.charAt(0) == '-') {
+        index = 1;
+      }
+
+      while (index < part.length() && Character.isDigit(part.charAt(index))) {
+        index++;
+      }
+
+      String numPart = part.substring(0, index);
+      part = part.substring(index);
+      return numPart;
+    } else {
+      // ... or if this is the non-numeric part of version string
+      int index = 0;
+      while (index < part.length() && !Character.isDigit(part.charAt(index))) {
+        index++;
+      }
+
+      String alphaPart = part.substring(0, index);
+      part = part.substring(index);
+      return alphaPart;
+    }
+  }
+
+  public String nextToken() {
+    return (String) nextElement();
+  }
+
+  /**
+   * Returns what remains of the original string, without tokenization.  This
+   * method is useful for getting the <code>&lt;string-d (everything else)&gt;
+   * </code> section of a version string.
+   * 
+   * @return remaining version string
+   */
+  public String getRemainder() {
+    return part;
+  }
+
+}
+

Added: 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	                        (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/updateFinder/utils/WarFetcher.java	2007-07-02 10:52:40 UTC (rev 25695)
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.updatefinder.utils;
+
+import org.apache.log4j.Category;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.io.FileUtils;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.net.JarURLConnection;
+
+
+public class WarFetcher {
+    
+    private static Category log = Category.getInstance(WarFetcher.class);
+    //private String contextDirectoryPath;
+    private String DestinationDirectoryPath;
+    private String updateLink;
+    //private HttpServletRequest request;
+    private InstallInfo installInfo;
+    
+    
+    public WarFetcher(HttpServletRequest request, String updateLink, String destDir) throws Exception {
+        //this.request = request;
+        //this.contextDirectoryPath = request.getSession().getServletContext().getRealPath(".");
+        this.DestinationDirectoryPath = destDir;
+        this.updateLink = updateLink;
+        this.installInfo = new InstallInfo(request);
+    }
+    
+    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();
+
+        JarURLConnection URLcon=(JarURLConnection)(updateWarUrl.openConnection());
+        JarFile jar=URLcon.getJarFile();
+        
+        URL UpdateRdfUrl = new URL(installInfo.getUpdateURL());
+        InputStream updateRdfIn = UpdateRdfUrl.openStream();
+        UpdateInfo updateInfo = new UpdateInfo(updateRdfIn, 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");
+        
+        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();
+    }
+    
+    private void extractJar(JarFile jar, String destDir) throws Exception {
+        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();
+            }
+    }
+}
\ No newline at end of file




More information about the Yanel-commits mailing list