1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package dalvik.system;
18
19import java.io.File;
20import java.net.URL;
21import java.util.Enumeration;
22
23/**
24 * Base class for common functionality between various dex-based
25 * {@link ClassLoader} implementations.
26 */
27public class BaseDexClassLoader extends ClassLoader {
28    /** originally specified path (just used for {@code toString()}) */
29    private final String originalPath;
30
31    /** originally specified library path (just used for {@code toString()}) */
32    private final String originalLibraryPath;
33
34    /** structured lists of path elements */
35    private final DexPathList pathList;
36
37    /**
38     * Constructs an instance.
39     *
40     * @param dexPath the list of jar/apk files containing classes and
41     * resources, delimited by {@code File.pathSeparator}, which
42     * defaults to {@code ":"} on Android
43     * @param optimizedDirectory directory where optimized dex files
44     * should be written; may be {@code null}
45     * @param libraryPath the list of directories containing native
46     * libraries, delimited by {@code File.pathSeparator}; may be
47     * {@code null}
48     * @param parent the parent class loader
49     */
50    public BaseDexClassLoader(String dexPath, File optimizedDirectory,
51            String libraryPath, ClassLoader parent) {
52        super(parent);
53
54        this.originalPath = dexPath;
55        this.originalLibraryPath = libraryPath;
56        this.pathList =
57            new DexPathList(this, dexPath, libraryPath, optimizedDirectory);
58    }
59
60    @Override
61    protected Class<?> findClass(String name) throws ClassNotFoundException {
62        Class clazz = pathList.findClass(name);
63
64        if (clazz == null) {
65            throw new ClassNotFoundException("Didn't find class \"" + name + "\" on path: " + originalPath);
66        }
67
68        return clazz;
69    }
70
71    @Override
72    protected URL findResource(String name) {
73        return pathList.findResource(name);
74    }
75
76    @Override
77    protected Enumeration<URL> findResources(String name) {
78        return pathList.findResources(name);
79    }
80
81    @Override
82    public String findLibrary(String name) {
83        return pathList.findLibrary(name);
84    }
85
86    /**
87     * Returns package information for the given package.
88     * Unfortunately, instances of this class don't really have this
89     * information, and as a non-secure {@code ClassLoader}, it isn't
90     * even required to, according to the spec. Yet, we want to
91     * provide it, in order to make all those hopeful callers of
92     * {@code myClass.getPackage().getName()} happy. Thus we construct
93     * a {@code Package} object the first time it is being requested
94     * and fill most of the fields with dummy values. The {@code
95     * Package} object is then put into the {@code ClassLoader}'s
96     * package cache, so we see the same one next time. We don't
97     * create {@code Package} objects for {@code null} arguments or
98     * for the default package.
99     *
100     * <p>There is a limited chance that we end up with multiple
101     * {@code Package} objects representing the same package: It can
102     * happen when when a package is scattered across different JAR
103     * files which were loaded by different {@code ClassLoader}
104     * instances. This is rather unlikely, and given that this whole
105     * thing is more or less a workaround, probably not worth the
106     * effort to address.
107     *
108     * @param name the name of the class
109     * @return the package information for the class, or {@code null}
110     * if there is no package information available for it
111     */
112    @Override
113    protected synchronized Package getPackage(String name) {
114        if (name != null && !name.isEmpty()) {
115            Package pack = super.getPackage(name);
116
117            if (pack == null) {
118                pack = definePackage(name, "Unknown", "0.0", "Unknown",
119                        "Unknown", "0.0", "Unknown", null);
120            }
121
122            return pack;
123        }
124
125        return null;
126    }
127
128    @Override
129    public String toString() {
130        return getClass().getName()
131                + "[dexPath=" + originalPath + ",libraryPath=" + originalLibraryPath + "]";
132    }
133}
134