Java URL Value Check isNewer(URL file, URL reference)

Here you can find the source of isNewer(URL file, URL reference)

Description

is Newer

License

Open Source License

Declaration

public static boolean isNewer(URL file, URL reference) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Red Hat, Inc./*from   ww w .j  av  a2 s.com*/
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.File;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.core.runtime.FileLocator;

public class Main {
    public static boolean isNewer(URL file, URL reference) {
        if (file == null || reference == null)
            throw new IllegalArgumentException("null file value");
        file = getFileURL(file);
        reference = getFileURL(reference);
        if (!isFile(file) && !isFile(reference))
            throw new IllegalArgumentException("destination is not a file URL");

        long fileLastModified = 0;
        long referenceLastModified = 0;

        if ("file".equals(file.getProtocol())) {
            File srcFile = new File(file.getFile());
            fileLastModified = srcFile.lastModified();
        }
        if ("file".equals(reference.getProtocol())) {
            File referenceFile = new File(reference.getFile());
            referenceLastModified = referenceFile.lastModified();
        }

        if ("jar".equals(file.getProtocol())) {
            ZipFile zipFile = getZipFile(file);
            ZipEntry zipEntry = getZipEntry(file, zipFile);
            if (zipEntry == null) {
                throw new IllegalArgumentException(file + " can not be found on the zip file");
            }
            fileLastModified = zipEntry.getTime();
        }
        if ("jar".equals(reference.getProtocol())) {
            ZipFile zipFile = getZipFile(reference);
            ZipEntry zipEntry = getZipEntry(reference, zipFile);
            if (zipEntry == null) {
                throw new IllegalArgumentException(reference + " can not be found on the zip file");
            }
            referenceLastModified = zipEntry.getTime();
        }

        return fileLastModified >= referenceLastModified;
    }

    private static URL getFileURL(URL url) {
        try {
            url = FileLocator.resolve(url);
            return FileLocator.toFileURL(url);
        } catch (IOException e) {
            return null;
        }
    }

    private static boolean isFile(URL url) {
        return "file".equals(url.getProtocol());
    }

    private static ZipFile getZipFile(URL url) {
        if (!"jar".equals(url.getProtocol()))
            return null;
        String file = url.getFile();
        int exclamation = file.indexOf('!');
        if (exclamation < 0)
            return null;
        URL fileUrl = null;
        try {
            fileUrl = new URL(file.substring(0, exclamation));
        } catch (MalformedURLException mue) {
            return null;
        }
        File pluginJar = new File(fileUrl.getFile());
        if (!pluginJar.exists())
            return null;

        try {
            ZipFile zipFile = new ZipFile(pluginJar);
            return zipFile;
        } catch (IOException e) {
            return null;
        }
    }

    private static ZipEntry getZipEntry(URL file, ZipFile zipFile) {
        String fileString = file.getFile();
        int exclamation = fileString.indexOf('!');
        String jarLocation = fileString.substring(exclamation + 2); // remove jar separator !/ 
        return zipFile.getEntry(jarLocation);
    }
}

Related

  1. isLocalFile(URL url)
  2. isLocalhost(URL url)
  3. isLocalURL(String s)
  4. isLocalURL(URL url)
  5. isLocalURL(URL url)
  6. isResponseCode(String url, int expectedCode)
  7. isResponsive(URL url)
  8. isSameFile(URL u, File out)
  9. isURL(final String urlString)