Package com.GestDB.update

Source Code of com.GestDB.update.Updater

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.GestDB.update;

import com.GestDB.Gestor;
import com.GestDB.general.Trackbug;
import com.GestDB.swing.Alerta;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
* @author seni
*/
public class Updater extends Thread {
    public void run()
    {
        try {
            String sUrlDescarga = "http://sourceforge.net/projects/gestdb/files/gestdb/";
            URL url = new URL("http://gestdb.piensayactua.com/gestdb/version.htm");
            BufferedInputStream bin = new BufferedInputStream((InputStream)url.getContent());
            StringBuilder sb = new StringBuilder();
            byte buffer[] = new byte[512];
            int leido = 0;
            while((leido = bin.read(buffer)) != -1)
                sb.append(new String(buffer,0,leido));

            bin.close();
            int iVersion = 0;
            int iVersion2 = 0;
            try {
                iVersion = Integer.parseInt(Gestor.Version.replaceAll("\\.", ""));
                iVersion2 = Integer.parseInt(sb.toString().replaceAll("\\.", ""));
            } catch(Exception e)
            {
                ;
            }
            if(iVersion2 > iVersion)
            {
                if(Gestor.propiedades.containsKey("sql.ActualizarAutomaticamente") && Gestor.propiedades.getProperty("sql.ActualizarAutomaticamente").toLowerCase().equals("true"))
                {
                    // tenemos que llamar a la url general para buscar la url de descarga de uno de los mirros
                    // la url esta en un href que enpieza con la url que hemos llamado y tien dentro ";use_mirror="
                    try {
                        url = new URL(sUrlDescarga + sb.toString() + "/gestdb_" + sb.toString() + ".zip/download");
                        URLConnection url1 = url.openConnection();
                        url1.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0");
                        bin = new BufferedInputStream((InputStream)url1.getContent());
                        StringBuilder sb2 = new StringBuilder();
                        leido = 0;
                        while((leido = bin.read(buffer)) != -1)
                            sb2.append(new String(buffer,0,leido));

                        bin.close();

                        // Tenemos que buscar el patron.
                        String sTexto = sb2.toString();
                        Pattern pattern1 = Pattern.compile("<a href=\\\"(.*)>");
                        String stemp = "";
                        String urlEncontrada = null;
                        try {
                            Matcher matcher1 = pattern1.matcher(sTexto);
                            while (matcher1.find())
                            {
                                stemp = matcher1.group(1);
                                if(stemp.indexOf("use_mirror") != -1)
                                {
                                    urlEncontrada = stemp.substring(0,stemp.indexOf("\""));
                                    break;
                                }
                            }       
                        } catch(Exception e)
                        {
                            ;
                        }

                        if(urlEncontrada != null)
                        {
                            FileOutputStream fout = null;
                            // tenemos que asegurarnos que hay directorio temp
                            File fileTarget = new File("gestdb.jar");
                            File fileSource = new File("temp/gestdb.zip.part");
                            File dirtemp = new File("temp");
                            if(dirtemp.exists() == false)
                                dirtemp.mkdirs();

                            url = new URL(urlEncontrada);
                            try {
                                bin = null;
                                bin = new BufferedInputStream((InputStream)url.getContent());
                                fout = new FileOutputStream(fileSource);
                                leido = 0;
                                while((leido = bin.read(buffer)) != -1)
                                    fout.write(buffer,0,leido);
                            } finally
                            {
                                if(bin != null)
                                {
                                    try { bin.close(); } catch(Exception e) {;}
                                }

                                if(fout != null)
                                {
                                    try { fout.close(); } catch(Exception e) {;}
                                }
                            }

                            // reemplazamos el archivo
                            File dirUpdates = new File("updates");
                            if(dirUpdates.exists() == false)
                                dirUpdates.mkdirs();

                            fileTarget.renameTo(new File("updates/gestdb_" + Gestor.Version + ".jar"));

                            // Tenemos que abrir el zip y descomprimirlo sobre nuestro directorio.
                            Unzip.UnzipDist(fileSource, new File("."));

                            Alerta al = new Alerta(Gestor.elGestorActivo, Gestor.GetLiteral("gestor.error_1","Error"), true,
                                Gestor.GetLiteral("gestor.alert_update_complete","El programa se ha actualizado. Reinicie la aplicaci�n para tener los nuevos cambios."), Alerta.OK);
                            al.setVisible(true);
                            al = null;
                        }
                        else
                        {
                            Alerta al = new Alerta(Gestor.elGestorActivo, Gestor.GetLiteral("gestor.error_1","Error"), true,
                                Gestor.GetLiteral("gestor.alert_update_avalaible","Hay disponible una nueva versi�n del programa. la puede descargar en la siguiente direcci�n.") +
                                sUrlDescarga + sb.toString(), Alerta.OK);
                            al.setVisible(true);
                            al = null;
                        }

                    } catch(Exception e)
                    {
                        Alerta al = new Alerta(Gestor.elGestorActivo, Gestor.GetLiteral("gestor.error_1","Error"), true,
                            Gestor.GetLiteral("gestor.alert_update_avalaible","Hay disponible una nueva versi�n del programa. la puede descargar en la siguiente direcci�n.") +
                            sUrlDescarga + sb.toString(), Alerta.OK);
                        al.setVisible(true);
                        al = null;
                    }
                }
                else
                {
                    Alerta al = new Alerta(Gestor.elGestorActivo, Gestor.GetLiteral("gestor.error_1","Error"), true,
                        Gestor.GetLiteral("gestor.alert_update_avalaible","Hay disponible una nueva versi�n del programa. la puede descargar en la siguiente direcci�n.") +
                        sUrlDescarga + sb.toString(), Alerta.OK);
                    al.setVisible(true);
                    al = null;
                }
            }
        } catch(Throwable e)
        {
            Trackbug.fatal(e,"Error: " + e.getMessage());
        }
    }

}
TOP

Related Classes of com.GestDB.update.Updater

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.