blob: a26b88cd4af06384db9d7d0ddaf5f30823fedf4c [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
Dianne Hackbornf7be4802013-04-12 14:52:58 -070019import android.os.Trace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import dalvik.system.PathClassLoader;
21
22import java.util.HashMap;
Kenny Root85387d72010-08-26 10:13:11 -070023import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25class ApplicationLoaders
26{
27 public static ApplicationLoaders getDefault()
28 {
29 return gApplicationLoaders;
30 }
31
Kenny Root85387d72010-08-26 10:13:11 -070032 public ClassLoader getClassLoader(String zip, String libPath, ClassLoader parent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 {
34 /*
35 * This is the parent we use if they pass "null" in. In theory
36 * this should be the "system" class loader; in practice we
37 * don't use that and can happily (and more efficiently) use the
38 * bootstrap class loader.
39 */
40 ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent();
41
42 synchronized (mLoaders) {
43 if (parent == null) {
44 parent = baseParent;
45 }
46
47 /*
48 * If we're one step up from the base class loader, find
49 * something in our cache. Otherwise, we create a whole
50 * new ClassLoader for the zip archive.
51 */
52 if (parent == baseParent) {
Kenny Root85387d72010-08-26 10:13:11 -070053 ClassLoader loader = mLoaders.get(zip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 if (loader != null) {
55 return loader;
56 }
57
Dianne Hackbornf7be4802013-04-12 14:52:58 -070058 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 PathClassLoader pathClassloader =
Kenny Root85387d72010-08-26 10:13:11 -070060 new PathClassLoader(zip, libPath, parent);
Dianne Hackbornf7be4802013-04-12 14:52:58 -070061 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 mLoaders.put(zip, pathClassloader);
64 return pathClassloader;
65 }
66
Dianne Hackbornf7be4802013-04-12 14:52:58 -070067 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);
68 PathClassLoader pathClassloader = new PathClassLoader(zip, parent);
69 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
70 return pathClassloader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 }
72 }
73
Kenny Root85387d72010-08-26 10:13:11 -070074 private final Map<String, ClassLoader> mLoaders = new HashMap<String, ClassLoader>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
76 private static final ApplicationLoaders gApplicationLoaders
77 = new ApplicationLoaders();
78}