Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.annotationProcessors.classloader; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.File; |
michael@0 | 8 | import java.io.IOException; |
michael@0 | 9 | import java.net.URL; |
michael@0 | 10 | import java.net.URLClassLoader; |
michael@0 | 11 | import java.util.Collections; |
michael@0 | 12 | import java.util.Enumeration; |
michael@0 | 13 | import java.util.Iterator; |
michael@0 | 14 | import java.util.LinkedList; |
michael@0 | 15 | import java.util.jar.JarEntry; |
michael@0 | 16 | import java.util.jar.JarFile; |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * A classloader which can be initialised with a list of jar files and which can provide an iterator |
michael@0 | 20 | * over the top level classes in the jar files it was initialised with. |
michael@0 | 21 | * classNames is kept sorted to ensure iteration order is consistent across program invocations. |
michael@0 | 22 | * Otherwise, we'd forever be reporting the outdatedness of the generated code as we permute its |
michael@0 | 23 | * contents. |
michael@0 | 24 | */ |
michael@0 | 25 | public class IterableJarLoadingURLClassLoader extends URLClassLoader { |
michael@0 | 26 | LinkedList<String> classNames = new LinkedList<String>(); |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * Create an instance and return its iterator. Provides an iterator over the classes in the jar |
michael@0 | 30 | * files provided as arguments. |
michael@0 | 31 | * Inner classes are not supported. |
michael@0 | 32 | * |
michael@0 | 33 | * @param args A list of jar file names an iterator over the classes of which is desired. |
michael@0 | 34 | * @return An iterator over the top level classes in the jar files provided, in arbitrary order. |
michael@0 | 35 | */ |
michael@0 | 36 | public static Iterator<ClassWithOptions> getIteratorOverJars(String[] args) { |
michael@0 | 37 | URL[] urlArray = new URL[args.length]; |
michael@0 | 38 | LinkedList<String> aClassNames = new LinkedList<String>(); |
michael@0 | 39 | |
michael@0 | 40 | for (int i = 0; i < args.length; i++) { |
michael@0 | 41 | try { |
michael@0 | 42 | urlArray[i] = (new File(args[i])).toURI().toURL(); |
michael@0 | 43 | |
michael@0 | 44 | Enumeration<JarEntry> entries = new JarFile(args[i]).entries(); |
michael@0 | 45 | while (entries.hasMoreElements()) { |
michael@0 | 46 | JarEntry e = entries.nextElement(); |
michael@0 | 47 | String fName = e.getName(); |
michael@0 | 48 | if (!fName.endsWith(".class")) { |
michael@0 | 49 | continue; |
michael@0 | 50 | } |
michael@0 | 51 | final String className = fName.substring(0, fName.length() - 6).replace('/', '.'); |
michael@0 | 52 | |
michael@0 | 53 | aClassNames.add(className); |
michael@0 | 54 | } |
michael@0 | 55 | } catch (IOException e) { |
michael@0 | 56 | System.err.println("Error loading jar file \"" + args[i] + '"'); |
michael@0 | 57 | e.printStackTrace(System.err); |
michael@0 | 58 | } |
michael@0 | 59 | } |
michael@0 | 60 | Collections.sort(aClassNames); |
michael@0 | 61 | return new JarClassIterator(new IterableJarLoadingURLClassLoader(urlArray, aClassNames)); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Constructs a classloader capable of loading all classes given as URLs in urls. Used by static |
michael@0 | 66 | * method above. |
michael@0 | 67 | * |
michael@0 | 68 | * @param urls URLs for all classes the new instance shall be capable of loading. |
michael@0 | 69 | * @param aClassNames A list of names of the classes this instance shall be capable of loading. |
michael@0 | 70 | */ |
michael@0 | 71 | protected IterableJarLoadingURLClassLoader(URL[] urls, LinkedList<String> aClassNames) {// Array to populate with URLs for each class in the given jars. |
michael@0 | 72 | super(urls); |
michael@0 | 73 | classNames = aClassNames; |
michael@0 | 74 | } |
michael@0 | 75 | } |