blob: b20c0912573327441cb32b902a024e893042317d [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;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070020import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import dalvik.system.PathClassLoader;
22
Dimitry Ivanova55c7f12016-02-23 14:25:50 -080023class ApplicationLoaders {
24 public static ApplicationLoaders getDefault() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025 return gApplicationLoaders;
26 }
27
Dimitry Ivanova55c7f12016-02-23 14:25:50 -080028 public ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled,
29 String librarySearchPath, String libraryPermittedPath,
30 ClassLoader parent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 /*
32 * This is the parent we use if they pass "null" in. In theory
33 * this should be the "system" class loader; in practice we
34 * don't use that and can happily (and more efficiently) use the
35 * bootstrap class loader.
36 */
37 ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent();
38
39 synchronized (mLoaders) {
40 if (parent == null) {
41 parent = baseParent;
42 }
43
44 /*
45 * If we're one step up from the base class loader, find
46 * something in our cache. Otherwise, we create a whole
47 * new ClassLoader for the zip archive.
48 */
49 if (parent == baseParent) {
Kenny Root85387d72010-08-26 10:13:11 -070050 ClassLoader loader = mLoaders.get(zip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 if (loader != null) {
52 return loader;
53 }
54
Dianne Hackbornf7be4802013-04-12 14:52:58 -070055 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);
Dimitry Ivanova55c7f12016-02-23 14:25:50 -080056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 PathClassLoader pathClassloader =
Dimitry Ivanova55c7f12016-02-23 14:25:50 -080058 new PathClassLoader(zip, librarySearchPath, parent);
59
60 String errorMessage = createClassloaderNamespace(pathClassloader,
61 targetSdkVersion,
62 librarySearchPath,
63 libraryPermittedPath,
64 isBundled);
Dianne Hackbornf7be4802013-04-12 14:52:58 -070065 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
66
Dimitry Ivanova55c7f12016-02-23 14:25:50 -080067 if (errorMessage != null) {
68 throw new UnsatisfiedLinkError("Unable to create namespace for the classloader " +
69 pathClassloader + ": " + errorMessage);
70 }
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 mLoaders.put(zip, pathClassloader);
73 return pathClassloader;
74 }
75
Dianne Hackbornf7be4802013-04-12 14:52:58 -070076 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);
77 PathClassLoader pathClassloader = new PathClassLoader(zip, parent);
78 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
79 return pathClassloader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
81 }
82
Dimitry Ivanova55c7f12016-02-23 14:25:50 -080083 private static native String createClassloaderNamespace(ClassLoader classLoader,
84 int targetSdkVersion,
85 String librarySearchPath,
86 String libraryPermittedPath,
87 boolean isShared);
88
Dianne Hackbornadd005c2013-07-17 18:43:12 -070089 private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<String, ClassLoader>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 private static final ApplicationLoaders gApplicationLoaders
92 = new ApplicationLoaders();
93}