blob: 9e3cd7eb8b57d9d1c7cead78e1c7340f9e466259 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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 android.app;
18
19import dalvik.system.PathClassLoader;
20
21import java.util.HashMap;
Kenny Root85387d72010-08-26 10:13:11 -070022import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24class ApplicationLoaders
25{
26 public static ApplicationLoaders getDefault()
27 {
28 return gApplicationLoaders;
29 }
30
Kenny Root85387d72010-08-26 10:13:11 -070031 public ClassLoader getClassLoader(String zip, String libPath, ClassLoader parent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 {
33 /*
34 * This is the parent we use if they pass "null" in. In theory
35 * this should be the "system" class loader; in practice we
36 * don't use that and can happily (and more efficiently) use the
37 * bootstrap class loader.
38 */
39 ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent();
40
41 synchronized (mLoaders) {
42 if (parent == null) {
43 parent = baseParent;
44 }
45
46 /*
47 * If we're one step up from the base class loader, find
48 * something in our cache. Otherwise, we create a whole
49 * new ClassLoader for the zip archive.
50 */
51 if (parent == baseParent) {
Kenny Root85387d72010-08-26 10:13:11 -070052 ClassLoader loader = mLoaders.get(zip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 if (loader != null) {
54 return loader;
55 }
56
57 PathClassLoader pathClassloader =
Kenny Root85387d72010-08-26 10:13:11 -070058 new PathClassLoader(zip, libPath, parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 mLoaders.put(zip, pathClassloader);
61 return pathClassloader;
62 }
63
64 return new PathClassLoader(zip, parent);
65 }
66 }
67
Kenny Root85387d72010-08-26 10:13:11 -070068 private final Map<String, ClassLoader> mLoaders = new HashMap<String, ClassLoader>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70 private static final ApplicationLoaders gApplicationLoaders
71 = new ApplicationLoaders();
72}