The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 19 | import android.os.Trace; |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 20 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import dalvik.system.PathClassLoader; |
| 22 | |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 23 | class ApplicationLoaders { |
| 24 | public static ApplicationLoaders getDefault() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | return gApplicationLoaders; |
| 26 | } |
| 27 | |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 28 | public ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled, |
| 29 | String librarySearchPath, String libraryPermittedPath, |
| 30 | ClassLoader parent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /* |
| 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 Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 50 | ClassLoader loader = mLoaders.get(zip); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | if (loader != null) { |
| 52 | return loader; |
| 53 | } |
| 54 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 55 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | PathClassLoader pathClassloader = |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 58 | new PathClassLoader(zip, librarySearchPath, parent); |
| 59 | |
| 60 | String errorMessage = createClassloaderNamespace(pathClassloader, |
| 61 | targetSdkVersion, |
| 62 | librarySearchPath, |
| 63 | libraryPermittedPath, |
| 64 | isBundled); |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 65 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 66 | |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 67 | if (errorMessage != null) { |
| 68 | throw new UnsatisfiedLinkError("Unable to create namespace for the classloader " + |
| 69 | pathClassloader + ": " + errorMessage); |
| 70 | } |
| 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | mLoaders.put(zip, pathClassloader); |
| 73 | return pathClassloader; |
| 74 | } |
| 75 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 76 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 83 | private static native String createClassloaderNamespace(ClassLoader classLoader, |
| 84 | int targetSdkVersion, |
| 85 | String librarySearchPath, |
| 86 | String libraryPermittedPath, |
| 87 | boolean isShared); |
| 88 | |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 89 | private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<String, ClassLoader>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
| 91 | private static final ApplicationLoaders gApplicationLoaders |
| 92 | = new ApplicationLoaders(); |
| 93 | } |