⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applicationlauncher.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (idx > -1) {
                source = source.substring(0, idx) + value
                                + ((source.length() - idx <= token.length()) ? "" : source.substring(idx + token.length()));
            }

        } while (idx > -1);

        return source;
    }

    private boolean isArgument(XMLElement e) {
        return e.getName().equalsIgnoreCase("arg") || e.getName().equalsIgnoreCase("jvm");
    }

    public static boolean checkVersion(String applicationJDK) {

        int[] applicationVersion = ApplicationLauncher.getVersion(applicationJDK);
        int[] installedJREVersion = ApplicationLauncher.getVersion(System.getProperty("java.version"));

        for (int i = 0; i < applicationVersion.length && i < installedJREVersion.length; i++) {
            if (applicationVersion[i] > installedJREVersion[i])
                return false;
        }

        return true;
    }

    public static int[] getVersion(String version) {

        int idx = 0;
        int pos = 0;
        int[] result = new int[0];
        do {

            idx = version.indexOf('.', pos);
            int v;
            if (idx > -1) {
                v = Integer.parseInt(version.substring(pos, idx));
                pos = idx + 1;
            } else {
                try {
                    int sub = version.indexOf('_', pos);
                    if (sub == -1) {
                        sub = version.indexOf('-', pos);
                    }
                    if (sub > -1) {
                        v = Integer.parseInt(version.substring(pos, sub));
                    } else {
                        v = Integer.parseInt(version.substring(pos));
                    }
                } catch (NumberFormatException ex) {
                    // Ignore the exception and return what version we have
                    break;
                }
            }
            int[] tmp = new int[result.length + 1];
            System.arraycopy(result, 0, tmp, 0, result.length);
            tmp[tmp.length - 1] = v;
            result = tmp;

        } while (idx > -1);

        return result;
    }

    public void processFiles(XMLElement element) throws IOException {
    	processFiles(element, null);
    }
    
    public void processFiles(XMLElement element, String app) throws IOException {

        Enumeration en = element.enumerateChildren();
        XMLElement e;

        while (en.hasMoreElements()) {
            e = (XMLElement) en.nextElement();
            if (e.getName().equalsIgnoreCase("file")) {
                addFile(e, app);
            } else if (e.getName().equalsIgnoreCase("if")) {

                try {
                    if (type.checkFileCondition(e)) {
                        processFiles(e, app);
                    }
                } catch (IllegalArgumentException iae) {
                    String parameter = (String) e.getAttribute("parameter");

                    if (parameter != null) {
                        String requiredValue = (String) e.getAttribute("value");
                        boolean not = "true".equalsIgnoreCase(((String) e.getAttribute("not")));

                        // Check the parameter
                        String value = (String) descriptorParams.get(parameter);

                        if ((!not && requiredValue.equalsIgnoreCase(value)) || (not && !requiredValue.equalsIgnoreCase(value))) {
                            processFiles(e, app);
                        }

                    } else
                        throw new IOException("<if> element requires type specific attributes or parameter/value attributes");
                }

            } else
                throw new IOException("Invalid element <" + e.getName() + "> found in <files>");
        }

    }

    private long generateChecksum(File f) throws IOException {

        Adler32 alder = new Adler32();
        CheckedInputStream in = new CheckedInputStream(new FileInputStream(f), alder);
        try {
            byte[] buf = new byte[4096];
            int read;
            while ((read = in.read(buf)) > -1)
                ;
    
            alder = (Adler32) in.getChecksum();
    
            return alder.getValue();
        }
        finally {
            in.close();
        }
    }

    private boolean isSupportedPlatform(String os) {
        if (os != null) {
            // If the os does not start with the current platform then ignore
            // this
            String platform = System.getProperty("os.name").toUpperCase();
            return platform.startsWith(os.toUpperCase());
        } else
            return true;
    }

    public Hashtable getDescriptorParams() {
        return descriptorParams;
    }
    
    public File addShared(XMLElement e) throws IOException {
    	return addShared(e, null);
    }

    public File addShared(XMLElement e, String app) throws IOException {
        if (!isSupportedPlatform((String) e.getAttribute("os")))
            return null;
        File entry;
        String name = e.getContent();

        if (e.getAttribute("checksum") != null && e.getAttribute("size") != null) {

            String filename = (String) e.getAttribute("checksum") + "." + (String) e.getAttribute("size") + "." + name;

            entry = new File(sharedDir, filename);
            long checksum = Long.parseLong((String) e.getAttribute("checksum"));
            long size = Long.parseLong((String) e.getAttribute("size"));

            if (!entry.exists()) {
            	DownloadableFile df = new DownloadableFile();
            	df.filename = filename;
            	df.applicationName = app;
                sharedFilesToDowload.put(name, df);
                totalBytesToDownload += size;
            } else {

                long currentChecksum = generateChecksum(entry);

                if (currentChecksum != checksum || entry.length() != size) {

                	DownloadableFile df = new DownloadableFile();
                	df.filename = filename;
                	df.applicationName = app;
                    sharedFilesToDowload.put(name, df);
                    totalBytesToDownload += entry.length();

                }
            }
        } else {
            throw new IOException("Expected checksum and size for file object");
        }

        return entry;

    }

    public File addFile(XMLElement e) throws IOException {
    	return addFile(e, null);
    }
    public File addFile(XMLElement e, String app) throws IOException {

        if (!isSupportedPlatform((String) e.getAttribute("os")))
            return null;

        DownloadableFile df = new DownloadableFile();
        
        /**
         * LDP - Make sure any downloaded files are using the correct seperator char
         * for the platform.
         */
        df.filename = e.getContent().replace('/', File.separatorChar);
        df.applicationName = app;
        
        File entry = new File(installDir, df.filename);
        long size = Long.parseLong((String) e.getAttribute("size"));
        if (events != null) {
            events.debug("Adding file " + entry.getAbsolutePath() + " (" + size + ")");
        }

        if (e.getAttribute("checksum") == null || e.getAttribute("size") == null) {
            throw new IOException("Expected checksum and size for file object");
        }
        if (entry.exists()) {

            // The file exists so lets check its size and checksum to determine
            // whether we need to download it again
            long checksum = Long.parseLong((String) e.getAttribute("checksum"));

            long currentChecksum = generateChecksum(entry);

            if (events != null) {
                events.debug("File exists - current checksum = " + currentChecksum + ", files checksum = " + checksum);
            }

            if (currentChecksum != checksum || entry.length() != size) {
                if (events != null) {
                    events.debug("Checksum mismatch, downloading");
                }

                filesToDownload.addElement(df);
                totalBytesToDownload += entry.length();
            }
        } else {
            if (events != null) {
                events.debug("File doesn't exists downloading");
            }

            // We currently do not have the file so add it to the download list
            filesToDownload.addElement(df);
            totalBytesToDownload += size;
        }

        return entry;
    }

    private void downloadFiles() throws IOException {

        if (events != null)
            events.debug("Downloading files");

        if (events != null)
            events.startDownload(totalBytesToDownload);

        long bytesSoFar = 0;

        DownloadableFile df;
        for (int i = 0; i < filesToDownload.size(); i++) {
            df = (DownloadableFile) filesToDownload.elementAt(i);
            bytesSoFar = downloadFile(installDir, (df.applicationName==null ? name : df.applicationName), df.filename, df.filename, bytesSoFar);
        }

        for (Enumeration e = sharedFilesToDowload.keys(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            df = (DownloadableFile) sharedFilesToDowload.get(name);
            bytesSoFar = downloadFile(sharedDir, (df.applicationName==null ? name : df.applicationName), name, df.filename, bytesSoFar);
        }

        if (events != null)
            events.completedDownload();

        if (events != null)
            events.debug("Completed downloading files");
    }

    public InputStream getDownloadFile(String name, String ticket, String filename) throws IOException {
      URL file = new URL(applicationStoreProtocol, applicationStoreHost, applicationStorePort, "/getApplicationFile.do"
                        + "?name=" + name + "&ticket=" + ticket + "&file=" + filename);
     if (events != null)
            events.debug("Request application using " + file.toExternalForm());


        URLConnection con = file.openConnection();
        con.setUseCaches(false);

        try {
            Method m = con.getClass().getMethod("setConnectTimeout", new Class[] {
                int.class
            });
            if (events != null) {
                events.debug("Using a 1.5 runtime, so setting connection and read timeout to 20 seconds");
            }
            m.invoke(con, new Object[] {
                new Integer(20000)
            });
            m = con.getClass().getMethod("setReadTimeout", new Class[] {
                int.class
            });
            m.invoke(con, new Object[] {
                new Integer(20000)
            });
        } catch (Throwable t) {
        }

        return con.getInputStream();
    }

    public long downloadFile(File basedir, String app, String filename, String tofilename, long bytesSoFar) throws IOException {

        /**
         * LDP - Another method that delegates collection of a HTTP request to a seperate method so that
         * it can be overidden.
         */
        if (events != null)
            events.debug("Downloading " + filename + " to " + basedir.getAbsolutePath() + File.separator + tofilename);

        File entry = new File(basedir, tofilename);
        File f = new File(entry.getParent());
        f.mkdirs();

        if (events != null)
            events.debug("Creating " + entry.getAbsolutePath());

        FileOutputStream out = new FileOutputStream(entry);
        InputStream in = getDownloadFile(app, ticket, filename);
        byte[] buf = new byte[16384];
        int read;
        while ((read = in.read(buf)) > -1) {
            out.write(buf, 0, read);
            bytesSoFar += read;

            if (events != null)
                events.progressedDownload(bytesSoFar);
        }
        if (events != null)
            events.debug("Finshing download file.");

        in.close();
        out.close();

        return bytesSoFar;

    }

    /**
     * @return Returns the applicationStoreHost.
     */
    public String getApplicationStoreHost() {
        return applicationStoreHost;
    }

    /**
     * @return Returns the applicationStorePort.
     */
    public int getApplicationStorePort() {
        return applicationStorePort;
    }

    /**
     * @return Returns the applicationStoreProtocol.
     */
    public String getApplicationStoreProtocol() {
        return applicationStoreProtocol;
    }

    class DownloadableFile {
    	String filename;
    	String applicationName;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -