swt结合httpclient实现自动更新 [autoupdate in swt with httpclient]

/**
* 本地程序应当自知版本号已与更新站点相比较,
* plugin的版本号是在plugin.xml里面设置的,导出的jar包会依据此版本号命名,
* 而eclipse会自动依据此版本信息选择加载最新版本
* 此程序的测试:不能直接在eclipse里面运行,应将此rcp打包,然后将version改大,又打个plugin包,将plugin包放在站点上
* 启动原来的rcp程序则会出现下载重启动作
  * @date Nov 4, 2008
* @see
*/
public class AutoUpdate
{
    private Display display;
    private Shell shell;
    private boolean needReload;
    private String version = "1.0.0";
    public AutoUpdate()
    {
        display = new Display();
        shell = new Shell(display);   
        shell.setText(version);
        shell.setSize(500, 200);
    }
    private byte[] httpGetRequest(String url)
    {
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        try
        {
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK)
            {
                System.err.println("Method failed: " + getMethod.getStatusLine());
                return null;
            }
            byte[] responseBody = getMethod.getResponseBody();
            return responseBody;           
        }
        catch (HttpException e)
        {
            e.printStackTrace();
            return null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }       
        finally
        {
            getMethod.releaseConnection();
        }
    }
    public void checkAndDown()
    {       
        try
        {
            byte[] responseBody = httpGetRequest("http://localhost:10010/SmileScriptWeb/autoupdate_config.xml");
            if (null == responseBody)
            {
                System.err.println("can't get config.");
                return;
            }
            String strXml = new String(responseBody);
            System.out.println(strXml);
            Document doc = DocumentHelper.parseText(strXml);
            Element el = doc.getRootElement().element("update");
            String sversion = el.attributeValue("version");
            String fileName = el.attributeValue("filename");
            String url = el.attributeValue("url");
            int curversion = Integer.valueOf(version.replaceAll("\\.", "")).intValue();
            int serverversion = Integer.valueOf(sversion.replaceAll("\\.", "")).intValue();
            if (curversion < serverversion)
            {
                System.out.println("need download new version.");
            }else{
                return;               
            }
            System.out.println("got config successfully: version=" + sversion);
//            download           
            url = "http://localhost:10010/SmileScriptWeb/" + url;
            responseBody = httpGetRequest(url);
            if (null == responseBody)
            {
                System.err.println("can't download update.");
                return;
            }
            fileName = getAppRootPath() + "plugins/" + fileName;
            System.out.println("the download file full name=" + fileName);
            File storeFile = new File(fileName); 
            FileOutputStream output = new FileOutputStream(storeFile);
            output.write(responseBody);
            output.close();
            System.out.println("download successfully.");
            needReload = true;
        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    private static String installPath;
    public static String getAppRootPath() {
        if (null == installPath)
        {
            Location installLoc = Platform.getInstallLocation();       
            URL installURL = installLoc.getURL();
            installPath = installURL.getPath();           
        }       
        return installPath;
    }
    public Object run()
    {
        final int totalColumn = 1 ;
        GridLayout layout = new GridLayout(totalColumn,false);
        shell.setLayout(layout);
        checkAndDown();
        if (needReload)
        {
            return IPlatformRunnable.EXIT_RESTART;       
        }
//        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
        return IPlatformRunnable.EXIT_OK;           
    }
}
------------------------------------------------------------------------autoupdate_config.xml-------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <update version="1.0.1" filename="rcp_swt_1.0.1.jar" url="rcp_swt_1.0.1.jar" />
</config>


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1