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

📄 shadingpathresolver.java

📁 java插件系统源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                try {
                    InputStream in = zipFile.getInputStream(entry);
                    try {
                        IoUtil.copyStream(in, out, 1024);
                    } finally {
                        in.close();
                    }
                } finally {
                    out.close();
                }
            }
            entryFile.setLastModified(entry.getTime());
        }
    }
    
    static void unpack(final InputStream strm,
            final File destFolder) throws IOException {
        ZipInputStream zipStrm = new ZipInputStream(strm);
        ZipEntry entry = zipStrm.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            File entryFile =
                new File(destFolder.getCanonicalPath() + "/" + name); //$NON-NLS-1$
            if (name.endsWith("/")) { //$NON-NLS-1$
                if (!entryFile.exists() && !entryFile.mkdirs()) {
                    throw new IOException("can't create folder " + entryFile); //$NON-NLS-1$
                }
            } else {
                File folder = entryFile.getParentFile();
                if (!folder.exists() && !folder.mkdirs()) {
                    throw new IOException("can't create folder " + folder); //$NON-NLS-1$
                }
                OutputStream out = new BufferedOutputStream(
                        new FileOutputStream(entryFile, false));
                try {
                    IoUtil.copyStream(zipStrm, out, 1024);
                } finally {
                    out.close();
                }
            }
            entryFile.setLastModified(entry.getTime());
            entry = zipStrm.getNextEntry();
        }
    }
    
    static boolean deleteFile(final File file) {
        if (file.isDirectory()) {
            IoUtil.emptyFolder(file);
        }
        return file.delete();
    }
    
    static Date getLastModified(final URL url) throws IOException {
        long result = 0;
        File sourceFile = null;
        if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
            String urlStr = url.toExternalForm();
            int p = urlStr.indexOf("!/"); //$NON-NLS-1$
            if (p != -1) {
                sourceFile = IoUtil.url2file(new URL(urlStr.substring(4, p)));
            }
        }
        if (sourceFile == null) {
            sourceFile = IoUtil.url2file(url);
        }
        if (sourceFile != null) {
            result = sourceFile.lastModified();
        } else {
            URLConnection cnn = url.openConnection();
            try {
                cnn.setUseCaches(false);
                result = cnn.getLastModified();
            } finally {
                cnn.getInputStream().close();
            }
        }
        if (result == 0) {
            throw new IOException(
                    "can't retrieve modification date for resource " //$NON-NLS-1$
                    + url);
        }
        // for some reason modification milliseconds for some files are unstable
        Calendar cldr = Calendar.getInstance(Locale.ENGLISH);
        cldr.setTime(new Date(result));
        cldr.set(Calendar.MILLISECOND, 0);
        return cldr.getTime();
    }
    
    private static String getRelativePath(final File base, final File file)
            throws IOException {
        String basePath;
        String filePath = file.getCanonicalPath();
        if (base.isFile()) {
            File baseParent = base.getParentFile();
            if (baseParent == null) {
                return null;
            }
            basePath = baseParent.getCanonicalPath();
        } else {
            basePath = base.getCanonicalPath();
        }
        if (!basePath.endsWith(File.separator)) {
            basePath += File.separator;
        }
        int p = basePath.indexOf(File.separatorChar);
        String prefix = null;
        while (p != -1) {
            String newPrefix = basePath.substring(0, p + 1);
            if (!filePath.startsWith(newPrefix)) {
                break;
            }
            prefix = newPrefix;
            p = basePath.indexOf(File.separatorChar, p + 1);
        }
        if (prefix == null) {
            return null;
        }
        filePath = filePath.substring(prefix.length());
        if (prefix.length() == basePath.length()) {
            return filePath;
        }
        int c = 0;
        p = basePath.indexOf(File.separatorChar, prefix.length());
        while (p != -1) {
            c++;
            p = basePath.indexOf(File.separatorChar, p + 1);
        }
        for (int i = 0; i < c; i++) {
            filePath = ".." + File.separator + filePath; //$NON-NLS-1$
        }
        return filePath;
    }
    
    private static String getRelativeUrl(final File base, final File file)
            throws IOException {
        String result = ShadingUtil.getRelativePath(base, file);
        if (result == null) {
            return null;
        }
        result = result.replace('\\', '/');
        if (file.isDirectory() && !result.endsWith("/")) { //$NON-NLS-1$
            result += "/"; //$NON-NLS-1$
        }
        return result;
    }

    static String getRelativeUrl(final File base, final URL url)
            throws IOException {
        File file = IoUtil.url2file(url);
        if (file != null) {
            String result = getRelativeUrl(base, file);
            if (result != null) {
                return result;
            }
        }
        if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
            String urlStr = url.toExternalForm();
            int p = urlStr.indexOf("!/"); //$NON-NLS-1$
            if (p != -1) {
                return "jar:" //$NON-NLS-1$
                    + getRelativeUrl(base, new URL(urlStr.substring(4, p)))
                    + urlStr.substring(p);
            }
        }
        return url.toExternalForm();
    }
    
    static URL buildURL(final URL base, final String url)
            throws MalformedURLException {
        if (!url.toLowerCase(Locale.ENGLISH).startsWith("jar:")) { //$NON-NLS-1$
            return new URL(base, url);
        }
        int p = url.indexOf("!/"); //$NON-NLS-1$
        if (p == -1) {
            return new URL(base, url);
        }
        return new URL("jar:" //$NON-NLS-1$
                + new URL(base, url.substring(4, p)).toExternalForm()
                + url.substring(p));
    }
    
    private ShadingUtil() {
        // no-op
    }
}

final class ShadowDataController {
    private static final String META_FILE_NAME = ".meta"; //$NON-NLS-1$
    
    private final Log log = LogFactory.getLog(ShadowDataController.class);
    private final File shadowFolder;
    private final URL shadowFolderUrl;
    private final Properties metaData;
    private final DateFormat dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
    private final FileFilter fileFilter;
    
    static ShadowDataController init(final File shadowFolder,
            final FileFilter filter) throws IOException {
        ShadowDataController result =
            new ShadowDataController(shadowFolder, filter);
        result.quickCheck();
        result.save();
        return result;
    }

    private ShadowDataController(final File folder, final FileFilter filter)
            throws IOException {
        shadowFolder = folder;
        fileFilter = filter;
        shadowFolderUrl = IoUtil.file2url(folder);
        File metaFile = new File(shadowFolder, META_FILE_NAME);
        metaData = new Properties();
        if (metaFile.isFile()) {
            try {
                InputStream in = new FileInputStream(metaFile);
                try {
                    metaData.load(in);
                } finally {
                    in.close();
                }
                if (log.isDebugEnabled()) {
                    log.debug("meta-data loaded from file " + metaFile); //$NON-NLS-1$
                }
            } catch (IOException ioe) {
                log.warn("failed loading meta-data from file " + metaFile, ioe); //$NON-NLS-1$
            }
        }
    }
    
    private void save() {
        File metaFile = new File(shadowFolder, META_FILE_NAME);
        try {
            OutputStream out = new FileOutputStream(metaFile, false);
            try {
                metaData.store(out, "This is automatically generated file."); //$NON-NLS-1$
            } finally {
                out.close();
            }
            if (log.isDebugEnabled()) {
                log.debug("meta-data saved to file " + metaFile); //$NON-NLS-1$
            }
        } catch (IOException ioe) {
            log.warn("failed saving meta-data to file " + metaFile, ioe); //$NON-NLS-1$
        }
    }
    
    private void quickCheck() {
        File[] files = shadowFolder.listFiles(new ShadowFileFilter());
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (metaData.containsValue(file.getName())) {
                continue;
            }
            if (ShadingUtil.deleteFile(file)) {
                if (log.isDebugEnabled()) {
                    log.debug("deleted shadow file " + file); //$NON-NLS-1$
                }
            } else {
                log.warn("can't delete shadow file " + file); //$NON-NLS-1$
            }
        }
        Set uids = new HashSet();
        for (Iterator it = metaData.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            if (!key.startsWith("uid:")) { //$NON-NLS-1$
                continue;
            }
            uids.add(entry.getValue());
        }
        for (Iterator it = uids.iterator(); it.hasNext();) {
            quickCheck((String) it.next());
        }
    }
    
    private void quickCheck(final String uid) {
        if (log.isDebugEnabled()) {
            log.debug("quick check of UID " + uid); //$NON-NLS-1$
        }
        String url = metaData.getProperty("source:" + uid, null); //$NON-NLS-1$
        String file = metaData.getProperty("file:" + uid, null); //$NON-NLS-1$
        String modified = metaData.getProperty("modified:" + uid, null); //$NON-NLS-1$
        if ((url == null) || (file == null) || (modified == null)) {
            if (log.isDebugEnabled()) {
                log.debug("meta-data incomplete, UID=" + uid); //$NON-NLS-1$
            }
            remove(uid);
            return;
        }
        try {
            if (!dtf.parse(modified).equals(ShadingUtil.getLastModified(
                    ShadingUtil.buildURL(shadowFolderUrl, url)))) {
                if (log.isDebugEnabled()) {
                    log.debug("source modification detected, UID=" + uid //$NON-NLS-1$
                            + ", source=" + url); //$NON-NLS-1$
                }
                remove(uid);
            }
        } catch (IOException ioe) {
            log.warn("quick check failed", ioe); //$NON-NLS-1$
            remove(uid);
        } catch (ParseException pe) {
            log.warn("quick check failed", pe); //$NON-NLS-1$
            remove(uid);
        }
    }
    
    private void remove(final String uid) {
        String file = metaData.getProperty("file:" + uid, null); //$NON-NLS-1$
        if (file != null) {
            File lostFile = new File(shadowFolder, file);
            if (ShadingUtil.deleteFile(lostFile)) {
                if (log.isDebugEnabled()) {
                    log.debug("deleted lost file " + file); //$NON-NLS-1$
                }
            } else {
                log.warn("can't delete lost file " + file); //$NON-NLS-1$
            }

⌨️ 快捷键说明

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