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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | class ApplicationLoaders |
| 24 | { |
| 25 | public static ApplicationLoaders getDefault() |
| 26 | { |
| 27 | return gApplicationLoaders; |
| 28 | } |
| 29 | |
Dimitry Ivanov | 75b10ec | 2015-12-11 17:34:21 -0800 | [diff] [blame] | 30 | public ClassLoader getClassLoader(String zip, boolean isBundled, String librarySearchPath, |
Dmitriy Ivanov | 048a0db | 2015-11-15 14:58:36 -0800 | [diff] [blame] | 31 | String libraryPermittedPath, ClassLoader parent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | { |
| 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 Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 52 | ClassLoader loader = mLoaders.get(zip); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | if (loader != null) { |
| 54 | return loader; |
| 55 | } |
| 56 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 57 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | PathClassLoader pathClassloader = |
Dimitry Ivanov | 75b10ec | 2015-12-11 17:34:21 -0800 | [diff] [blame] | 59 | new PathClassLoader(zip, isBundled, librarySearchPath, |
| 60 | libraryPermittedPath, parent); |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 61 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | mLoaders.put(zip, pathClassloader); |
| 64 | return pathClassloader; |
| 65 | } |
| 66 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 67 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | private static final ApplicationLoaders gApplicationLoaders |
| 77 | = new ApplicationLoaders(); |
| 78 | } |