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