From 76868d93ea6d86093231f05f429a3c0c06a973d6 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 6 Sep 2012 15:54:38 +0400 Subject: [PATCH] Jar handler minor refactoring and cleanup --- .../openapi/vfs/impl/jar/JarHandlerBase.java | 33 +++++++++----- .../openapi/vfs/impl/jar/JarHandler.java | 43 ++++++++----------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/platform/core-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandlerBase.java b/platform/core-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandlerBase.java index 61422556810a..1c054a6c84b6 100644 --- a/platform/core-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandlerBase.java +++ b/platform/core-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandlerBase.java @@ -15,6 +15,7 @@ */ package com.intellij.openapi.vfs.impl.jar; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.io.BufferExposingByteArrayInputStream; import com.intellij.openapi.util.io.FileAttributes; import com.intellij.openapi.util.io.FileUtil; @@ -38,34 +39,44 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class JarHandlerBase { + private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vfs.impl.jar.JarHandlerBase"); + private static final long DEFAULT_LENGTH = 0L; private static final long DEFAULT_TIMESTAMP = -1L; - protected final TimedReference myJarFile = new TimedReference(null); - protected SoftReference> myRelPathsToEntries = new SoftReference>(null); - protected final Object lock = new Object(); + private final TimedReference myJarFile = new TimedReference(null); + private SoftReference> myRelPathsToEntries = new SoftReference>(null); + private final Object lock = new Object(); + protected final String myBasePath; protected static class EntryInfo { + protected final boolean isDirectory; + protected final String shortName; + protected final EntryInfo parent; + public EntryInfo(final String shortName, final EntryInfo parent, final boolean directory) { this.shortName = shortName; this.parent = parent; - isDirectory = directory; + this.isDirectory = directory; } - - final boolean isDirectory; - protected final String shortName; - final EntryInfo parent; } public JarHandlerBase(String path) { myBasePath = path; } + protected void clear() { + synchronized (lock) { + myRelPathsToEntries = null; + myJarFile.set(null); + } + } + @NotNull protected Map initEntries() { synchronized (lock) { - Map map = myRelPathsToEntries.get(); + Map map = myRelPathsToEntries != null ? myRelPathsToEntries.get() : null; if (map == null) { final JarFile zip = getJar(); @@ -105,8 +116,9 @@ public class JarHandlerBase { @Nullable protected JarFile createJarFile() { + final File originalFile = getOriginalFile(); try { - final ZipFile zipFile = new ZipFile(getMirrorFile(getOriginalFile())); + @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") final ZipFile zipFile = new ZipFile(getMirrorFile(originalFile)); class MyJarEntry implements JarFile.JarEntry { private ZipEntry myEntry; @@ -180,6 +192,7 @@ public class JarHandlerBase { }; } catch (IOException e) { + LOG.warn(e.getMessage() + ": " + originalFile.getPath()); return null; } } diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandler.java b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandler.java index 630dfe6a3803..61cd1e63e740 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandler.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/jar/JarHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,18 +59,13 @@ public class JarHandler extends JarHandlerBase implements FileSystemInterface { @Nullable public VirtualFile markDirty() { - synchronized (lock) { - myRelPathsToEntries = null; - myJarFile.set(null); + clear(); - final NewVirtualFile root = (NewVirtualFile) - JarFileSystem.getInstance().findFileByPath(myBasePath + JarFileSystem.JAR_SEPARATOR); - if (root != null) { - root.markDirty(); - return root; - } - return null; + final VirtualFile root = JarFileSystem.getInstance().findFileByPath(myBasePath + JarFileSystem.JAR_SEPARATOR); + if (root instanceof NewVirtualFile) { + ((NewVirtualFile)root).markDirty(); } + return root; } @Override @@ -78,16 +73,15 @@ public class JarHandler extends JarHandlerBase implements FileSystemInterface { if (!myFileSystem.isMakeCopyOfJar(originalFile) || !originalFile.exists()) return originalFile; String folderPath = getJarsDir(); - if (!new File(folderPath).exists()) { - if (!new File(folderPath).mkdirs()) { - return originalFile; - } + if (!new File(folderPath).exists() && !new File(folderPath).mkdirs()) { + return originalFile; } String fileName = originalFile.getName() + "." + Integer.toHexString(originalFile.getPath().hashCode()); - final File mirror = new File(folderPath, fileName); + File mirror = new File(folderPath, fileName); - if (!mirror.exists() || Math.abs(originalFile.lastModified() - mirror.lastModified()) > 2000) { + if (!mirror.exists() || + Math.abs(originalFile.lastModified() - mirror.lastModified()) > 2000) { return copyToMirror(originalFile, mirror); } @@ -100,25 +94,24 @@ public class JarHandler extends JarHandlerBase implements FileSystemInterface { } private File copyToMirror(final File original, final File mirror) { - ProgressManager progressManager = ProgressManager.getInstance(); - ProgressIndicator progress = progressManager.getProgressIndicator(); - if (progress != null){ + ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator(); + if (progress != null) { progress.pushState(); progress.setText(VfsBundle.message("jar.copy.progress", original.getPath())); progress.setFraction(0); } - try{ + try { FileUtil.copy(original, mirror); } - catch(final IOException e){ + catch (final IOException e) { final String path1 = original.getPath(); final String path2 = mirror.getPath(); ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { - Messages.showMessageDialog(VfsBundle.message("jar.copy.error.message", path1, path2, e.getMessage()), VfsBundle.message("jar.copy.error.title"), - Messages.getErrorIcon()); + Messages.showErrorDialog(VfsBundle.message("jar.copy.error.message", path1, path2, e.getMessage()), + VfsBundle.message("jar.copy.error.title")); } }, ModalityState.NON_MODAL); @@ -126,7 +119,7 @@ public class JarHandler extends JarHandlerBase implements FileSystemInterface { return original; } - if (progress != null){ + if (progress != null) { progress.popState(); }