JarLoader( URL url, @SuppressWarnings("unused") boolean canLockJar, int index, boolean preloadJarContents) throws IOException { super(new URL("jar", "", -1, url + "!/"), index); myCanonicalFile = new File(FileUtil.unquote(url.getFile())).getCanonicalFile(); myCanLockJar = canLockJar; ZipFile zipFile = getZipFile(); // IOException from opening is propagated to caller if zip file isn't valid, try { myAttributes = getAttributes(zipFile); if (preloadJarContents) { JarMemoryLoader loader = JarMemoryLoader.load(zipFile, getBaseURL(), myAttributes); if (loader != null) { myMemoryLoader = new SoftReference<JarMemoryLoader>(loader); } } } finally { releaseZipFile(zipFile); } }
@Override @Nullable Resource getResource(String name, boolean flag) { JarMemoryLoader loader = myMemoryLoader != null ? myMemoryLoader.get() : null; if (loader != null) { Resource resource = loader.getResource(name); if (resource != null) return resource; } try { ZipFile zipFile = getZipFile(); try { ZipEntry entry = zipFile.getEntry(name); if (entry != null) { return MemoryResource.load(getBaseURL(), zipFile, entry, myAttributes); } } finally { releaseZipFile(zipFile); } } catch (Exception e) { error("file: " + myCanonicalFile, e); } return null; }
@Override void buildCache(final ClasspathCache cache) throws IOException { ZipFile zipFile = null; try { zipFile = acquireZipFile(); if (zipFile == null) return; final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); String name = zipEntry.getName(); cache.addResourceEntry(name, this); cache.addNameEntry(name, this); } } finally { releaseZipFile(zipFile); } }
@NotNull @Override public ClasspathCache.LoaderData buildData() throws IOException { ZipFile zipFile = getZipFile(); try { ClasspathCache.LoaderData loaderData = new ClasspathCache.LoaderData(); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String name = entry.getName(); loaderData.addResourceEntry(name); loaderData.addNameEntry(name); } return loaderData; } finally { releaseZipFile(zipFile); } }
@Override @Nullable Resource getResource(String name, boolean flag) { final long started = myDebugTime ? System.nanoTime() : 0; if (myMemoryLoader != null) { JarMemoryLoader loader = myMemoryLoader.get(); if (loader != null) { Resource resource = loader.getResource(name); if (resource != null) return resource; } } ZipFile file = null; try { file = acquireZipFile(); if (file == null) return null; ZipEntry entry = file.getEntry(name); if (entry != null) { ++hits; if (hits % 1000 == 0 && ClasspathCache.doDebug) { ClasspathCache.LOG.debug("Exists jar loader: misses:" + misses + ", hits:" + hits); } return new MyResource(entry, new URL(getBaseURL(), name)); } if (misses % 1000 == 0 && ClasspathCache.doDebug) { ClasspathCache.LOG.debug("Missed " + name + " from jar:" + myURL); } ++misses; } catch (Exception e) { return null; } finally { try { releaseZipFile(file); } catch (IOException ignored) { } final long doneFor = myDebugTime ? System.nanoTime() - started : 0; if (doneFor > NS_THRESHOLD) { ClasspathCache.LOG.debug(doneFor / 1000000 + " ms for jar loader get resource:" + name); } } return null; }
void preLoadClasses() { ZipFile zipFile = null; try { zipFile = acquireZipFile(); if (zipFile == null) return; try { File file = new File(zipFile.getName()); myMemoryLoader = new SoftReference<JarMemoryLoader>(JarMemoryLoader.load(file, getBaseURL())); } catch (Exception e) { LOG.error(e); } } catch (Exception e) { // it happens :) eg tools.jar under MacOS } finally { try { releaseZipFile(zipFile); } catch (IOException ignore) { } } }